//;(function(window){
	//document.addEventListener('touchmove', function (e) { e.preventDefault(); }, false);
	var SkyPhone={};
	SkyPhone.iscache=true;
	SkyPhone.firstpage=true;
	SkyPhone._nlink=SkyPhone._nback="";
	//计数器
	SkyPhone.iTimes=new Array();
	SkyPhone.ispost=false;
	SkyPhone.sc_y=new Array();
	SkyPhone.sc_x=new Array();
	SkyPhone.isajax=true;
	window.SkyPhone=window.s=SkyPhone;
//})(window);
function SkyIsPost(time){
	if(typeof(time)=="undefined"){
		time=600;
	}
	console.log(time);
	SkyPhone.ispost=true;
	setTimeout(function(){
		SkyPhone.ispost=false;
	},time);
}
function skyClearInterval(){
	var len=SkyPhone.iTimes.length;
	console.log("计时器："+len);
	for(var i=0;i<len;i++){
		clearInterval(SkyPhone.iTimes[i]);
	}
	SkyPhone.iTimes=new Array();
}
/***Start Cache*****/
;SkyPhone.cache=(function(S,$){
	var set=function(key,val,expire){
		key=key.replace(/\W/i,"_");
		if(typeof(expire)=="undefined"){
			expire=36000000;//默认永不过期
		}
		
		var data={
			data:val,
			expire: Date.parse(new Date())+expire*1000,
			isold:0	
		};
		window.localStorage.setItem(key,JSON.stringify(data));
	};
	
	var get=function(key){
		key=key.replace(/\W/i,"_");
		var data=window.localStorage.getItem(key);
		if(data==null){
			return false;
		}else{
			var arr=JSON.parse(data);
			if(arr.expire<=Date.parse(new Date())){
				arr['isold']=1;
				return arr;
			}else{
				return arr;
			}
		}		
	};
	
	var del=function(key){
		key=key.replace(/\W/i,"_");
		window.localStorage.removeItem(key);
	}
	
	var clear = function(){
        var storage = window.localStorage;
        for(var key in storage){
            storage.removeItem(key);
        }
    }
	return {
		get : get,
		set : set,
		del : del,
		clear : clear
	}
	
})(SkyPhone,Zepto);
/***END cache***/

/**Start Scroll */
;SkyPhone.scroll=(function(S,$){
	
	var sc_y_h;
	var scroll_y=function(obj) {
		if($(obj).length==0) return false;
		var obj=obj;
		var key=obj.replace(/\W/i,"_");
		SkyPhone.sc_y[key] = new IScroll(obj, { useTransition: true,tap: true });
	
		var s_y= obj+" .scroller-y" ;
		sc_y_h=$(s_y).height();
		skyClearInterval();
		var si=0;
		var it=setInterval(function(){
			si++;
			console.log('执行了一次');
			console.log(sc_y_h +" "+$(s_y).height() +" "+ SkyPhone.iTimes);
			if(sc_y_h!=$(s_y).height()){
				sc_y_h=$(s_y).height();
				console.log(key+ " "+typeof(SkyPhone.sc_y[key]));
				if(typeof(SkyPhone.sc_y[key])=='object'){			
					SkyPhone.sc_y[key].refresh();
				}
			}else{
				if(si>5){
					skyClearInterval();
				}
			}
		},500);
		SkyPhone.iTimes.push(it);
		 
	}
	
	var scroll_x=function(obj){
		if($(obj).length==0) return false;
		var obj=obj;
		var key=obj.replace(/\W/i,"_");
		SkyPhone.sc_x[key] = new IScroll(obj, { scrollX: true, scrollY: false, mouseWheel: true,useTransition: true });
	}
	var destroy=function(){
		for(var sci in SkyPhone.sc_y){
			SkyPhone.sc_y[sci].destroy();		
		}
		for(var sci in SkyPhone.sc_x){
			
			SkyPhone.sc_x[sci].destroy();		
		}
		SkyPhone.sc_y=new Array();
		SkyPhone.sc_x=new Array();
	};
	var scrollto=function(obj,obj2){
		var key=obj.replace(/\W/i,"_");
		SkyPhone.sc_y[key].scrollToElement($(obj2), 1200, null, null, IScroll.utils.ease.elastic)
	}
	return {
		scroll_y:scroll_y,
		scroll_x:scroll_x,
		destroy:destroy,
		scrollto:scrollto,
	}
	
})(SkyPhone,Zepto);

