var Util = {};	//公共组件

/*
 * 配置信息
 */
Util.Config = {
	//树--是否下拉
	Tree_Can_Drop: true,
	//遮罩层背景颜色
	Screen_Background: "#333",
	//遮罩层透明度
	Screen_Opacity: "6",
	//遮罩层内容背景颜色
	Screen_ContentBg: "#fff",
	//菜单滑动效果间隔时间
	Tab_TimeLong: 250,
	Img_TimeLong: 150,
	Result_TimeOut:4000,
	Reulst_Top:0
}

/*
 * 菜单滑动效果
 *
 */
Util.TabControl = function(pBtnList,pActiveStyle,pDisableStyle,defaultKey){
	var self = this;
	var _obj = {
		List: pBtnList,
		ActiveStyle: pActiveStyle,
		DisableStyle: pDisableStyle
	};
	
	this.ChangeHandler;
	var init = function(){
		var time ;
		var overObj;
		function display(){
			for(var j = 0; j < _obj.List.length; j++){
				var ele = $(_obj.List[j].Tab);
				ele.removeClass(_obj.ActiveStyle);
				ele.addClass(_obj.DisableStyle);
				$(_obj.List[j].Content).style.display = "none";
			}
			$(overObj.Tab).addClass(_obj.ActiveStyle);
			$(overObj.Content).style.display = "";
			if(self.ChangeHandler){
				self.ChangeHandler(overObj);
			}
		}
		for(var i = 0; i < _obj.List.length; i++){
			var item = _obj.List[i];
			$(item.Tab).bind("mouseover",function(e){
				overObj = e;
				time = window.setTimeout(display,Util.Config.Tab_TimeLong);
			},[item]);
			$(item.Tab).bind("click",function(e){
				overObj = e;
				if(time != undefined){
					window.clearTimeout(time);
				}
				display();
				e.Tab.blur();
			},[item]);
			$(item.Tab).bind("mouseout",function(){
				if(time != undefined){
					window.clearTimeout(time);
				}
			});
		}
		if(defaultKey != undefined){
			overObj = _obj.List[defaultKey];
			display();
		}
	}
	init();
}

/*
 * 弹出层
 */
Util.ScreenManager = {
	Hide: function(){
		this.canClose = true;
		this.popCoverDiv(false);
	},
	Show: function(containBox,isClickHide){
		if(isClickHide != undefined){
			Util.ScreenManager.IsClickHide = isClickHide;
		}
		else{
			Util.ScreenManager.IsClickHide = false;
		}
		this.popCoverDiv(true,containBox);
	},
	//取得页面的高宽
	getBodySize: function (){
		var bodySize = [];
		with(document.documentElement) {
			bodySize[0] = (scrollWidth>clientWidth)?scrollWidth:clientWidth;//如果滚动条的宽度大于页面的宽度，取得滚动条的宽度，否则取页面宽度
			bodySize[1] = (scrollHeight>clientHeight)?scrollHeight:clientHeight;//如果滚动条的高度大于页面的高度，取得滚动条的高度，否则取高度
		}
		return bodySize;
	},
	canClose:true,
	//创建遮盖层
	popCoverDiv: function (isShow,containBox,setWidth){
		var screenBox = $("screen_cover_div");
		if (screenBox == undefined) {
			//如果存在遮盖层，则让其显示 
			//否则创建遮盖层
			var coverDiv = document.createElement('div');
			document.body.appendChild(coverDiv);
			coverDiv.id = 'screen_cover_div';
			var bodySize;
			with(coverDiv.style) {
				if ($.browser.msie && $.browser.version == 6) {
					position = 'absolute';
					background = Util.Config.Screen_Background;
					left = '0px';
					top = '0px';
					bodySize = this.getBodySize();
					width = '100%';
					height = bodySize[1] + 'px';
				}
				else{
					position = 'fixed';
					background = Util.Config.Screen_Background;
					left = '0';
					top = '0';
					width = '100%'
					height = '100%';
				}
				zIndex = 0;
				if ($.browser.msie) {
			   		filter = "Alpha(Opacity=" + Util.Config.Screen_Opacity + "0)";	//IE逆境
				} else {
			   		opacity = Number("0."+Util.Config.Screen_Opacity);
				}
			}
			screenBox = $(coverDiv);
			screenBox.bind("click",function(){
				if(Util.ScreenManager.canClose){
					
					if(Util.ScreenManager.IsClickHide == undefined || Util.ScreenManager.IsClickHide == false){
						$(this).style.display = "none";
						$("screen_content_box").style.display = "none";
					}
				}
			});
			
			var contentDiv = document.createElement("div");
			contentDiv.id = "screen_content_box";
			with(contentDiv.style){
				position = "absolute";
				backgroundColor = Util.Config.Screen_ContentBg;
				var widthNum = Number(setWidth != undefined?setWidth:500);
				width = widthNum + "px";
				left = '50%';
				var mfNum = widthNum/2;
				marginLeft = "-" + mfNum + 'px';
				top = '23%';
				zIndex = 1;
			}
			document.body.appendChild(contentDiv);
			var  contentBox = $(contentDiv);
			contentBox.bind("mouseover",function(){
				Util.ScreenManager.canClose = false;
			});
			contentBox.bind("mouseout",function(){
				Util.ScreenManager.canClose = true;
			});
		}
		screenBox.style.display = isShow ? "block" : "none" ;
		$("screen_content_box").style.display = isShow ? "block" : "none" ;
		if(isShow && containBox){
			//创建Cache Box
			var cacheBox = $("screen_cache_box");
			if(cacheBox == undefined){
				var cBox = document.createElement("div");
				document.body.appendChild(cBox);
				cBox.id = "screen_cache_box";
				cBox.style.display = "none";
				cacheBox = $(cBox);
			}
			var cBox = $("screen_content_box");
			var contentNodes = cBox.childNodes;
			for(var i = 0,len = contentNodes.length; i < len; i++){
				cacheBox.appendChild(contentNodes[i]);
			}
			containBox.style.display = "";
			cBox.appendChild(containBox);
		}
		this.canClose = true;
	}
}

