// -----------------------------------------------------------
// Univeral method for javascript->flash setvariable
// -----------------------------------------------------------

/* -----------------------------------------------------------
----------Supporting Browsers----------
PC:
IE 5 and higher
Netscape 6 and higher
Moz/Firebird all
Opera 7 and higher

Mac OSX:
IE 5.2
Safari all
Netscape 6 and higher
Moz/Firebird/Camino all
Opera 6 and higher

Linux:
Konqueror assumed
----------------------------------------------------------- */
var SearchCountry='false';
// -----------------------------------------------------------
// Detection snippet from http://www.dithered.com/javascript/browser_detect/index.html
// -----------------------------------------------------------

var ua        = navigator.userAgent.toLowerCase(); 
var is_pc_ie  = ( (ua.indexOf('msie') != -1 ) && ( ua.indexOf('win') != -1 ) && ( ua.indexOf('opera') == -1 ) && ( ua.indexOf('webtv') == -1 ) );



/* -----------------------------------------------------------
function setFlashVariables(movieid, flashquery)
movieid: id of object tag, name of movieid passed in through FlashVars
flashquery: querystring of values to set. example( var1=foo&var2=bar )
----------------------------------------------------------- */

function setFlashVariables(movieid, flashquery){
	
	var i,values;
		if(is_pc_ie){
			var chunk = flashquery.split("&");
			for(i in chunk){
				values = chunk[i].split("=");
				document[movieid].SetVariable(values[0],values[1]);
			}
		}else{
			var divcontainer = "flash_setvariables_"+movieid;
			if(!document.getElementById(divcontainer)){
				var divholder = document.createElement("div");
				divholder.id = divcontainer;
				document.body.appendChild(divholder);
			}
			document.getElementById(divcontainer).innerHTML = "";
			var divinfo = "<embed src='JavaIncludes/gateway.swf' FlashVars='lc="+movieid+"&fq="+escape(flashquery)+"' width='0' height='0' type='application/x-shockwave-flash'></embed>";
			document.getElementById(divcontainer).innerHTML = divinfo;
		}
}
//----------------------------------------------------------------------------------------------------------
// Load check (is experimental)
//-----------------------------------------------------------------------------------------------------------
function Loaded()
{
	//alert("page is loaded");
	//document.getElementById('Load').style.visibility="hidden";
}
//----------------------------------------------------------------------------------------------------------
// Communcation flash-html 
//-----------------------------------------------------------------------------------------------------------
//MV: get movie object
function getFlashMovieObject(movieName)
{
  if (window.document[movieName]) 
  {
      return window.document[movieName];
  }
  if (navigator.appName.indexOf("Microsoft Internet")==-1)
  {
    if (document.embeds && document.embeds[movieName])
      return document.embeds[movieName]; 
  }
  else // if (navigator.appName.indexOf("Microsoft Internet")!=-1)
  {
    return document.getElementById(movieName);
  }
}

function createJSFCommunicatorObject_1(playerObj)
{
		//create an instance of JSFCommunicator, pass the flashMovie's reference
		//make sure flash object is loaded when you create this object with parameter otherwise
		//you can JSFCommunicator.setMovie(flashMovie) once flash object is loaded
		fc = new JSFCommunicator(playerObj);
}
function createJSFCommunicatorObject_2(playerObj)
{
		//create an instance of JSFCommunicator, pass the flashMovie's reference
		//make sure flash object is loaded when you create this object with parameter otherwise
		//you can JSFCommunicator.setMovie(flashMovie) once flash object is loaded
		fc_2 = new JSFCommunicator(playerObj);
}

//MV: send data from big swf to small swf
function fl_sendDataToSmallSWF(data)
{
 //alert('senddata to small swf'+data);
 //MV: tell small swf which country to show
  setFlashVariables('countryInfo','showCountryInfoID='+data)
  //MV: Set the countyr name in smallflash,php (span id=CountryName)
  x_SetCountryName(data,CallbackCountryName);
  //MV: Set the country Info in smallflash.php (span id=CountryThemeInfo)
  x_SetCountryThemeInfo(data,CurrentTheme,CallbackCountryThemeInfo);
  //MV: Update The Docs Page, the if statement makes sure the docs page is not updated twice after a country search
  if(SearchCountry != 'true')
  {
	x_IncludePageGen('docs.php','Country',data+'|'+CurrentTheme,CallbackDocs);
  }
  SearchCountry='false';
  
  //MV: Set the name of the divcontainer containing the internal links
  SetDivID('CountryThemeInfoContainer');
  
}

function sendDataToHtml(data)
{
	//MV: with this function the page smallflash.php is being loaded in page world.php in <span id='CountryInfo'></span>
	//The declaration of this function can be found inin this file
	//After the smallflash has loaded it triggers the function swfLoadComplete(), which starts fl_sendDataToSmallSWF()
	//in which the country with its info is displayd
	SwitchIntro('smallflash.php',data);
	//setClass('world.php','active' );	
}


