var lang = new Array();
var userAgent = navigator.userAgent.toLowerCase();
var is_opera = userAgent.indexOf('opera') != -1 && opera.version();
var is_moz = (navigator.product == 'Gecko') && userAgent.substr(userAgent.indexOf('firefox') + 8, 3);
var is_ie = (userAgent.indexOf('msie') != -1 && !is_opera) && userAgent.substr(userAgent.indexOf('msie') + 5, 3);
var is_mac = userAgent.indexOf('mac') != -1;

function doane(event) {
	e = event ? event : window.event;
	if(is_ie) {
		e.returnValue = false;
		e.cancelBubble = true;
	} else if(e) {
		e.stopPropagation();
		e.preventDefault();
	}
}

function setcopy(txt,txtmsg) {   
     if(window.clipboardData) {   
             window.clipboardData.clearData();   
             window.clipboardData.setData("Text", txt);   
     } else if(navigator.userAgent.indexOf("Opera") != -1) {   
          window.location = txt;   
     } else if (window.netscape) {   
          try {   
               netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");   
          } catch (e) {   
		  		alert("请复制并粘贴到其他网站哦，按ctrl+c马上复制！");
               //alert("被浏览器拒绝！\n请在浏览器地址栏输入'about:config'并回车\n然后将'signed.applets.codebase_principal_support'设置为'true'");   
          }   
          var clip = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard);   
          if (!clip)   
               return;   
          var trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable);   
          if (!trans)   
               return;   
          trans.addDataFlavor('text/unicode');   
          var str = new Object();   
          var len = new Object();   
          var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);   
          var copytext = txt;   
          str.data = copytext;   
          trans.setTransferData("text/unicode",str,copytext.length*2);   
          var clipid = Components.interfaces.nsIClipboard;   
          if (!clip)   
               return false;   
          clip.setData(trans,null,clipid.kGlobalClipboard); 
	} 
    alert(txtmsg);
}

function isUndefined(variable) {
	return typeof variable == 'undefined' ? true : false;
}

var jsmenu = new Array();
var ctrlobjclassName;
jsmenu['active'] = new Array();
jsmenu['timer'] = new Array();
jsmenu['iframe'] = new Array();

function initCtrl(ctrlobj, click, duration, timeout, layer) {
	if(ctrlobj && !ctrlobj.initialized) {
		ctrlobj.initialized = true;
		ctrlobj.unselectable = true;

		ctrlobj.outfunc = typeof ctrlobj.onmouseout == 'function' ? ctrlobj.onmouseout : null;
		ctrlobj.onmouseout = function() {
			if(this.outfunc) this.outfunc();
			if(duration < 3) jsmenu['timer'][ctrlobj.id] = setTimeout('hideMenu(' + layer + ')', timeout);
		}

		ctrlobj.overfunc = typeof ctrlobj.onmouseover == 'function' ? ctrlobj.onmouseover : null;
		ctrlobj.onmouseover = function(e) {
			doane(e);
			if(this.overfunc) this.overfunc();
			if(click) {
				clearTimeout(jsmenu['timer'][this.id]);
			} else {
				for(var id in jsmenu['timer']) {
					if(jsmenu['timer'][id]) clearTimeout(jsmenu['timer'][id]);
				}
			}
		}
	}
}

function initMenu(ctrlid, menuobj, duration, timeout, layer, drag) {
	if(menuobj && !menuobj.initialized) {
		menuobj.initialized = true;
		menuobj.ctrlkey = ctrlid;
		menuobj.onclick = ebygum;
		menuobj.style.position = 'absolute';
		if(duration < 3) {
			if(duration > 1) {
				menuobj.onmouseover = function() {
					clearTimeout(jsmenu['timer'][ctrlid]);
				}
			}
			if(duration != 1) {
				menuobj.onmouseout = function() {
					jsmenu['timer'][ctrlid] = setTimeout('hideMenu(' + layer + ')', timeout);
				}
			}
		}
		menuobj.style.zIndex = 999;
		if(drag) {
			menuobj.onmousedown = function(event) {try{menudrag(menuobj, event, 1);}catch(e){}};
			menuobj.onmousemove = function(event) {try{menudrag(menuobj, event, 2);}catch(e){}};
			menuobj.onmouseup = function(event) {try{menudrag(menuobj, event, 3);}catch(e){}};
		}
	}
}