/*End Scroll*/

/* Start loadPage*/

SkyPhone.loadPage=function(href,callback,delcache){
		SkyPhone.scroll.destroy();
		$("body").scrollTop("0px");
		if(href){
			if(delcache==1){
				SkyPhone.cache.del(href);				 
			}
			var callback=callback;
			if($("#loading").length==0){
				$("body").append('<div id="loading"><div style=""></div></div>');
			}
			$("#loading").show();
			var url_data;
			var pend=function(callback){
				if(typeof(callback)!=="undefined" && callback!=null){
					$("#loading").hide();
					return url_data;
				}else{
					SkyPhone.firstpage=false;
					if(url_data){
						$("#loading").hide();
						var bo=url_data.match(/<body[^>]*>([\w\W]*)<\/body>/i);
							if(bo!==null){										
								$("body").html(bo[1]);
								/*
								var ptArr=new Array("pt-page-scaleUp","pt-page-scaleDown","pt-page-scaleUpDown","pt-page-scaleDownUp","pt-page-scaleDownCenter","pt-page-scaleUpCenter","pt-page-rotateOutNewspaper","pt-page-rotateInNewspaper","pt-page-rotatePullLeft");
								var ptLen=ptArr.length;
								var ptN=Math.round(Math.random()*ptLen);
								var ptD=ptArr[ptN];//动画效果
								 
								$("body").addClass(ptD);
								 
								setTimeout(function(){
									$("body").removeClass(ptD);
								},1000);
								*/
								ajax_jsa();
							}else{
								alert('出错了');
							}
					}
					$("#loading").hide();
				}
			};
			var d;
			
			if(SkyPhone.iscache){			
				d=SkyPhone.cache.get(href);
			}
			if(d){
				 
				url_data=d['data'];
				pend(callback);
			}else{
				
				
				$.get(href,function(data){
					//正则获取body
					if(!data){
						skyAlert('数据出错了');
						SkyPhone.cache.del(href);
						$("#loading").hide();
					}else{
						url_data=data;
						  
						SkyPhone.cache.set(href,url_data,60);
						pend(callback);
					}
				});
			}
			
			
		}
		
	}
/**End loadPage***/
/*Start Slider*/
function SkySlider(id){
		var Sc = new IScroll("#"+id, {
		scrollX: true,
		scrollY: false,
		momentum: false,
		snap: true,
		snapSpeed: 400,
		keyBindings: true, 
	});
}

/*End Slider*/

/*Start alert*/
function skyAlert(content,title,url,url2){
	var len=$("#alert").length;
	var html='';
	if(typeof(title)=='undefined'){
		var title='提醒';
	}
	if(typeof(url)=='undefined'){
		url='javascript:;';
	}
	html+='<div class="hd"><span>'+title+'</span><div class="close alert_close"><i class="fa fa-times"></i></div></div>';
	html+='<div class="cont"><div class="inner">'+content+'</div></div>';
	html+='<div class="ft">';
	html+='<a href="'+url+'" class="alert_close aurl success">确定</a>';
	if(typeof(url2)!=='undefined'){  
		html+='<a href="'+url2+'" class="aurl alert_close fail">取消</a>';
	}
	html+='</div>';
	if(len>0){
		$("#alert").html(html).show();
	}else{
		html='<div class="alert" id="alert">'+html+'</div>';
		$("body").append(html);
		$("#alert").show();
	}
	$(".alert_close").live("click",function(){
		$("#alert").hide();
	});
}

/*End Alert*/


/*Start toast*/
SkyPhone.toast=function(message,type){
	if(typeof(type)=="undefined"){
		type="";
	}
	if($("#toast").length==0){
		var html="<div id='toast'><div class='bg "+type+"'>"+message+"</div></div>";
		$("body").append(html);		
	}
	$("#toast").show();
	setTimeout(function(){
		$("#toast").hide();
	},2000);
}
/*End toast*/



