// JavaScript Document
///ajax替换内容
basepath="http://www.lelebox.com/lelebox/";
//basepath="http://localhost:8080/lelebox/";
downtopref=basepath+"ajax/topapps.jsp";
voteref=basepath+"ajax/countapp.jsp";
logincheckref=basepath+"ajax/checklogin.jsp";
appnewref=basepath+"ajax/appnew.jsp";
appfreeref=basepath+"ajax/appfree.jsp";
apploggetref=basepath+"ajax/applogget.jsp";	//取得用户是否已经投票的接口 
applogpostref=basepath+"ajax/applogpost.jsp";	//投票接口 
appbuyref=basepath+"buy?type=select";
logref=basepath+"ajax/ajaxlog.jsp";
logoutref=basepath+"logout.jsp";
var islogin=false;
$(document).ready(function() {  
//读取是否登陆
ajax_login_check();
/*
为所有class为ajaxload的容器读取内容
*/
//读取下载排行
$('.ajaxload_top_down').load(downtopref);
$('.ajaxload_app_new').load(appnewref);
$('.ajaxload_app_free').load(appfreeref);
//读取数量信息
ajax_count();
//读取是否已经投票
ajax_isvote()

//读取其他
ajax_load();
});

//更新投票信息
function ajax_count(){
	//获取全部编号
	votes=$('.ajaxcount');
	//alert(votes.length);
	str='';
	for(i=0;i<votes.length;i++){
		//alert(votes[i].id);
		if(i==0){
			str+=votes[i].id.split('_')[1];
		}else{
			str+=""+","+votes[i].id.split('_')[1];	
		}
	}
	//alert(str);
$.ajax({
   type: "get",
   url: voteref,
   dataType:"json",
   data: "type=searchvote&ids="+str+"&"+Math.random(),
   success: function(msg){
	   //填充
	   for(i=0;i<msg.length;i++){
		   $('#votecount_'+msg[i].id).html(msg[i].vote);
		   $('#downcount_'+msg[i].id).html(msg[i].down);
		   $('#reviewcount_'+msg[i].id).html(msg[i].review);
	   }
     //alert( "Data Saved: " + msg );
   }
});	
}



//检查是否登陆
function ajax_login_check(){
$.ajax({
   type: "get",
   url: logincheckref+"?"+Math.random(),
   dataType:"json",
   async:false,
   success: function(msg){
	   //填充
	   if(msg.state==true){
		   islogin=true;
		   $('#sign_user').html("<a href='#' onclick='var a=\""+logoutref+"?r=\"+location.href;location.href=a;'><span>"+msg.username+" 退出</span></a>");
	   }else{
		//请登陆   
		}
	  //alert(msg.state);
     //alert( "Data Saved: " + msg );
   }
});	
}

//class为ajaxload中 包含ajaxload属性，属性里为链接
function ajax_load(){
	arr=$('.ajaxload');
	//alert(arr.length);
		for(i=0;i<arr.length;i++){
			//alert($('.ajaxload:eq('+i+')').attr('ajaxload'));
			$('.ajaxload:eq('+i+')').load(basepath+$('.ajaxload:eq('+i+')').attr('ajaxload'));
		}
	}
//

//投票的处理
function vote_app(app_id){
	$.ajax({
   type: "get",
   url: applogpostref,
   dataType:"json",
   data: "type=vote&id="+app_id+"&"+Math.random(),
   success: function(msg){
	   //填充
	   if(msg.state==true){
		//投票成功
		//更新投票数量
		//读取数量信息
			ajax_count();
	   }
	   alert(msg.msg);   
   }
});	
}

/**
	取得指定的应用是否已经被登录的用户投票过
*/
function ajax_isvote(){
		//获取全部编号
	isvotes=$('.isvote');
	//alert(votes.length);
	str='';
	for(i=0;i<isvotes.length;i++){
		//alert(votes[i].id);
		if(i==0){
			str+=isvotes[i].id.split('_')[1];
		}else{
			str+=""+","+isvotes[i].id.split('_')[1];	
		}
	}
$.ajax({
   type: "get",
   url: apploggetref,
   dataType:"json",
   data: "ids="+str+"&"+Math.random(),
   success: function(msg){
	   //填充
	   for(i=0;i<msg.length;i++){
		   if(msg[i].vote==true){
				   $('#isvote_'+msg[i].id).html('您已投票');
		   }
	   }
   }
});	
}

//复制当前链接到剪切板
function copyuri()
    {
        var getId2 = document.location.href;
       alert("已经复制页面地址到剪切板!");
	   window.clipboardData.setData('text',getId2); 
    }
function lelebox_download(appd_id){
		$("#iframe_down").html('<iframe id="iframed"  frameborder="0" scrolling="no"></iframe>');
		$("#iframed").attr('src',appbuyref+'&app_id='+appd_id);
		//$("#iframed").attr('src','http://www.baidu.com');
		//alert($('html').height());
}