/**
 * Language selector 
 * 
 * @constructor
 * @requires None
 * @author Ollie Maitland
 * @copyright Byng Systems LLP
 * 
 * @param String lang Active language
 * @param DomElement element 
 */
function LanguageSelector ( lang, element ) 
{
	/**
	 * Holds the current language
	 * 
	 * @param String lang
	 */
	this.lang = lang;
	
	/**
	 * Holds the selector element
	 * 
	 * @param DomElement element
	 */
	this.selector = element;
	
	/**
	 * Holds the window location
	 * 
	 * @param String uri
	 */
	this.uri = String(location.pathname);
	
	/**
	 * Holds the tier 1 languages
	 * 
	 * @param Array tier1
	 */
	this.tier1 = ['gb','de','au'];
	

	this.tier2 = ['nz','pl','ie'];


	this.tier3 = ['hu','co','us'];

	// unobtrusive replace of the href tag
	var link = this.selector.firstChild;
		link.setAttribute ('href', "javascript:Lang.select();");
	
	/**
	 * Return the active language
	 * 
	 * @return String
	 */
	this.getLang = function ()
	{ 
		return this.lang;
	}
	
	/**
	 * Start the select sequence
	 * 
	 */
	this.select = function ()
	{
		if (this.active == true) {
			this.selector.removeChild(this.container);
			this.active = false;			
		} else {
			
			this.container = ce('div');
			this.container.className = 'country_selection';

			var tier1 = this.attachLangs (ce('div'), this.tier1);
				tier1.className = 'country_row_1';
				
			var tier2 = this.attachLangs (ce('div'), this.tier2);
				tier2.className = 'country_row_2';
			
			var tier3 = this.attachLangs (ce('div'), this.tier3);
				tier3.className = 'country_row_3';		
			
	
			this.container.appendChild(tier1);
			this.container.appendChild(tier2);	
			this.container.appendChild(tier3);
	
			
			this.selector.appendChild(this.container);
			
			this.active = true;
		}
	}
	
	/**
	 * Attach languages
	 * 
	 * @param DomElement c
	 * @param Array ls
	 */
	this.attachLangs = function (c, ls)
	{
		var a;
		var img;
		if (this.lang) {
			if (this.uri.substring(3,4) == "/") {
				var uri = this.uri.substring(3);
			} else {
				var uri = this.uri;
			}
		} else { 
			var uri = this.uri;
		}
		
		ls.each(function(lang) {
			a = ce('a');
			a.setAttribute('href', '/' + lang + uri);
			a.setAttribute('title','Switch to ' + lang.toUpperCase());
			if (lang == this.getLang()) a.className = "active";
			img = ce('img');
			img.setAttribute('src','/html/images/country/sub/' + lang + '.gif');	
			a.appendChild(img);
			c.appendChild(a);
		}.bind(this));
		return c;
	}
	
			
}
