// Code in 2007.1

var
/*
后台框架应用
*/
oHome		= 'Main.html',
sName		= '网站后台管理系统',
iFrame		= null,
oFrame		= null,
wFrame		= null,
prevMenu	= null,
prevMMenu	= null,
prevVMenu	= null,
prevVer		= null,
screenWidth	= window.screen.width,
pageSize	= null,
// 初始化
init = function() {
	if(window != top) {
		top.location.replace(self.location);
		return;
	}
	
	// 生成并初始化语言版本
	//version.init();
	var tempPMenu = getCookie('prevMenu');
	if(tempPMenu!='') {
		menu.show(tempPMenu, 1);
	}else{
		menu.show($t('div', $("menuWarp"))[0], 1);
	}
	
	// 显示当前日期
	dateTime.showObj = $('showDate');
	dateTime.show(true, true, true);

	// 框架内容页指向
	iFrame = $('main_data');
	oFrame = iFrame.Document ? iFrame.Document : iFrame.contentDocument;
	
	// 指向到最后一次访问页面
	var lastPage = getCookie('LastPage');
	if(lastPage=='') {
		fGoUrl(oHome);
	}else{
		var tmpPage = LCase(lastPage);
		var isHome = tmpPage.indexOf('default')==-1 ? false : true;
		isHome ? fGoUrl(oHome) : fGoUrl(lastPage);
	}
	
	// 设置键盘监听
	document.onkeydown = function(e) {keyDown(e);}
	
	
	// 去除框架链接虚框
	var aEle = $t('a');
	for(var a in aEle) {
		aEle[a].onfocus = function() {if(this.blur) this.blur();}
	}
	
	// 初始化框架布局
	frame.init();
	
	// 设置闲置事件
	timeOut.init();
},

// 闲置事件
timeOut = {
	time : 20,
	startTime : null,
	date : new Date(),
	doCheck : null,
	init : function() {
		//
	},
	check : function() {
		//
	}
},

// 页面reSize控制布局
frame = {
	reSizeEnd : false,
	reSizeTime : null,
	init : function() {
		frame.oHeight = $('top').clientHeight + $('main_toolbar').clientHeight + 1;
		frame.footheight = $('footer').clientHeight;
		frame.menuHeight = $('menu_label').offsetHeight+$('menuWarp').offsetHeight+$('menuCopyright').offsetHeight;
		frame.iFrameHeight = null;
		frame.reSizeEnd = true;
	},
	findFDoc : function() {
		frame.reSizeEnd = false;
		iFrame = $('main_data');
		oFrame = iFrame.Document ? iFrame.Document : iFrame.contentDocument;
		wFrame = iFrame.contentWindow;
	},
	onLoad : function() {
		frame.findFDoc();
		if(oFrame.location=='about:blank') return;
		// 设置工具栏
		var _oTool = oFrame.getElementById('locationTool');
		var _tVar = '<a href="javascript:self.history.back();" class="icoBtn_ct" onfocus="if(this.blur) this.blur();"><img src="Images/Empty.gif" class="btnIco btnBack" \/>返回<\/a>'+
					'<a href="javascript:self.location.reload();" class="icoBtn_ct" onfocus="if(this.blur) this.blur();"><img src="Images/Empty.gif" class="btnIco btnReload" \/>刷新<\/a>'+
					'<span class="split ftSplit" ><\/span>';
		if(_oTool != null) _oTool.innerHTML = _tVar;
		// 设置整站标题，添加鼠标双击监听
		oFrame.ondblclick = function(e) {fDblClick(e);}
		oFrame.onkeydown = function(e) {keyDown(e);}
		document.title = sName +' ['+ oFrame.title +']';
		$('manage_title').innerHTML = oFrame.title;
		// 设置最后访问页面
		page.setLastPage();
		wFrame.onresize = function() {
			//alert('window.onresize');
			window.parent.frame.setHeight();
		}
		// 设置iFrame高度
		frame.setHeight();
	},
	setHeight : function() {
		window.clearTimeout(frame.reSizeTime);
		frame.iFrameHeight = iFrame.Document ? oFrame.body.scrollHeight : oFrame.body.offsetHeight;
		pageSize = getPageSize();
		var minHeight = pageSize[3] - frame.oHeight;
			minHeight = frame.menuHeight > minHeight-frame.footheight ? frame.menuHeight : minHeight-frame.footheight;
			minHeight = minHeight-20 < 500 ? 500 : minHeight-20;
		//
		//alert(frame.iFrameHeight +' _ '+ minHeight);
		//
		// 获取并设置框架内容页高度
		iFrame.style.height = frame.iFrameHeight>minHeight ? frame.iFrameHeight +'px' : minHeight +'px';
		
		frame.reSizeEnd = true;
	},
	setWidth : function() {
		// 调整整体框架大小(最小1003)
		var screenWidth = window.screen.width;
		if(pageSize[2]<1003 || screenWidth<1024){
			$('warp').style.width = '1003px';
		}else{
			$('warp').style.width = '';
		}
		frame.setHeight();
	}
},

