function map_init () { // initialize application
	if ((document.getElementById ( "selectContainer" )) && (!window.sImgPath)) {
		window.sImgPath = "/images/worldmap/"
		window.aSpots = new Array ();
		window.aContinents = new Array ( "SouthAmerica", "Africa", "Asia", "Australia", "NorthAmerica", "Europe" );
		window.oStage = document.getElementById ( "stage" );
		for ( var i = 0; i < document.getElementById ( "continents" ).childNodes.length; i++ ) {
			var oNode = document.getElementById ( "continents" ).childNodes.item ( i );
			if ( oNode.nodeType == 1 ) {
				oNode.onmouseover = map_behaviour;
				oNode.onclick = map_behaviour;
			}
		}
	
		oStage.onmouseout = map_hideContinents;
		if (document.getElementById ( "mapSelectbox" ) && document.getElementById ( "selectContainer" )){
			document.getElementById  ( "mapSelectbox" ).innerHTML = document.getElementById ( "selectContainer" ).innerHTML;
			document.getElementById ( "selectContainer" ).innerHTML = "";					
		}
	}
}

function rePosMap (){
	if(document.getElementById ( "selectContainer" )){
		document.getElementById ( "imagemap" ).style.position = "absolute"
		document.getElementById ( "imagemap" ).style.position = "relative"
	}
}

function map_behaviour ( e ) { // attach eventListeners to map

		if ( !e ) var e = window.event;
		var oArea = e.target ? e.target : e.srcElement;
		
		var sIdentifier = oArea.id.split ( "area" ) [ 1 ]
		if ( e.type == "mouseover" ) map_mouseover ( sIdentifier );
		if ( e.type == "click" ) map_click ( sIdentifier );
		if ( oArea.blur ) oArea.blur ();
}


function map_hideContinents () { // utility function to hide continent bitmaps
	for ( var i=0; i < window.aContinents.length; i++ )	{
		if (document.getElementById ( window.aContinents [ i ] )) {
			document.getElementById ( window.aContinents [ i ] ).style.display = "none";
		}
	}
}

function map_mouseover ( sContinent ) { // world map, mouseover on continent
	map_hideContinents ();
	document.getElementById ( sContinent ).style.display = "block";
}


function map_click ( sContinent, oCountry ) { // world map, mousedown on continent
		if (document.getElementById("mapSelectbox")) {
			if (document.getElementById("mapSelectbox").innerHTML.length == 0) {
				return;
			}
		}
		map_hideContinents ();
		
		if ( window.sContinent && sContinent != window.sContinent ) {

			if ( window.aSpots ) { // intercontinental jump - clear residious spots
				for ( var i = 0; i < window.aSpots.length; i++ ) 
					window.oStage.removeChild ( document.getElementById ( "mapspot" + i ));
				window.aSpots = new Array ( 0 );
			}
		}
		
		window.sContinent = sContinent;
		// four steps to a better perfomance!
		
		var step1 = function () { // update backdrop graphic
			if ( window.oStage.style.backgroundImage != 'url("' + sImgPath + 'zoom_' + sContinent + '.gif")' );
				window.oStage.style.backgroundImage = 'url("' + sImgPath + 'zoom_' + sContinent + '.gif")';
		}
		var step2 = function () { // disable imagemap and present world map link
			document.getElementById ( "imagemap" ).style.display = "none";
			document.getElementById ( "return" ).style.display = "block";
		}
		var step3 = function () { // generate hotspots
			map_hotSpots ( sContinent );
		}
		if ( oCountry ) { // auto-select country
			var step4 = function () {
				map_presentMarkup ( oCountry );
			}
		}

		if ( document.all && navigator.platform != "Win32" ) { 
			if ( step1 ) step1 ();
			step2 ();
			step3 ();
			if ( step4 ) step4 ();
		}
		else {
			if ( step1 ) setTimeout ( step1, 1 );
			setTimeout ( step2, 2 );
			setTimeout ( step3, 3 );
			if ( step4 ) setTimeout ( step4, 4 );
		}
}


