window.onresize = updLayerPosition;

function lib_bwcheck() { //Browsercheck (needed)
	this.ver=navigator.appVersion
	this.agent=navigator.userAgent
	this.dom=document.getElementById?1:0
	this.opera5=this.agent.indexOf("Opera 5")>-1
	this.ie5=(this.ver.indexOf("MSIE 5")>-1 && this.dom && !this.opera5)?1:0;
	this.ie6=(this.ver.indexOf("MSIE 6")>-1 && this.dom && !this.opera5)?1:0;
	this.ie7=(this.ver.indexOf("MSIE 7")>-1 && this.dom && !this.opera5)?1:0;
	this.ie4=(document.all && !this.dom && !this.opera5)?1:0;
	this.ie=this.ie4||this.ie5||this.ie6||this.ie7
	this.mac=this.agent.indexOf("Mac")>-1
	this.ns6=(this.dom && parseInt(this.ver) >= 5) ?1:0;
	this.ns4=(document.layers && !this.dom)?1:0;
	this.bw=(this.ie7 ||this.ie6 || this.ie5 || this.ie4 || this.ns4 || this.ns6 || this.opera5)
	return this
}
var bw=new lib_bwcheck()
var ourShopID = "3brand_1010";
var yearScroll = true;
var s1="";
var s2="";
var s3="";

var displayType = "block";

if(!bw.ie)
	displayType = "table-row-group";

function swapLang(currLang, nextLang)
{
	var thisLocation = this.location.toString();
	
	if(currLang.indexOf("index") > -1)
	{
		this.location = thisLocation.substring(0, thisLocation.lastIndexOf("/")+1) + nextLang + ".html";
	}
	else
	{
		this.location = thisLocation.replace("/"+currLang+"/","/"+nextLang+"/");
	}
}

function emptyIt(thisID) {
	var thisObj = document.getElementById(thisID);
	
	if (!thisObj)
		return;
	
	thisObj.value = "";
}

function returnImgPath(thisObj, nextStatus) {
	var imgOn  = thisObj.id + "_b";
	var imgOff = thisObj.id + "_a";
	var imagePath = "";


	if (nextStatus=="off")
		imagePath = thisObj.src.toString().replace(imgOn, imgOff);

	else
	
		imagePath = thisObj.src.toString().replace(imgOff, imgOn);
	

	return imagePath;
}

function imageSwap(thisID, nextStatus) {
	var thisObj = document.getElementById(thisID);
	
	if (!thisObj)
		return;

	if (thisObj.id2!=null)
		return;

	//alert(thisID +"--"+nextStatus);		
	thisObj.src = returnImgPath(thisObj, nextStatus);
	//alert(thisObj.src);
	if (thisID.substring(0,6)=="3brand")
		updArrowPosition(thisID, nextStatus, 61);
}

function imageSwap2(thisID, nextStatus) {
	var thisObj = document.getElementById(thisID);
	
	if (!thisObj)
		return;

	if (thisObj.id2!=null)
		return;
	thisObj.src = returnImgPath(thisObj, nextStatus);

}


function updArrowPosition(thisID, nextStatus, thisHeight) {
	var arrow3brandObj = document.getElementById("3brand_arrow");
	
	if (nextStatus=="off")
	{
		updPosition("3brand_arrow", ourShopID, 0, 61);
		arrow3brandObj.style.display = displayType;
	}
	else
	{
		updPosition("3brand_arrow", thisID, 0, thisHeight);
		arrow3brandObj.style.display = displayType;
	}
}

function contentSwap(contentID, imageID) {
	var contentObjArr= new Array();
	var imageObjArr  = new Array();
	
	contentObjArr= [document.getElementById("m01Ctn"),document.getElementById("m02Ctn"),document.getElementById("m03Ctn"),document.getElementById("m04Ctn"),document.getElementById("m05Ctn"),document.getElementById("m06Ctn"),document.getElementById("m07Ctn"),document.getElementById("m08Ctn"),document.getElementById("m09Ctn"),document.getElementById("m10Ctn"),document.getElementById("m11Ctn"),document.getElementById("m12Ctn")];
	imageObjArr  = [document.getElementById("m01"),document.getElementById("m02"),document.getElementById("m03"),document.getElementById("m04"),document.getElementById("m05"),document.getElementById("m06"),document.getElementById("m07"),document.getElementById("m08"),document.getElementById("m09"),document.getElementById("m10"),document.getElementById("m11"),document.getElementById("m12")];
	
	var contentObj = document.getElementById(contentID);
	var imageObj = document.getElementById(imageID);
	
	if(!contentObj || !imageObj)
		return;
	
	for (var i=0; i<contentObjArr.length; i++)
	{
		contentObjArr[i].style.display = "none";
		imageObjArr[i].src = returnImgPath(imageObjArr[i], "off");
	}
	
	contentObj.style.display = displayType;
	imageObj.src = returnImgPath(imageObj, "on");
}