page = {
	// 设置最后访问页面
	setLastPage : function() {
		var oHref = oFrame.location.href.toString();
		var oPage = oHref.substr(oHref.lastIndexOf('/')+1);
		setCookie('LastPage', oPage);
	}
},

// 语言版本
version = {
	// 初始化
	init : function() {
		var labItem = $t('li', $('menu_label_warp'));
		var lastLab = labItem[labItem.length-1];
		$('menu_label_item').style.width = lastLab.offsetLeft+lastLab.offsetWidth+4 +'px';
		labItem.scrollLeft = 0;
		
		// 展开刷新前菜单
		var tempPVer = getCookie('prevVer');
		var tempPMenu = getCookie('prevMenu');
		if(tempPVer!='') {
			version.show(tempPVer);
			menu.show(tempPMenu, 1);
		}else{
			version.show($t('li', $('menu_label_warp'))[0]);
		}

		$('menu_label_doLeft').onmousedown = function() {version.startScroll(1)};
		$('menu_label_doRight').onmousedown = function() {version.startScroll(0)};
		$('menu_label_doLeft').onmouseup = $('menu_label_doRight').onmouseup = function() {version.stopScroll();};
	},
	// 生成菜单
	create : function(Versions) {
		if(typeof(Menus) != 'object') {
			$('menu_label').display = 'none';
			return;
		}
		var mHtml = '';
		mHtml += '<ul id="menu_label_item">\n';
		for(var v in Versions){
			mHtml += '	<li id="ver_'+ v +'" menu="menu_'+ v +'"><a href="javascript:void(0);" onmousedown="version.show(\'ver_'+ v +'\');">\n'+
					 '		<span class="label_left"><\/span>\n'+
					 '		<span class="label_text">'+ Versions[v] +'<\/span>\n'+
					 '		<span class="label_right"><\/span>\n'+
					 '	<\/a><\/li>\n';
		}
		mHtml += '<\/ul>\n';
		$('menu_label_warp').innerHTML = mHtml;
	},
	// 显示语言版本Lable
	show : function(ver) {
		ver = $(ver);
		if(prevVer == ver) return;
		/* 设置Lable样式 */
		ver.className = 'selected';
		if(prevVer != null) prevVer.className = '';
		prevVer = ver;
		setCookie('prevVer', prevVer.id);
		
		/* 滚动到显示当前Lable */
		var warp = $('menu_label_warp');
		var sclLX =  ver.offsetLeft-warp.scrollLeft-3;
		var sclRX = (ver.offsetLeft+ver.offsetWidth)-(warp.scrollLeft+warp.offsetWidth);
		var sclX = 0;
		if(sclRX>0) {
			sclX = sclRX;
		}else if(sclLX<0) {
			sclX = sclLX;
		}
		if(sclX!=0) warp.scrollLeft += sclX;
		// 显示菜单
		var vMenu = ver.getAttribute('menu');
		vMenu = $(vMenu);
		menu.showWarp(vMenu);
	},
	// 滚动语言版本Lable
	startScroll : function(_m) {
		version.isScroll = true;
		clearInterval(version.doScl);
		version._sclX = 2;
		version._sclX = _m==0 ? version._sclX : -version._sclX;
		version.doScl = setInterval(version.scroll, 10);
	},
	stopScroll : function() {
		version.isScroll = false;
		clearInterval(version.doScl);
	},
	scroll : function() {
		if(!version.isScroll) {
			version.stopScroll();
			return;
		}
		var warp = $('menu_label_warp');
		warp.scrollLeft += version._sclX;
		if(warp.scrollLeft==(warp.scrollWidth-warp.offsetWidth) || warp.scrollLeft==0) version.stopScroll();
	}
},

