﻿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_safari = (userAgent.indexOf('webkit') != -1 || userAgent.indexOf('safari') != -1);
function strlen(str) {
	return (is_ie && str.indexOf('\n') != -1) ? str.replace(/\r?\n/g, '_').length : str.length;
}
function $(id) {
	return document.getElementById(id);
}
function trim(str) {
    return str.replace(/^\s*(.*?)[\s\n]*$/g, '$1');
}
function getTaskWidth() {
	var taskbarWidth = $('taskbar').clientWidth;
	var startWidth = $('start').clientWidth;
	var quicklaunchWidth = $('quicklaunch') ? $('quicklaunch').clientWidth : 0;
	if ( is_safari ) {
		$('taskbody').style.width = taskbarWidth - 308  + 'px';
		$('taskscroll').style.width = taskbarWidth - 350  + 'px';
	} else if (is_ie) {
		$('taskbody').style.width = taskbarWidth - startWidth - quicklaunchWidth - 2 + 'px';
		$('taskscroll').style.width = taskbarWidth - startWidth - quicklaunchWidth - 45 + 'px';
	} else  {
		$('taskbody').style.width = taskbarWidth - startWidth - quicklaunchWidth - 25  + 'px';
		$('taskscroll').style.width = taskbarWidth - startWidth - quicklaunchWidth - 67  + 'px';
	}
}

//导航二级菜单
function openSub(obj, target, isclick) {
	var tSub = $('sub' + target);
	if (tSub.style.display != 'block') {
		closeSub();
		var tMenu = obj.previousSibling;
		var tMenuP = obj.parentNode;
			tMenuP.id = 'subopen';
		var tLeft = tMenu.offsetLeft;
		var tTop = tMenu.offsetTop + 20;
		while(tMenu=tMenu.offsetParent) {
			tLeft+=tMenu.offsetLeft;
			tTop+=tMenu.offsetTop;
		}
		tSub.style.left = tLeft + 'px';
		tSub.style.top = tTop + 'px';
		tSub.style.display = 'block';
		tMenuP.className = 'active';
		obj.blur();
	} else {
		closeSub();
	}
	
	if(isclick) {
		var clickId = 0;
		document.body.onclick = function() {
			clickId = clickId + 1;
			if( clickId > 1 ) {
				closeSub();
			}
		}
	} else {
		document.body.onclick = function() {
			closeSub();
		}
	}
}

function closeSub() {
	var evt = getEvent();
	if(evt != null) {
		var srcElement = evt.srcElement || evt.target;
		if (srcElement.rel != 'menufocus') {
			var subMenu = $('submenu').getElementsByTagName('div');
			if($('subopen')){
				$('subopen').className = '';
				$('subopen').id = '';
			}
			for (i=0;i<subMenu.length;i++) {
				subMenu[i].style.display = 'none';
			}
		}
	}
}
function taskScroll(direction) {
	var task = $('task');
	var offset;
	task.className ? offset = parseInt(task.className) : offset = 0;
	var taskWidth = 0;
	var taskNum = task.getElementsByTagName('li');
	for ( i=0; i<taskNum.length; i++ ) {
		taskWidth += taskNum[i].clientWidth;
	}
	var taskbarWidth = $('taskbar').clientWidth;
	var startWidth = $('start').clientWidth;
	var quicklaunchWidth = $('quicklaunch') ? $('quicklaunch').clientWidth : 0;
	var taskScrollWidth = $('taskbar').clientWidth - startWidth - quicklaunchWidth - 70;
	var stop = false;
	$('scrollright').onmouseout = function(){stop = true;};
	$('scrollleft').onmouseout = function(){stop = true;};
	function scrollRight() {
		if(!stop && taskWidth > taskScrollWidth) {
			if ( direction == 'right') {
				$('scrollright').className = '';
				is_ie ? offset += 10 : offset += 3;
				if ( offset > 0 ) { offset = 0; $('scrollleft').className = 'unuse'; }
			} else {
				$('scrollleft').className = '';
				is_ie ? offset -= 10 : offset -= 3;
				if ( offset < - (taskWidth - taskScrollWidth) ) { offset = -(taskWidth - taskScrollWidth); $('scrollright').className = 'unuse'; }
			}
			task.style.left = offset + 'px';
			task.className = offset;
		}
		setTimeout(scrollRight, 5);
	}
	setTimeout(scrollRight, 80);
}
function getEvent() {
	if (document.all) return window.event;
	func = getEvent.caller;
	while (func != null) {
		var arg0 = func.arguments[0];
		if (arg0) {
			if((arg0.constructor==Event || arg0.constructor ==MouseEvent) || (typeof(arg0)=="object" && arg0.preventDefault && arg0.stopPropagation)) {
				return arg0;
			}
		}
		func=func.caller;
	}
	return null;
 }
function appExpand() { 
	if ($('subapp').className == 'submenu') {
		$('subapp').className = 'submenu expand';
		$('app-expand').childNodes[1].innerHTML = '收起';
	} else {
		$('subapp').className = 'submenu';
		$('app-expand').childNodes[1].innerHTML = '更多';
	}
	$('app-expand').childNodes[1].blur();
}
function showcheckresult(obj,errorID, errorMsg , okMsg)
{
    var res = obj.getElementsByTagName('result');
	var resContainer = document.getElementById(errorID);
	var result = "";
	if (res[0] != null && res[0] != undefined)
	{
		if (res[0].childNodes.length > 1) {
			result = res[0].childNodes[1].nodeValue;
		} else {
			result = res[0].firstChild.nodeValue;    		
		}
	}
	if (result == "1")
	{
	    resContainer.className = "ErrorMsg";
		resContainer.innerHTML = errorMsg;
	}
	else
	{
	    if(okMsg)
	    {
	        resContainer.className = "OkMsg";
		    resContainer.innerHTML = okMsg;
	    }
	    else
	    {
	        resContainer.className = "OkMsg";
		    resContainer.innerHTML = "输入正确";
		}
	}
}

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 in_array(needle, haystack) {
	if(typeof needle == 'string' || typeof needle == 'number') {
		for(var i in haystack) {
			if(haystack[i] == needle) {
					return true;
			}
		}
	}
	return false;
}

function isUndefined(variable) {
	return typeof variable == 'undefined' ? true : false;
}

function showattachdel(obj,id) 
{
    var res = obj.getElementsByTagName('result');
    var result = "";
    if (res[0] != null && res[0] != undefined)
    {
        if (res[0].childNodes.length > 1) {
	        result = res[0].childNodes[1].nodeValue;
        } else {
	        result = res[0].firstChild.nodeValue;    		
        }
    }
    if (result == "1")
    {
        
    }
    else
    {
        document.getElementById('attach_'+id).style.display="none";
        hideMenu();
    }
    
}

function showdownloaddel(obj,id) 
{
    var res = obj.getElementsByTagName('result');
    var result = "";
    if (res[0] != null && res[0] != undefined)
    {
        if (res[0].childNodes.length > 1) {
	        result = res[0].childNodes[1].nodeValue;
        } else {
	        result = res[0].firstChild.nodeValue;    		
        }
    }
    if (result == "1")
    {
        
    }
    else
    {
        document.getElementById('download_'+id).style.display="none";
        hideMenu();
    }
    
}