function coverageSwap(toCoverage) {
	var imageObj = document.getElementById('coverage_map');
	var contentObj = document.getElementById('content_'+toCoverage);
	
	if(!contentObj || !imageObj)
		return;
	
	var contentObjArr= new Array();
	contentObjArr= [document.getElementById("content_nt"),document.getElementById("content_kl"),document.getElementById("content_hk"),document.getElementById("content_oi")];
	
	imageObj.src = imageObj.src.substring(0, imageObj.src.toString().indexOf("_map")+4) + "_" + toCoverage + ".gif";
	
	for (var i=0; i<contentObjArr.length; i++)
	{
		if(contentObjArr[i].id == ("content_"+toCoverage))
			contentObj.style.display = displayType;
		else
			contentObjArr[i].style.display = "none";
	}
}

//If you want it to move faster you can set this lower, it's the timeout:
var speed = 65

//Sets variables to keep track of what's happening
var loop, timer

//Object constructor
function makeObj(obj,nest) {
    nest=(!nest) ? "":'document.'+nest+'.'
	this.el=bw.dom?document.getElementById(obj):bw.ie4?document.all[obj]:bw.ns4?eval(nest+'document.'+obj):0;
  	this.css=bw.dom?document.getElementById(obj).style:bw.ie4?document.all[obj].style:bw.ns4?eval(nest+'document.'+obj):0;
  	this.scrollHeight=bw.ns4?this.css.document.height:this.el.offsetHeight
	this.scrollWidth=bw.ns4?this.css.document.width:this.el.offsetWidth
	this.clipHeight=bw.ns4?this.css.clip.height:this.el.offsetHeight
	this.clipWidth=bw.ns4?this.css.clip.width:this.el.offsetWidth
	this.up=goUp;this.down=goDown;
	this.moveIt=moveIt; this.x=0; this.y=0;
   	this.obj = obj + "Object";
	
	if (obj.toLowerCase().indexOf("horizontal") > -1)
		this.direction = "horizontal";
	else
		this.direction = "vertical";
	eval(this.obj + "=this")
    return this
}

// A unit of measure that will be added when setting the position of a layer.
var px = bw.ns4||window.opera?"":"px";

function moveIt(x,y) {
	if(this.direction=="vertical")
	{
		this.x=x;
		this.y=y;
	}
	else
	{
		this.x=y;
		this.y=x;
	}
	
	this.css.left = this.x+px;
	this.css.top = this.y+px;
}

//Makes the object go down
function goDown(move) {
	if(this.direction=="vertical")
	{
		if (this.y>-this.scrollHeight+oCont.clipHeight) {
			this.moveIt(0,this.y-move)
			if (loop) setTimeout(this.obj+".down("+move+")",speed)
		}
	}
	else
	{
		if (this.x>-this.scrollWidth+oCont.clipWidth) {
			this.moveIt(0,this.x-move)
			if (loop) setTimeout(this.obj+".down("+move+")",speed)
		}
	}
}

//Makes the object go up
function goUp(move) {
	if(this.direction=="vertical")
	{
		if (this.y<0) {
			this.moveIt(0,this.y-move)
			if (loop) setTimeout(this.obj+".up("+move+")",speed)
		}
	}
	else
	{
		if (this.x<0) {
			this.moveIt(0,this.x-move)
			if (loop) setTimeout(this.obj+".up("+move+")",speed)
		}
	}
}

//Calls the scrolling functions. Also checks whether the page is loaded or not.
function scroll(speed) {
	if (scrolltextLoaded) {
		loop = true;
		if (speed>0) oScroll.down(speed)
		else oScroll.up(speed)
	}
}

//Stops the scrolling (called on mouseout)
function noScroll() {
	loop = false
	if (timer) clearTimeout(timer)
}
//Makes the object
var scrolltextLoaded = false
function scrolltextInit(layerID1, layerID2) {

	var objLayerID1= document.getElementById(layerID1);
	var objLayerID2= document.getElementById(layerID2);

	if (!objLayerID1|| !objLayerID2) return;


	oCont = new makeObj(layerID1)
	oScroll = new makeObj(layerID2,layerID1)
	oScroll.moveIt(0,0)
	oCont.css.visibility = "visible"
	scrolltextLoaded = true
}

