
function $box(title,content,options){
	options = options || {};
	//content = content || {};
	this.title= title ? title : '大地百科-提示';
	
	var argu = $.extend({
		width:'270',
		height:'100',
		ok:'close',
		ajaxCom:'closeload',
		bg:true,
		drag:true,
		url:'javascript:;',
		button:'确定',
		classname:'',
		num:false,
		box_id:'dadi-box',
		title_id:'box-title',
		content_id:'box-content',
		hasCancel:'取消',
		hasCancelFun:false,
		sure_id:'box-sure'
	},options);

	//可以弹出多个弹出框
	if(argu.num){
		argu.box_id = 'dadi-box'+argu.num;
		argu.title_id = 'box-title'+argu.num;
		argu.content_id = 'box-content'+argu.num;
		argu.close_id = 'box-close'+argu.num;
		argu.sure_id = 'box-sure'+argu.num;
	}else{
		$("#dadi-box").remove();
	}
	if(argu.hasCancel){
		if(argu.hasCancelFun){
			var cancleHtml = '<a href="javascript:;" onclick="'+argu.hasCancelFun+'" class="'+ argu.close_id +' box-sure">'+argu.hasCancel+'</a>';
		}else{
			var cancleHtml = '<a href="javascript:;"  class="'+ argu.close_id +' box-sure">'+argu.hasCancel+'</a>';
		}
	}else{
		var cancleHtml = '';
	}

	//遮罩层DIV
	var winDiv = '<div class="boxbg"></div>'+
					'<table cellspacing="0" cellpadding="0" border="0" id="'+argu.box_id+'" class="dadi-box">'+
						'<tr><td class="top-left op"></td><td class="top op"></td><td class="top-right op"></td></tr>'+
						'<tr><td class="left op"></td><td class="box-wrap">'+
								'<h1 id="'+argu.title_id+'" class="box-title">'+this.title+'<a href="javascript:;" class="'+argu.close_id+' close">关闭</a></h1>'+
								'<div  id="'+argu.content_id+'" class="box-content"></div>'+
								'<div>'+cancleHtml+'<a href='+argu.url+' id="'+argu.sure_id+'" class='+argu.classname+' box-sure>'+argu.button+'</a></div>' +
							'</td>'+
							'<td class="right op"></td></tr>'+
						'<tr><td class="bottom-left op"></td><td class="bottom op"></td><td class="bottom-right op"></td></tr>'+
					'</table>';

	//加入到body元素上
	$("body").append(winDiv);
	//设置弹出框的DOM标记 ，方便DOM操作
	var boxbg = $(".boxbg"); //背景句柄
	var boxclose = $("."+argu.close_id); //关闭句柄
	var boxTitle = $("#"+argu.title_id); //标题句柄
	var boxContent = $("#"+argu.content_id); //内容句柄
	var dadibox = $("#"+argu.box_id); //弹出框句柄
	var boxsure = $("#"+argu.sure_id);

	//判断弹出的层是文本还是AJAX请求过来的数据
	switch (content.type)
	{
		case 'html':
			boxContent.html(content.message);
			break;
		case 'ajax':
			boxContent.ajaxStart(function(){
				boxContent.html('<img src="/images/loading_max.gif" class="loading" tyle="border:none;width:32px;height:32px;"/>');
			});
			$.ajax({
				type:"get",
				url:content.message,
				error:function(){
					boxContent.html("<p>加载数据出错...</p>");
				},
				success:function(data){
					boxContent.html(data);
				}
			});
			boxContent.ajaxComplete(function(){
				ajaxComplete = eval(argu.ajaxCom);
				try{ajaxComplete();}catch(e){}
			});
			break;
		case 'ifram':
			boxContent.ajaxStart(function(){
				boxContent.html('<img src="/images/loading_max.gif" class="loading" style="border:none;width:32px;height:32px;"/>');
			});
			$.ajax({
				error:function(){
					boxContent.html("<p>加载数据出错...</p>");
				},
				success:function(html){
					boxContent.html("<iframe name=\"box\" src=\""+content.message+"\" width=\"100%\" height=\""+argu.height+"\""+"\" scrolling=\"no\" frameborder=\"0\" marginheight=\"0\" marginwidth=\"0\"></iframe>");
				}
			});
			boxContent.ajaxComplete(function(){
				ajaxComplete = eval(argu.ajaxCom);
				try{ajaxComplete();}catch(e){}
			});
			break;
		default:
			return false;
			break;
	}
	
	//是否显示遮罩层
	if(argu.bg){
		boxbg.show();
	//	boxbg.animate({opacity:"0.5"},"normal");
	}else{boxbg.remove();}

	//计算当前方档的尺寸

	var win_size = get_win_size();
	var scTop = document.documentElement.scrollTop;
	var isIE = (document.all) ? true : false;
	var isIE6 = isIE && ([/MSIE (\d)\.0/i.exec(navigator.userAgent)][0][1] == 6);
	//if($.browser.msie && $.browser.version == 6.0){ 
	if(isIE6){ 
			//var t = scTop + win_size.height/2 - parseInt(argu.height)/2;//for IE
			dadibox.css({
				'marginTop':-(parseInt(argu.height)/2)+'px',
				'marginLeft':-(parseInt(argu.width)/2)+'px',
				//'left':(win_size.width/2-60)+'px'
				'left':'50%'
			});
	}else{
		var t = win_size.height/2 - parseInt(argu.height)/2;
		if(parseInt(argu.height) >=400){
			var mt = 0;
		}else mt = -(parseInt(argu.height)/2)+'px';
		dadibox.css({
			'top':t+'px',
			//'top':'50%',
			'marginTop':mt,
			'marginLeft':-(parseInt(argu.width)/2)+'px',
			//'left':(win_size.width/2-60)+'px'
			'left':'50%'
		});
	}
	
	//设置样式
	boxTitle.css({'width':argu.width+'px'});
	boxContent.css({'width':argu.width+'px'});
	$(".op").css('opacity','0.75');
	//显示
	dadibox.show();
	
	//关闭函数
	var close = function(){
		boxbg.remove();
		$(".boxbg").remove();
		dadibox.fadeOut("slow",function(){$(this).remove()});
	}

	this.close = close;
	var closeload = loading(false);

	boxclose.click(function(){
		setTimeout(close,100);
	});
	if(argu.drag){
		try{draggg(boxTitle,dadibox,argu.drag);}catch(e){}
	}
	
	if(!argu.button){
		boxsure.remove();
	}else{
		//确定函数
		var fun = argu.ok ? argu.ok : close;
		boxsure.click(function(){
			fun=eval(fun);
			try{fun();setTimeout(close,600);}catch(e){};
		});
	}
}
function closebox(){
		$(".boxbg").remove();
		$("#dadi-box").fadeOut("normal",function(){$(this).remove()});
}
/*-- drag --*/
function draggg(handle,div,drag){
	var client_width = document.documentElement.clientWidth;//当前文档的宽度
	var client_height = document.documentElement.clientHeight;//当前文档的高度
	var scroll_top = document.documentElement.scrollTop;//滚动的高度
	var dragBox = div, dragHandle = handle;
		var moveX = 0,moveY = 0,moveTop,moveLeft = 0,moveable = false;
		if ($.browser.version == 6.0 ) {
			moveTop = scroll_top;
		}else {
			moveTop = 0;
		}
	var	sw = dragBox[0].scrollWidth,sh = dragBox[0].scrollHeight;
	dragHandle.mouseover(function(e) {
			if(drag){dragHandle.css("cursor","move");}else{dragHandle.css("cursor","default");}
	});
	dragHandle.mousedown(function(e) {
		if(drag){moveable = true;}else{moveable = false;}
		var dragBoxLeft = dragBox[0].offsetLeft, dragBoxTop = dragBox[0].offsetTop-moveTop;
		moveX = e.clientX-dragBoxLeft;//
		moveY = e.clientY-dragBoxTop;
		document.onmousemove = function(ev) {
				if (moveable) {
				ev = window.event?window.event:ev;
				var x = ev.clientX - moveX;
				var y = ev.clientY - moveY;
					if ( x > 0 &&( x + sw < client_width) && y > 0 && (y + sh < client_height) ) {
						dragBox.css("left",x+"px");
						dragBox.css("top",parseInt(y+moveTop) + "px");
						dragBox.css("margin","auto");
						}
					}
				}
		document.onmouseup = function () {moveable = false;};
		dragBox.onselectstart = function(e){return false;}
	});
}
/*--// drag --*/
function resize_image(obj, MaxW, MaxH){
	var imageObject = obj;
	var state = imageObject.readyState;
	alert(state);
	if(state!='complete'){
		setTimeout("resizeImage("+imageObject+","+MaxW+","+MaxH+")",50);
		return;
	}
	var oldImage = new Image();
	oldImage.src = imageObject.src;
	var dW = oldImage.width;
	var dH = oldImage.height;
	a = dW/MaxW; b = dH/MaxH;
	if( b>a ) b = a;
	dW = dW/a; dH = dH/a;
	alert(dW);
	if(dW > 0 && dH > 0){
		imageObject.width = dW;
		imageObject.height = dH;
	}
}