//----------------------------------------------------------------------------------------------------------
//Navigation Functions
//-----------------------------------------------------------------------------------------------------------
function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}


function ScrollToAnchor(name,direct)
{
	//MV: to know Where to scroll the ID of the div tag is set as "DivID"
	//This var is set in:
	//--function WhichTheme() -used when switched from theme
	//--function fl_sendDataToSmallSWF(data) -used when country is clicked
	//--file: docs.php when the info button is clicked or the title is clicked DivID is set
	
	//MV: get the x and y  posisiton of the <a name > </a> tag
	//MV: function is found in AnchorPosition.js
	var c= getAnchorPosition(name);
	//var c=getAnchorWindowPosition(name);
	
	//MV: get the y position of the div container to set the OffSet
	var OffSet=(findPosY(document.getElementById(DivID))+5);
	//MV: get the position of the scrollbar for when the user has scrolled to an internal link
	var ScrollPos=document.getElementById(DivID).scrollTop;
	//MV: calculate where to scroll to c.y=position of <a id="" name=""> </a> tag ScrollPos=possible offset of scrollbar OffSet= offsety of the div container
	c.y=(c.y-ScrollPos)-OffSet;
	
	//MV: if the scroll direction is down scroll down
	if(direct =='+')
	{
		document.getElementById(DivID).scrollTop +=c.y;
	}
	//MV: if the scroll direction is up scroll up
	else if(direct == '-')
	{
		document.getElementById(DivID).scrollTop =c.y;
	}
	
}

function SetActivePage(setvalue)
{
	ActivePage=setvalue;
	//return ActivePage;
}

var ids= Array('taba','tab1','tabb','tab2','tabc','tab3','tabd','tab4','tabe')
function setClass(Tab) 
{
	
	//MV: sets the visibillity of the docs navigation menu
	if(Tab=='tab1') 
	{ document.getElementById('docsmenu').style.visibility="visible"; }
	else
	{ document.getElementById('docsmenu').style.visibility="hidden"; }
	
	//MV: sets which parts are active and whic parts are not	
	if(Tab=='tab1')
	{
		//var className= Array('active-taba','active-mid','active-tabb','non-active-mid','tab_3','non-active-mid','non-active-tabd');
		var className= Array('active-corner_tab_left','active-mid','tab_1','non-active-mid','tab_3','non-active-mid','tab_3','non-active-mid','non-active-corner_tab_right');
		for (var i=0; i<ids.length; i++)
		{
			document.getElementById(ids[i]).className = className[i];
		}
	}	
	else if(Tab=='tab2')
	{
		var className= Array('non-active-corner_tab_left','non-active-mid','tab_2','active-mid','tab_1','non-active-mid','tab_3','non-active-mid','non-active-corner_tab_right');
		for (var i=0; i<ids.length; i++)
		{
			document.getElementById(ids[i]).className = className[i];
		}
	}	
	else if(Tab=='tab3')
	{
		var className= Array('non-active-corner_tab_left','non-active-mid','tab_3','non-active-mid','tab_2','active-mid','tab_1','non-active-mid','non-active-corner_tab_right');
		for (var i=0; i<ids.length; i++)
		{
			document.getElementById(ids[i]).className = className[i];
		}
	}
	else if(Tab=='tab4')
	{
		var className= Array('non-active-corner_tab_left','non-active-mid','tab_3','non-active-mid','tab_2','non-active-mid','tab_2','active-mid','active-corner_tab_right');
		for (var i=0; i<ids.length; i++)
		{
			document.getElementById(ids[i]).className = className[i];
		}
	}		
}

//----------------------------------------------------------------------------------------------------------
// Search Functions 
//-----------------------------------------------------------------------------------------------------------

//MV: this function gets the value fromn the search form and sends it to the docs page
//also when searched for country this function calls "GetCountryID" which resides in remote.php and gets the right country id of the country name
function GetValue(ActivePage)
{
	SearchString=document.getElementById("SearchString").value;
	SearchParameter=document.Search.SearchParameter.value;
	x_IncludePageGen('docs.php','Search',SearchParameter+"|"+SearchString,CallbackDocs);
	if(SearchParameter == 'Country')
	{
		if(ActivePage == 'world')
		{
			x_GetCountryID(SearchString,IDCallback);
		}
	}
	else
	{
		if(ActivePage == 'world')
		{
			//MV: unsets the countries
			setFlashVariables('worldmap','CountryID=0')
		}
	}
}


//MV: this function "catches" the pressing of the enter key and "submits" the form
function CatchEnter(e)
{
	var code;
	if (!e) var e = window.event;
	if (e.keyCode) code = e.keyCode;
	else if (e.which) code = e.which;
	
	if(code=='13')
	{
		GetValue(ActivePage);
	}
	
	return !(code == 13); 
}