function map_hotSpots ( sContinent ) { // create and distribute hotspots
		document.getElementById ( "FrontIntroText" ).style.display = "none";

		var oContinent = window [ window.sContinent ];

		for ( var a = 0; a < oContinent.aCountries.length; a++ ) {
			for ( var b = 0; b < oContinent.aCountries [ a ].aSpots.length; b++ ) {
			
				// create spot
				var oSpot = document.createElement ( "div" );
				oSpot.className 	= "mapspot";
				oSpot.id			= "mapspot" + window.aSpots.length;
				oSpot.title 		= " " + oContinent.aCountries [ a ].aSpots [ b ].sTooltip + " ";
				
				// attach behaviour
				oSpot.onclick		= map_updateSpot;//map_prepareMarkup;
				oSpot.onmouseover 	= map_updateSpot;
				oSpot.onmouseout 	= map_updateSpot;
				
				// position spot
				if (oContinent.aCountries [ a ].aSpots [ b ].iX != '') {
					oSpot.style.left 	= oContinent.aCountries [ a ].aSpots [ b ].iX - 3 + "px";
					oSpot.style.top 	= oContinent.aCountries [ a ].aSpots [ b ].iY - 3 + "px";
				}
				else {
					oSpot.style.left 	= "0px";
					oSpot.style.top 	= "0px";
				}
				
				// insert spot
				window.oStage.appendChild ( oSpot );
				
				// add to array
				window.aSpots [ window.aSpots.length ] = oContinent.aCountries [ a ];
			}
		}
}


var oLastCountrySelected;

function map_presentMarkup ( oCountry ) { // present markup associated with country
		document.getElementById ( "FrontIntroText" ).style.display = "none";

		// update display
		for ( var i = 0; i < window.aSpots.length; i++ ) {
			// reset old selected
			if (window.aSpots [ i ] == oLastCountrySelected) {
				document.getElementById ( "mapspot" + i ).className = "mapspot";
			}
			else if (window.aSpots [ i ] == oCountry) {
				document.getElementById ( "mapspot" + i ).className = "spotSelected";
			}
		}
		oLastCountrySelected = oCountry;
			
		// present markup
		document.getElementById ( "mapInfo" ).style.display = "block";
		document.getElementById ( "mapInfo" ).innerHTML = oCountry.sMarkup;
		map_fixBug ();
}


function map_fixBug () { // macintosh display bug

		if ( document.all && navigator.platform != "Win32" ) { 
			window.oStage.style.position = "static";
			window.oStage.style.position = "relative";
		}
}



function mapfocus ( val ) { // selectbox, zoom to continent and target country
		document.getElementById ( "FrontIntroText" ).style.display = "none";
		var sCountry = val;

		if ( sCountry == "allcountries" ) {
			var aAll = new Array ();
		}
		for ( var a = 0; a < window.aContinents.length; a++ ) { // build markup / find country
			if (window [ aContinents [ a ] ]) {
				if (window [ aContinents [ a ] ].aCountries) {
					for ( var b = 0; b < window [ aContinents [ a ] ].aCountries.length; b++ ) {
						//if ( bAll ) sAll += window [ aContinents [ a ] ].aCountries [ b ].sMarkup;
						if ( aAll ) {
							aAll [ aAll.length ] = new Array ( window [ aContinents [ a ] ].aCountries [ b ].sName, window [ aContinents [ a ] ].aCountries [ b ] );
						}
						else if ( window [ aContinents [ a ] ].aCountries [ b ].sName == sCountry ) {
							window.oSelectedCountry = window [ aContinents [ a ] ].aCountries [ b ]
							map_click ( aContinents [ a ], window.oSelectedCountry );
							map_hideContinents ();
							break;
						}
					}
				}
			}
		}
		
		if ( aAll ) { // sort countries alphabetically - collect, combine and present markup
			map_return ();
			document.getElementById ( "FrontIntroText" ).style.display = "none";
			aAll.sort ();
			var sAll = "";
			for ( var i = 0; i < aAll.length; i++ ) sAll += aAll [ i ][ 1 ].sMarkup;
			document.getElementById ( "mapInfo" ).style.display = "block";
			document.getElementById ( "mapInfo" ).innerHTML = sAll;
			map_fixBug ();
			fixNetscape7Too();
		}
}



