/************************************
*	js模擬Select					*

*	Author:Daviv					*

*	Date:2007-4-28					*

*	Blog:http://www.iwcn.net		*

*	Email:jxdawei#163.com			*
************************************/
var tag=0;
var childCreate=false;
var cDiv = document.createElement("div");

function divhidden(obj)
{
	//alert();
	if(childCreate==true)
	{
		childCreate=false;
	}
	else
	{
		childCreate=true;
	}
	//alert(childCreate);
	cDiv.style.display="none";
	//document.getElementById("selectchild" + obj.name).style.display="none";
	//cDiv = document.createElement("div");
	//childCreate=true;
	//alert(childCreate);
	//loadSelect(obj);
	
}

function getlink(obj)
{
	if(obj.value=="en")
	{
		window.location="#";
	}else if(obj.value=="cn")
	{
		window.open("http://hongkong.langhamplacehotels.com/china/index.html","_blank");
	}else if(obj.value=="jp")
	{
		window.open("http://hongkong.langhamplacehotels.com/japan/index.html","_blank"); 
	}
}


function Offset(e)
//取標簽的絕對位置
{
	var t = e.offsetTop;
	var l = e.offsetLeft;
	var w = e.offsetWidth;
	var h = e.offsetHeight-2;

	while(e=e.offsetParent)
	{
		t+=e.offsetTop;
		l+=e.offsetLeft;
	}
	return {
		top : t,
		left : l,
		width : w,
		height : h
	}
}