//Call the init on page load if the browser is ok...
//if (bw.bw) onload = scrolltextInit

//update current flowing layer position
function findPosX(obj) {
	var curleft = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
			if(getStyle(obj, "position")=="absolute") break;
		}
	}
	else if (obj.x)	curleft += obj.x;
	return curleft;
}

function findPosY(obj) {
	var curtop = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curtop += obj.offsetTop
			if(bw.opera) {	//doesn't work
				curtop+= depx(getStyle(obj, 'margin-top'));
				curtop+= depx(getStyle(obj, 'padding-top'));
			}
			obj = obj.offsetParent;
			if(getStyle(obj, "position")=="absolute") break;
		}
	}
	else if (obj.y)	curtop += obj.y;	// NS4
	return curtop;
}

function getStyle(x,styleProp) {
	//var x = document.getElementById(el);
	if (window.getComputedStyle)
	var y = window.getComputedStyle(x,null).getPropertyValue(styleProp);
	else if (x.currentStyle)
	var y = eval('x.currentStyle.' + styleProp);
	return y;
}

function updLayerPosition() {
	if(document.getElementById("divVerticalContent"))
		updPosition("divVerticalContent","dateNews", 0, 0);
	
	if(document.getElementById("divHorizontalContent"))
		updPosition("divHorizontalContent","yearSelect", 2, 15);
	
	if(document.getElementById("3brand_arrow"))
		updPosition("3brand_arrow", ourShopID, 0, 61);
}

function updPosition(layerID, cellID, adj_width, adj_height) {
	var layerObj = document.getElementById(layerID);
	var cellObj = document.getElementById(cellID);
	
	if (!layerObj || !cellObj) return;
	layerObj.style.left = (findPosX(cellObj) + parseInt(adj_width,10)) +'px';
	layerObj.style.top  = (findPosY(cellObj) + parseInt(adj_height,10)) + 2 +'px';
}

function updYrPosition(currentYr, highlightYr) {
	
	var OcurrentYr= document.getElementById(currentYr);
	var OhighlightYr= document.getElementById(highlightYr);
	
//	if (!OcurrentYr || !OhighlightYr) return;
//	if !oScroll return;

	var maxTime = currentYr - highlightYr;
	
	for(var i=0; i < maxTime; i++)
	{
		if(i < maxTime)
			oScroll.down(71)
	}
}

function updYrPosition2(currentYr, highlightYr) {
	
	var OcurrentYr= document.getElementById(currentYr);
	var OhighlightYr= document.getElementById(highlightYr);
	
//	if (!OcurrentYr || !OhighlightYr) return;
//	if !oScroll return;

	var maxTime = currentYr - highlightYr;
	
	for(var i=0; i < maxTime; i++)
	{
		if(i < maxTime)
			oScroll.down(71)
	}
}
var popEventWin = null;
function popEventImg(thisID, w, h) {
	if(popEventWin && !popEventWin.closed)
		popEventWin.close();
	else
		popEventWin=null;
	
	var thisObj = document.getElementById(thisID);
	
	var thisSrcPath = thisObj.src.substring(0, (thisObj.src.length-4));
	var thisSrcExt  = thisObj.src.substring((thisObj.src.length-4), thisObj.src.length);
	var thisURL = thisSrcPath + "_pop" + thisSrcExt;
	
	w = w || 800
	h = h || 600
	popEventWin=window.open(thisURL,'csl_event_photo','toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,screenX=0,screenY=0,top=0,left=0,resizable=1,width='+w+',height='+h)
	popEventWin.focus();
}

function addClass(ele, cname){
	if(!ele) return;
	if(!ele.className || ele.className=='') {
		ele.className=cname;
		return;
	}
	var re = new RegExp('\\b' + cname + '\\b', 'g');
	if(re.test(ele.className)) return;	//if already has this className
 	else ele.className += ' ' + cname;
}

function swapNav(thisObj)
{
	if (thisObj.src.indexOf("_b.")>-1){
		thisObj.src = thisObj.src.replace("_b.","_a.");
	
	}else{
		thisObj.id2="hac";
		thisObj.src = thisObj.src.replace("_a.","_b.");

	}
}