function map_focus ( oSelect ) { // selectbox, zoom to continent and target country
		document.getElementById ( "FrontIntroText" ).style.display = "none";
		var sCountry = oSelect.options [ oSelect.selectedIndex ].value;

		if ( oSelect.options [ oSelect.selectedIndex ].value.toLowerCase() == "allcountries" ) {
			var aAll = new Array ();
		}
		for ( var a = 0; a < window.aContinents.length; a++ ) { // build markup / find country
			if (window [ aContinents [ a ] ]) {
				if (window [ aContinents [ a ] ].aCountries) {
					for ( var b = 0; b < window [ aContinents [ a ] ].aCountries.length; b++ ) {
						//if ( bAll ) sAll += window [ aContinents [ a ] ].aCountries [ b ].sMarkup;
						if ( aAll ) {
							aAll [ aAll.length ] = new Array ( window [ aContinents [ a ] ].aCountries [ b ].sName, window [ aContinents [ a ] ].aCountries [ b ] );
						}
						else if ( window [ aContinents [ a ] ].aCountries [ b ].sName == sCountry ) {
							window.oSelectedCountry = window [ aContinents [ a ] ].aCountries [ b ]
							map_click ( aContinents [ a ], window.oSelectedCountry );
							map_hideContinents ();
							break;
						}
					}
				}
			}
		}
		
		if ( aAll ) { // sort countries alphabetically - collect, combine and present markup
			map_return ();
			document.getElementById ( "FrontIntroText" ).style.display = "none";
			aAll.sort ();
			var sAll = "";
			for ( var i = 0; i < aAll.length; i++ ) sAll += aAll [ i ][ 1 ].sMarkup;
			document.getElementById ( "mapInfo" ).style.display = "block";
			document.getElementById ( "mapInfo" ).innerHTML = sAll;
			map_fixBug ();
			fixNetscape7Too();
		}
}


function map_return () { // continent view, return to world map

		document.getElementById ( "stage" ).style.backgroundImage = 'url("' + sImgPath + 'continents.gif")';
		document.getElementById ( "FrontIntroText" ).style.display = "block";
		document.getElementById ( "imagemap" ).style.display = "block";
		document.getElementById ( "return" ).style.display = "none";
		
		for ( var i = 0; i < window.aSpots.length; i++ ) {
			window.oStage.removeChild ( document.getElementById ( "mapspot" + i ));
		}
		
		if ( document.getElementById ( "mapSelector" )) document.getElementById ( "mapSelector" ).options [ 0 ].selected = true;
		document.getElementById ( "mapInfo" ).style.display = "none";
		map_fixBug ();
		
		window.aSpots = new Array ( 0 );
		window.oSelectedCountry = null;
}


var oLastCountryMouseOver;

function map_updateSpot ( e ) { // change spot graphics onmouseover

		if ( !e ) var e = window.event;
		var oNode = e.target ? e.target : e.srcElement;
		var oCountry = window.aSpots [ Number ( oNode.id.split ( "mapspot" ) [ 1 ] ) ];
		
		// update display
		if ( e.type == "mouseover" ) { // h
			for ( var i = 0; i < window.aSpots.length; i++ ) {
				if (window.aSpots [ i ] != window.oSelectedCountry) {
					if (window.aSpots [ i ] == oLastCountryMouseOver) {
						document.getElementById ( "mapspot" + i ).className = "mapspot";
					}
					else if (window.aSpots [ i ] == oCountry) {
						document.getElementById ( "mapspot" + i ).className = "spotSelected";
						oLastCountryMouseOver = oCountry;
					}
				}
			}
		}
		
		if ( e.type == "mouseout" ) {
			for ( var i = 0; i < window.aSpots.length; i++ ) {
				if (window.aSpots [ i ] != window.oSelectedCountry) {
					if (window.aSpots [ i ] == oLastCountryMouseOver) {
						document.getElementById ( "mapspot" + i ).className = "mapspot";
						oLastCountryMouseOver = '';
					}
				}
			}
		}

		if ( e.type == "click" ) {
			map_presentMarkup ( oCountry );
			window.oSelectedCountry = oCountry;
			if ( document.getElementById ( "mapSelector" )) {
				for ( var i = 0; i < document.getElementById ( "mapSelector" ).options.length; i++ ) {
					if ( document.getElementById ( "mapSelector" ).options [ i ].value == oCountry.sName ) 
						document.getElementById ( "mapSelector" ).options [ i ].selected = true;
				}
			}
		}
}


function oContinent () { // add new continent
		this.aCountries = new Array ();
		this.addCountry = function ( sObjectName, sDisplayName ) {
			this [ sObjectName ] = new oCountry ( sDisplayName );
			this.aCountries [ this.aCountries.length ] = this [ sObjectName ];
		}
}


function oCountry ( sDisplayName ) { // add new country

		this.sName = sDisplayName;
		this.sMarkup = "";
		
		this.aSpots = new Array ();
		this.addSpot = function ( sSpot, x, y, sText ) {
			this [ sSpot ] = new oSpot ( x, y, sText );
			this.aSpots [ this.aSpots.length ] = this [ sSpot ];
		}
}


function oSpot ( x, y, sText ) { // add new spot
		this.iX = x;
		this.iY = y;
		this.sTooltip = sText
}