var menudragstart = new Array();
function menudrag(menuobj, e, op) {
	if(op == 1) {
		if(in_array(is_ie ? event.srcElement.tagName : e.target.tagName, ['TEXTAREA', 'INPUT', 'BUTTON', 'SELECT'])) {
			return;
		}
		menudragstart = is_ie ? [event.clientX, event.clientY] : [e.clientX, e.clientY];
		menudragstart[2] = parseInt(menuobj.style.left);
		menudragstart[3] = parseInt(menuobj.style.top);
		doane(e);
	} else if(op == 2 && menudragstart[0]) {
		var menudragnow = is_ie ? [event.clientX, event.clientY] : [e.clientX, e.clientY];
		menuobj.style.left = (menudragstart[2] + menudragnow[0] - menudragstart[0]) + 'px';
		menuobj.style.top = (menudragstart[3] + menudragnow[1] - menudragstart[1]) + 'px';
		doane(e);
	} else if(op == 3) {
		menudragstart = [];
		doane(e);
	}
}

function showMenu(ctrlid, click, offset, duration, timeout, layer, showid, maxh, drag) {
	var ctrlobj = $(ctrlid);
	if(!ctrlobj) return;
	if(isUndefined(click)) click = false;
	if(isUndefined(offset)) offset = 0;
	if(isUndefined(duration)) duration = 2;
	if(isUndefined(timeout)) timeout = 400;
	if(isUndefined(layer)) layer = 0;
	if(isUndefined(showid)) showid = ctrlid;
	var showobj = $(showid);
	var menuobj = $(showid + '_menu');
	if(!showobj|| !menuobj) return;
	if(isUndefined(maxh)) maxh = 400;
	if(isUndefined(drag)) drag = false;

	if(click && jsmenu['active'][layer] == menuobj) {
		hideMenu(layer);
		return;
	} else {
		hideMenu(layer);
	}

	var len = jsmenu['timer'].length;
	if(len > 0) {
		for(var i=0; i<len; i++) {
			if(jsmenu['timer'][i]) clearTimeout(jsmenu['timer'][i]);
		}
	}

	initCtrl(ctrlobj, click, duration, timeout, layer);
	ctrlobjclassName = ctrlobj.className;
	ctrlobj.className += ' hover';
	initMenu(ctrlid, menuobj, duration, timeout, layer, drag);

	menuobj.style.display = '';
	if(!is_opera) {
		menuobj.style.clip = 'rect(auto, auto, auto, auto)';
	}

	setMenuPosition(showid, offset);

	if(maxh && menuobj.scrollHeight > maxh) {
		menuobj.style.height = maxh + 'px';
		if(is_opera) {
			menuobj.style.overflow = 'auto';
		} else {
			menuobj.style.overflowY = 'auto';
		}
	}

	if(!duration) {
		setTimeout('hideMenu(' + layer + ')', timeout);
	}

	jsmenu['active'][layer] = menuobj;

}