function imgLazy(o){
	setTimeout(function(){
		var obj=$("body").find(o);
		var len=obj.length;
		for(var i=0;i<len;i++){
			obj.eq(i).attr("src",obj.eq(i).attr("url"));
		}
	},200);
}

/*瀑布流布局*/
function list_pubu(ap,items,cols){
	  var items=$(items);
	  var len=items.length;
	  var j=0;
	  var obj=new Array();
	  var lens=new Array();
	  for(var k=0;k<cols;k++){
		  obj[k]=$(ap+" .pubu-col-"+k);
		  lens[k]=$(ap+" .pubu-col-"+k).length;
	  }
	  
	  var m=Math.min.apply( Math, lens );
	  for(var k=0;k<cols;k++){
		  if(m==lens[k]){
			  j=m;
			  break;
		  }
	  }				
	  
	  for(var i=0;i<len;i++){
		  obj[j].append(items.eq(i).html());
		  j++;
		  if(j>=cols) j=0;
		  items.eq(i).remove();
	  }
}
/*End 瀑布流布局*/

$(function(){
	
	/*Start Panel*/
	$(".panel-btn").die().live("click",function(e){
		e.preventDefault();
		$("#panel-box").toggle();
	});
	$(".panel-close").die().live("click",function(e){
		e.preventDefault();
		$("#panel-box").hide();
	});
	/*End Panel*/	
	//当前点击的链接
	$(".footer-bar a").die().live("click",function(){
		$(".footer-bar a").removeClass("active");
		$(this).addClass("active");
	});
	SkyPhone.cache.clear();
	/*Start Navigator*/
	$("#navigator a").die().live("click",function(e){
		e.preventDefault();
		$("#navigator a").removeClass("active");
		$(this).addClass("active");
		
		var href=$(this).attr("href");
		if(href.match(/#/)){
			 var id=href.substr(1);
			 $(".navigator-item").hide();
			 $("#"+id).show();
			 SkyPhone.scroll.scroll_y("#"+id+' .wrapper-y');
		}else{
			var id=$(this).attr("data-id");
			if(id==null){
				set_hisurl(href);				
				SkyPhone.loadPage(href);
			}else{
				var data=SkyPhone.loadPage(href,1);			
				$(".navigator-item").hide();				
				$("#"+id).html(data).show();
				SkyPhone.scroll.scroll_y("#"+id+' .wrapper-y');
			}
		}
	});
	
	
	/*End navigator*/
	/*Start Tabs*/
	$(".tabs a").die().live("click",function(e){
		e.preventDefault();
		var p=$(this).parents(".tabs-box");
		p.find("a").removeClass("active");
		$(this).addClass("active");
		
		var href=$(this).attr("href");
		if(href.match(/#/)){
			 var id=href.substr(1);
			 $(".tabs-item").hide();
			 $("#"+id).show();
			 SkyPhone.scroll.scroll_y("#"+id+' .wrapper-y');
		}else{
			var id=$(this).attr("data-id");
			if(id==null){
				set_hisurl(href);	
				SkyPhone.loadPage(href);
			}else{
				var data=SkyPhone.loadPage(href,1);			
				$(".tabs-item").hide();				
				$("#"+id).html(data).show();
				SkyPhone.scroll.scroll_y("#"+id+' .wrapper-y');
			}
		}
	});
	
	
	/*End Tabs*/
	
	$("#loading").live("click",function(){
		$(this).hide();
	});
	
	$(".aurl").live("click",function(e){
		e.preventDefault();
		var href=$(this).attr("href");
		var delcache=0;
		if(typeof($(this).attr("iscache")==0)){
			delcache=1;
		}
		
		
		if(href!="" && href!="undefined" && /javascript:/.test(href)==false && href.length>3 &&  /^#/.test(href)==false){
			
			set_hisurl(href);
			
			SkyPhone.loadPage(href,null,delcache);
		}
	});
	$(".pages a").live("click",function(e){
		e.preventDefault();
		var href=$(this).attr("href");
		var delcache=0;
		if(typeof($(this).attr("iscache")==0)){
			delcache=1;
		}
		if(href!="" && href!="undefined" && /javascript:/.test(href)==false && href.length>3 &&  /^#/.test(href)==false){
			
			set_hisurl(href);
			
			SkyPhone.loadPage(href,null,delcache);
		}
	});
	
	$(".back").live("click",function(e){
		e.preventDefault();
		if(SkyPhone.isajax==false){
			window.history.back();
		}else{
			var href=get_hisurl();
			SkyPhone.loadPage(href);
		}
	});
	
	$(".sky-delete").die().live("click",function(){
		var obj=$(this);
		if(confirm(obj.attr("msg"))){
			$.get($(this).attr("url"),function(data){
				obj.parents(".sky-row").remove();
			});
		}
	});
 
	window.addEventListener("popstate", handleBackButton);
	function handleBackButton(e) {		 
		var href=window.location.href;
		var h2=get_hisurl();
		if(SkyPhone.firstpage==true){
			SkyPhone.firstpage=false;
		}else{
			SkyPhone.loadPage(href);
		}
		
	}
	 				 
	 
});
var ajax_jsa_loaded=false;

function ajax_jsa(){
	 
	//加载js文件
	var sr=$("body").find(".jsa-src");
	var srlen=sr.length;
	for(var j=0;j<srlen;j++){
		var src=$(sr[j]).attr("src");
			loadScript($(sr[j]).attr("src"),ajax_jsa_isload);  
	}
	ajax_jsa_text();
}

function ajax_jsa_text(){
	//加载js片段 0.1s延时加载
	
	if(!ajax_jsa_loaded){
		setTimeout("ajax_jsa_text()",1);
	}else{	 
		var d=$("body").find(".jsa-text");
		var jslen=d.length;
		if(jslen>0){
		for(var i=0;i<jslen;i++){
							//alert(d[i].innerHTML);
			eval(d[i].innerHTML);
		}
		}
	}
}

function ajax_jsa_isload(){
	ajax_jsa_loaded=true;
}


//设置历史url
function set_hisurl(href){
	if(href!="" && href!="undefined" && /javascript/.test(href)==false && href.length>3 &&  /^#/.test(href)==false){	
		var t_url = window.location.href;
	//	history.pushState(null, null, href);
		var his_url=localStorage.his_url; 
		if(typeof(his_url)=="undefined" || !his_url){
			his_url="";
		}
		if(his_url.substr(0,t_url.length)!=t_url){			
				his_url= t_url +"@|@"+his_url;
				his_url=his_url.substr(0,4000);
				console.log(his_url);
				localStorage['his_url']=his_url;
		}
	}
}

function get_hisurl(){
	
	var his_url=localStorage.his_url;
	console.log(his_url);
	if(typeof(his_url)=="undefined" || his_url==""){
			return null;
	}

	var arr=his_url.split("@|@");
	var len=arr.length;
	if(len>1){
		//arr.push(arr[0]); 
		var t_url=arr.shift();
		localStorage.setItem("his_url",arr.join("@|@"));
	}else{
		var t_url=arr.shift();
		localStorage.setItem("his_url","");
	}
	//history.pushState(null, null, t_url);
	return t_url;	
}


function loadScript(url,callback) {
	var head = document.head || document.getElementsByTagName("head")[0] || document.documentElement,
	script,options,s;
	if (typeof url === "object") { 
		options = url;
		url = undefined;
	}
	s = options || {};
	url = url || s.url;
	callback = callback || s.success;
	script = document.createElement("script");
	script.async = s.async || false;
	script.type = "text/javascript";
	if (s.charset) {
		script.charset = s.charset;
	}
	if(s.cache === false){
		url = url+( /\?/.test( url ) ? "&" : "?" )+ "_=" +(new Date()).getTime();
	}
	script.src = url;
	head.insertBefore(script, head.firstChild);
	if(callback){
		ajax_jsa_loaded=false;
		document.addEventListener ? script.addEventListener("load", callback, false) : script.onreadystatechange = function() {
			if (/loaded|complete/.test(script.readyState)) {
			script.onreadystatechange = null
			
			callback()
			}
		}
	}
}