
/* Initialisation ----------------------------------------------------------------------*/

var d = document, dE = d.documentElement, dB = d.body, dA = d.all;
IE5 = navigator.userAgent.match('MSIE 5.');

var modules = new Object();
var library = new Object();

library={
	init:function(){
		if(d.getElementById){
			modules.countryMap.init();
			if(d.getElementById('style01')){
				modules.sidebarimagechange.init();
			}
			modules.rollovers.init('_o','_h','container');
			modules.quickjump.init();
			correctPNG();
			modules.h3.init();
			if(d.getElementById('specialityInformation')){
				modules.imageresize.init();
			}
			if(d.getElementById('popup')){
				modules.popup.init();
			}

		}

		var anchor = d.getElementsByTagName('a');
		for (var i=0;i<anchor.length;i++) {
			if (anchor[i].getAttribute('rel') == 'popup') {
				anchor[i].onclick = function () {
			    return winOpen(this.href)
				}
				anchor[i].title += '(Popup)';
			}
		}
	},
	end:function(){ modules.ie.end(); }
};

window.onload = library.init;
window.onunload = library.end;

places = ['tokyo','kanagawa','fukuoka'];


countryLinks = [
	'/','/japan/','/brunei/','/cambodia/','/indonesia/','/laos/','/malaysia/','/myanmar/','/philippines/','/singapore/','/thailand/','/vietnam/'
]


/* Functions --------------------------------------------------------------------------*/

if(!Array.prototype.push){
	Array.prototype.push = function(){
		for(var i = 0; i < arguments.length; i++){ this[this.length] = arguments[ i ]; }
		return this.length;
	}
}
function $($key, $parent){
	var elements = [];
	var tags = 'A LINK IMG P DIV OL UL LI DL DT DD SPAN EM STRONG LINK TABLE THEAD TBODY TR TD TH H5 H4 H3 H2 H1 OBJECT OPTION SELECT AREA';
	$parent = (typeof $parent == 'undefined') ? false : $parent;
	keys = $key.split(',');
	for (var i = 0; i < keys.length; i++) {
		var key = keys[i];
		if(tags.match(key)){
			$parent = $parent ? $parent : d;
			elements = $parent.getElementsByTagName(key);
			return elements;
		} else if(d.getElementById(key)){
			element = d.getElementById(key);
			return element;
		} else {
			limit = 5;
			IE5 = navigator.userAgent.match('MSIE 5.');
			if(!IE5){
				$parent = $parent ? $parent : d;
				all = $parent.getElementsByTagName('*');
				for(var j=0, el; el=all[j]; j++){ if(el.className.match(' '+key) || el.className==key){ elements.push(el); } }
			} else {
				all = d.all;
				for(var j=0, el; el=all[j]; j++){ 
					if($parent){
						if(el.className.match(' '+key) || el.className==key){
							var $node = el;
							for(k=0; k<=limit; k++){ if($node.parentNode){ $node = $node.parentNode; } if($node == $parent){ elements.push(el); break; } }
						}
					} else { if(el.className.match(' '+key) || el.className==key){ elements.push(el); } }
				}
			}
		}
	}
	if(elements.length == 1){ return elements[0]; }
	if(elements.length == 0){ return false }
	return elements;
}

