/*
	DOMhelp 1.0
	written by Chris Heilmann
	http://www.wait-till-i.com
*/
DOMhelp={
	debugWindowId:'DOMhelpdebug',
	init:function(){
		if(!document.getElementById || !document.createTextNode){return;}
	},
/* helper methods */
	stopDefault:function(e){
		if(window.event && window.event.returnValue){
			window.event.returnValue = false;
		} 
		if (e && e.preventDefault){
			e.preventDefault();
		}
	},
	addEvent: function(elm, evType, fn, useCapture){
		if (elm.addEventListener){  //DOM-2
			elm.addEventListener(evType, fn, useCapture);
			return true;
		} else if (elm.attachEvent) { //MSIE
			var r = elm.attachEvent('on' + evType, fn);
			return r;
		} else { // DOM-1
			elm['on' + evType] = fn;
		}
	},
	//Add and remove classes from an element where:
	// "a" is the action ("swap" replaces one class with another / "add" adds a new class; / "remove" removes a class /"check" tests whether the class is already applied or not)
	// "o" is the object you want to add classes to or remove classes from.
	// "c1" and "c2" are the class names (c2 is only needed when the action is swap)
	cssjs:function(a,o,c1,c2){
		switch (a){
			case 'swap':
				o.className=!DOMhelp.cssjs('check',o,c1)?o.className.replace(c2,c1):o.className.replace(c1,c2);
			break;
			case 'add':
				if(!DOMhelp.cssjs('check',o,c1)){o.className+=o.className?' '+c1:c1;}
			break;
			case 'remove':
				var rep=o.className.match(' '+c1)?' '+c1:c1;
				o.className=o.className.replace(rep,'');
			break;
			case 'check':
				var found=false;
				var temparray=o.className.split(' ');
				for(var i=0;i<temparray.length;i++){
					if(temparray[i]==c1){found=true;}
				}
				return found;
			break;
		}
	},
    safariClickFix:function(){
      return false;
    }
}
DOMhelp.addEvent(window, 'load', DOMhelp.init, false);
// End sugar functions 





// Country selector
cs = {
 	label: 'Please select',
	container: '',
	content: '',
	countryNames: [],
	URLs: [], 
	f:'',
	jsClass: 'js-style',
	
	init: function()	{
		if(!document.getElementById || !document.createTextNode)	{return;}
		cs.container = document.getElementById('countries');
		if(!cs.container) 	{return;}
		//style js version
		cs.content = document.getElementById('content');
		DOMhelp.cssjs('add', cs.content, cs.jsClass);
		
		//Get the country names and URLs
		cs.list = cs.container.getElementsByTagName('ul')[0];
		var items = cs.list.getElementsByTagName('a');
		for(var i=0; i<items.length; i++)	{
			var link = items[i].getAttribute('href');
			var name = items[i].firstChild.nodeValue;
			cs.countryNames.push(name);
			cs.URLs.push(link);
		}
		//Remove the old list
		cs.container.innerHTML = "";
		
		//create select form
		cs.f = document.createElement('form');
		cs.f.setAttribute('id','myForm');
		var selectNode = document.createElement('select');
		selectNode.options[0] = new Option('Please select:','');		
		var firstOpt = selectNode.options[0];
		firstOpt.selected = true;
		firstOpt.disabled = true;		
		for(var i=0; i<cs.URLs.length; i++)	{				
			selectNode.options[i+1] = new Option(cs.countryNames[i],cs.URLs[i]);
		}
		cs.f.appendChild(selectNode);
		var subBtn = document.createElement('input');
		subBtn.setAttribute('type','submit');
		subBtn.setAttribute('value','Go');
		subBtn.setAttribute('class','subBtn');
		DOMhelp.addEvent(cs.f,'submit',cs.goToURL, false);
		cs.f.appendChild(subBtn);
		cs.container.appendChild(cs.f);		
	},
	goToURL: function(e)	{
		var selected = cs.f.elements[0].selectedIndex;
		window.location.href = cs.URLs[selected-1];
		DOMhelp.stopDefault(e);
		return false;
	}
}
DOMhelp.addEvent(window,'load',cs.init,false)


