
function unloadHandlerSmartNav(){
	dayPart = 1/((24*3600)/30); // 30 seconds
	set_Cookie("smartNavTop",getScrollingPosition()[1],dayPart,smartNavCookiePath);
	set_Cookie("smartNavLeft",getScrollingPosition()[0],dayPart,smartNavCookiePath);
	set_Cookie("smartNavIDs",smartNavIds,dayPart,smartNavCookiePath);
	if (smartNavCookieLastKeyDownId==null){
		del_Cookie("smartNavCookieLastKeyDownId");
	}else{
		set_Cookie("smartNavCookieLastKeyDownId",smartNavCookieLastKeyDownId,dayPart,smartNavCookiePath);
	}
	//alert("Unload: "+unescape(document.cookie))
}
function getScrollingPosition() { 
	var position = [0, 0]; 
	if (typeof window.pageYOffset != 'undefined') { 
		position = [ 
		window.pageXOffset, 
		window.pageYOffset 
		]; 
	} else if (typeof document.documentElement.scrollTop != 'undefined' && (document.documentElement.scrollTop > 0 || document.documentElement.scrollLeft > 0)) { 
		position = [ 
		document.documentElement.scrollLeft, 
		document.documentElement.scrollTop 
		]; 
	} else if (typeof document.body.scrollTop != 'undefined') { 
		position = [ 
		document.body.scrollLeft, 
		document.body.scrollTop 
		]; 
	} 
	return position; 
}
function setScrollingPosition(x,y) { 
	if (typeof document.documentElement.scrollTop != 'undefined') { 
		document.documentElement.scrollLeft=x;
		document.documentElement.scrollTop=y;
	} else {
		if (typeof document.body.scrollTop != 'undefined') { 
			document.body.scrollLeft=x; 
			document.body.scrollTop=y;
		}
	} 
	if (typeof document.body.scrollTop != 'undefined' 
	&& typeof document.documentElement.scrollTop != 'undefined'
	&& document.documentElement.scrollLeft == 0
	&& document.documentElement.scrollTop == 0) { 
		document.body.scrollLeft=x;
		document.body.scrollTop=y;
	}	
	if (typeof window.pageYOffset != 'undefined') { 
		window.pageXOffset=x; 
		window.pageYOffset=y; 
	} 
}
function startSmartNav(){
	if (typeof window.addEventListener!='undefined'){
		window.addEventListener('unload', unloadHandlerSmartNav , false);
		window.addEventListener('load', loadHandlerSmartNav , false);
		document.addEventListener('keydown', handleOnKeyDown , false);
	} else {
		if (typeof window.attachEvent!='undefined'){
			window.attachEvent('onunload', unloadHandlerSmartNav, false);
			window.attachEvent('onload', loadHandlerSmartNav, false);
			document.attachEvent('onkeydown', handleOnKeyDown, false);
		}
	}
}
function loadHandlerSmartNav(){
	//alert("Load: "+unescape(document.cookie))
	smartNavCookieTop = get_Cookie("smartNavTop");
	smartNavCookieLeft = get_Cookie("smartNavLeft");
	smartNavCookieId = get_Cookie("smartNavIDs");
	smartNavCookieLastKeyDownId = get_Cookie("smartNavCookieLastKeyDownId");
	del_Cookie("smartNavIDs");
	del_Cookie("smartNavCookieLastKeyDownId");
	var idArr = smartNavIds.split('|');
	var isId  = false;
	if (smartNavIds.length>1 && smartNavIds!=null && smartNavCookieId!=null){
		for (var x=0; x<idArr.length; x++){
			if (smartNavCookieId.indexOf('|'+idArr[x]+'|') > -1 && idArr[x].length > 0){
				isId = true;
				break;
			}
		}
	}	
	if (smartNavCookieTop!=null && isId == true){
		setScrollingPosition(smartNavCookieLeft,smartNavCookieTop);
		if (document.getElementById && SmartNavFocusAllow){
			el = document.getElementById(smartNavCookieLastKeyDownId);
			if (el!=null && el!='undefined'){
				var go = true;
				if (el.style!=null && el.style!='undefined'){
					if (el.style.display=='none'){
						go=false;
					}
				}
				if (go==true){
					el.focus();
					if (el.createTextRange) {
						var v = el.value;
						var r = el.createTextRange();
						if (v!=null && v !='undefined'){
							if (v.length!=null && v.length !='undefined'){
								r.moveStart('character', v.length);
								r.select();
							}
						}
					}
				}
			}
		}
	}
}


function get_Cookie(NameOfCookie)
{
	if (document.cookie.length > 0){
		begin = document.cookie.indexOf(NameOfCookie+"=");
		if (begin != -1){
			begin += NameOfCookie.length+1;
			end = document.cookie.indexOf(";", begin);
			if (end == -1) end = document.cookie.length;
			return unescape(document.cookie.substring(begin, end)); 
		}
	}
	return null;
}


function set_Cookie(NameOfCookie, value, expiredays, path){
	var ExpireDate = new Date ();
	ExpireDate.setTime(ExpireDate.getTime() + (expiredays * 24 * 3600 * 1000));
	cookieStr = NameOfCookie + "=" + escape(value)  ;
	if (path != null && path != '') {
		cookieStr = cookieStr + "; path="+path;
	}
	if (expiredays == null) {
		cookieStr = cookieStr + "; expires=" + ExpireDate.toGMTString();
	}
	document.cookie = cookieStr;
}


function del_Cookie(NameOfCookie){
	if (get_Cookie(NameOfCookie)) {
		document.cookie = NameOfCookie + "="+"; expires=Thu, 01-01-1970 00:00:01 GMT";
	}
}
function handleOnKeyDown(e){ 
	if (document.getElementById){
		if (!e) {
			var e = window.event
		};
		if (e.target) {
			smartNavEventTarget = e.target
		}else if (e.srcElement) {
			smartNavEventTarget = e.srcElement
		}else {
			smartNavEventTarget=null
		};
		smartNavCookieLastKeyDownId=smartNavEventTarget.id;
	} else {
		smartNavCookieLastKeyDownId=null;
	}
} 


if (typeof smartNavIds=='undefined'){smartNavIds = ''};
if (typeof smartNavCookiePath=='undefined'){smartNavCookiePath = ''};
var smartNavCookieTop;
var smartNavCookieLeft;
var smartNavCookieId;
var smartNavCookieLastKeyDownId;
var SmartNavFocusAllow;


