Global.locations = {} || Global.locations;

Global.locations.region = null;
Global.locations.office = null;
Global.locations.info = null;
Global.locations.hint = null;

Global.locations.hide = function (obj) {
	obj.style.display = "none";
	if(obj.shadow.parentNode) {
		obj.shadow.remove();
		obj.shadow = null;
	}
}

Global.locations.closeRegion = function (){
	Global.locations.hide(this);
	Global.locations.region = null;
}

Global.locations.showRegion = function( id ){
    if(Global.locations.region !== null){
    	Global.locations.hide(Global.locations.region);
    	if(Global.locations.office !== null){
    		Global.locations.hide(Global.locations.office);
    		Global.locations.office = null;
    	}
    }
    
    if(Global.locations.info !== null){
		Global.locations.hide(Global.locations.info);
		Global.locations.info = null;
	}
    
    if(Global.locations.hint !== null){
		Global.locations.hide(Global.locations.hint);
		Global.locations.hint = null;
	}
    
	Global.locations.region = $('region_'+id);
	Global.locations.region.style.display='block';
    Global.locations.region.style.top = '100px';
    Global.locations.region.style.left = parseInt(Global.locations.region.parentNode.offsetWidth/2 - Global.locations.region.offsetWidth)+'px';
    
    Global.locations.showShadow(Global.locations.region);
    Global.locations.showClose(Global.locations.region);
    
    var el = Global.locations.region.getElementsByClassName('close');
    el[0].onclick = Global.locations.closeRegion.bind(Global.locations.region);
}

Global.locations.showOffices = function ( id ) {
	if(Global.locations.office !== null){
		Global.locations.office.style.display = "none";
    	Global.locations.office.shadow.remove();
	}
	
	Global.locations.office = $('country_'+id);
	Global.locations.office.style.display='block';
	var el = Global.locations.office.getElementsByClassName('scroll');
	if(el[0].offsetHeight > 200) {
		el[0].style.height = "200px";
	}
    Global.locations.office.style.top = Global.locations.region.style.top;
    Global.locations.office.style.left = parseInt(Global.locations.region.parentNode.offsetWidth/2)+'px';
    
    Global.locations.showShadow(Global.locations.office);
    Global.locations.hideClose(Global.locations.region);
    
    var el = Global.locations.office.getElementsByClassName('close');
    el[0].onclick = Global.locations.closeOffices.bind(Global.locations.office);
}

Global.locations.closeOffices = function (){
	Global.locations.hide(this);
	Global.locations.office = null;
	Global.locations.showClose(Global.locations.region);
}

Global.locations.hideClose = function ( obj ){
	var el = obj.getElementsByClassName('close');
	el[0].style.display = 'none';
}

Global.locations.showClose = function ( obj ){
	var el = obj.getElementsByClassName('close');
	el[0].style.display = 'block';
}

Global.locations.showShadow = function ( obj ){
	obj.shadow = $(document.createElement("div"));
	obj.shadow.addClassName("window-shadow");
	obj.shadow.style.width = obj.offsetWidth+'px';
	obj.shadow.style.height = obj.offsetHeight+'px';
	obj.shadow.style.top = parseInt(obj.style.top)+2 + 'px';
	obj.shadow.style.left = parseInt(obj.style.left)+2 + 'px';
	obj.shadow.style.display = 'block';
	
	obj.parentNode.appendChild(obj.shadow);
}

Global.locations.showOfficeInfo = function ( id ) {
	if(Global.locations.region !== null){
    	Global.locations.hide(Global.locations.region);
    	Global.locations.region = null;
	}
	if(Global.locations.office !== null){
  		Global.locations.hide(Global.locations.office);
  		Global.locations.office = null;
    }
	
	Global.locations.info = $('office_'+id);
	Global.locations.info.style.display='block';
	Global.locations.info.style.top= parseInt(Global.locations.info.parentNode.offsetHeight/2 - Global.locations.info.offsetHeight/2)+'px';
    Global.locations.info.style.left = parseInt(Global.locations.info.parentNode.offsetWidth/2 - Global.locations.info.offsetWidth/2)+'px';
    
    Global.locations.showShadow(Global.locations.info);
    
    var el = Global.locations.info.getElementsByClassName('close');
    el[0].onclick = Global.locations.closeInfo.bind(Global.locations.info);
}

