/*********************************** BEGIN region selection scripts ************************************************/
//global variable indicating that the region menu is set by the map
var menu_is_set_by_map = false;	

//global variable indicating which region was selected last
var menu_current_region;

//global variable indicating the time the last hover event was fired by the map for setting the menu
var starttime_when_last_hover_event_was_fired = false;

//is called, if the region is selected via the map.
//now the menu is set according to the map selection
function SlideMenu()
{
	var currentDate = new Date();
	var currentTime = currentDate.getTime();
	if(currentTime - starttime_when_last_hover_event_was_fired >= 1000)
	{
		var headline = "pop_content_header_" + menu_current_region;
		//the mode must be set and the menu item must not be activated yet
		if(menu_is_set_by_map && document.getElementById(headline).className.indexOf("active") < 0)
		{
			//expand the div immediately following the region specific H3 tag
		  	$("H3#" + headline).next("div").slideDown("slow");
			//collapse all other divs, that are visible
	  		$("H3#" + headline).next("div").siblings("div:visible").slideUp("slow");
			//set the active class for the selected region h3 tag to
	  		$("H3#" + headline).addClass("active");
			//remove the active class for all neighbor h3 tags
			$("H3#" + headline).siblings("h3").removeClass("active");	
		}
	}
}

//Change the image file to show a map with a region selected
function SetNewActiveRegionInMap()
{
			//now get the image object
			var map_image = document.getElementById("img_worldwide");
			//replace the filename in the full path without the file extention
			//the goal is to keep the full path. The image name starts with "map_" and ends with a dot for the file extention
			var old_map_name = map_image.src.substr(map_image.src.indexOf("map_"),100);
			// the dot has to occur after the "map_" filename prefix. Otherwise it could be a part of a relative path etc.
			old_map_name = old_map_name.substring(0, old_map_name.indexOf("."));
			//replace the old region name with the current one
			map_image.src = map_image.src.replace(old_map_name, GetRegionMapFilename(menu_current_region));
}

//the region is resolved by getting the active menu healdine and removing the prefix
function ResolveActiveRegionInMenu()
{
		if($("div.worldwide_nav h3.active").get(0) == undefined)
		{	//if no menu item is active no region is selected
			menu_current_region = "worldwide";
		}
		else
		{
			menu_current_region = $("div.worldwide_nav h3.active").get(0).id.replace("pop_content_header_", "");
		}
}

//this function allows mapping the region to a given filename
function GetRegionMapFilename(region)
{
	switch(region)
	{
		case "europe" : 
			return "map_europe";
			break;
		default:
			return "map_" + region;
			break;			
	}
}
//jquery document ready event handler similar to window.onload
$(document).ready
(
	function()
	{

		/* specify which panel to open as default 
		menu_current_region = "europe";*/
		
		menu_current_region = "latinamerica";		
		SetNewActiveRegionInMap();
		//either show menu item open from the start
		//$(".worldwide_nav h3").eq(1).addClass("active");
		//$(".worldwide_nav div").eq(1).show();
		//or sliding open after start
		menu_is_set_by_map = true;
		SlideMenu();
	
		//register click eventhandler for the regions in the menu
		$(".worldwide_nav h3").click
		(
			function()
			{
			  //expand or collapse the div with the countries following the selected H3 tag
			  $(this).next("div").slideToggle("slow");
			  //collapse all other divs that are visible
			  $(this).next("div").siblings("div:visible").slideUp("slow");
			  //toggle the style of the selected Region H3 tag
			  $(this).toggleClass("active");
			  //remove the active style from all other h3 tags
			  $(this).siblings("h3").removeClass("active");
			  //since the region is set by the menu, the map has to follow the selection
			  menu_is_set_by_map = false;
			  //resolve the region via the active menu h3 tag
			  ResolveActiveRegionInMenu();
			  //Set the map accordingly
		  	  SetNewActiveRegionInMap();
			}
		);
		
		//register hover events for element of type map with id "img_ww_map area"
		//Here according to the image map area the image file is replaced an an hover effect in the country selection menu is initiated
		//This code only works if the regional names in the 
		// -- ID attributes of the HTML objects of the image map
		// -- filenames of the images with regions marked
		// -- ID attributes of the menu H3 headlines
		// are named exactly the same way.
		$("map#img_ww_map area").hover
		(
			//image map hover on (onmouseover) event handler
			function()
			{
				//The AREA element, that was hovered is resolved by "this"
				//Its ID contains the region name after the prefix
	
				//a global status variable is set
				menu_current_region = this.id.replace("maparea_","");
	
				//Set the image file of the map
				SetNewActiveRegionInMap();

				var currentDate = new Date();
				starttime_when_last_hover_event_was_fired = currentDate.getTime();
					
				//set global variable of current region			
				menu_is_set_by_map = true;
	
				//The delay of 1000 milliseconds slows down the reation in the menu, but not in the map
				//SlideMenu uses the global variable "menu_current_region" to always show the newest hovered region
				window.setTimeout("SlideMenu();",1000);
			},
			
			//imagemap hover off (onmouseout) function
			function()
			{
				menu_is_set_by_map = false;
				//now the menu is leading. Resolving the region from the menu.
				//Reason: the menu is delayed. after hovering over different regions without stoping the menu may stay unchanged			
				ResolveActiveRegionInMenu();
				//set the country in the map according to the selection in the menu
				SetNewActiveRegionInMap(menu_current_region);
			}		
		);
		
		//register imagemap click event handler
		//the region is selected immediately in the menu
		$("map#img_ww_map area").click
		(
			//anonymous mouse click event on the image map
			function()
			{
				menu_current_region = this.id.replace("maparea_","");
				SetNewActiveRegionInMap();
	
				//set global variable of current region			
				menu_is_set_by_map = true;
	
				//SlideMenu uses the global variable "menu_current_region" to always show the newest hovered region
				SlideMenu();
			}
		);

		
	}
); // close $(document).ready(function()
/*********************************** END region selection scripts **************************************************/



