// JavaScript Document
var isIE6 = (navigator.userAgent.indexOf('MSIE 6.0') >= 0) && (navigator.userAgent.indexOf('Opera') < 0);
var checkbox = document.getElementById("checkbox");
var imgOriWidth = 120;
var imgOriHeight = 60;
var imgOriAnchorWidth = 120;
var imgOriAnchorHeight = 33;
var px = "px";
var _steps = [0, 25, 50, 70, 85, 95, 97, 99, 100];
var _slot = new Array();
var _slotImg = new Array();
var _inc = new Array();
var _flag = new Array();
var Intervals = new Array();

var position = {
    "UnitedKingdomLeft": 290,
    "UnitedKingdomTop": 310,
    "UnitedKingdomLeftRise": -1,
    "UnitedKingdomTopRise": -1,
    "GermanyLeft": 520,
    "GermanyTop": 210,
    "GermanyLeftRise": 0,
    "GermanyTopRise": -1,
    "EuropeEnglishLeft": 447,
    "EuropeEnglishTop": 383,
    "EuropeEnglishLeftRise": 0,
    "EuropeEnglishTopRise": 0,
    "JapanLeft": 683,
    "JapanTop": 417,
    "JapanLeftRise": 0,
    "JapanTopRise": 0,
    "AustraliaLeft": 700,
    "AustraliaTop": 543,
    "AustraliaLeftRise": 0,
    "AustraliaTopRise": -1,
    "USALeft": 188,
    "USATop": 343,
    "USALeftRise": 0,
    "USATopRise": -1
};
var officeposition = {
	"SingaporeLeft" : 611,
	"SingaporeTop" : 500,
	"SingaporeLeftRise" : -1,
	"SingaporeTopRise" : 0
	}
var imgLeft = {
	"UnitedKingdomLeft": "-80px",
	"GermanyLeft": "0px",
	"EuropeEnglishLeft": "0px",
	"JapanLeft": "0px",
	"AustraliaLeft": "0px",
	"USALeft": "0px"
}
var linkmanWidth = 200;
var linkmanHeight = 140;
var countryID = new Array();
var countrys = new Array("UnitedKingdom", "Germany", "EuropeEnglish", "Japan", "Australia", "USA");
function addFilter(countryImg) {
    countryImg.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src=" + countryImg.src + "),sizingMethod='scale');";
    countryImg.src = "/mysite/images/blank.gif";
}
function bindoffice(){	
var officediv = document.getElementById("worldMap");
var officedivid = officediv.getElementsByTagName("div");
for (i=0;i<officedivid.length;i++){
	var pattenoffice = /^RegionOffice.*/;
	if(pattenoffice.test(officedivid[i].id)){
	//alert(officedivid[i].id);
	var tempdiv = document.getElementById(officedivid[i].id);
	var tempanchor = document.getElementById(officedivid[i].id + "Anchor");
	if (isIE6){
		var forfilter = document.getElementById(officedivid[i].id + "IMG");
		//alert(forfilter);
		addFilter(forfilter);
    }
	tempanchor.onmouseover = new Function("officeover('"+officedivid[i].id+"')");
	tempanchor.onmouseout = new Function("officeout('"+officedivid[i].id+"')");
	}
}
}
function officeover(id){
	var tempimg = document.getElementById(id + "IMG");
	tempimg.src = "/mysite/images/landing-page/02.png";
	if (isIE6){
		var forfilter = document.getElementById(id + "IMG");
		//alert(forfilter);
		addFilter(forfilter);
    }
	}
function officeout(id){
	var tempimg = document.getElementById(id + "IMG");
	tempimg.src = "/mysite/images/landing-page/01.png";
	if (isIE6){
		var forfilter = document.getElementById(id + "IMG");
		//alert(forfilter);
		addFilter(forfilter);
    }
	}
function Over(id) {
    _flag[id + "Spread"] = true;
    if (_flag[id + "Shrink"] == true) {
        setTimeout(function() {
            Over(id);
        },
        50);
        return;
    }
    var div = document.getElementById(id);
    div.className = "Spread";
    var _anchor = document.getElementById(id + "Anchor");
    var img = document.getElementById(id + "Img");
	img.style.left = "-200px";
    _inc[id + "Spread"] = _inc[id + "Shrink"] + 1;
    var _Interval = setInterval(function() {
        spread(id, div, img, _anchor, _Interval);
    },
    40);
}

function Out(id) {
    _flag[id + "Shrink"] = true;
    if (_flag[id + "Spread"] == true) {
        setTimeout(function() {
            Out(id);
        },
        20);
        return;
    }
    var div = document.getElementById(id);
    var _anchor = document.getElementById(id + "Anchor");
    _inc[id + "Shrink"] = _inc[id + "Spread"] - 1;
    var _Interval = setInterval(function() {
        shrink(id, div, document.getElementById(id + "Img"), _anchor, _Interval);
    },
    40);
}