function setMenuPosition(showid, offset) {
	var showobj = $(showid);
	var menuobj = $(showid + '_menu');
	if(isUndefined(offset)) offset = 0;
	if(showobj) {
		showobj.pos = fetchOffset(showobj);
		showobj.X = showobj.pos['left'];
		showobj.Y = showobj.pos['top'];
		if($(InFloat) != null) {
			var InFloate = InFloat.split('_');
			if(!floatwinhandle[InFloate[1] + '_1']) {
				floatwinnojspos = fetchOffset($('floatwinnojs'));
				floatwinhandle[InFloate[1] + '_1'] = floatwinnojspos['left'];
				floatwinhandle[InFloate[1] + '_2'] = floatwinnojspos['top'];
			}
			showobj.X = showobj.X - $(InFloat).scrollLeft - parseInt(floatwinhandle[InFloate[1] + '_1']);
			showobj.Y = showobj.Y - $(InFloat).scrollTop - parseInt(floatwinhandle[InFloate[1] + '_2']);
			InFloat = '';
		}
		showobj.w = showobj.offsetWidth;
		showobj.h = showobj.offsetHeight;
		menuobj.w = menuobj.offsetWidth;
		menuobj.h = menuobj.offsetHeight;
		if(offset < 3) {
			menuobj.style.left = offset == 1 ? (showobj.X + showobj.w + menuobj.w > document.body.clientWidth) && (showobj.X + showobj.w - menuobj.w >= 0) ? showobj.X - menuobj.w - 5 + 'px' : showobj.X + showobj.w + 5 + 'px' :(showobj.X + menuobj.w > document.body.clientWidth) && (showobj.X + showobj.w - menuobj.w >= 0) ? showobj.X + showobj.w - menuobj.w + 'px' : showobj.X + 'px';
			menuobj.style.top = offset == 1 ? showobj.Y + 'px' : (offset == 2 || ((showobj.Y + showobj.h + menuobj.h > document.documentElement.scrollTop + document.documentElement.clientHeight) && (showobj.Y - menuobj.h >= 0)) ? (showobj.Y - menuobj.h) + 'px' : showobj.Y + showobj.h + 'px');
		} else if(offset == 3) {
			menuobj.style.left = (document.body.clientWidth - menuobj.clientWidth) / 2 + document.body.scrollLeft + 'px';
			menuobj.style.top = (document.body.clientHeight - menuobj.clientHeight) / 2 + document.body.scrollTop + 'px';
		}

		if(menuobj.style.clip && !is_opera) {
			menuobj.style.clip = 'rect(auto, auto, auto, auto)';
		}
	}
}

function hideMenu(layer) {
	if(isUndefined(layer)) layer = 0;
	if(jsmenu['active'][layer]) {
		try {
			$(jsmenu['active'][layer].ctrlkey).className = ctrlobjclassName;
		} catch(e) {}
		clearTimeout(jsmenu['timer'][jsmenu['active'][layer].ctrlkey]);
		jsmenu['active'][layer].style.display = 'none';
		if(is_ie && is_ie < 7 && jsmenu['iframe'][layer]) {
			jsmenu['iframe'][layer].style.display = 'none';
		}
		jsmenu['active'][layer] = null;
	}
}

function offMenu(obj){
	$(obj).style.display="none";
}

function fetchOffset(obj) {
	var left_offset = obj.offsetLeft;
	var top_offset = obj.offsetTop;
	while((obj = obj.offsetParent) != null) {
		left_offset += obj.offsetLeft;
		top_offset += obj.offsetTop;
	}
	return { 'left' : left_offset, 'top' : top_offset };
}

function ebygum(eventobj) {
	if(!eventobj || is_ie) {
		window.event.cancelBubble = true;
		return window.event;
	} else {
		if(eventobj.target.type == 'submit') {
			eventobj.target.form.submit();
		}
		eventobj.stopPropagation();
		return eventobj;
	}
}

function menuoption_onclick_function(e) {
	this.clickfunc();
	hideMenu();
}

function menuoption_onclick_link(e) {
	choose(e, this);
}

function menuoption_onmouseover(e) {
	this.className = 'popupmenu_highlight';
}

function menuoption_onmouseout(e) {
	this.className = 'popupmenu_option';
}

function choose(e, obj) {
	var links = obj.getElementsByTagName('a');
	if(links[0]) {
		if(is_ie) {
			links[0].click();
			window.event.cancelBubble = true;
		} else {
			if(e.shiftKey) {
				window.open(links[0].href);
				e.stopPropagation();
				e.preventDefault();
			} else {
				window.location = links[0].href;
				e.stopPropagation();
				e.preventDefault();
			}
		}
		hideMenu();
	}
}


