var TerritoryFinder = Class.create({

	/*	@brief	Set up all the class properties required for functionality
		@return	void */
	initialize: function(){
		
		this.strSiteRoot='http://'+document.location.hostname+'/coverage/sms/premium/';
		
		this.intAfricaId=1,this.intAsiaId=2,this.intAustralasiaId=3,this.intCentralAmericaId=4,this.intEuropeId=5,this.intMiddleEastId=6,this.intNordicsId=7,this.intNorthAmericaId=8,this.intSouthAmericaId=9;
		
		this.strTerritorySelect='territory_select';
		this.strCountrySelect='country_select';
		
		this.arrTerritoryNames=new Array();
		this.arrCountries=new Array();
		
		this.arrTerritoryNames[this.intAfricaId]='Africa',this.arrTerritoryNames[this.intAsiaId]='Asia',this.arrTerritoryNames[this.intCentralAmericaId]='Central America',this.arrTerritoryNames[this.intEuropeId]='Europe',this.arrTerritoryNames[this.intMiddleEastId]='Middle East',this.arrTerritoryNames[this.intNordicsId]='Nordics',this.arrTerritoryNames[this.intNorthAmericaId]='North America',this.arrTerritoryNames[this.intAustralasiaId]='Australasia',this.arrTerritoryNames[this.intSouthAmericaId]='South America';
		
		this.arrCountries[this.intAfricaId]=['South Africa', 'Nigeria', 'Ghana', 'Egypt', 'Cameroon', 'Senegal', 'Tanzania', 'Uganda', 'Kenya', 'Algeria', 'Morocco', 'Tunisia', 'Burkino Faso', 'Benin', 'Ivory Coast', 'Niger', 'Togo'],
		this.arrCountries[this.intAsiaId]=['China', 'India', 'Philippines', 'Hong Kong', 'Thailand', 'Malaysia', 'Singapore', 'Taiwan', 'Brunei', 'Indonesia', 'Vietnam'],
		this.arrCountries[this.intAustralasiaId]=['Australia','New Zealand'],
		this.arrCountries[this.intCentralAmericaId]=['Guatemala','Honduras','Costa Rica','Panama'],
		this.arrCountries[this.intEuropeId]=['Austria', 'Belgium', 'Czech Republic', 'France', 'Germany', 'Greece','Hungary', 'Ireland', 'Italy', 'Netherlands', 'Poland', 'Portugal', 'Spain', 'Switzerland', 'United Kingdom', 'Luxembourg', 'Turkey', 'Russia', 'Estonia', 'Georgia', 'Belarus', 'Ukraine', 'Lithuania', 'Latvia', 'Slovakia', 'Slovenia', 'Cyprus', 'Moldavia', 'Kosovo', 'Croatia'],
		this.arrCountries[this.intMiddleEastId]=['Bahrain', 'Iraq', 'Jordan', 'Kuwait', 'Lebanon', 'Oman', 'Qatar', 'Syria', 'UAE', 'Yemen', 'Tunisia', 'Azerbaijan', 'Kazakhstan', 'Israel', 'Armenia', 'Kyrgyzstan', 'Tajikistan', 'Saudi Arabia', 'Palestine', 'Mauritania', 'Sudan'],
		this.arrCountries[this.intNordicsId]=['Denmark','Finland','Norway','Sweden'],
		this.arrCountries[this.intNorthAmericaId]=['Canada','USA','Mexico'],
		this.arrCountries[this.intSouthAmericaId]=['Argentina', 'Bolivia', 'Chile', 'Colombia', 'Ecuador', 'Peru', 'Venezuela', 'Brazil'];
		this.arrCountries[this.intCentralAmericaId]=['Costa Rica', 'El Salvador', 'Guatemala', 'Honduras', 'Nicaragua', 'Panama'];
		
		this.makeDropDowns();
	},
	
	/*	@brief	Automatically populate the territory_select dropdown menu and enable it
		@return	void */
	makeDropDowns: function(){
	
		var objTerritory=$(this.strTerritorySelect);
		
		var objCountry=$(this.strCountrySelect);
		
		objTerritory.innerHTML='<option value="0">Choose Territory...</option>';
		
		var intCount=1;
		
		this.arrTerritoryNames.each(function(strCountry) {
			
			var objOption = document.createElement('option');
			
			objOption.innerHTML=strCountry;
			
			objOption.value=intCount;
			
			intCount++;
			
			objTerritory.appendChild(objOption);
			
		}.bind(this));
		
		objCountry.innerHTML='<option value="0">Choose Country...</option>';
		
		objTerritory.enable();
		
		objCountry.disable();
	},
	
	/*	@brief	When the territory_select is changed we should update the country_select dropdown with relevant countries
		@return	void */
	updateTerritory: function(){
	
		var strTerritory=$F(this.strTerritorySelect);
		
		if('0'!=strTerritory){
			
			$(this.strCountrySelect).innerHTML='<option value="0">Choose Country...</option>';
				
			if(undefined!=this.arrCountries[$F(this.strTerritorySelect)]){
				
				this.arrCountries[strTerritory].sort().each(function(strCountry) {
					
					var objOption = document.createElement('option');
					
					objOption.innerHTML=strCountry;
					
					objOption.value=strCountry;
					
					$(this.strCountrySelect).appendChild(objOption);
					
				}.bind(this));
				
				$(this.strCountrySelect).enable();
			}
		}
		else{
		
			$(this.strCountrySelect).disable();
		}
	},
	
	/*	@brief	Automatically populate the territory_select dropdown menu and enable it
		@return	void */
	findTerritory: function(){
	
		var strTerritory=$F(this.strTerritorySelect);
		
		var strCountry=$F(this.strCountrySelect);
		
		if('0'!=strTerritory) {
		
			if('0'!=strCountry){
			
				var strCountryName=this.escapeLink(strCountry);
				
				var strLink=this.escapeLink(this.arrTerritoryNames[strTerritory]);
				
				if('unitedkingdom'==strCountryName){
				
					strCountryName='uk';
				}
				
				strLink=strLink+'/'+strCountryName;
				
				this.redirect(strLink);
			}
			else{
			
				$(this.strCountrySelect).pulsate({pulses: 2, duration: 1});
			}
		}
		else{
		
			$(this.strTerritorySelect).pulsate({pulses: 2, duration: 1});
		}
	},
	
	/*	@brief	Make the link to redirect the end user to
		@param	String	The text to escape
		@return	String */
	escapeLink: function(strLink){
	
		return strLink.toLowerCase().replace(' ','');
	},
	
	/*	@brief	Redirect the user to the new country page
		@param	String	The country link to use when redirecting
		@return	void */
	redirect: function(strCountry){
	
		window.location=this.strSiteRoot+strCountry;
	}
});

document.observe("dom:loaded", function() {

	if(undefined!=$('territory_select')&&undefined!=$('country_submit')){
	
		var myTerritoryFinder=new TerritoryFinder();
	
		myTerritoryFinder.updateTerritory();
		$('territory_select').observe('change', function() {myTerritoryFinder.updateTerritory()});
		$('country_submit').observe('click', function() {myTerritoryFinder.findTerritory()});
	}
});
