// =================================================================================================
// debdirectory.js - Functions supporting the DEB Directory pages on Entrust's website.
//
// Author: Mark Stickley, Evolving Media ©2007
// =================================================================================================

Event.observe(window, 'load', initialise, false);

var debcountries;
var debregions;
var debsearch;
var flashmap;
var flashcontainer;

function initialise(){
	debcountries = $$('.debcountry');
	debregions = $$('.debregion');
	debsearch = insertSearchBox();

	
	flashcontainer = $('flashcontainer');
	if(flashcontainer){
		flashcontainer.setStyle({visibility: 'visible'}); //this line to show map only to flash + javascript users
	}
	flashmap = window.document.flashmap;

	collapseAllCountries();
		
	var countrylinks = $$('.debcountry a');
	for(var i=0;i<countrylinks.length;i++){
		countrylinks[i].onclick = function(){
			var country = this.parentNode;
			if(country.expanded){
				collapseCountry(country);
			}
			else{
				expandCountry(country);
			}
			return false;
		}
	}
	
	var regionlinks = $$('.debregion a');
	for(var i=0;i<regionlinks.length;i++){
		regionlinks[i].onmouseover = function(){
			regionname = getRegionNameFromAnchor(this);
			countryname = getCountryNameFromAnchor(this);
			flash_hover_region(countryname, regionname);
			
		}
		regionlinks[i].onmouseout = function(){
			regionname = getRegionNameFromAnchor(this);
			countryname = getCountryNameFromAnchor(this);
			flash_unhover_region(countryname, regionname);
		}
	}
	
	if(debsearch){
		debsearch.previouslyEmpty = true;
	
		debsearch.onkeyup = function(){
			updateSearch(this.value);
		}
	}
	
	flash_initialise();
}

//collapses all countries
function collapseAllCountries(){
	for(var i=0;i<debcountries.length;i++){
		collapseCountry(debcountries[i]);
	}
}

//collapse a specific country
function collapseCountry(country){
	country.expanded = false;
	var link = country.getElementsByClassName('debcountrylink');
	link[0].setStyle({backgroundImage: 'url(/static/images/deb-arrow-right.gif)', backgroundPosition: '5px center'});
	var regions = country.getElementsByClassName('debregion');
	for(var i=0;i<regions.length;i++){
		regions[i].hide();
	}
}

//expand a specific country
function expandCountry(country){
	country.expanded = true;
	var link = country.getElementsByClassName('debcountrylink');
	link[0].setStyle({backgroundImage: 'url(/static/images/deb-arrow-down.gif)', backgroundPosition: '3px center'});
	var regions = country.getElementsByClassName('debregion');
	for(var i=0;i<regions.length;i++){
		var regionlinks = regions[i].getElementsByTagName('a');
		//there will only be one link per region.
		if(regionlinks[0].innerHTML.toLowerCase().indexOf(debsearch.value.toLowerCase())!=-1 || debsearch.value == ''){
			regions[i].show();
		}
	}
}

function insertSearchBox(){
/*
	We have initially:
			<div id="debsearchctr">
			</div> 
	We need to have:
			<div class="debsearch" id="debsearchctr">
				<form method="post" action="/'.$this->me->url.'">
					<label for="debsearch">Search:</label> <input type="text" name="debsearch" id="debsearch" />
				</form>
			</div> 
*/
	var debsearch = $('debsearchctr');
	debsearch.setAttribute('class', 'debsearch');
	
	var form = document.createElement('form');
	form.setAttribute('method', 'post');
	form.setAttribute('action', '#');
	debsearch.appendChild(form);
	
	var label = document.createElement('label');
	label.setAttribute('for', 'debsearch');
	labeltext = document.createTextNode('Search: ');
	label.appendChild(labeltext);
	debsearch.appendChild(label);
	
	var input = document.createElement('input');
	input.setAttribute('type', 'text');
	input.setAttribute('name', 'debsearch');
	input.setAttribute('id', 'debsearch');
	debsearch.appendChild(input);
	
	return input;
}