/*
* 验证
*/
Util.Validate = {
	reg:{
		url:/^\b(((https?|ftp):\/\/)?[-a-z0-9]+(\.[-a-z0-9]+)*\.(?:com|edu|gov|int|mil|net|org|biz|info|name|museum|coop|aero|[a-z][a-z]|((25[0-5])|(2[0-4]\d)|(1\d\d)|([1-9]\d)|\d))(:\d+)?\b(\/[-a-z0-9_:\@&?=+,.!/~%\$#]*)?)$/i,
		email:/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/
	},
	type:{
		extent:function(inputDom){
			//输入长度验证
			var minSize = Number(inputDom.attr("minsize"));
			var maxSize = Number(inputDom.attr("maxsize"));
			var v = $.trim(inputDom.value);
			var len = v.length;
			if(len >= minSize && len <= maxSize){
				return 0;	//通过
			}
			return -1;	//failed
		},
		url:function(inputDom){
			//网址验证
			var v = $.trim(inputDom.value);
			v = v.replace(/^((https|http|ftp|rtsp|mms)?:\/\/)?/,"");
			v = "http://" + v;
			if(Util.Validate.reg.url.test(v)){
				return 0;
			}
			return -2;
		},
		email:function(inputDom){
			//邮箱验证
			if(Util.Validate.reg.email.test($.trim(inputDom.value))){
				return 0;
			}
			return -3;
		}
	},
	checkInput: function(ele){
		var valArr = ele.attr("validate").split("|");
		var valState = "";
		for(var i = 0,len = valArr.length; i < len; i++){
			var valItem = valArr[i];
			var state = Util.Validate.type[valItem](ele);
			if(state != 0){
				valState += "," +  state;
			}
		}
		if(valState != ""){
			valState = valState.substring(1,valState.length);
			ele.attr("pass","0");
		}
		else{
			ele.attr("pass","1");
		}
		var msgSpanBox = $(ele.attr("msgbox"));
		if(Util.Validate.StateChangeHandler){
			Util.Validate.StateChangeHandler(ele,msgSpanBox,valState);
		}
	},
	BindingForm:function(formEle){
		var formDom = $(formEle);
		var inputList = Util.Validate.getCheckEleList(formDom);
		for(var i = 0,len = inputList.length; i < len; i++){
			var item = $(inputList[i]);
			item.bind("blur,change",function(){
				//鼠标离开事件
				var ele = $(this);
				Util.Validate.checkInput(ele);
			});
			item.bind("focus",function(){
				//焦点事件
				var ele = $(this);
				ele.select();
				ele.attr("pass","0");
				var msgSpanBox = $($(this).attr("msgbox"));
				if(Util.Validate.StateInitHandler){
					Util.Validate.StateInitHandler(ele,msgSpanBox);
				}
			});
		}
	},
	getCheckEleList:function(formDom){
		var inputList = formDom.find("input","%validate");
		var textareaList = formDom.find("textarea","%validate");
		if(textareaList && textareaList.length > 0){
			for(var i = 0,len = textareaList.length; i < len; i++){
				inputList.push(textareaList[i]);
			}
		}
		return inputList;
	},
	FormSubmitHandler: function(ele){
		//form 提交事件
		var formDom = $(ele);
		var inputList = Util.Validate.getCheckEleList(formDom);
		
		var passState = true;
		var errorInput = [];
		for(var i = 0,len = inputList.length; i < len; i++){
			var boxItem = $(inputList[i]);
			if(boxItem.attr("pass") == undefined || boxItem.attr("pass") == "0"){
				Util.Validate.checkInput(boxItem);
			}
			if(boxItem.attr("pass") == "0"){
				if(passState) passState = false;
				errorInput.push(boxItem);
			}
		}
		if(passState){
			return true;
		}
		else{
			if(Util.Validate.FormErrorHandler){
				Util.Validate.FormErrorHandler(errorInput);
			}
			return false;
		}
	}
}

/**
 * 验证模块
 * 
 */
Util.NameValidate = {};

/**
 * 验证名称
 * 
 * @param {String}
 *            category_name
 * @return {Boolean}
 */
Util.NameValidate.FolderName = function(folderName) {
	folderName = folderName.replace(' ', '');
	if (folderName.length < 1) {
		alert('文件夹名不能少于2个字！');
		return false;
	}
	if (folderName.length > 20) {
		alert('文件夹名不能超过20个字！');
		return false;
	}
    var regular = /^[^\\/:*?<>|\\\\\"]*$/;
	if (!regular.test(folderName)) {
	   alert("文件名不能包含下列任意字符之一\n\ / :. * ?\" < > |");
	   return false;
	}

	return true;
}

/**
 * 显示结果静态方法
 * PageResult.Show(msg,isSuccess); //(msg: 内容; isSuccess： true成功操作，false失败操作)
 */
var PageResult = {};
PageResult.Show = function(msg,isSuccess,timeOut,isHide){
	if(PageResult.ControlObject == undefined){
		PageResult.ControlObject = new PageResult.ShowResultObject({
			Top: Util.Config.Reulst_Top + "px"
		});
	}
    PageResult.ControlObject.ShowMessage(msg,isSuccess);
	var time = Util.Config.Result_TimeOut;
	if(timeOut){
		time = timeOut
	}
	if(isHide == undefined || isHide == true){
		window.clearTimeout(PageResult.Timer);
    	PageResult.Timer = window.setTimeout("PageResult.Hide()",time);
	}
}

PageResult.Hide = function(){
	PageResult.ControlObject.Hide();
	window.clearTimeout(PageResult.Timer);
}
 
PageResult.ShowResultObject = function(obj){
    var _ResultBpx;
	var _obj = obj == undefined?{}:obj;
    var _DisplayTextBox;
    if(_obj.BoxID !=undefined){
        _ResultBpx = document.getElementById(_obj.BoxID);
    }
    else{
        _ResultBpx = document.createElement("div");
        document.body.appendChild(_ResultBpx);
    }
    if(_obj.DisplayBoxId != undefined){
        _DisplayTextBox = document.getElementById(_obj.DisplayBoxId);
    }
    else{
        _DisplayTextBox = document.createElement("span");
        _ResultBpx.appendChild(_DisplayTextBox);
    }

    var setStyle = function(isPerfect){
        _DisplayTextBox.style.color = isPerfect?"#48A01B" : "#FF0000";
        var  resultStyle = _ResultBpx.style;
        resultStyle.backgroundColor =  "#FFF8CC";
        resultStyle.position = "absolute";
        resultStyle.zIndex = 1000;
        resultStyle.padding = "1px 6px";
        var l = document.documentElement.clientWidth * 44/100;
        resultStyle.left = _obj.Left == undefined? (l + "px") : _obj.Left;
		resultStyle.top = _obj.Top == undefined? "0px" : _obj.Top;
    }

    this.getResultBox = _ResultBpx;
    this.getDisplayBox = _DisplayTextBox;
    this.Hide = function(){
        this.getResultBox.style.display = "none";
		this.getDisplayBox.innerHTML =  "";
    },
    this.ShowMessage = function(text,isSuccess){
        setStyle(isSuccess);
        this.getDisplayBox.innerHTML = text;
        this.getResultBox.style.display = "";
    }
}