function loading(flag,msg){
	var flag = !flag ? false : true;
	if(!flag){try{$(".loading").remove();}catch (e){}return false;}
	var msg = msg ? msg : '信息加载中...';
	var loading_html = "<div class='loading'>"+msg+"</div>";
	$("body").append(loading_html);
	var win_size = get_win_size();
	if($.browser.msie && $.browser.version == 6.0){
		var scTop = document.documentElement.scrollTop;
		var t = scTop + win_size.height/2 - 16;//for IE
		$(".loading").css({
			'top':t+'px',
			'left':'50%'
		});
	}else{
		$(".loading").css({
		'top':(win_size.height/2 -60)+'px',
		'left':(win_size.width/2-60)+'px'
	});
	}
}
function get_win_size() {
		var winWidth = 0;
		var winHeight = 0;
		
		//获取窗口宽度
		if (window.innerWidth)
		   winWidth = window.innerWidth;
		else if ((document.body) && (document.body.clientWidth))
		   winWidth = document.body.clientWidth;
		//获取窗口高度
		if (window.innerHeight)
		   winHeight = window.innerHeight;
		else if ((document.body) && (document.body.clientHeight))
		   winHeight = document.body.clientHeight;
		//通过深入Document内部对body进行检测，获取窗口大小
		if (document.documentElement && document.documentElement.clientHeight && 
		    document.documentElement.clientWidth)
		{
		   winHeight = document.documentElement.clientHeight;
		   winWidth = document.documentElement.clientWidth;
		}

		return {width: winWidth, height: winHeight};
		
}
function isset(el){if(!el || el == '' || el == 'underfined')return false;return true;}
function info(msg ,back ,fun ,cancel){
	try{tip.close()}catch(e){};
	var content = '<p style="text-align:center;">'+msg+'</p>';
	//alert(cancel);
	var hasCancel =  (cancel == undefined || cancel) ? '取消' : false;
	var url = (back == false || back == true || back == undefined) ? 'javascript:;' : back;
	var call = fun == undefined ? 'close' : fun;
	if(!back && !fun){
		tip = new $box('大地百科-提示',{'type':'html','message':content},{'width':'250','height':'50','button':'','hasCancel':false});
		setTimeout(closebox,500);
	}else{
		tip = new $box('大地百科-提示',{'type':'html','message':content},{'width':'250','height':'50','ok':call,'url':url,'hasCancel':hasCancel});
	}
	return tip;
}