//FloatWin
var hiddenobj = new Array();
var floatwinhandle = new Array();
var floatscripthandle = new Array();
var floattabs = new Array();
var floatwins = new Array();
var InFloat = '';
var floatwinreset = 0;
var floatwinopened = 0;
function floatwin(action, script, w, h, scrollpos) {
	var actione = action.split('_');
	action = actione[0];
	if((!allowfloatwin || allowfloatwin == 0) && action == 'open' && w >= 600) {
		location.href = script;
		return;
	}
	var handlekey = actione[1];
	var layerid = 'floatwin_' + handlekey;
	if(is_ie) {
		var objs = $('wrap').getElementsByTagName("OBJECT");
	} else {
		var objs = $('wrap').getElementsByTagName("EMBED");
	}
	if(action == 'open') {
		loadcss('float');
		floatwinhandle[handlekey + '_0'] = layerid;
		if(!floatwinopened) {
			$('wrap').onkeydown = floatwin_wrapkeyhandle;
			for(i = 0;i < objs.length; i ++) {
				if(objs[i].style.visibility != 'hidden') {
					objs[i].setAttribute("oldvisibility", objs[i].style.visibility);
					objs[i].style.visibility = 'hidden';
				}
			}
		}
		scrollpos = !scrollpos ? '' : 'floatwin_scroll(\'' + scrollpos + '\');';
		var clientWidth = document.body.clientWidth;
		var clientHeight = document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight;
		var scrollTop = document.body.scrollTop ? document.body.scrollTop : document.documentElement.scrollTop;
		if(script && script != -1) {
			if(script.lastIndexOf('/') != -1) {
				script = script.substr(script.lastIndexOf('/') + 1);
			}
			var scriptfile = script.split('?');
			scriptfile = scriptfile[0];
			if(floatwinreset || floatscripthandle[scriptfile] && floatscripthandle[scriptfile][0] != script) {
				if(!isUndefined(floatscripthandle[scriptfile])) {
					$('append_parent').removeChild($(floatscripthandle[scriptfile][1]));
					$('append_parent').removeChild($(floatscripthandle[scriptfile][1] + '_mask'));
				}
				floatwinreset = 0;
			}
			floatscripthandle[scriptfile] = [script, layerid];
		}
		if(!$(layerid)) {
			floattabs[layerid] = new Array();
			div = document.createElement('div');
			div.className = 'floatwin';
			div.id = layerid;
			div.style.width = w + 'px';
			div.style.height = h + 'px';
			div.style.left = floatwinhandle[handlekey + '_1'] = ((clientWidth - w) / 2) + 'px';
			div.style.position = 'absolute';
			div.style.zIndex = '999';
			div.onkeydown = floatwin_keyhandle;
			$('append_parent').appendChild(div);
			$(layerid).style.display = '';
			$(layerid).style.top = floatwinhandle[handlekey + '_2'] = ((clientHeight - h) / 2 + scrollTop) + 'px';
			$(layerid).innerHTML = '<div><h3 class="float_ctrl"><em><img src="' + IMGDIR + '/loading.gif"></em><span><a href="javascript:;" class="float_close" onclick="floatwinreset = 1;floatwin(\'close_' + handlekey + '\');">&nbsp</a></span></h3></div>';
			divmask = document.createElement('div');
			divmask.className = 'floatwinmask';
			divmask.id = layerid + '_mask';
			divmask.style.width = (parseInt($(layerid).style.width) + 14) + 'px';
			divmask.style.height = (parseInt($(layerid).style.height) + 14) + 'px';
			divmask.style.left = (parseInt($(layerid).style.left) - 6) + 'px';
			divmask.style.top = (parseInt($(layerid).style.top) - 6) + 'px';
			divmask.style.position = 'absolute';
			divmask.style.zIndex = '998';
			divmask.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity=90,finishOpacity=100,style=0)';
			divmask.style.opacity = 0.9;
			$('append_parent').appendChild(divmask);
			if(script && script != -1) {
				script += (script.search(/\?/) > 0 ? '&' : '?') + 'infloat=yes&handlekey=' + handlekey;
				try {
					ajaxget(script, layerid, '', '', '', scrollpos);
				} catch(e) {
					setTimeout("ajaxget('" + script + "', '" + layerid + "', '', '', '', '" + scrollpos + "')", 1000);
				}
			} else if(script == -1) {
				$(layerid).innerHTML = '<div><h3 class="float_ctrl"><em id="' + layerid + '_title"></em><span><a href="javascript:;" class="float_close" onclick="floatwinreset = 1;floatwin(\'close_' + handlekey + '\');">&nbsp</a></span></h3></div><div id="' + layerid + '_content"></div>';
				$(layerid).style.zIndex = '1099';
				$(layerid + '_mask').style.zIndex = '1098';
			}
		} else {
			$(layerid).style.width = w + 'px';
			$(layerid).style.height = h + 'px';
			$(layerid).style.display = '';
			$(layerid).style.top = floatwinhandle[handlekey + '_2'] = ((clientHeight - h) / 2 + scrollTop) + 'px';
			$(layerid + '_mask').style.width = (parseInt($(layerid).style.width) + 14) + 'px';
			$(layerid + '_mask').style.height = (parseInt($(layerid).style.height) + 14) + 'px';
			$(layerid + '_mask').style.display = '';
			$(layerid + '_mask').style.top = (parseInt($(layerid).style.top) - 6) + 'px';
		}
		floatwins[floatwinopened] = handlekey;
		floatwinopened++;
	} else if(action == 'close' && floatwinhandle[handlekey + '_0']) {
		floatwinopened--;
		for(i = 0;i < floatwins.length; i++) {
			if(handlekey == floatwins[i]) {
				floatwins[i] = null;
			}
		}
		if(!floatwinopened) {
			for(i = 0;i < objs.length; i ++) {
				if(objs[i].attributes['oldvisibility']) {
					objs[i].style.visibility = objs[i].attributes['oldvisibility'].nodeValue;
					objs[i].removeAttribute('oldvisibility');
				}
			}
			$('wrap').onkeydown = null;
		}
		hiddenobj = new Array();
		$(layerid + '_mask').style.display = 'none';
		$(layerid).style.display = 'none';
	} else if(action == 'size' && floatwinhandle[handlekey + '_0']) {
		if(!floatwinhandle[handlekey + '_3']) {
			var clientWidth = document.body.clientWidth;
			var clientHeight = document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight;
			var w = clientWidth > 800 ? clientWidth * 0.9 : 800;
			var h = clientHeight * 0.9;
			floatwinhandle[handlekey + '_3'] = $(layerid).style.left;
			floatwinhandle[handlekey + '_4'] = $(layerid).style.top;
			floatwinhandle[handlekey + '_5'] = $(layerid).style.width;
			floatwinhandle[handlekey + '_6'] = $(layerid).style.height;
			$(layerid).style.left = floatwinhandle[handlekey + '_1'] = ((clientWidth - w) / 2) + 'px';
			$(layerid).style.top = floatwinhandle[handlekey + '_2'] = ((document.documentElement.clientHeight - h) / 2 + document.documentElement.scrollTop) + 'px';
			$(layerid).style.width = w + 'px';
			$(layerid).style.height = h + 'px';
		} else {
			$(layerid).style.left = floatwinhandle[handlekey + '_1'] = floatwinhandle[handlekey + '_3'];
			$(layerid).style.top = floatwinhandle[handlekey + '_2'] = floatwinhandle[handlekey + '_4'];
			$(layerid).style.width = floatwinhandle[handlekey + '_5'];
			$(layerid).style.height = floatwinhandle[handlekey + '_6'];
			floatwinhandle[handlekey + '_3'] = '';
		}
		$(layerid + '_mask').style.width = (parseInt($(layerid).style.width) + 14) + 'px';
		$(layerid + '_mask').style.height = (parseInt($(layerid).style.height) + 14) + 'px';
		$(layerid + '_mask').style.left = (parseInt($(layerid).style.left) - 6) + 'px';
		$(layerid + '_mask').style.top = (parseInt($(layerid).style.top) - 6) + 'px';
	}
}

