Event.observe(window, "load", function() {
	var $continent = $$(".worldmap #wm-continent");
	var $countries = $$(".worldmap #wm-countries");
	var $countries_a = $$(".worldmap #wm-continent li a");
	var $country = $$(".worldmap .wm-country");
	var $country_a = $$(".worldmap .wm-country li a");
	var $contacts = $$(".worldmap .wm-contacts");
	var $continent_values = $w("europe northamerica asiapacific middleeast latinamerica australia");
	
	function hideAll($elems) {
		$elems.each(function($elem) {
			$elem.hide();
		})
	}
	function showAll($elems) {
		$elems.each(function($elem) {
			$elem.show();
		})
	}	
	function removeAllBoldFont($elems) {
		$elems.each(function($elem) {
			if ($elem.className) {
				$elem.removeClassName("bold");
			}	
		})
	}
	
	hideAll($countries);
	hideAll($country);
	hideAll($contacts);
	$("wm-line").hide();
	$("contentWrapper").addClassName("js");
	
	$countries_a.each(function($elem) {
		$elem.observe("mouseover", function() {
			this.addClassName("over");
			if ($('wm-continent').className == "") {
				showAll($countries);
				if (this.className) {
					$elem = this.className.split(" ")[0];
					$($elem).show();
				}
			}
		}).observe("mouseout", function() {
			if ($('wm-continent').className != "") {
				if ($('wm-continent').className != this.className.split(" ")[0]) {
					this.removeClassName("over");
				}
			}
			else {
				this.removeClassName("over");
				hideAll($countries);
				hideAll($country);
			}
		}).observe("click", function() {
			var $splitClassName = this.className.split(" ")[0];
			$('wm-continent').className = "";
			$('wm-continent').addClassName($splitClassName);
			show_line($splitClassName);
			var $element = "lnk-"+$('wm-continent').className;
			hideAll($contacts);
			$($element).removeClassName("over");
			hideAll($country);
			if (this.className) {
				$($splitClassName).show();
			}			
		});
	});

	$country_a.each(function($elem) {
		$elem.observe("click", function() {
			removeAllBoldFont($country_a);
			hideAll($contacts);
			var attr = $elem.readAttribute('href');
			this.addClassName("bold");
			$(attr.split("#")[1]).show();
		});
	});
	
	function show_line($akt_elem) {
		$continent_values.each(function ($elem) {
			if ($akt_elem == $elem) {
				$("wm-line").setStyle({
					top: 302-(302-$("li-"+$akt_elem).positionedOffset(this)[1]-($("lnk-"+$akt_elem).getHeight()/2))-3+'px',
					width: 650-$("li-"+$akt_elem).positionedOffset(this)[0]-$("lnk-"+$akt_elem).getWidth()-$("wm-countries").getWidth()+3+'px',
					left: $("li-"+$akt_elem).positionedOffset(this)[0]+$("lnk-"+$akt_elem).getWidth()+11+'px'
				});
			}
		})
		$("wm-line").show();
	}
});
