$(document).ready(function() {
 	initRadioCheckbox();
	
 	iconButton();
 	initInput();
 	initDatagrid();
 	initForm();
 	initToggle();
 	initTextarea();
	initChangeSkin();
	/**/
})
// 添加原型
Number.prototype.NaN0=function(){return isNaN(this)?0:this;}
// 计算对象的座标
function getPosition(e){
    var left = 0;
    var top  = 0;
    while (e.offsetParent){
        left += e.offsetLeft + (e.currentStyle?(parseInt(e.currentStyle.borderLeftWidth)).NaN0():0);
		top  += e.offsetTop  + (e.currentStyle?(parseInt(e.currentStyle.borderTopWidth)).NaN0():0);
		e     = e.offsetParent;
    }
    left += e.offsetLeft + (e.currentStyle?(parseInt(e.currentStyle.borderLeftWidth)).NaN0():0);
	top  += e.offsetTop  + (e.currentStyle?(parseInt(e.currentStyle.borderTopWidth)).NaN0():0);
    return {x:left, y:top};
}
//base jquery
//读取当前coolie
  function  readCookie(){
    var skin='default';
    var mycookie=document.cookie;
    var name="BLUE_Manager";
    var start1=mycookie.indexOf(name+"=");
    if(start1==-1)
	{
     skin='default';//如果没有设置则显示默认样式
    }else{
        var start=mycookie.indexOf("=",start1)+1;
        var end=mycookie.indexOf(";",start);
        if(end=-1)
		{
            end = mycookie.length;
        }
        var values= unescape(mycookie.substring(start,end));
        if (values!=null)
        {
            skin = values;
        } 
    }	
    return skin;
}
//需要改进
  function initChangeSkin(){
	var skins = readCookie().split(";")[0];
	 if($("link").attr("href").indexOf("left")!=-1)
	 {
		$("link").eq(0).attr("href","skins/"+skins+"/left.css") 
	 }
	 if($("link").attr("href").indexOf("main")!=-1 && window.top.frames["leftFrame"])
	 {
		// alert(readCookie());
		var skinurl = $(window.top.frames["leftFrame"].document.getElementsByTagName("link")[0]).attr("href");
		var start = skinurl.indexOf("skins")+6  ,end = skinurl.lastIndexOf("/") ; 
	    $("link").eq(0).attr("href",$("link").attr("href").substring(0,$("link").attr("href").indexOf("skins"))+"skins/"+skinurl.substring(start,end)+"/main.css")
	 }
}

//初始化控件radio,checkbox,select
function initRadioCheckbox(){
		var onStyle = "green";
		var editBlock =".editBlock";
		//radio选中变色
		$(editBlock).find(":radio:checked").parent().toggleClass(onStyle);
	    $(editBlock).find(":radio").click(function(){$(this).parent().parent().find("label").removeClass(onStyle);$(this).parent().addClass(onStyle)})
		//checkbox选中变色
		/*
        if(!$(editBlock).find("input:checked").parent().hasClass(onStyle)){
		$(editBlock).find("input:checked").parent().addClass(onStyle);
		}	
		*/
		$(editBlock).find(":checkbox").click(function(){
			if($(this).attr('checked')==true){
			if(!$(this).parent().hasClass(onStyle)){
			  $(this).parent().addClass(onStyle)
			}
			}
			else{
			if($(this).parent().hasClass(onStyle)){
			  $(this).parent().removeClass(onStyle)
			}
			}			
			}
			)
		//select选中变色
		$("select option:selected").toggleClass(onStyle);
		$("select").change(function(){$(this).find("option").removeClass(onStyle);$(this).find("option:selected").addClass(onStyle);})
		$("select").find("option").each(function(i){$(this).attr("title",$(this).text())});
		}
//初始化图标按钮,主要用于图标操作按钮
function iconButton(){
		$(":image").addClass("iconBtn")
		$(":image").focus(function(){this.blur()})
		.hover(function(){$(this).attr("class","iconBtnOver")},function(){$(this).attr("class","iconBtn")})
		.mousedown(function(){$(this).attr("class","iconBtnDown")})     
		.mouseout(function(){$(this).attr("class","iconBtn")});
		$(":image:disabled").addClass("iconBtnDisabled").attr("title","不可用");
		}