// 菜单
menu = {
	// 生成菜单
	create : function(Menus, ver) {
		if(typeof(Menus) != 'object') return false;
		var mHtml = '';
		var sMenus = new Object();
		var row = 1;
		var i = 0, j, n = 0;
		for(var m in Menus){
			sMenus = Menus[m];	// 菜单项列表
			i++;
			j = n = 0;
			// 开始一个新的菜单项
			mHtml += '<div class="left_menu" id="menu_'+ ver +'_'+ i +'" menu="'+ m +'">\n'+
					 '	<div class="title toolbarBg"><a href="javascript:void(0);" onclick="menu.show(\'menu_'+ ver +'_'+ i +'\');"><img src="Images/Empty.gif" class="btnIco btnClose" \/>'+ m +'<\/a><\/div>\n'+
					 '	<div class="link">\n'+
					 '		<table>\n'+
					 '			<tr>\n';
			for(var sm in sMenus){
				row = getLen(sm)>10 ? 1 : 2;
				if(row != 2){		// 是否两列显示
					if(j%2 != 0) {
						mHtml += '			<\/tr>\n'+
								 '			<tr>\n';
						j++;
					}
					mHtml += '				<td colspan="3"><a href="'+ sMenus[sm] +'" target="main_data">'+ sm +'<\/a><\/td>\n'+
							 '			<\/tr>\n'+
							 '			<tr>\n';
				}else{
					if(j%2 == 0){
						mHtml += '				<td><a href="'+ sMenus[sm] +'" target="main_data">'+ sm +'<\/a><\/td>\n';
					}else{
						mHtml += '				<td width="1">|<\/td>\n'+
								 '				<td align="right"><a href="'+ sMenus[sm] +'" target="main_data">'+ sm +'<\/a><\/td>\n'+
								 '			<\/tr>\n'+
								 '			<tr>\n';
					}
					j++;
				}
				n++;
			}
			// 如果菜单表格没结束
			if((j%2 != 0) && (row == 2)){
				mHtml += '				<td width="1">&nbsp;<\/td>\n'+
						 '				<td align="right">&nbsp;<\/td>\n'+
						 '			<\/tr>\n'+
						 '			<tr>\n';
			}else if(n == 0){		// 如果菜单项为空
				mHtml += '				<td align="center" height="30">当前没有可管理项！<\/td>\n'+
						 '			</tr>\n';
			}
			// 结束菜单项
			/*
			mHtml += '			<\/tr>\n'+
					 '		<\/table>\n'+
					 '	<\/div>\n'+
					 '<\/div>\n';
			*/
			mHtml = mHtml.substr(0, mHtml.lastIndexOf('/')+6);
			mHtml += '		<\/table>\n'+
					 '	<\/div>\n'+
					 '<\/div>\n';
		}
		// 输出菜单
		var cvMenu = appendElement('div', {'id' : 'menu_'+ ver}, mHtml, $('menuWarp'));
		cvMenu.style.display = 'none';
	},
	// 显示隐藏菜单
	showWarp : function(vMenu) {
		vMenu = $(vMenu);
		if(prevVMenu != null && prevVMenu != vMenu) prevVMenu.style.display = 'none';
		if(vMenu==undefined) return;
		vMenu.style.display = '';
		prevVMenu = vMenu;
		menu.show($t('div', vMenu)[0], 1);
	},
	// 显示隐藏菜单项
	show : function(menu, isShow) {
		menu = $(menu);
		if(menu==undefined) return;
		if(prevMenu != null && prevMenu != menu) prevMenu.className = 'left_menu';
		if(isShow != undefined) {
			menu.className = isShow == 1 ? 'left_menu_open' : 'left_menu';
		}else{
			menu.className = menu.className == 'left_menu' ? 'left_menu_open' : 'left_menu';
		}
		prevMenu = menu;
		setCookie('prevMenu', prevMenu.id);
	},
	// 显示隐藏左侧整列菜单
	showLeft : function() {
		if($('left').className == 'left_close'){
			$('left').className = 'left_border';
			$('main').className = 'main_border';
			$('btnShowLeft').className = 'sh_left_open';
			$('btnShowLeft').setAttribute('title', '隐藏左侧导航菜单');
		}else{
			$('left').className = 'left_close';
			$('main').className = '';
			$('btnShowLeft').className = 'sh_left';
			$('btnShowLeft').setAttribute('title', '显示左侧导航菜单');
		}
	}
},