function updateSearch(searchtext){
	//check if the search box was previously empty
	var showall;
	if(debsearch.previouslyEmpty == true){
		if(searchtext!=''){
			showall = true;
			debsearch.previouslyEmpty = false;
		}
		else{
			showall = false;
		}
	}
	else{
		showall = false;
		if(searchtext==''){
			collapseAllCountries();
			showall = false;
			debsearch.previouslyEmpty = true;
		}
	}

	for(var i=0;i<debregions.length;i++){
		var regionlinks = debregions[i].getElementsByTagName('a');
		//there will only be one link per region.
		if(regionlinks[0].innerHTML.toLowerCase().indexOf(searchtext.toLowerCase())!=-1 && searchtext!=''){
			if(showall || (!showall && debregions[i].parentNode.parentNode.expanded)){
				debregions[i].show();
				debregions[i].visible = true;
				expandCountry(debregions[i].parentNode.parentNode);
			}
		}
		else{
			debregions[i].hide();
			debregions[i].visible = false;
		}
	}
}

function getRegionNameFromAnchor(anchor){
	regionname = anchor.innerHTML;
	regionname = regionname.toLowerCase();
	splitregions = regionname.split(' ');
	regionname = '';
	for(var j=0; j<splitregions.length;j++){
		regionname += splitregions[j];
	}
	
	return regionname;
}

function getRegionFullNameFromAnchor(anchor){
	return anchor.innerHTML;
}

function getRegionIDFromAnchor(anchor){
		href = anchor.getAttribute('href');
		regionid = href.substring(href.lastIndexOf('region=')+7);
		return regionid;
}

function getCountryNameFromAnchor(anchor){
		country = anchor.parentNode.parentNode.parentNode.getElementsByTagName('a');
		country = country[0].innerHTML;
		country = country.toLowerCase();
		splitcountry = country.split(' ');
		country = '';
		for(var j=0; j<splitcountry.length;j++){
			country += splitcountry[j];
		}
		
		return country;
}


/*function insertFlashInto(element){
	element.innerHTML = '<object width="300" height="500" id="flashmap">'+"\n"+'<param name="movie" value="/static/flash/ukmap.swf">'+"\n"+'<embed src="/static/flash/ukmap.swf" width="300" height="500" name="flashmap" swLiveConnect="true">'+"\n"+'</embed>'+"\n"+'</object>';
}*/

//FLASH INTERACTION FUNCTIONS ---------------------------------
function flash_initialise(){
	if(flashmap && flashmap.PercentLoaded()==100){
		var regionlinks = $$('.debregion a');
		for(var i=0;i<regionlinks.length;i++){
			countryname = getCountryNameFromAnchor(regionlinks[i]);
			regionname = getRegionNameFromAnchor(regionlinks[i]);
			regionfullname = getRegionFullNameFromAnchor(regionlinks[i]);
			regionid = getRegionIDFromAnchor(regionlinks[i]);
			flash_initialise_region(countryname, regionname, regionfullname, regionid);
		}
	}
	else{
		setTimeout("flash_initialise()", 200);
	}
}

function flash_initialise_region(country, name, fullname, id){
	flashmap.SetVariable("/uk/"+country+"/"+name+":region", id);
	flashmap.SetVariable("/uk/"+country+"/"+name+":fullname", fullname);
	flashmap.SetVariable("/uk/"+country+"/"+name+":readytosetup", 1); //this line last as it signals to the flash that all variables have been set.	
}

function flash_hover_region(country, name){
	flashmap.SetVariable("/:focus", country);
	flashmap.SetVariable("/uk/"+country+"/"+name+":hover", 1);
}

function flash_unhover_region(country, name){
	flashmap.SetVariable("/:focus", "all");
	flashmap.SetVariable("/uk/"+country+"/"+name+":hover", 0);
}

function flash_regionclick(name, id){
}


// ADD DEB FUNCTIONS ----------------------------------------
function autoselect(country){
	//autoselects regions when the user clicks on a country
	for(var i=0;i<country.length;i++){
		$(country[i]).checked = true;
	}
}