/*********************************** Begin region layer scripts ****************************************************/

//Due to a bug in IE selects and flash objects are always visible, if not deactivated. 
//For alpha filtering the enabled  selects are disabled, alpha shown and afterwards re-enabled

//global variable containing hidden selects and hidden flash objects to be restored after the alpha filtering is ended
var listOfHiddenSelects;
var listOfHiddenFlashs;

function HideHTMLElements(hide)
{
  var selects = document.getElementsByTagName('select');
  var flashs = document.getElementsByTagName('object');
  var content = document.getElementById('content');
	if(hide == true)
	{
	  listOfHiddenSelects = new Array();
	  for(i = 0; i < selects.length; i++) 
	  {
			if(selects[i].style.visibility != "hidden")
			{
				listOfHiddenSelects.push(selects[i]);
				selects[i].style.visibility = "hidden";
			}
	  }
	  
	  listOfHiddenFlashs = new Array();
	  for(i = 0; i < flashs.length; i++) 
	  {
			if(flashs[i].style.visibility != "hidden")
			{
				listOfHiddenFlashs.push(flashs[i]);
				flashs[i].style.visibility = "hidden";
			}
	  }
	  
	  if (content != null)
	  {
		  if(content.style.visibility != "hidden")
		  {
			  content.style.visibility = "hidden";
		  }
	  }
	}
	else
	{
	  for(i = 0; i < listOfHiddenSelects.length; i++) 
	  {
			listOfHiddenSelects[i].style.visibility = "";
	  }
	  for(i = 0; i < listOfHiddenFlashs.length; i++) 
	  {
			listOfHiddenFlashs[i].style.visibility = "";
	  }
	  if (content != null)
	  {
		  content.style.visibility = "";
	  }
	}
}

//Open the Region Layer
//if setCloseButtonVisible is set, the "X" in the upper right corner is visible
function RegionLayerShow(setCloseButtonVisible)
{
	if(setCloseButtonVisible == true)
	{
		$("#pop_close").css({"display": "inline"});
	}
	
	//fixes the IE bug that select tags remain on top even if hidden by other divs
	HideHTMLElements(true);

	//set popup layer size to document size
	var windowHeight = document.documentElement.scrollHeight;
	if(windowHeight < document.documentElement.clientHeight)
	{
		windowHeight = document.documentElement.clientHeight;
	}
	$("#pop_bg_overlay").css({"height": windowHeight});
	$("#pop_bg_overlay").css({"display": "inline"});
	$("div#pop_bg_overlay").fadeTo("fast",0.8,ShowContainer2);
}

//Animation for the opening
function ShowContainer2() { $("div#pop_container").slideDown("slow", ShowContainer3); }
function ShowContainer3() { $("#pop_ww_content_bg").css({"display": "inline"}); ShowContainer4(); }
function ShowContainer4() {	$("#pop_content").css({"display": "inline"}); }

//Close the Region Layer
function RegionLayerClose()
{
	HideHTMLElements(false);
	HideContainer4();
}

//Animation for the closing
function HideContainer4() { $("#pop_content").css({"display": "none"});	HideContainer3(); }
function HideContainer3() {	$("#pop_ww_content_bg").css({"display": "none"}); HideContainer2(); }
function HideContainer2() { $("div#pop_container").slideUp("slow", HideContainer1);}
function HideContainer1() { $("div#pop_bg_overlay").fadeOut("fast"); }

/*********************************** END region layer scripts ****************************************************/

/**************** START Hover Effekt for mini map - left navigation - international channel **********************/
$(document).ready(function(){
	$("#img_worldwide_small").hover(
		function(){ smallMapHoverOn() },
		function(){	smallMapHoverOff() }
	)
	$("#map_small_lnk").hover(
		function(){ smallMapHoverOn() },
		function(){	smallMapHoverOff() }
	)
}); // close $(document).ready(function()

function smallMapHoverOn (){
 	var image_ww_small = document.getElementById("img_worldwide_small");
  	image_ww_small.src = image_ww_small.src.replace("map_worldwide_small","map_worldwide_small_hover");
}
function smallMapHoverOff (){
 	var image_ww_small = document.getElementById("img_worldwide_small");
	image_ww_small.src = image_ww_small.src.replace("map_worldwide_small_hover","map_worldwide_small");
}
/**************** END Hover Effekt for mini map - left navigation - international channel **********************/