function floatwin_scroll(pos) {
	var pose = pos.split(',');
	try {
		pagescroll.defaultleft = pose[0];
		pagescroll.defaulttop = pose[1];
		pagescroll.init();
	} catch(e) {}
}

function floatwin_wrapkeyhandle(e) {
	e = is_ie ? event : e;
	if(e.keyCode == 9) {
		doane(e);
	} else if(e.keyCode == 27) {
		for(i = floatwins.length - 1;i >= 0; i--) {
			floatwin('close_' + floatwins[i]);
		}
	}
}

function floatwin_keyhandle(e) {
	e = is_ie ? event : e;
	if(e.keyCode == 9) {
		doane(e);
		var obj = is_ie ? e.srcElement : e.target;
		var srcobj = obj;
		j = 0;
		while(obj.className.indexOf('floatbox') == -1) {
			obj = obj.parentNode;
		}
		obj.id = obj.id ? obj.id : 'floatbox_' + Math.random();
		if(!floattabs[obj.id]) {
			floattabs[obj.id] = new Array();
			var alls = obj.getElementsByTagName("*");
			for(i = 0;i < alls.length;i++) {
				if(alls[i].getAttribute('tabindex') == 1) {
					floattabs[obj.id][j] = alls[i];
					j++;
				}
			}
		}
		if(floattabs[obj.id].length > 0) {
			for(i = 0;i < floattabs[obj.id].length;i++) {
				if(srcobj == floattabs[obj.id][i]) {
					j = e.shiftKey ? i - 1 : i + 1;break;
				}
			}
			if(j < 0) {
				j = floattabs[obj.id].length - 1;
			}
			if(j > floattabs[obj.id].length - 1) {
				j = 0;
			}
			do{
				focusok = 1;
				try{ floattabs[obj.id][j].focus(); } catch(e) {
					focusok = 0;
				}
				if(!focusok) {
					j = e.shiftKey ? j - 1 : j + 1;
					if(j < 0) {
						j = floattabs[obj.id].length - 1;
					}
					if(j > floattabs[obj.id].length - 1) {
						j = 0;
					}
				}
			} while(!focusok);
		}
	}
}

