﻿var SwapCountry = Class.create();
SwapCountry.prototype = {
	
	url: '',
	timeout: 0,
	
	initialize: function() {

		this.countries = new Hash();
		this.countries.set('ap', '0');
		this.countries.set('au', '1');
		this.countries.set('hk', '2');
		this.countries.set('in', '3');
		this.countries.set('id', '4');
		this.countries.set('mo', '5');
		this.countries.set('my', '6');
		this.countries.set('nz', '7');
		this.countries.set('ph', '8');
		this.countries.set('sg', '9');
		this.countries.set('th', '10');
		this.countries.set('tw', '11');
		this.setMouseover();
	},
	
	setMouseover: function() {
		this.checkUrl();
		
	},
	
	checkUrl: function() {
		if (this.url != document.location.hash) {
			window.scrollTo(0, 0);
			this.url = document.location.hash;
			if(this.url.match(/#(.*)/)) {
				selected = this.url.match(/#(.*)/)[1];
			} else {
				return;
				//selected = 'ar';
				//document.location.href = '#' + selected;
			}
			
			// check for correct anchor
			/*
			if(!this.countries.get(selected)) {
				selected = 'ar';
				document.location.href = '#' + selected;
			}
			*/
			

			// change select index
			var countryIndex = this.countries.get(selected);
			$('selectCountry').selectedIndex = countryIndex;
			this.turnOnCountryDiv(selected);
		} else if(!document.location.hash) {
			//document.location.href = '#ar';
		}

		this.timeout = window.setTimeout(this.checkUrl.bind(this, document.location.hash), 200);
	},
	
	turnOnCountryDiv: function(selectedCountry) {
		

		if(!this.countries.get(selectedCountry)) {
			selectedCountry = 'ap';
		}
		/**
		// if english speaking, go to lae/. CHANGE this flow when more than one country in lae/!
		if(selectedCountry == 'ap') {
			document.location.href = '/asia/iphone/buy/';
			return;
		}
		**/
		window.scrollTo(0, 0);
		var contentdivs = $$('.contentblock');
		contentdivs.each(function(div) {
			div.hide();
		});

		var divToTurnOn = 'content_' + selectedCountry;
		$(divToTurnOn).appear({ duration: 1.0 });
		var flagspan = $$('.flagimg');
		flagspan.each(function(s) {
		    if(selectedCountry =="ap")
			{
					selectedCountry = "gl";
					
			s.src = "/asia/iphone/buy/images/tinyflag_" + selectedCountry + "20080609.png";
			}
			if(selectedCountry =="in")
			{
					selectedCountry = "india";
					
			s.src = "/asia/iphone/countries/images/flag_" + selectedCountry + "20090225.png";
			}
			if(selectedCountry =="id")
			{
					selectedCountry = "indonesia";
					
			s.src = "/asia/iphone/countries/images/flag_" + selectedCountry + "20090225.png";
			}
			if(selectedCountry =="au")
			{
					selectedCountry = "australia";
					
			s.src = "/asia/iphone/countries/images/flag_" + selectedCountry + "20090225.png";
			}
			if(selectedCountry =="hk")
			{
					selectedCountry = "hongkong";
					
			s.src = "/asia/iphone/countries/images/flag_" + selectedCountry + "20090225.png";
			}
			if(selectedCountry =="mo")
			{
					selectedCountry = "macau";
					
			s.src = "/asia/iphone/countries/images/flag_" + selectedCountry + "20090225.png";
			}
			if(selectedCountry =="my")
			{
					selectedCountry = "malaysia";
					
			s.src = "/asia/iphone/countries/images/flag_" + selectedCountry + "20090225.png";
			}
				if(selectedCountry =="nz")
			{
					selectedCountry = "newzealand";
					
			s.src = "/asia/iphone/countries/images/flag_" + selectedCountry + "20090225.png";
			}
			if(selectedCountry =="ph")
			{
					selectedCountry = "philippines";
					
			s.src = "/asia/iphone/countries/images/flag_" + selectedCountry + "20090225.png";
			}
			if(selectedCountry =="sg")
			{
					selectedCountry = "singapore";
					
			s.src = "/asia/iphone/countries/images/flag_" + selectedCountry + "20090225.png";
			}
			if(selectedCountry =="th")
			{
					selectedCountry = "thailand";
					s.src = "/asia/iphone/countries/images/flag_" + selectedCountry + "20090225.png";
			}
			if(selectedCountry =="tw")
			{
					selectedCountry = "taiwan";
					s.src = "/asia/iphone/countries/images/flag_" + selectedCountry + "20090225.png";
			}
		});
	},
	
	handleSelect: function(event, obj) {
		selectedCountry = obj[obj.selectedIndex].value;
		document.location.href = '#' + selectedCountry;
	}
}