// 列表
list = {
	clickList : function(e, c) {
		e = e.target || e.srcElement || e;
		if(LCase(e.tagName) == 'input' && LCase(e.type) == 'checkbox' ){
			list.setListBg(e);
			return;
		}
		
		var E = e.parentNode;
		if((LCase(E.tagName) != 'tr' && LCase(E.tagName)!='li') || E.id == 'Title') return;
		
		try{
			var Is = E.getElementsByTagName('input')[0];
			if(Is.disabled) return;
			if(c!=undefined) {
				Is.checked = c ? true : false;
			}else{
				Is.checked = Is.checked ? false : true;
			}
			list.setListBg(Is);
		}catch(e){}
	},
	setListBg : function(e) {
		var eTr = e.parentNode.parentNode;
		if(LCase(eTr.tagName)!='tr' && LCase(eTr.tagName)!='li') {
			eTr = eTr.parentNode;
		}else if((LCase(eTr.tagName)!='tr' && LCase(eTr.tagName)!='li') || eTr.id=='Title'){
			return;
		}
		//alert(eTr.tagName);
		//eTd = LCase(eTd.tagName)=='tr' ? eTd.parentNode : eTd;
		eTr.style.cssText = e.checked ? 'background: #FFFCD9;' : '';
	},
	selectAll : function(Form, eleName, Sort, SetBg, disable) {
		//var El = Form.getElementsByTagName('input');
		var El = Form.elements;
		//全选,不选,反选
		for (var i=0;i<El.length;i++) {
			var e = El[i];
			if(LCase(e.tagName)=='input' && LCase(e.type)=='checkbox' && e.name==eleName && !e.disabled) {
			//if(LCase(e.tagName) == 'input' && LCase(e.type) == 'checkbox'){
				if(Sort == 3) {
					e.checked = e.checked ? false : true;
				}else{
					e.checked = Sort == 0 ? true : false;
				}
				if(disable==true) {
					e.disabled = e.checked ? false : true;
				}
				if(e.name == eleName && SetBg) list.setListBg(e);
			}
		}
	}

},

// 信息提示栏
hint = {
	_obj : null,
	init : function() {
		frame.findFDoc();
		hint._obj = oFrame.getElementById('frame_Hint');
		if(hint._obj == null) {
			var _html = '<span class="right light_gray">点击关闭提示</span>'+
						'<img src="Images/Empty.gif" class="btnIco icoHint" />'+
						'<span id="frame_Hint_Text"></span>';
			
			hint._obj = wFrame.document.createElement('div');
			with(hint._obj){
				id = 'frame_Hint';
				innerHTML = _html;
				style.display = "none";
			}
			hint._obj.onclick = function() {
				hint.hide();
			}
			// 插入到导航栏下方
			var refChild = oFrame.getElementById('frame_Toolbar');
			// 如果没有导航栏，插入为第一个元素
			refChild = refChild==null ? wFrame.document.body.firstChild : refChild.nextSibling;
			while(refChild.nodeType!=1) {
				refChild = refChild.nextSibling;
			}
			wFrame.document.body.insertBefore(hint._obj, refChild);
		}
	},
	show : function(msg, thide) {
		hint.init();
		var _text = wFrame.document.getElementById('frame_Hint_Text');
		//if(_text==undefined) return;
		_text.innerHTML = msg;
		hint._obj.style.display = '';
		if(thide!=0) window.setTimeout('hint.hide()', thide);
	},
	hide : function() {
		try{
			hint._obj.style.display='none';
			frame.setHeight();
		}catch(e){}
	}
	
},

// 设置搜索关键字是否必填（有一项搜索条件就可搜索）
setSearchKey = function(sel, key) {
	if(sel==undefined || key==undefined) return;
	
	var Require = true;
	var len = sel.length;
	var tSel = null;
	var val = '';
	try{
		for(var i=0; i<len; i++) {
			tSel = $(sel[i][0]);
			if(tSel==null) continue;
			val = sel[i][1]==undefined ? '' : sel[i][1];
			Require = (Require && tSel.options[tSel.selectedIndex].value==val);
			if(!Require) break;
		}
	}catch(e){}
	Require = Require.toString()
	$(key).setAttribute('require', Require);
},