function correctPNG(){
	var arVersion = navigator.appVersion.split("MSIE");
	var version = parseFloat(arVersion[1]);
	if ((version >= 5.5) && (document.body.filters)){
		for(var i=0; i<document.images.length; i++){
			var img = document.images[i];
			var imgName = img.src.toUpperCase();
			if (imgName.substring(imgName.length-3, imgName.length) == "PNG"){
				var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' ";
				var imgStyle = "display:block; float:left;";
				var strNewHTML = "<span " + imgTitle + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";" + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader" + "(src=\'" + img.src + "\', sizingMethod='scale');\" /></span>";
				img.outerHTML = strNewHTML
				i = i-1
			}
		}
	}
}

function winOpen(url) {
	window.open(
		url,
		'popup',
		'width=380,height=380,scrollbars=0,resizable=1'
	);

	return false;
};

/* Modules --------------------------------------------------------------------------*/

modules.rollovers = {
	init:function($ext, $hoverExt, $key){
		$key = $($key);
		$key[0]=!$key.length ? $key : $key[0];
		var rollElm = new Array();
		var preloadImg = new Array();
		for(var h=0, key; key=$key[h]; h++){
			rollElm[h] = $('IMG',key);
			j=0;
			for(var i=0, el; el=rollElm[h][i]; i++){
				j=0;
				if(el.src){
					j++;
					elExt = el.src.substring(el.src.length-4-$ext.length, el.src.length-4);
					if(elExt == $ext && !el.src.match($hoverExt+'.')){
						el.source = el.src;
						el.file = el.src.split('/')[el.src.split('/').length-1];
						el.fext = el.file.split('.')[1];
						el.hoverSrc = el.src.replace($ext+'.'+el.fext,$hoverExt+'.'+el.fext);
						preloadImg[j] = new Image();
						preloadImg[j].src = el.hoverSrc;
						j++;
						if(el.parentNode.nodeName == 'A'){
							el.parentNode.onfocus = function(){ this.firstChild.src = this.firstChild.hoverSrc; }
							el.parentNode.onblur = function(){ this.firstChild.src = this.firstChild.source; }
						}
						el.onmouseover = function(){ this.src = this.hoverSrc; }
						el.onmouseout = function(){ this.src = this.source; }
					}
				}
			}
		}
	}
}

modules.countryMap ={
	init:function(){
		if(d.getElementById('countryMap')){
			$topPreload = [];
			$areas = d.getElementById('countryMap').getElementsByTagName('AREA');//$('AREA',$countryMap);
			$countries = [];
			$topImage = d.getElementById('top-holder').getElementsByTagName('IMG')[0];
			$topImage._sources = [];
			$topImage._source  =$topImage.src;
			for(var i=0, $cArea; $cArea = $areas[i]; i++){
				$countries[$countries.length] = $cArea.alt;
				$topImage._sources[$topImage._sources.length] = $topImage.src.split('.jpg')[0] + '_' + $cArea.alt + '.jpg';
				$topPreload[i] = new Image();
				$topPreload[i].src = $topImage._sources[i];
				$areas[i]._index = i;
				$areas[i].onmouseover = function(){ $topImage.src = $topImage._sources[this._index]; }
				$areas[i].onmouseout = function(){ $topImage.src = $topImage._source; }
			}
		}
	}
}

modules.quickjump = {
	init:function(){
		$quickjump = $('quickjump');
		$quickSelect = $('SELECT',$quickjump)[0];
		$quickSelect.onchange = function(){
			d.location = countryLinks[ $quickSelect.options[$quickSelect.selectedIndex].value ];
		}
	}
}

modules.h3 = {
	init:function(){
		h3 = $('H3');
		for (i in h3){ h3[i].innerHTML = '<span>'+ h3[i].innerHTML + '</span>'; }
	}
}

modules.ie={
	init:function(){
		this.flash();
	},
	flash:function(){
		if(navigator.userAgent.match('MSIE 6') ){
			$obj = $('OBJECT');
			for (var i=0,el; el=$obj[i]; i++){ el.outerHTML = el.outerHTML; }
		}
	},
	end:function(){
		if (d.all && window.attachEvent) {
			var elProps = [
				'data', 'onmouseover', 'onmouseout',
				'onmousedown', 'onmouseup', 'ondblclick',
				'onclick', 'onselectstart', 'oncontextmenu'
			];
			var all = d.all;
			for (var i = 0, el; el = all[i]; i++) {
				for (var j = 0, elProp; elProp = elProps[j]; j++) { el[elProp] = null; }
			}
		}
	}
}

modules.imageresize={
	init:function(){
		this.resize();
	},
	resize:function(){
		var links;
		var imageWidth = 51;
		var thisWidth;
		var thisHeight;
		var imageRatio;
	
		links = d.getElementById('specialityInformation').getElementsByTagName("img");

		for(i=0;i<links.length;i++){
			if (!links[i].className.match("specialityInformationtitle")){
				thisWidth = links[i].width;
				thisHeight = links[i].height;
				imageRatio = imageWidth/thisWidth;

				links[i].width = imageWidth;
				links[i].height = Math.round(thisHeight*imageRatio);
			}
		}
	}
}

modules.sidebarimagechange={
	init:function(){
		this.imageSet();
	},
	imageSet:function(){
		var imgArray = new Array('','brunei','cambodia','indonesia','laos','malaysia','myanmar','philippines','singapore','thailand','vietnam');
		var pageName;
		pageName = d.getElementById('style01').getElementsByTagName('div');

		for(i=0;i<pageName.length;i++){
			if(pageName[i].className.match('brunei'))this.change(1);
			if(pageName[i].className.match('cambodia'))this.change(2);
			if(pageName[i].className.match('indonesia'))this.change(3);
			if(pageName[i].className.match('laos'))this.change(4);
			if(pageName[i].className.match('malaysia'))this.change(5);
			if(pageName[i].className.match('myanmar'))this.change(6);
			if(pageName[i].className.match('philippines'))this.change(7);
			if(pageName[i].className.match('singapore'))this.change(8);
			if(pageName[i].className.match('thailand'))this.change(9);
			if(pageName[i].className.match('vietnam'))this.change(10);
		}
	},
	change:function(imageno){
		var subNavi;
		var imgPath;
		var splitChara = '_o';
		var hoverChara = '_a';
		var subNavi = d.getElementById('subNavi').getElementsByTagName('img');

		imgPath = subNavi[imageno].src;
		imgPath = imgPath.split(splitChara);
		subNavi[imageno].src = imgPath[0]+hoverChara+imgPath[1];
	}

}

modules.popup={
	init:function(){
		this.popup();
	},
	popup:function(){
		var anker = d.getElementById('popup').getElementsByTagName('a')
		var popupinfo;

		for(i=0;i<anker.length;i++){
			popupinfo = anker[i].rel.split('|');
			features = 'width='+popupinfo[1]+',height='+popupinfo[2];

			anker[i].onclick = function(){
				window.open(popupinfo[0],'window',features+',scrollbars=yes');
			}
		}
	}
}