var Gateway = {
	countrydiv: null,
	countries: null,
	languageimg: null,
	languagetext: null,
	languages: null,
	nocountry: null,
	gatewayform : null,
	country : null,
	language : null,

	initialize: function() {
		this.countrydiv = $('countries');
		this.languagediv = $('languages');
		this.languageimg = $('languageimg');
		this.languagetext = $('languagetext');
		this.gatewayform = $('gatewayform');
		this.country = $('country');
		this.language = $('language');
		this.nocountry = $('nocountry');
		
		this.countries = this.countrydiv.select('div.lang');
		this.languages = this.languagediv.select('div.lang');
		
		this.countries.each(function(el) {
			el.observe('click', this.selectCountry.bind(this));
		}.bind(this));
		
		this.languages.each(function(el) {
			el.observe('click', this.selectLanguage.bind(this));
		}.bind(this));
	},
	
	selectCountry: function(el) {
		var element = Event.element(el);
		this.countries.each(function(divel) {
			if (divel != element) {
				divel.removeClassName('active');
				divel.addClassName('inactive');
			}
			else {
				divel.removeClassName('inactive');
				divel.addClassName('active');			
			}
		});
		
		var continents = this.countrydiv.select('div.cont');
		continents.each(function(divel) {
			divel.addClassName('inactive');
		});
		
		this.country.value = element.id;
		this.nocountry.hide();
		this.languageimg.removeClassName('off');
		this.languageimg.addClassName('on');
		this.languagetext.removeClassName('inactive');
		this.languagetext.addClassName('active');		
		
		if (element.id == 'US') {
			this.languagediv.select('div.lang').each(function(divel) {
				if (divel.id == 'en_US') divel.style.display = 'block';
				else divel.style.display = 'none';
			});				
		} else if (element.id == 'CA') {
			this.languagediv.select('div.lang').each(function(divel) {
				if (divel.id == 'en_CA') divel.style.display = 'block';
				else divel.style.display = 'none';
			});				
		} else if (element.id == 'DE' || element.id == 'FR' || element.id == 'GB') {
			this.languagediv.select('div.lang').each(function(divel) {
				if (divel.id != 'en_US' && divel.id != 'en_CA' && divel.id != 'default') divel.style.display = 'block';
				else divel.style.display = 'none';
			});			
		}
		else {
			this.languagediv.select('div.lang').each(function(divel) {
				if (divel.id != 'en_US' && divel.id != 'en_CA' && divel.id != 'en') divel.style.display = 'block';
				else divel.style.display = 'none';
			});			
		}
		
		this.languagediv.show();
	},
	
	selectLanguage: function(el) {
		var element = Event.element(el);
		this.language.value = element.id;
		this.gatewayform.submit();
	}	
};

document.observe('dom:loaded', function() {
	Gateway.initialize();
});