//---------------------------------------------------------------------------------------------------------------------------
// Functions from index
//-------------------------------------------------------------------------------------------------------------------------

//MV: this function is used when a theme on the dossiers page is selected or when the wold in the navigation page is selected.
function SelectTheme(Page,ThemeID)
{
	CurrentTheme=ThemeID;	
	x_IncludePageGen(Page,ThemeID,Callback);setClass(Page,'active' );
}
//MV: with this function the way the doc list is build up is changed
function SelectDocs(Page,filter)
{
	x_IncludePageGen(Page,filter,CallbackDocs);setClass(Page,'active' );
}

//MV: switch content next to the map between intro page or world page
//This function is alway called when a country is selected to ensure that the smallflash.php ids being loaded.
function SwitchIntro(Page,CountryID)
{
	CurrentCountry=CountryID;
	x_IncludePageGen(Page,CurrentTheme,CallbackCountry);
	//alert("page"+Page+"Currentheme"+CurrentTheme);
}

//this functions sets the action in this case include the php file

function Callback(value)
{
	document.getElementById("MainContent").innerHTML=value;
}

//Clalback funtion to change the docs filter
function CallbackDocs(value)
{
	document.getElementById("Docs").innerHTML=value;
}


//Special Callback function for the world page it sets the flash objects
function CallbackWorld(value)
{
	Callback(value);
	home = true;
	//
	createJSFCommunicatorObject_1(getFlashMovieObject("worldmap"));
	//
	SwitchIntro('intro.php','0');
}

//Caalback function to only refresh the country info

function CallbackCountry(value)
{
	document.getElementById("CountryInfo").innerHTML=value;
	//
	createJSFCommunicatorObject_2(getFlashMovieObject("countryInfo"));
	//
}



//Calback function to set the country name in de smallflash.php
function CallbackCountryName(value)
{
	document.getElementById("CountryName").innerHTML=value;
}

function CallbackCountryThemeInfo(value)
{
	document.getElementById("CountryThemeInfo").innerHTML=value;
}

function swfLoadComplete()
{
	if(CurrentCountry!=0)
	{ fl_sendDataToSmallSWF(CurrentCountry); }
}

//MV: is called by the dropdown menu and sets the menu
function WhichTheme()
{
	//MV: get the value from the dropdownbox
	box = document.ThemeChoice.SearchParameter;
	ThemeID=box.options[box.selectedIndex].value;
	
	//MV: switch to no theme or '--choose a theme--'
	if(ThemeID=='default')
	{
		CurrentTheme=ThemeID;
		x_IncludePageGen('intro.php','default',CallbackCountry);
		x_IncludePageGen('docs.php','newreleases',CallbackDocs);
		//MV: the settimeuot is a hack to let the page load before the flash
		setTimeout("setFlashVariables('worldmap','themeID='+ThemeID)","500")
		//setFlashVariables('worldmap','themeID='+ThemeID)
	}
	//MV: set chosen theme
	else
	{
		home = false;
		// CurrentCountry=0;
		CurrentTheme=ThemeID;
		x_IncludePageGen('smallflash.php',ThemeID,CallbackCountry);
		x_IncludePageGen('docs.php',ThemeID,CallbackDocs);
		//MV: the settimeuot is a hack to let the page load before the flash
		setTimeout("setFlashVariables('worldmap','themeID='+ThemeID)","500");
		//setFlashVariables('worldmap','themeID='+ThemeID)
		
		//MV: Set the countyr name in smallflash,php (span id=CountryName)
		x_SetCountryName('nocountryselected',CallbackCountryName);
	}
	//MV: Set the name of the divcontainer containing the internal links
	SetDivID('CountryThemeInfoContainer');
	setTimeout("setFlashVariables('worldmap','activeCountryID='+CurrentCountry)","1500");
}

//MV: use this function to set the CurrentTheme
//Now used to "reset" the theme when returning to the world page after visiting a file or dossier
function SetCurrentTheme(ThemeID)
{
	CurrentTheme=ThemeID;
}

//MV: with this function the var DivID is set this is used so the internal text links know in which div container they should scroll
//this var is used in ScrollToAnchor()
function SetDivID(setID)
{
	DivID=setID;
}

//MV:This function is called when the world map has finished loading
//is redundant for the moment

function worldMapInitiated()
{
	var WorldMaploaded='true';
}


//MV: callback function for function GetCountryID in remote
function IDCallback(value)
{
	SearchCountry='true';
	SearchCountryID=value;
	//TEST
	//alert("searchcountryid"+value);
	SwitchIntro('smallflash.php',value);
	//TEST
	if(value != '0')
	{
		//MV: tell the world map flash which country is being selected/searched
		setFlashVariables('worldmap','activeCountryID='+value);
		//MV: tell the world map flash which country is being selected/searched, but now for the "country animation"
		setFlashVariables('worldmap','selectedCountryID='+value);
	}
}