//ShowSelect
function showselect(obj, inpid, t, rettype) {
	if(!obj.id) {
		var t = !t ? 0 : t;
		var rettype = !rettype ? 0 : rettype;
		obj.id = 'calendarexp_' + Math.random();
		div = document.createElement('div');
		div.id = obj.id + '_menu';
		div.style.display = 'none';
		div.className = 'showselect_menu';
		if($(InFloat) != null) {
			$(InFloat).appendChild(div);
		} else {
			$('append_parent').appendChild(div);
		}
		s = '';
		if(!t) {
			s += showselect_row(inpid, '一天', 1, 0, rettype);
			s += showselect_row(inpid, '一周', 7, 0, rettype);
			s += showselect_row(inpid, '一个月', 30, 0, rettype);
			s += showselect_row(inpid, '三个月', 90, 0, rettype);
			s += showselect_row(inpid, '自定义', -2);
		} else {
			if($(t)) {
				var lis = $(t).getElementsByTagName('LI');
				for(i = 0;i < lis.length;i++) {
					s += '<a href="javascript:;" onclick="$(\'' + inpid + '\').value = this.innerHTML">' + lis[i].innerHTML + '</a><br />';
				}
				s += showselect_row(inpid, '自定义', -1);
			} else {
				s += '<a href="javascript:;" onclick="$(\'' + inpid + '\').value = \'\'">永久</a><br />';
				s += showselect_row(inpid, '7 天', 7, 1, rettype);
				s += showselect_row(inpid, '14 天', 14, 1, rettype);
				s += showselect_row(inpid, '一个月', 30, 1, rettype);
				s += showselect_row(inpid, '三个月', 90, 1, rettype);
				s += showselect_row(inpid, '半年', 182, 1, rettype);
				s += showselect_row(inpid, '一年', 365, 1, rettype);
				s += showselect_row(inpid, '自定义', -1);
			}
		}
		$(div.id).innerHTML = s;
	}
	showMenu(obj.id);
	if(is_ie && is_ie < 7) {
		doane(event);
	}
}