function score(score,message,url){
	var score_html = $("<p>您积分将<span class='score'>+"+score+"分</span></p>");
	new $box(message,{'type':'html','message':score_html},{'width':'250','height':'50','button':'','hasCancel':false,'url':url});
	delay(function(){
		$(".boxbg").remove();
		$("#dadi-box").fadeOut("slow",function(){$(this).remove();window.location.href=url});
	},1000);
}

function $ajax(url, data, fun) {
	var d = eval(data);
	$.post(
		url,d,function(data){
			var json=$.parseJSON(data);
			fun=eval(fun);
			try{fun(json);}catch(e){};
		}
	);
}

/* 表单ajax请求的统一接口 */
$.fn.$send = function(options){
	var setting = $.extend({
		id:'id',
		call:'call'
	},options);
	var form = $("#"+setting.id);
	form.submit(function(){return false});
	var queryString = '{' + form.$formValue() + '}';
	var url = $.trim(form.attr("action"));
	var query = eval('(' + queryString + ')');
	$ajax(url,query,setting.call);
	return false;
}


/* 获取表单数据 */
$.fn.$formValue = function(options){
	var a = [];
	//input
	var form = this[0].getElementsByTagName('input');
	for(var i=0,max = form.length; i<max; i++ ){
		var el = form[i];
		if(el.type == 'checkbox' ){
			if(el.checked == true){
			//	el.value = 'checked';
				a.push('"'+encodeURIComponent(el.name)+'":"'+encodeURIComponent(el.value)+'"');
			}
		}else if(el.type == 'radio'){
			if (el.checked == true){
				a.push('"'+encodeURIComponent(el.name)+'":"'+encodeURIComponent(el.value)+'"');
			}
		}else
		a.push('"'+encodeURIComponent(el.name)+'":"'+encodeURIComponent(el.value)+'"');
	}
	//textarea
	var textarea = this[0].getElementsByTagName("textarea");
	if(textarea.length == 1){
		a.push('"'+encodeURIComponent($(textarea).attr("name"))+'":"'+encodeURIComponent($(textarea).val())+'"');
	}else if(textarea.length >1){
		for(var j=0;max=textarea.length;j++){
			a.push('"'+encodeURIComponent($(textarea[i]).attr("name"))+'":"'+encodeURIComponent($(textarea[i]).val())+'"');
		}
	}
	//select的值
	var select = this[0].getElementsByTagName("select");
	$.each(select,function(i,item){
		a.push('"'+encodeURIComponent($(item).attr("name"))+'":"'+encodeURIComponent($(item).val())+'"');
	});
	return a;
}
function header(){
	var headinfo = $("#headinfo");
	headinfo.html("<img src='/images/loading1.gif'><span style='line-height:16px;height:16px;display;float:right;display:block;'>正在加载...</span>");
	$.get("/index/header",function(data){headinfo.html(data)});
}

