function getFlash(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]; 
		}
	}
	return document.getElementById(movieName);
}

function setContinentInFlash(sContinent) {
	var flash = getFlash(worldMapFlash);
	if (flash) {
		flash.setContinent(sContinent);
	}
}
	
function onContinentChanged(s) {
	var code = s.options[s.selectedIndex].value;
	loadContinentCountries(code);
	setContinentInFlash(code);
}

function setContinent(code) {
	loadContinentCountries(code);
	var s = document.getElementById(continentId);
	for (var i=1;i<s.length;i++) {
		if (s.options[i].value == code) {
			s.options[i].selected = true;
			break;
		}
	}
}

function setCountry(code) {
	var s = document.getElementById(countryId);
	for (var i=0; i < s.options.length; i++) {
		if ( s.options[i].value == code ) {
			s.selectedIndex = i;
			break;
		}
	}
}
	
function loadContinentCountries(code) {
	code = code || 'all';
	var s = document.getElementById(countryId);
	for (var i=0; i < continents.length; i++) {
		if ( continents[i].code == code ) {
			s.options.length = 0;
			for (var j=0;j<continents[i].countries.length;j++) {
				s.options[j] = new Option(continents[i].countries[j].name,continents[i].countries[j].code);
			}
			break;
		}
	}
}

function toggleDetailsRow(img, id) {
	var details = document.getElementById(id);
	toggleClassName(details, "Details");
	var tmp = img.src;
	img.src = img.longDesc;
	img.longDesc = tmp;
}

/*** LIB ***/
function addEvent( obj, type, fn ) {
	if (obj.addEventListener) {
		obj.addEventListener( type, fn, false );
		EventCache.add(obj, type, fn);
	}
	else if (obj.attachEvent) {
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
		obj.attachEvent( "on"+type, obj[type+fn] );
		EventCache.add(obj, type, fn);
	}
	else {
		obj["on"+type] = obj["e"+type+fn];
	}
}
	
var EventCache = function(){
	var listEvents = [];
	return {
		listEvents : listEvents,
		add : function(node, sEventName, fHandler){
			listEvents.push(arguments);
		},
		flush : function(){
			var i, item;
			for(i = listEvents.length - 1; i >= 0; i = i - 1){
				item = listEvents[i];
				if(item[0].removeEventListener){
					item[0].removeEventListener(item[1], item[2], item[3]);
				};
				if(item[1].substring(0, 2) != "on"){
					item[1] = "on" + item[1];
				};
				if(item[0].detachEvent){
					item[0].detachEvent(item[1], item[2]);
				};
				item[0][item[1]] = null;
			};
		}
	};
}();
addEvent(window,'unload',EventCache.flush);

function getFormElementByName(name)
{
	var form = document.forms[0];
	return form != null ? form.elements[name] : null;
}

function hasClass(el,c) 
{
	var re = new RegExp('\\b'+c+'\\b', 'i');
    return el && re.test(el.className);
}

function removeClassName(el,c)
{
	if ( el && c != null && c.length > 0 && el.className.length > 0 )
	{
		var re = new RegExp('\\b'+c+'\\b', 'ig');
		el.className = el.className.replace(re, "").trim();
	}
}

function appendClassName(el,c) 
{
	if ( !hasClass(el) ) { el.className = (el.className + " " + c).trim();}
}

function toggleClassName(el, c)
{
	if ( hasClass(el,c) ) { removeClassName(el, c); }
	else { appendClassName(el, c); }
}

/// Adds class name on hover & focus for IE that doesn't have :hover, :focus
function addHoverClassName(el, c, focus)
{
	if ( el && el.attachEvent )
	{
		el.attachEvent("onmouseover",function(){appendClassName(el,c);});
		el.attachEvent("onmouseout",function(){removeClassName(el,c);});
		if ( focus )
		{
			el.attachEvent("onfocus",function(){appendClassName(el,c);});
			el.attachEvent("onblur",function(){removeClassName(el,c);});
		}
	}
}

if (!String.prototype.trim) 
{ 
	String.prototype.trim = function() { return this.replace( /^\s+|\s+$/, "" ); }; 
}

if (!Array.prototype.contains) 
{ 
	Array.prototype.contains = function (value) {
		var i;
		for (i=0; i < this.length; i++) {
			if (this[i] === value) {
				return true;
			}
		}
		return false;
	};
}