﻿/*===========================
// Author: 86115683@qq.com
// Create Date: 2011-08-28
// Last Modified: 2011-09-30
//===========================*/
({getMousePosition:function(e){
		var x,y;
		var e = e||window.event;
		return {
		x:e.clientX+document.body.scrollLeft+document.documentElement.scrollLeft,
		y:e.clientY+document.body.scrollTop+document.documentElement.scrollTop
		};
	},
	getOffsetLeft:function(o){
		var w = parseInt(o.offsetLeft);
		while(o.offsetParent){
			o = o.parentNode;
			w += parseInt(o.offsetLeft);
			if(o.id && o.id=='tpContent')break;//左侧距离计算到id为tpContent的div为止
		}
		return w;
	},
	getOffsetTop:function(o){
		var w = parseInt(o.offsetTop);
		while(o.offsetParent){
			o = o.parentNode;
			w += parseInt(o.offsetTop);
			if(o.id && o.id=='tpContent')break;//顶部距离计算到id为tpContent的div为止
		}
		return w;
	},
	initDropdownList:function(){
		var getOffsetLeft=this.getOffsetLeft;
		var getOffsetTop=this.getOffsetTop;
		var getMousePosition=this.getMousePosition;
		var o=document.getElementById('dropdownlist');
		var ddl=o.getElementsByTagName('ol')[0];
		var div=o.getElementsByTagName('div')[0];
		div.childNodes[0].onclick=function(){
			ddl.style.display=(ddl.style.display==''||ddl.style.display=='none')?'block':'none';
		}
		ddl.onmouseout=function(e){
			var ofLeft = parseInt(getOffsetLeft(this));
			var ofTop = parseInt(getOffsetTop(this));
			var w = parseInt(this.offsetWidth);
			var h = parseInt(this.offsetHeight);
			var x = parseInt(getMousePosition(e).x);
			var y = parseInt(getMousePosition(e).y);
			if((x-ofLeft<0 || x-(ofLeft+w)>0) || (y-ofTop<0 || y-(ofTop+h)>0)){
				this.style.display='none';
			}
		}
	},
	setCookie:function(v){
	    var n='DefaultSite';
	    var d=new Date();
	    var expire=new Date(d.getFullYear()+10,d.getMonth(),d.getDate(),d.getHours(),d.getMinutes(),d.getSeconds());
	    document.cookie = n + "=" + escape(v) + ("; expires=" + expire.toGMTString())+'; path=/';
	},
	isRemSite:function(){//对指定站点才显示记忆功能
	    var aDomains=['www.tp-link.com','www.tplink.com','localhost'];
	    var href=location.href.replace(/^ht{2}ps?\:\/{2}([^\/]+).*$/i,"$1");
	    for(var i=0; i<aDomains.length; i++){if(aDomains[i]==href){return true;}}
	    return false;
	},
	doSelect:function(){
	    var ok=document.getElementById('rdRememberOk');
	    var no=document.getElementById('rdRememberNo');
	    var setCookie=this.setCookie;
		var oDiv = document.getElementById('websiteGuideContent');
	    var d = document.getElementById('rememberAsDefault');
		var anchors=oDiv.getElementsByTagName('a');
		for(var i=0; i<anchors.length; i++){
		    var a=anchors[i];
		    if(a.href!='javascript:void(0);'){
		        a.onclick=function(){
		            d.style.display='block';
		            ok.value=this.href;
		            setTimeout("document.getElementById('rdRememberNo').click();", 6000);
		            return false;
		        };
		    }
		}
		ok.onclick=function(){
		    var h=document.getElementById('global').getElementsByTagName('a')[0].href;
		    if(this.value!='ok')h=this.value;
		    setCookie(h);
		    window.location.href=h;
		};
		no.onclick=function(){window.location.href=ok.value;};
	},
	show:function(){
		this.initDropdownList();
		var getOffsetLeft=this.getOffsetLeft;
		var getOffsetTop=this.getOffsetTop;
		var getMousePosition=this.getMousePosition;
		var oDiv = document.getElementById('websiteGuideMap');
		var oRegionlist = document.getElementById('regionlist');
		var aIds = ['americas','asia-pacific','europe','middle-east-africa'];
		for(var i=0; i<aIds.length; i++){
			var id = aIds[i];
			var oT = document.getElementById(id);
			oT.childNodes[0].onmouseover=function(){
				oRegionlist.className=this.parentNode.id;
				this.parentNode.className='current';
			};
			oT.onmouseout=function(e){
				var ofLeft = parseInt(getOffsetLeft(this));
				var ofTop = parseInt(getOffsetTop(this));
				var w = parseInt(this.offsetWidth)-10;
				var h = parseInt(this.offsetHeight)-10;
				var x = parseInt(getMousePosition(e).x);
				var y = parseInt(getMousePosition(e).y);
				if((x-ofLeft<0 || x-(ofLeft+w)>0) || (y-ofTop<0 || y-(ofTop+h)>0)){//alert('');
					oRegionlist.className='';
					this.className='';
				}
				for(var i=0; i<oRegionlist.childNodes.length; i++){
					var t=oRegionlist.childNodes[i];
					if(t!==this && t.className && t.className=='current')t.className='';
				}
			};
		}
		if(this.isRemSite()){
		    for(var i=0; i<oRegionlist.childNodes.length; i++){
			    var t=oRegionlist.childNodes[i];
			    if(t.tagName && t.tagName.toLowerCase()=='li'){t.getElementsByTagName('a')[0].onclick=function(){return false;}}
		    }
		    this.doSelect();
		}
	}
}).show();