/*-- 统一的ajax请求 --*/
function login_sucesss(json){
	if(json.flag){
		closebox();
		header();
		$.rebindEvents();
		$("#" + json.id).trigger("click");
	}else{
		$("#loginerror").append("<span style='background:#fff;color:red;font-size:12px;padding-left:37px;'  class='logintip'>"+json.info+"</span>").show();
		setTimeout(function(){
			$(".logintip").fadeOut('normal',function(){$(this).remove();});
		},1000);
		$("#luname").select();
		$("#luname").focus();
	}
}
//统一的ajax请求,失败不刷新
function ajax_sucesss(json){
	if(json.flag){
		info(json.info);
		window.location.reload();
	}else{
		info(json.info);
	}
}
function login(id){
	var div = '<div class="index_login" style="background:none;border:none;height:auto;margin:auto;">'+
				'<form name="login_form" id="login_form" method="post" action="/sign/ajaxlogin">'+
				'<ul>'+
					'<li>'+
						'<label class="label" for="luname">账号:</label>'+
						'<input type="text" class="text" id="luname" name="uname" value="" onfocus="this.select();">'+
						'<input type="hidden" name="domid" id="domid" value="'+id+'" />'+
					'</li>'+
					'<li>'+
						'<label for="passwd">密 码:</label>'+
						'<input type="password" class="text" id="passwd" name="passwd">'+
					'</li>'+
					'<p id="loginerror" style="clear:both;"></p>'+
					'<li class="loin_bottom">'+
						'<input type="checkbox" id="remberme" name="remberme">'+
						'<label for="remberme">在这台电脑上记住我</label>'+
					'</li>'+
					'<li class="loin_bottom">'+
						'<input type="submit" class="submitbutton" value="登录" onclick=$(this).$send({"id":"login_form","call":"login_sucesss"}); name="login">'+
						'<a href="/sign/up" >还没有账号</a>'+
					'</li>'+
				
			'</ul></form></div>';
	$box("快速登录",{"type":"html","message":div},{'width':'280','height':'190','button':'','hasCancel':false});
}
var timerId;
function delay(fun,t){
	if (timerId) {
		window.clearTimeout(timerId);
		timerId = null;
	}
	timerId = window.setTimeout(fun,t);
}