// 日期
dateTime = {
	now		: new Date(),
	showObj	: null,
	timer	: null,
	tInt	: null,
	date : function() {
		var nY = dateTime.now.getFullYear(), nM = dateTime.now.getMonth()+1, nD = dateTime.now.getDate();
		return (nY +'年'+ nM +'月'+ nD +'日');
	},
	week : function() {
		var day = dateTime.now.getDay();
		var wStr = null;
		switch(day){
			case 0 : wStr = '日'; break;
			case 1 : wStr = '一'; break;
			case 2 : wStr = '二'; break;
			case 3 : wStr = '三'; break;
			case 4 : wStr = '四'; break;
			case 5 : wStr = '五'; break;
			case 6 : wStr = '六'; break;
		}
		wStr = '星期'+ wStr;
		return wStr;
	},
	time : function() {
		var now = new Date();
		var nH = now.getHours(), nM = now.getMinutes(), nS = now.getSeconds();
		nH = nH >= 10 ? nH : '0'+ nH;
		nM = nM >= 10 ? nM : '0'+ nM;
		nS = nS >= 10 ? nS : '0'+ nS;
		var tmpStr = null;
		var tInt = null;
		if(dateTime.timer == null){
			clearInterval(dateTime.tInt);
			tmpStr = '<span id="__timer">'+
					 nH +':'+ nM +':'+ nS +
					 '<\/span>';
			dateTime.timer = $('__timer');
			dateTime.tInt = setInterval('dateTime.time()', 1000);
			return tmpStr;
		}else{
			tmpStr = nH +':'+ nM +':'+ nS;
			dateTime.timer.innerHTML = tmpStr;
		}
	},
	show : function(d, w, t) {
		if(dateTime.showObj == null){ return;}
		var tnpStr = null;
		var tmpDate = new Array();
		tmpDate[0] = d ? dateTime.date() : null;
		tmpDate[1] = w ? dateTime.week() : null;
		tmpDate[2] = t ? dateTime.time() : null;
		tnpStr = tmpDate.join('&nbsp;');
		dateTime.showObj.innerHTML = tnpStr;
	}
},

// 邮件
mail = {
	
},

keyDown = function(e) {
	e = e || event || iFrame.contentWindow.event;
	if(e.keyCode == 122) frame.reSizeTime = window.setTimeout('frame.setWidth()', 50);
},

fDblClick = function(e) {
	var E = e || iFrame.contentWindow.event;
	E = E.target || E.srcElement;
	var eTag = LCase(E.tagName);
	var peTag;
	if(eTag!=undefined) peTag = LCase(E.parentNode.tagName);
	if(eTag!=undefined && (eTag != 'a' && peTag != 'a') && eTag != 'input' && eTag != 'select' && eTag != 'textarea' && eTag != 'label') {
		chkIsEditor(E);
		if(!isEditor) menu.showLeft();
	}
},

isEditor = false,
chkIsEditor = function(e) {
	isEditor = false;
	var eTag = LCase(e.tagName);
	if(eTag!='body' && eTag!='html'){
		var pe = e.parentNode
		var pnTag = LCase(pe.tagName);
		if(pe.className=='editor') {
			isEditor = true;
		}else if(pnTag!='body' && pnTag!='html'){
			chkIsEditor(e.parentNode);
		}
	}
},

fGoUrl = function(_url) {
	_url = _url.replace(/&amp;/, '&');
	oFrame.location = _url;
},

// 按钮单击
goTo = function(e){
	var eRel = LCase(e.getAttribute('rel'));
	var eUrl = e.getAttribute('href');
	switch(eRel){
		case 'siteinfo' :
			fGoUrl('SetConfig.asp');
			break;
		case 'userpanel' :
			//fGoUrl('UserPanel.aspx');
			break;
		case 'changepass' :
			fGoUrl('Manager_Action.asp?Action=ChangePass');
			break;
		case 'home' :
			fGoUrl(oHome);
			break;
		case 'exit' :
			removeCookie('LastPage');
			window.location.replace('Login.asp?Action=Logout');
			break;
		case 'showleft' :
			menu.showLeft();
			break;
		default :
			return;
	}
};