function loadSelect(obj){
   
	//第一步：取得Select所在的位置
	var offset=Offset(obj);
	//第二步：將真的select隱藏
	obj.style.display="none";

	//第三步：虛擬一個div出來代替select
	var iDiv = document.createElement("div");
		iDiv.id="selectof" + obj.name;
		iDiv.style.position = "absolute";
		iDiv.style.width=Number(offset.width)-13 + "px";
		iDiv.style.height=offset.height + "px";
		iDiv.style.top=offset.top + "px";
		iDiv.style.left=offset.left + "px";
		//iDiv.style.background="url(../images/btn_pulldown.jpg) no-repeat right 4px ";
		iDiv.style.border="1px solid #ffffff";
		iDiv.style.backgroundColor="#ffffff";
		iDiv.style.fontSize="11px";
		iDiv.style.color="#717073";
	
	var iDiv2 = document.createElement("div");
	    iDiv2.style.position= "absolute";
		iDiv2.style.width="13px";
		iDiv2.style.height="15px";
		iDiv2.style.left=Number(offset.left)+Number(offset.width)-13+"px";
		iDiv2.style.top=Number=offset.top+"px";
		iDiv2.style.background="url(../images/btn_pulldown_ETHKG_cn.jpg) no-repeat";
		iDiv2.style.backgroundColor="#EDEFFF";
		
		iDiv.style.fontSize="11px";
		iDiv.style.lineHeight=offset.height + "px";
		iDiv.style.textIndent="4px";
		iDiv.style.color="#717073";
		
	document.body.appendChild(iDiv);
	document.body.appendChild(iDiv2);

	//第四步：將select中默認的選項顯示出來
	var tValue=obj.options[obj.selectedIndex].innerHTML;
	iDiv.innerHTML=tValue;

	//第五步：模擬鼠標點擊
	iDiv2.onmouseover=function(){//鼠標移到
		iDiv2.style.background="url(../images/btn_pulldown_on_ETHKG_cn.jpg) no-repeat";
		iDiv.style.backgroundColor="#ffffff";
		iDiv2.style.backgroundColor="#868686";
	}
	iDiv2.onmouseout=function(){//鼠標移走
		iDiv2.style.background="url(../images/btn_pulldown_ETHKG_cn.jpg) no-repeat";
		iDiv.style.backgroundColor="#ffffff";
		//iDiv2.style.backgroundColor="#EDEFFF";
		
	}
	iDiv2.onclick=function(){//鼠標移到
	    iDiv2.style.background="url(../images/btn_pulldown_on_ETHKG_cn.jpg) no-repeat";
		iDiv.style.backgroundColor="#ffffff";
		if (document.getElementById("selectchild" + obj.name)){
		//判斷是否創建過div
	
		//alert(document.getElementById("selectchild" + obj.name)+"1"+childCreate);
			if (childCreate){
				//alert("1");
				//判斷當前的下拉是不是打開狀態，如果是打開的就關閉掉。是關閉的就打開。
				document.getElementById("selectchild" + obj.name).style.display="none";
				childCreate=false;
			}else{
				//alert("2");
				document.getElementById("selectchild" + obj.name).style.display="";
				childCreate=true;
			}
		}else
		{
			//初始一個div放在上一個div下邊，當options的替身。
			//alert(document.getElementById("selectchild" + obj.name)+"2");
		    cDiv = document.createElement("div");
			cDiv.id="selectchild" + obj.name;
			cDiv.style.position = "absolute";
			cDiv.style.width=offset.width-1 + "px";
			cDiv.style.height=obj.options.length *20 + "px";
			cDiv.style.top=(offset.top+offset.height+2) + "px";
			cDiv.style.left=offset.left + "px";
			cDiv.style.background="#f7f7f7";
			cDiv.style.border="1px solid silver";
            //cDiv.style.fontsize="8px";
			
			var uUl = document.createElement("ul");
			uUl.id="uUlchild" + obj.name;
			uUl.style.listStyle="none";
			uUl.style.margin="0";
			uUl.style.padding="0";
			uUl.style.fontSize="11px";
			cDiv.appendChild(uUl);
			document.body.appendChild(cDiv);		
			childCreate=true;
			//alert(obj.options[2].innerText);
			var text =new Array();
			text[0]="---------language---------";
			text[1]="English";
			text[2]="简体中文";
			text[3]="日本語";
			
			for (var i=0;i<obj.options.length;i++)
			{
				//將原始的select標簽中的options添加到li中
				var lLi=document.createElement("li");
				lLi.id=obj.options[i].value;
				lLi.style.textIndent="4px";
				lLi.style.height="20px";
				lLi.style.lineHeight="20px";
				//lLi.innerHTML=obj.options[i].innerHTML;
				lLi.style.fontSize="11px";
				lLi.style.color="#717073";
				lLi.innerHTML=text[i];
				uUl.appendChild(lLi);
			}
			var liObj=document.getElementById("uUlchild" + obj.name).getElementsByTagName("li");//alert(obj.options.length);
			for (var j=0;j<obj.options.length;j++)
			{
				//為li標簽添加鼠標事件
				liObj[j].onmouseover=function(){
					this.style.background="gray";
					this.style.color="white";
				}
				liObj[j].onmouseout=function(){
					this.style.background="white";
					this.style.color="#717073";
				}
				liObj[j].onclick=function()
				{
					//做兩件事情，一是將用戶選擇的保存到原始select標簽中，要不做的再好看表單遞交后也獲取不到select的值了。
					obj.options.length=0;
					obj.options[0]=new Option(this.innerHTML,this.id);
					//同時我們把下拉的關閉掉。
					document.getElementById("selectchild" + obj.name).style.display="none";
					obj.options.onClick=getlink(obj.options[0]);
					childCreate=false;
					iDiv.align="center";
					iDiv.innerHTML=this.innerHTML;
				}
			}
			
			
		}

	}
	
	/*iDiv2.onclick=function(){//鼠標點擊
		if (document.getElementById("selectchild" + obj.name)){
		//判斷是否創建過div
			if (childCreate){
				//判斷當前的下拉是不是打開狀態，如果是打開的就關閉掉。是關閉的就打開。
				document.getElementById("selectchild" + obj.name).style.display="none";
				childCreate=false;
			}else{
				document.getElementById("selectchild" + obj.name).style.display="";
				childCreate=true;
			}
		}else{
			//初始一個div放在上一個div下邊，當options的替身。
			//var cDiv = document.createElement("div");
			cDiv.id="selectchild" + obj.name;
			cDiv.style.position = "absolute";
			cDiv.style.width=offset.width-1 + "px";
			cDiv.style.height=obj.options.length *20 + "px";
			cDiv.style.top=(offset.top+offset.height+2) + "px";
			cDiv.style.left=offset.left + "px";
			cDiv.style.background="#f7f7f7";
			cDiv.style.border="1px solid silver";
            
			var uUl = document.createElement("ul");
			uUl.id="uUlchild" + obj.name;
			uUl.style.listStyle="none";
			uUl.style.margin="0";
			uUl.style.padding="0";
			uUl.style.fontSize="11px";
			cDiv.appendChild(uUl);
			document.body.appendChild(cDiv);		
			childCreate=true;
			for (var i=0;i<obj.options.length;i++){
				//將原始的select標簽中的options添加到li中
				var lLi=document.createElement("li");
				lLi.id=obj.options[i].value;
				lLi.style.textIndent="4px";
				lLi.style.height="20px";
				lLi.style.lineHeight="20px";
				lLi.innerHTML=obj.options[i].innerHTML;
				uUl.appendChild(lLi);
			}
			var liObj=document.getElementById("uUlchild" + obj.name).getElementsByTagName("li");
			
			for (var j=0;j<obj.options.length;j++){
				//為li標簽添加鼠標事件
				liObj[j].onmouseover=function(){
					this.style.background="gray";
					this.style.color="white";
				}
				liObj[j].onmouseout=function(){
					this.style.background="white";
					this.style.color="black";
				}
				liObj[j].onclick=function(){
					//做兩件事情，一是將用戶選擇的保存到原始select標簽中，要不做的再好看表單遞交后也獲取不到select的值了。
					obj.options.length=0;
					obj.options[0]=new Option(this.innerHTML,this.id);
					//同時我們把下拉的關閉掉。
					document.getElementById("selectchild" + obj.name).style.display="none";
					obj.options.onClick=test(obj.options[0]);
					childCreate=false;
					iDiv.align="center";
					iDiv.innerHTML=this.innerHTML;
				}
			}
		}
	}*/
}
                                                                            