$(function(){

	
	//输入框样式
	$("input[type=text]").focusin(function(){$(this).css("border","1px solid #59c3e1")});
	$("input[type=text]").focusout(function(){$(this).css("border","1px solid #B5B5B6")});
	$("input[type=password]").focusin(function(){$(this).css("border","1px solid #59c3e1")});
	$("input[type=password]").focusout(function(){$(this).css("border","1px solid #B5B5B6")});
	$("textarea[class=text]").focusin(function(){$(this).css("border","1px solid #59c3e1")});
	$("textarea[class=text]").focusout(function(){$(this).css("border","1px solid #B5B5B6")});
});


(function($){

	$.EventsStack = [];

	$.EventManager = null;

	$.isLogin = false;

	//注册所有bind的事件
	$.fn.bindAfterLogin = function(type ,data){
		return this.each(function(){
			$this = $(this);
			//register events to events stack;
			$.EventsStack.push(this.id);
			$this.data("click" , data);
			if($.isLogin){
				$this.bind(type, data);
			}else{
				$this.bind(type , function(){
					login(this.id);
				});
			}
		});
	};

	//注册所有onclick事件
	$.fn.checkClick = function(){
		return this.each(function(){
			$this = $(this);
			data = $this.attr("onclick");
			$this.data("click" , data);
			if(!$.isLogin){
				$this.attr("onclick" , "");
				$this.bind('click' , function(){
					login(this.id);
				});
			}
			$.EventsStack.push(this.id);
		});
	}

	$.rebindEvents = function(){
		for(i=0;i<$.EventsStack.length;i++){
			$this = $("#" + $.EventsStack[i])
			$this.unbind();
			$this.bind("click" , $this.data("click"));
		}
	}


	$.setLogin = function(flag){
		$.isLogin = flag ? true : false;
		return this;
	}


})(jQuery);

//判断是否为图片
function isPic(v){
	if(!v)return;
	var pic = ['jpg','png','gif','bmp','JPG','PNG','GIF','BMP'];
	v = v.split('.');
	v = v[v.length-1];

	for(var i in pic){
		if(pic[i]==v){
			return true;
		}
	}
	return false;
}
$(function(){
	//get_win_size();
});

jQuery.fn.LoadImage=function(scaling,width,height,loadpic){
    if(loadpic==null)loadpic="/images/loading.gif";
	return this.each(function(){
		var t=$(this);
		var src=$(this).attr("src")
		var img=new Image();
		//alert("Loading...")
		img.src=src;
		//自动缩放图片
		var autoScaling=function(){
			if(scaling){
			
				if(img.width>0 && img.height>0){ 
			        if(img.width/img.height>=width/height){ 
			            if(img.width>width){ 
			                t.width(width); 
			                t.height((img.height*width)/img.width); 
			            }else{ 
			                t.width(img.width); 
			                t.height(img.height); 
			            } 
			        } 
			        else{ 
			            if(img.height>height){ 
			                t.height(height); 
			                t.width((img.width*height)/img.height); 
			            }else{ 
			                t.width(img.width); 
			                t.height(img.height); 
			            } 
			        } 
			    } 
			}	
		}
		//处理ff下会自动读取缓存图片
		if(img.complete){
		    //alert("getToCache!");
			autoScaling();
		    return;
		}
		$(this).attr("src","");
		var loading=$("<img alt=\"加载中...\" title=\"图片加载中...\" src=\""+loadpic+"\" />");
		
		t.hide();
		t.after(loading);
		$(img).load(function(){
			autoScaling();
			loading.remove();
			t.attr("src",this.src);
			t.show();
			//alert("finally!")
		});
		
	});
}