Global.locations.closeInfo = function (){
	Global.locations.hide(this);
	Global.locations.info = null;
	
}

Global.locations.back = function (region,office){
	Global.locations.showRegion(region);
	Global.locations.showOffices(office);
}

Global.locations.showHint = function (id, obj){
	if(Global.locations.region !== null || Global.locations.info !== null){
		return false;
	}
	
	if(Global.locations.timerArea){
		clearTimeout(Global.locations.timerArea);
	}
	if(Global.locations.hint && Global.locations.hint.id == 'region_hint_'+id){
		clearTimeout(Global.locations.timer);
		return false;
	}else if(Global.locations.hint){
		Global.locations.hideHintForce();
	}
	
	Global.locations.hint = $('region_hint_'+id);
	Global.locations.hint.style.display = 'block';
	Global.locations.hint.onmouseout = function(){Global.locations.timer = setTimeout(Global.locations.hideHint,300);}
	Global.locations.hint.onmouseover = function(){ clearTimeout(Global.locations.timer); clearTimeout(Global.locations.timerArea);};
	
	//var offset = Global.locations.hint.cumulativeOffset();
	var left = Global.locations.hint.getAttribute('x') - Global.locations.hint.offsetWidth;
	if(left < 0){
		left = parseInt(Global.locations.hint.getAttribute('x'));
		Global.locations.hint.addClassName("left_corner");
	}else{
		Global.locations.hint.removeClassName("left_corner");
	}
	
	var img = $('imagemap');
	
	//var el = Global.locations.hint.getElementsByClassName("hint_image");
	
	var top = parseInt(Global.locations.hint.getAttribute('y'));
	if(top > img.offsetHeight*2/3){
		top = top - Global.locations.hint.offsetHeight;
		Global.locations.hint.addClassName("bottom_image");
	}else{
		Global.locations.hint.removeClassName("bottom_image");
	}
	Global.locations.hint.style.top = top + 'px';
	Global.locations.hint.style.left = left +'px';
	
	
	var el = Global.locations.hint.getElementsByClassName("window_hint");
	
	var shadow = $(document.createElement("div"));
	shadow.addClassName("window-shadow");
	shadow.style.width = (el[0].offsetWidth)+3+'px';
	shadow.style.height = el[0].offsetHeight+3+'px';
	if(Global.locations.hint.hasClassName("bottom_image")){
		shadow.style.top = parseInt(Global.locations.hint.style.top)-1+ 'px';
	}else {
		shadow.style.top = parseInt(Global.locations.hint.style.top)+24+ 'px';
	}
	if(Global.locations.hint.hasClassName("left_corner")){
		shadow.style.left = parseInt(Global.locations.hint.style.left)-1+20 + 'px';
	}else {
		shadow.style.left = parseInt(Global.locations.hint.style.left)-1 + 'px';
	}
	shadow.style.display = 'block';
	
	Global.locations.hint.shadow = shadow;
	Global.locations.hint.parentNode.appendChild(shadow);
	
	obj.onmouseout = function(){ Global.locations.timerArea = setTimeout(Global.locations.hideHintForce,300)};
}

Global.locations.hideHint = function (){
	if(Global.locations.hint !== null){
		var offset = Global.locations.hint.cumulativeOffset();
		if( Global.mouseX < offset[0] || Global.mouseX > offset[0] + Global.locations.hint.offsetWidth || 
			Global.mouseY < offset[1] || Global.mouseY > offset[1] + Global.locations.hint.offsetHeight){
			
			Global.locations.hideHintForce();
		}
	}
}

Global.locations.hideHintForce = function(){
	if(Global.locations.hint !== null){
		Global.locations.hide(Global.locations.hint);
		Global.locations.hint = null;
	}
}

Global.locations.showHintSelection = function (region, office){
	Global.locations.showRegion(region);
	Global.locations.showOffices(office);
}