function spread(id, div, img, _anchor, _Interval) {
    var count = _inc[id];
    if (count >= _steps.length || _flag[id + "Shrink"] == true) {
        clearInterval(_Interval);
        if (_flag[id + "Shrink"] == false) _inc[id] = _steps.length - 1;
        _inc[id]--;
        _flag[id + "Spread"] = false;
        return;
    }
    _anchor.style.width = _slot[id + "SpreadWidth"][count] + px;
    _anchor.style.height = _slot[id + "SpreadHeight"][count] - 25 + px;

    div.style.width = _slot[id + "SpreadWidth"][count] + px;
    div.style.height = _slot[id + "SpreadHeight"][count] + px;

    div.style.left = _slot[id + "SpreadLeft"][count] + px;
    div.style.top = _slot[id + "SpreadTop"][count] + px;

    img.style.left = -200 + _slotImg[id + "SpreadLeft"][count] + px;
    img.style.top = _slotImg[id + "SpreadTop"][count] + px;
    _inc[id + "Spread"]++;
    _inc[id]++;
}
function shrink(id, div, img, _anchor, _Interval) {
    var count = _inc[id];
    if (count < 0 || _flag[id + "Spread"] == true) {
        clearInterval(_Interval);
        div.className = "Shrink";
        if (_flag[id + "Spread"] == false) {
			document.getElementById(id + "Img").style.left = imgLeft[id + 'Left'];
        }
        _inc[id] = 0;
        _flag[id + "Shrink"] = false;
        return;
    }
    div.style.width = _slot[id + "SpreadWidth"][count] + px;
    div.style.height = _slot[id + "SpreadHeight"][count] + px;
    div.style.left = _slot[id + "SpreadLeft"][count] + px;
    div.style.top = _slot[id + "SpreadTop"][count] + px;
    _anchor.style.width = _slot[id + "SpreadWidth"][count] + px;
    _anchor.style.height = _slot[id + "SpreadHeight"][count] - 25 + px;
    img.style.left = -200 + _slotImg[id + "SpreadLeft"][count] + px;
    img.style.top = _slotImg[id + "SpreadTop"][count] + px;
    _inc[id + "Shrink"]--;
    _inc[id]--;
}
function modifySrc(img, src) {
    img.src = src;
    if (isIE6) addFilter(img);
}
function bind() {
    var world = document.getElementById("worldMap");
    var countryDivs = world.getElementsByTagName("div");
	for (i = 0; i < countryDivs.length; i++) {
		//alert(countryDivs[i].id);
		}
    for (i = 0; i < countryDivs.length; i++) {
		var testoffice = /^RegionOffice.*/;
		if (countryDivs[i].id != "" && testoffice.test(countryDivs[i].id) == 0){
        var id = countryID[i] = countryDivs[i].id;
		//alert("/mysite/images/landing-page/" + id + ".png");
        _inc[id + "Shrink"] = -1;
        _inc[id + "Spread"] = _steps.length;
        _inc[id] = 0;
        _flag[id + "Spread"] = false;
        _flag[id + "Shrink"] = false;
        var _anchor = document.getElementById(id + "Anchor");
        var temp = _anchor.href;
        if (isIE6) {
            modifySrc(_anchor.parentElement.getElementsByTagName('img')[0], "/mysite/images/landing-page/" + id + ".png");
        }
        if (temp.indexOf('javascript') == -1) {
            _anchor.href = "javascript:redirectToSite('" + temp + "')";
        }
        var scrollLeft = linkmanWidth - imgOriWidth;
        var scrollTop = linkmanHeight - imgOriHeight;
        _slot[id + "SpreadLeft"] = new Array();
        _slot[id + "SpreadTop"] = new Array();
        _slot[id + "SpreadWidth"] = new Array();
        _slot[id + "SpreadHeight"] = new Array();
        _slotImg[id + "SpreadLeft"] = new Array();
        _slotImg[id + "SpreadTop"] = new Array();
        for (j = 0; j < _steps.length; j++) {
            var _stepThisX = Math.round(scrollLeft * _steps[j] / 100);
            var _stepThisY = Math.round(scrollTop * _steps[j] / 100);

            _slot[id + "SpreadLeft"][j] = position[id + "Left"] + _stepThisX * position[id + "LeftRise"];
            _slot[id + "SpreadTop"][j] = position[id + "Top"] + _stepThisY * position[id + "TopRise"];
            _slot[id + "SpreadWidth"][j] = imgOriWidth + _stepThisX;
            _slot[id + "SpreadHeight"][j] = imgOriHeight + _stepThisY;
            _slotImg[id + "SpreadLeft"][j] = (linkmanWidth - _slot[id + "SpreadWidth"][j]) * position[id + "LeftRise"];
            _slotImg[id + "SpreadTop"][j] = (linkmanHeight - _slot[id + "SpreadHeight"][j]) * position[id + "TopRise"];
        }
        _anchor.onmouseover = new Function("Over('" + id + "')");
        _anchor.onmouseout = new Function("Out('" + id + "')");
        _anchor.onfocus = new Function("this.blur()");
        _anchor.style.display = "block";
    }
	}
}
function redirectToSite(url) {
    if (checkbox.checked) {
        createCookie("local", escape(url), 365 * 24 * 60 * 60 * 1000);
    } else {
        createCookie("local", escape(url));
    }
    window.location = url;
}
function createCookie(name, value, ms) {
		//alert(value);
    if (ms) {
        var date = new Date();
        date.setTime(date.getTime() + ms);
        var expires = "; expires=" + date.toGMTString();
    } else var expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
}
