$(document).ready(function(){
	$("#simplesearchbtn").bind("click" , function(){
		$("#simplesearchform").submit();
	});
	$("img.checkcodeimg").bind("click" , changecheckcodeimg);
	
	var username = $.cookie("account_un");
	if(username!=null && username!="")
	{
		$("#welcomemsg").html(wc_msg_login);
		$("span.username").text(username);
	}
	else
	{
		$("#welcomemsg").html(wc_msg_nologin);
	}
});
//全局函数
function getSafeJsStringValue(str)
{
	if(str==null)
	{
		return "";
	}
	return str.replace(/\'/g,"&#39;").replace(/\"/g,"&quot;");
	//return str.replace(/&/g,"&amp;").replace(/\'/g,"&#39;").replace(/\"/g,"&quot;").replace(/</g,"&lt;").replace(/>/g,"&gt;");
}
function dispatchWindowResizeEvent()
{
	if(!$.browser.msie)
	{
		var evt = document.createEvent("Events");
		evt.initEvent("resize", true, true);
		window.dispatchEvent(evt);
	}
}

function changecheckcodeimg()
{
	if($("#checkcode").length>0)
	{
		var date = new Date();
		$("#checkcodeid").val(date.getTime());
		$("img.checkcodeimg").attr("src", "checkcode.do?checkcodeid="+date.getTime());
		$("#checkcode").val("");
	}
}

function resizeMarkBackGround()
{
	$("#ajaxloginwindow_mark_div").css("width",$(document).width());
	$("#ajaxloginwindow_mark_div").css("height",$(document).height());
}

function ajaxMarkBackGround(display,zindex)
{	
	if(display)
	{
		if($("#ajaxloginwindow_mark_div").length<=0)
		{
			var markdivhtml='<div id="ajaxloginwindow_mark_div" />';
			$(markdivhtml).appendTo("body");
		}
		$("#ajaxloginwindow_mark_div").css("width",$(document).width());
		$("#ajaxloginwindow_mark_div").css("height",$(document).height());
		$("#ajaxloginwindow_mark_div").css("display","block");
		$(window).bind("resize",resizeMarkBackGround);
	}
	else
	{
		if($("#ajaxloginwindow_mark_div").length>0)
		{
			$("#ajaxloginwindow_mark_div").css("display","none");
			$(window).unbind("resize",resizeMarkBackGround);
		}
	}
	var oldzindex = $("#ajaxloginwindow_mark_div").css("z-index");
	if(zindex!=null && typeof(zindex)!= "undefined")
	{
		if(parseInt(zindex)>=10000)
		{
			$("#ajaxloginwindow_mark_div").css("z-index",parseInt(zindex));
		}
	}
	else
	{
		$("#ajaxloginwindow_mark_div").css("z-index",9999);
	}
	return oldzindex;
}

function showDialog(divid,left,top,modal,zindex)
{
	if($("#"+divid).length<=0)
	{
		return;
	}
	$("#"+divid).css("position","absolute");
	var body_width = $("#"+divid).find(".dialog_body_div").outerWidth(true);
	var title_width = $("#"+divid).find(".dialog_title_div").outerWidth(true);
	if(body_width!=title_width)
	{
		$("#"+divid).find(".dialog_title_div").width(body_width);
	}
	if(left == null)
	{
		var scrollLeft = document.documentElement.scrollLeft;
		var client_width =document.documentElement.clientWidth;
		var dialog_width = $("#"+divid).width(); 
		left = scrollLeft+(client_width-dialog_width)/2;
	}
	if(left<0)
	{
		left = 0;
	}
	$("#"+divid).css("left",left);
	
	if(top == null)
	{
		var scrollTop = document.documentElement.scrollTop;
		var client_height =document.documentElement.clientHeight;
		var dialog_height = $("#"+divid).height();
		top = scrollTop+document.body.scrollTop+(client_height-dialog_height)/2;
	}
	if(top<0)
	{
		top = 0;
	}
	$("#"+divid).css("top",top);
	
	if(typeof(zindex)== "undefined" || zindex==null || parseInt(zindex)<10001)
	{
		zindex = 10001;
	}
	$("#"+divid).css("z-index",zindex);
	if(modal)
	{
		ajaxMarkBackGround(true , parseInt(zindex)-1);
	}
	$("#"+divid).css("display","block");
	fitTitleWidth(divid);
}

function fitTitleWidth(divid)
{
	var titlediv = $("#"+divid+" > .dialog_title_div");
	if(titlediv.length>0)
	{
		var titleleftdiv = titlediv.children(".dialog_title");
		var titlerightdiv = titlediv.children(".dialog_close_btn");
		var width = titlediv.width();
		var rightwidth = titlerightdiv.width();
		titleleftdiv.width(width-rightwidth-10);
	}
}

function showIframeDialog(divid,url,left,top,modal,shadow,closedfunc,zindex)
{
	if($("#"+divid).length<=0)
	{
		var divhtml = '<div id="'+divid+'"><div class="dialog_title_div"><div class="dialog_title"></div><div class="dialog_close_btn"></div></div><div class="dialog_body_div iframe_dialog_body_div"><iframe id="'+divid+'iframe" src="'+url+'" scrolling=auto frameborder="no" border="0"/></div></div>';
		var dialog = $(divhtml);
		
		dialog.appendTo("body");
		var close_btn = $("#"+divid+" > div.dialog_title_div > div.dialog_close_btn");
		if(close_btn.length>0)
		{
			close_btn.click(function(){
				 closeDialog(divid,closedfunc);
			});
		}

			$("#"+divid+"iframe").bind("load",function(e){
				$("#"+divid).css("display","block");
				var iframe_body = $(this).contents().find("body");//$(document.getElementById(divid+'iframe').contentWindow.document.body);
				
				var iframewidth = iframe_body.width();
				if(iframewidth < iframe_body.outerWidth(true))
				{
					iframewidth = iframe_body.outerWidth(true);
				}
				var contentswidth = $(this).contents().width();
				if(iframewidth<contentswidth)
				{
					iframewidth = contentswidth;
				}
				$(this).width(iframewidth);
				var iframeheight = iframe_body.height();
				if(iframeheight < iframe_body.outerHeight(true))
				{
					iframeheight = iframe_body.outerHeight(true);
				}
				var contentsheight = $(this).contents().height();
				iframeheight = iframeheight+20;
				if(iframeheight < contentsheight)
				{
					iframeheight = contentsheight;
				}
				$(this).height(iframeheight);
				dialog.width($(this).outerWidth(true));
				$("#"+divid+" > div.dialog_title_div > div.dialog_title").text($(document.getElementById(divid+'iframe').contentWindow.document).attr("title"));
				$("#"+divid).css("display","none");
				/*
				if(!$.browser.msie)
				{
					$(document.getElementById(divid+'iframe').contentWindow).bind("change", function(e){
						iframewidth = iframe_body.width();
							if(iframewidth < iframe_body.outerWidth(true))
							{
								iframewidth = iframe_body.outerWidth(true);
							}
							$("#"+divid+"iframe").width(iframewidth);
							$("#"+divid+"iframe").height(iframe_body.height());
					});
				}
				else
				{
					$(document.getElementById(divid+'iframe').contentWindow).resize(function(){
							iframewidth = iframe_body.width();
							if(iframewidth < iframe_body.outerWidth(true))
							{
								iframewidth = iframe_body.outerWidth(true);
							}
							$("#"+divid+"iframe").width(iframewidth);
							$("#"+divid+"iframe").height(iframe_body.outerHeight(true));
					});
				}
				*/
				showDialog(divid,left,top,modal,zindex);
			});
	}
	else
	{		
		$("#"+divid+"iframe").attr("src",url);
		showDialog(divid,left,top,modal,zindex);
	}
}

function closeDialog(divid,closedfunc,zindex)
{
	if($("#"+divid).length>0)
	{
		$("#"+divid).css("display","none");
		$("#"+divid).css("top",0);
		$("#"+divid).css("left",0);
		ajaxMarkBackGround(false,zindex);
		if(jQuery.isFunction(closedfunc))
		{
			closedfunc();
		}
	}
}

function createDialog(dialogid,title,submitlabel,closelabel,labelArray,inputArray,closeFn,submitFn)
{
	var divstr = '<div id="'+dialogid+'"><div class="dialog_title_div"><div class="dialog_title">'+title+'</div><div class="dialog_close_btn"></div></div>';
	divstr += '<div class="dialog_body_div">';
	if(labelArray!=null && labelArray.length>0 && inputArray!=null && labelArray.length==inputArray.length)
	{
		for(var i=0;i<labelArray.length ;i++ )
		{
			divstr += '<div class="form_item"><label>'+labelArray[i]+'</label><div class="form_item_input">'+inputArray[i]+'</div></div>';
		}
	}
	else if((labelArray==null || labelArray.length==0) && inputArray!=null && inputArray.length>0)
	{
		for(var i=0;i<inputArray.length ;i++ )
		{
			divstr += '<div class="form_item"><div class="form_item_input">'+inputArray[i]+'</div></div>';
		}
	}
	divstr += '<div class="form_submit"><a href="#" id="'+dialogid+'_submit" class="button yellow">'+submitlabel+'</a><a href="#" id="'+dialogid+'_close" class="button blue">'+closelabel+'</a></div>';
	divstr += '</div></div>';
	$(divstr).appendTo("body");
	$("#"+dialogid+" > div.dialog_title_div > div.dialog_close_btn").click(function(e){
		if(e.target == this)
		{
			if(closeFn!=null && (typeof closeFn != 'undefined'))
			{
				closeFn();
			}
			else
			{
				closeDialog(dialogid);
			}
		}
	});
	$("#"+dialogid+"_submit").click(function(e){
		e.preventDefault();
		if(submitFn!=null &&  (typeof submitFn != 'undefined'))
		{
			submitFn();
		}
	});
	$("#"+dialogid+"_close").click(function(e){
		e.preventDefault();
		if(closeFn!=null && (typeof closeFn != 'undefined'))
		{
			closeFn();
		}
		else
		{
			closeDialog(dialogid);
		}
	});
}

function createCustomDialog(dialogid,title,submitlabel,closelabel,content,closeFn,submitFn)
{
	var divstr = '<div id="'+dialogid+'"><div class="dialog_title_div"><div class="dialog_title">'+title+'</div><div class="dialog_close_btn"></div></div>';
	divstr += '<div class="dialog_body_div">';
	divstr += content;
	divstr += '<div class="form_submit">';
	if(submitlabel!=null)
	{
		divstr += '<a href="#" id="'+dialogid+'_submit" class="button yellow">'+submitlabel+'</a>';
	}
	if(closelabel!=null)
	{
		divstr += '<a href="#" id="'+dialogid+'_close" class="button blue">'+closelabel+'</a>';
	}
	divstr += '</div></div></div>';
	$(divstr).appendTo("body");
	$("#"+dialogid+" > div.dialog_title_div > div.dialog_close_btn").click(function(e){
		if(e.target == this)
		{
			if(closeFn!=null && (typeof closeFn != 'undefined'))
			{
				closeFn();
			}
			else
			{
				closeDialog(dialogid);
			}
		}
	});
	$("#"+dialogid+"_submit").click(function(e){
		e.preventDefault();
		if(submitFn!=null &&  (typeof submitFn != 'undefined'))
		{
			submitFn();
		}
	});
	$("#"+dialogid+"_close").click(function(e){
		e.preventDefault();
		if(closeFn!=null && (typeof closeFn != 'undefined'))
		{
			closeFn();
		}
		else
		{
			closeDialog(dialogid);
		}
	});
}

function getPageNavDiv(totalpages,curpage,step,url,firstpagelabel,lastpagelabel,previouslabel,nextlabel)
{
	var start = 1;
	var end = 1;
	if(curpage<1)
		{
			curpage = 1;
		}
		if(curpage > totalpages)
		{
			curpage = totalpages;
		}
		if(step==1)
		{
			start = curpage;
			end = curpage;
		}
		else
		{
			var halfstep = 1;
			if(step%2==0)
			{
				halfstep = step/2;
			}
			else
			{
				halfstep = (step-1)/2;
			}
			if(curpage > halfstep && totalpages > step )
			{
				start =  curpage - halfstep;
			}
			if(start > (totalpages-step))
			{
				start = totalpages - step;
			}
			if(start<1)
			{
				start = 1;
			}
			if((curpage+halfstep) < totalpages && totalpages > 10)
			{
				end = curpage + halfstep;
			}
			else
			{
				end = totalpages;
			}
			if(end<step)
			{
				end = step;
			}
			if(end > totalpages)
			{
				end = totalpages;
			}
		}
		var str = '';
		if(curpage>1)
		{
			if(firstpagelabel!=null && firstpagelabel!='')
			{
				str += getPageNavLinkString(url,1,firstpagelabel);
			}
			if(previouslabel!=null && previouslabel!='')
			{
				str += getPageNavLinkString(url,curpage-1,previouslabel);
			}
		}
		if(start>1)
		{
			var pageindex = 1;
			if((curpage-step)>1)
			{
				pageindex = curpage-step;
			}
			str += getPageNavLinkString(url,pageindex,'...');
		}
		for(var i=start;i<=end;i++)
		{
			if(i!=curpage)
			{
				str += getPageNavLinkString(url,i,i);
			}
			else
			{
				str += '<strong>'+i+'</strong>';
			}
		}
		if(end < totalpages)
		{
			var pageindex = totalpages;
			if((curpage+step)<totalpages)
			{
				pageindex = curpage+step;
			}
			str += getPageNavLinkString(url,pageindex,'...');
		}
		if(curpage<totalpages)
		{
			if(nextlabel!=null && nextlabel!='')
			{
				str += getPageNavLinkString(url,curpage+1,nextlabel);
			}
			if(lastpagelabel!=null && lastpagelabel!='')
			{
				str += getPageNavLinkString(url,totalpages,lastpagelabel);
			}
		}
		return str;
}

function getPageNavLinkString(url,pageindex,label)
{
	var str = '<a pi="'+pageindex+'" href="';
	if(url!=null && url!='')
	{
		str += url;
		if(url.match(/=$/))
		{
			str += pageindex;
		}
	}
	else
	{
		str += '#';
	}
	str += '">';
	str += label;
	str += '</a>';
}
