var Gateway = {
	countrydiv: null,
	countries: null,
	languageimg: null,
	languagetext: null,
	languages: null,
	nocountry: null,
	gatewayform : null,
	country : null,
	language : null,

	initialize: function() {
		this.countrydiv = jQuery('#countries');
		this.languagediv = jQuery('#languages');
		this.languageimg = jQuery('#languageimg');
		this.languagetext = jQuery('#languagetext');
		this.gatewayform = jQuery('#gatewayform');
		this.country = jQuery('#country');
		this.language = jQuery('#language');
		this.nocountry = jQuery('#nocountry');
		
		this.countries = this.countrydiv.find('div.lang');
		this.languages = this.languagediv.find('div.lang');
		
		this.countries.each(function() {
			jQuery(this).bind('click', function() {
				Gateway.selectCountry(jQuery(this));
			});
		});
		
		this.languages.each(function() {
			jQuery(this).bind('click', function() {
				Gateway.selectLanguage(jQuery(this));
			});
		});
	},
	
	selectCountry: function(el) {
		var element = el;
		this.countries.each(function() {
			var divel = jQuery(this);
			if (divel.attr('id') != element.attr('id')) {
				divel.removeClass('active');
				divel.addClass('inactive');
			}
			else {
				divel.removeClass('inactive');
				divel.addClass('active');			
			}
		});
		
		var continents = this.countrydiv.find('div.cont');
		continents.each(function() {
			jQuery(this).addClass('inactive');
		});
		
		this.country.attr('value', element.attr('id'));
		this.nocountry.hide();
		this.languageimg.removeClass('off');
		this.languageimg.addClass('on');
		this.languagetext.removeClass('inactive');
		this.languagetext.addClass('active');		
		
		if (element.attr('id') == 'US') {
			this.languagediv.find('div.lang', 'a.lang').each(function() {
				var divel = jQuery(this);
				var id = divel.attr('id');
				if (id == 'en_US' || id == 'es_US') divel.show();
				else divel.hide();
			});				
		} else if (element.attr('id') == 'CA') {
			this.languagediv.find('div.lang', 'a.lang').each(function() {
				var divel = jQuery(this);
				var id = divel.attr('id');
				if (id == 'en_CA' || id == 'fr_CA') divel.show();
				else divel.hide();
			});				
		} else if (element.attr('id') == 'GB') {
			this.languagediv.find('div.lang', 'a.lang').each(function() {
				var divel = jQuery(this);
				var id = divel.attr('id');
				if (id != 'en_US' && id != 'en_CA' && id != 'default' && id != 'es_US' && id != 'fr_CA') divel.show();
				else divel.hide();
			});				
		} else {
			this.languagediv.find('div.lang', 'a.lang').each(function() {
				var divel = jQuery(this);
				var id = divel.attr('id');
				if (id != 'en_US' && id != 'en_CA' && id != 'en' && id != 'es_US' && id != 'fr_CA') divel.show();
				else divel.hide();
			});			
		}
		
		this.languagediv.show();
	},
	
	selectLanguage: function(el) {
		this.language.attr('value', el.attr('id'));
		this.gatewayform.submit();
	}	
};

jQuery(document).ready(function(){
	Gateway.initialize();
});