function showNav() 
{
	var i=0, j=0;
	var navObj, navLayerObj;
	                                                                                                        
	var navArr = ["aboutUs", "productNservice", "customerSupport", "mediaCenter", "ourLocation", "joinUs", "contactUs", 
	              "abs_corporate_profile", "abs_corporate_vision", "abs_society_community", "abs_milestone_corp", 
	              "pds_consumer", "pds_business", "pds_handset", "cus_service_channels", "cus_park_coverage", 
	              "cus_faq", "cus_privacy_policy", "med_press_releases", "med_corporate_profile", "med_gallery", 
	              "med_news_contact", "corporate_profile_1010", "corporate_profile_123", "corporate_profile_nwm", 
	              "milestone_corp_info", "milestone_corp_coverage", "milestone_corp_roam", "milestone_corp_handset", 
	              "milestone_corp_awards", "milestone_corp_others", "privacy_policy_csl", "privacy_policy_nwm", 
	              "press_archive_csl", "gal_major_events", "gal_our_shops"]
	
	for (i=0; i<navArr.length; i++)
	{
		navObj = document.getElementById(navArr[i]);
		
		if(navObj.id==s1)
		{
			swapNav(navObj);
			navLayerObj = document.getElementById(s1+"_layer");
			
			if(navLayerObj)
				navLayerObj.style.display = displayType;
		}
		else if(navObj.id==s2)
		{
			addClass(navObj,"nav2_on");
			navLayerObj = document.getElementById(s2+"_layer");
			
			if(navLayerObj)
				navLayerObj.style.display = displayType;
		}
		else if(navObj.id==s3)
		{
			addClass(navObj,"nav3_on");
		}
		else
		{
			navObj.onmouseover = function(){swapNav(this);}
			navObj.onmouseout = function(){swapNav(this);}
		}
	}
}

//CSL original JS from www.hkcsl.com
function linkCSL(who, ver) {
	targetCSL = "pccw";
	if (ver == 'c') {
		if (who == 'main') { top.location.href = "main_c.html"; }
		else if (who =='1010') { window.open("http://www.csl1010.com/personal_ch/", targetCSL);}
		else if (who =='123') { window.open("http://www.one2free.com/chi/", targetCSL);}
		else if (who =='nwm') { window.open("http://www2.nwmobility.com/html/chi/default.jsp", targetCSL);}
		else if (who =='1p1') { window.open("http://www.oneplusone.com.hk", targetCSL);}
		else if (who =='sim') { window.open("http://prepaid.hkcsl.com/chome.html", targetCSL);}
		else if (who =='roam') { window.open("http://roam.hkcsl.com/gb/index.jsp", targetCSL);}
		else if (who =='telstra') { window.open("http://www.telstra.com", targetCSL);}
		else if (who =='pccw') { window.open("http://www.pccw.com/", targetCSL);}
		else if (who =='csl') { window.open("http://www.hkcsl.com/index_c.html", targetCSL);}
		else if (who =='sdemo') { window.open("http://serviceguide.hkcsl.com/index_c.html", targetCSL);}
		else if (who =='ishop') { window.open("http://www.hkcsl.com/app/ishop/mobile/index_zh.html", targetCSL);}
	} else {
		if (who == 'main') { top.location.href = "main.html"; }
		else if (who =='1010') { window.open("http://www.csl1010.com/", targetCSL);}
		else if (who =='123') { window.open("http://www.one2free.com/eng/", targetCSL);}
		else if (who =='nwm') { window.open("http://www2.nwmobility.com/html/eng/default.jsp", targetCSL);}
		else if (who =='1p1') { window.open("http://www.oneplusone.com.hk", targetCSL);}
		else if (who =='sim') { window.open("http://prepaid.hkcsl.com/", targetCSL);}
		else if (who =='roam') { window.open("http://roam.hkcsl.com/eng/index.jsp", targetCSL);}
		else if (who =='telstra') { window.open("http://www.telstra.com", targetCSL);}
		else if (who =='pccw') { window.open("http://www.pccw.com/", targetCSL);}
		else if (who =='csl') { window.open("http://www.hkcsl.com/index.html", targetCSL);}
		else if (who =='sdemo') { window.open("http://serviceguide.hkcsl.com/index.html", targetCSL);}
		else if (who =='ishop') { window.open("http://www.hkcsl.com/app/ishop/mobile/index_en.html", targetCSL);}
	}
}


function MM_openBrWindow(theURL) { //v2.0
  window.open(theURL);
}