//初始化按钮
function initInput(){
$(":button,:submit").hover(function(){ $(this).attr("class","buttonOver")}, function(){$(this).attr("class","inputButton") });
}
//初始化数据表格
function initDatagrid(){
	var dataGrid = ".dataGrid", editBlock =".editBlock";
	try{
		if($(dataGrid).find("thead>tr>th").eq(0).children().is(":checkbox"))
		{
		$(dataGrid).find("thead").find("th")[0].style.width = "20px" ;	
		}
    if(!$(editBlock).find("tbody >tr> th").attr("width"))
		{
		$(editBlock).find("tbody >tr> th").width("120px");
		}
	}catch(e)
	{
	}
	var tableHover = ".tableHover";
	$(tableHover).find("tr").hover(function(){ $(this).addClass("trHover")}, function(){$(this).removeClass("trHover") });
	/*
	$(":checkbox[checkSelector=1]").click(function(){
		var p = $(this);
		$(":checkbox[checkNode=1]").filter(function(){return !$(this).attr("disabled")}).each(function(){this.checked = p.attr("checked")});
	})
	
	$(":checkbox[checkNode=1]").click(function(){
		var p = $(this);
		x = $(":checkbox[checkNode=1]").filter(function(){return !$(this).attr("disabled")});
		var k = 0;
		for(var i=0;i<x.length;i++) {if(x.get(i).checked) { k++ };}
		var t = $(":checkbox").filter(function(){return $(this).attr("checkSelector") == p.attr("checkNode")});
		if(k==x.length) { 
			t.each(function(){this.checked = true}) 
		} else {
			t.each(function(){this.checked = false}) 
		}
	})
	*/
}
//初始化表单	
function initForm(){
		try{
		$(":text,:password,:file").addClass("inputText");
		$("textarea[readonly='readonly'],:text[readonly='readonly'],:password[readonly='readonly']").focus(function(){this.blur();})
		$("textarea,:text,:password,:file").focus(function(){ $(this).addClass("onFocus"); }).blur(function(){ $(this).removeClass("onFocus"); })
		$(".page select").removeClass("sele");
		$(":text").filter(function(){return $(this).attr("class") != "Wdate"}).get(0).focus();
       
	}
	catch(e){}
	}
////////////////////// initForm end ////////////////////////
	//以subtitle作为需要点击的class
 function initToggle()
 {
	$(".subtitle,.subtitleClose").click(function(){
		
		 var tb = $(this).parent().parent().next('tbody');
		
		 if(tb.css('display')=='none'){
			
		 tb.show();
		  $(this).attr("class","subtitleClose");
		 }
		 else{
			
		 tb.hide();
		  $(this).attr("class","subtitle");
		 }
		
         
	});

 }
//title栏
function initTitleBar()
{
	if($("div[class='title']").get() && top.frames["leftFrame"])
	{
	 var  leftKnot = top.frames["leftFrame"].document.getElementById("menu");
	 var  firstTitle =  $(leftKnot).find("dt[class='on']").text() ;
	 var  secondTitle = $(leftKnot).find(".focus").text() ;
	 var  thirdTitle  = $(".tab .on span").text();
		if(firstTitle!="" && secondTitle!="")
		{
		$("div[class='title']").html("<span>当前位置：</span>"+firstTitle+" &gt; "+secondTitle+" &gt; "+thirdTitle)	;
		}else{
		$("div[class='title']").html("<span>当前位置：</span>");
		}
		//国际化
	/*	if(firstTitle!="" && secondTitle!="")
		{
		$("div[@class=title]").find("span").after(firstTitle+" &gt; "+secondTitle+" &gt; "+thirdTitle)	;
		}else{
		$("div[@class=title]").find("span").after('')	;
		}	
	*/	
    }
}
//输入字数限制
function initTextarea()
{
	
	$("textarea[class='maxchar']").after("<div class='remain'>&nbsp;&nbsp;您已经输入<span></span>个字符&nbsp;&nbsp;</div>")
	$("textarea[class='maxchar']").each(function(){
								var lens= $(this).val().replace(/[^\x00-\xff]/gi,'c').length ;  
								$(this).next("div[class='remain']").children("span").text(lens);
								$(this).next("div[class='remain']").width($(this).width())
	})
	$("textarea[class='maxchar']").bind("keyup",charLeft).bind("keydown",charLeft).bind("change",charLeft);
	
}
	
 function charLeft()
{ 
   var lens= $(this).val().replace(/[^\x00-\xff]/gi,'h').length ;  
   $(this).next("div[class='remain']").children("span").text(lens); 
} 
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//居中弹出窗口
function popUp(url,width,height,winname,left,top)
{       
	    var width = width || 400,height =height|| 350;
		var left = (left==''||left==null)?(screen.width - width)/2:left;
		var top = (top==''||top==null)?(screen.height - height)/2:top;
		var winnames = (winname=='')?'popUpWin':winname;
		window.open(url, winnames, 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,copyhistory=yes,width='+width+',height='+height+',left='+left+', top='+top+',screenX='+left+',screenY='+top+'');
}
//返回
function goback()
{
	history.back();
}
//最大值
function checkMaxInput(_this,maxleng,leftInforId) 
{ 
	var left=document.getElementById(leftInforId); 
	var len=_this.value.replace(/[^\x00-\xff]/gi,'ch').length; 
	var remainnum =parseInt(maxleng)-len; 
	left.value = remainnum; 
	if(remainnum < 0) 
	{ 
		if(_this.value.length!=len) 
		{ 
			if((len-_this.value.length)>(maxleng/2)) 
			{ 
			_this.value=_this.value.substring(0,maxleng/2); 
			} 
			else 
			{ 
			_this.value=_this.value.substring(0,maxleng-(len-_this.value.length)); 
			} 
		}else{ 
			_this.value=_this.value.substring(0,maxleng); 
		} 
			left.value=0; 
			return; 
	} 
}
function reset_form(obj){
 obj_form=	obj.parents("form");
obj_form.find(":text").val("");
obj_form.find("select").attr("value",'');
return false;

}