//
//	CountrySelect
//	Custom for Aladdin / PMI
//	Author: POP - G.S.
//

//	Bounce transition from robertpenner.com :
Effect.Transitions.easeOutBounce = function(pos){
	if ((pos) < (1/2.75)) {
		return (7.5625*pos*pos);
	} else if (pos < (2/2.75)) {
		return (7.5625*(pos-=(1.5/2.75))*pos + .75);
	} else if (pos < (2.5/2.75)) {
		return (7.5625*(pos-=(2.25/2.75))*pos + .9375);
	} else {
		return (7.5625*(pos-=(2.625/2.75))*pos + .984375);
	}
};

var CountrySelect = Class.create({
	initialize: function(target, trigger,csMarkupPath) {
		
		//
		//	DOM SETUP
		//
		
		// Grab CountrySelect markup
		new Ajax.Request(csMarkupPath, {
			method: 'get',
			asynchronous: false,
			onComplete: function(response) {
				this.setContent(response.responseText);
			}.bind(this)
		});
		
		this.overlayVisible = false;
		
		// Element references:
		this.container = $(target);
		this.launchButton = $(trigger);
		this.goButton = this.container.select('a')[0];
		
		this.overlay = new Avram.Overlay("avramOverlay"); // NEW
		
		// Check to see if you're on the US homepage:
		document.location.pathname == '/' ? this.onHomepage = true : this.onHomepage = false;
		
		this.urlchunk = document.location.pathname.split('/',2)[1];
		
		switch(this.urlchunk) {
			case 'asia':
			this.siteRegion = 'asia';
			break;
			
			case 'eu':
			this.siteRegion = 'eu';
			break;
			
			default:
			this.siteRegion = 'na';
			break;
		}
		
		if(this.getCookie()) {
			this.userRegion = this.getCookie();
		} else {
			this.userRegion = "na"; // North America by default
		}
			
		// Hide the selector window and set its position:
		this.setPosition();
		this.container.hide();

		// Show selector for non-English users with no cookie that land on the homepage:
		if(!this.isEnglish() && !this.getCookie() && this.onHomepage) {
			this.toggleDisplay();
		} else if(this.siteRegion == 'na' && this.userRegion != 'na' && this.onHomepage) {
			this.redirect(this.userRegion);
		}
		
		
		//
		//	EVENT LISTENERS:
		//
		
		this.launchButton.observe('click', this.__changeRegionClick.bind(this));
		this.goButton.observe('click', this.__goButtonClick.bind(this));		
		Event.observe(window,'resize', this.__windowResize.bind(this));
	},
	__changeRegionClick: function(event) {
		event.stop();
		this.toggleDisplay();
	},
	__goButtonClick: function(event) {
		event.stop();
		
		// Determine which region has been picked:
		if($F('region-na')) {
			this.userRegion = 'na';
		} else if ($F('region-asia')) {
			this.userRegion = 'asia';
		} else if ($F('region-eu')) {
			this.userRegion = 'eu';
		}		
		
		// Check if "Always..." is checked - set a cookie if it has been
		if($F('checkbox-always')) {
			this.setCookie(this.userRegion);
		} else {
			this.destroyCookie();
		}
								
		this.toggleDisplay();
		this.redirect(this.userRegion);
	},
	__windowResize: function(event) {
		this.setPosition();
	},
	setContent: function(content) {
		$$('body').first().insert({top:content});
	},
	toggleDisplay: function() {
		this.setPosition();
		
		if(this.overlayVisible) {
			this.overlayVisible = false;
			this.overlay.hide();
		} else {
			this.overlayVisible = true;
			this.overlay.show();
		}
		
		Effect.toggle(this.container,'appear',{duration:0.25});
	},
	setPosition: function() {
		this.x = Math.round((document.viewport.getWidth() - this.container.getWidth()) / 2);
		this.y = Math.round((document.viewport.getHeight() - this.container.getHeight()) / 3);
		
		this.container.morph({
			left: this.x+'px',
			top: this.y+'px'
		},{
			duration:0.5,
			transition:Effect.Transitions.easeOutBounce
		});
	},
	isEnglish: function() {
		if (navigator.browserLanguage){language=navigator.browserLanguage;}
		if (navigator.userLanguage){language=navigator.userLanguage;}
		if (navigator.systemLanguage){language=navigator.systemLanguage;}
		if (navigator.language){language=navigator.language;}
		if (language.indexOf('-')==2) {language=language.substring(0,2);}
		
		if (language=='en')
			return true;
		else
			return false;
	},
	setCookie: function(region) {
		createCookie('cs-region',region,999999);	// Expire in 999999 days
	},
	destroyCookie: function() {
		eraseCookie('cs-region');
	},
	getCookie: function() {
		if(readCookie('cs-region')) {
			return readCookie('cs-region');
		} else {
			return false;
		}
	},
	redirect: function(region) {
		switch(region) {
			case 'na':
			window.location = 'http://www.aladdin-pmi.com/';
			break;
			
			case 'asia':
			window.location = 'http://www.aladdin-pmi.com/asia/';
			break;
			
			case 'eu':
			window.location = 'http://www.aladdin-pmi.com/eu/';
			break;
		}
	}
});

Event.observe(window,"load", function() {
	$('aladdin_abroad').update('<a id="btn-change_region" href="#change-region"><span>Change Region</span></a>');
	aladdinCountrySelector = new CountrySelect('country_select','btn-change_region','/country_select-widget.html');
});