﻿/*
 * country selector 
 */
var locationList = [];

function iniLocationSelector() {

    if ($('#intNav').length) {
		window.locationList = [
		'<a title="Australia" href="http://www.microsoftstore.com.au?WT.mc_id=StoreLocationToggle=AU" target="_parent">Australia</a>',
        '<a title="Belgique (MS Store France)" href="http://emea.microsoftstore.com/fr/fr-FR/?WT.mc_id=StoreLocationToggle_BE" target="_parent">Belgique (MS Store France)</a>',
        '<a title="België (MS Store Nederland)" href="http://emea.microsoftstore.com/nl/nl-NL/?WT.mc_id=StoreLocationToggle_BE" target="_parent">België (MS Store Nederland)</a>',
        '<a title="Brasil" href="http://microsoftstore.com.br" target="_parent">Brasil</a>',
        '<a title="Canada" href="http://www.microsoftstore.ca/shop/en-CA/" target="_parent">Canada</a>',
        '<a title="Danmark" href="http://www.microsoftstore.dk" target="_parent">Danmark</a>',
		'<a title="Deutschland" href="http://emea.microsoftstore.com/DE/de-DE/?WT.mc_id=StoreLocationToggle_DE" target="_parent">Deutschland</a>',
        '<a title="Espa&ntilde;a" href="http://emea.microsoftstore.com/es/es-ES/?WT.mc_id=StoreCountryToggle_ES" target="_parent">Espa&ntilde;a</a>',
		'<a title="France" href="http://emea.microsoftstore.com/fr/fr-FR/?WT.mc_id=StoreLocationToggle_FR" target="_parent">France</a>',
		'<a title="Hong Kong" href="http://apoc.microsoftstore.com/hk/en-HK/?WT.mc_id=StoreLocationToggle_HK/" target="_parent">Hong Kong</a>',                
        '<a title="India" href="http://www.microsoftstore.co.in/?WT.mc_id=StoreLocationToggle_IN" target="_parent">India</a>',
        '<a title="Italia" href="http://www.microsoftstore.it/?WT.mc_id=StoreLocationToggle_IT" target="_parent">Italia</a>',
        '<a title="Malaysia" href="http://www.microsoftstore.com.my/?WT.mc_id=StoreLocationToggle_MY" target="_parent">Malaysia</a>',
        '<a title="Nederland" href="http://emea.microsoftstore.com/nl/nl-NL/?WT.mc_id=StoreCountryToggle_NL" target="_parent">Nederland</a>',
        '<a title="New Zealand" href="http://www.microsoftstore.co.nz/?WT.mc_id=StoreLocationToggle_NZ" target="_parent">New Zealand</a>',
        '<a title="Norge" href="http://www.microsoftstore.no" target="_parent">Norge</a>',   
		'<a title="Österreich (MS Store Deutschland)" href="http://emea.microsoftstore.com/DE/de-DE?WT.mc_id=StoreLocationToggle_AT" target="_parent">Österreich (MS Store Deutschland)</a>',
        '<a title="Philippines" href="http://www.microsoftstore.com.ph/?WT.mc_id=StoreLocationToggle_PH" target="_parent">Philippines</a>',
		'<a title="Singapore" href="http://www.microsoftstore.com.sg/?WT.mc_id=StoreLocationToggle_SG" target="_parent">Singapore</a>',                
		'<a title="Suomi" href="http://www.microsoftstore.fi/?WT.mc_id=StoreLocationToggle_FI" target="_parent">Suomi</a>',             
		'<a title="Sverige" href="http://www.microsoftstore.se/?WT.mc_id=StoreLocationToggle_SE" target="_parent">Sverige</a>',
		'<a title="United Kingdom" href="http://emea.microsoftstore.com/UK/en-GB?WT.mc_id=StoreLocationToggle_UK" target="_parent">United Kingdom</a>',
        '<a title="United States" href="http://microsoftstore.com/store/msstore/home?WT.mc_id=StoreLocationToggle_US" target="_parent">United States</a>',
        '<a title="日本" href="http://www.microsoftstore.jp/?WT.mc_id=StoreLocationToggle_JP" target="_parent">日本</a>',
        '<a title="대한민국" href="http://www.microsoftstore.co.kr/?WT.mc_id=206" target="_parent">대한민국</a>',
        '<a title="Российская Федерация" href="http://www.microsoftstore.ru/shop/ru-RU?WT.mc_id=StoreCountryToggle_RU" target="_parent">Российская Федерация</a>'
        ];
        
        locationRepeater('row1',window.locationList.slice(0, 7));
        locationRepeater('row2',window.locationList.slice(7, 14));
        locationRepeater('row3',window.locationList.slice(14, 21));
		locationRepeater('row4',window.locationList.slice(21, 28));
		//locationRepeater('row5',locationList.slice(28, 34));

        toggleLocationSelector();
        locationSelectorTabOrdering();
    }
}

function locationRepeater(loc, list){
		for (var i = 0; i < list.length; i++) {
			$('#intNav dd.' + loc).append(list[i]);
		}
}

function toggleLocationSelector() {

    $('#intNav #currentLocation').mouseover(function() {
		$('#intNav').css('width', '855px');
		$('#intNav > dl').css('height', '215px');
		$('#intNav > dl').css('background-position', 'bottom left');	
        $('#intNav dd:not(#currentLocation)').css('display', 'block');
        
        
    });
    
    $('#intNav dl').mouseleave(function() {
		$('#intNav').css('width', '227px');
		$('#intNav > dl').css('height', '24px');
		$('#intNav dd#currentLocation').css('border', '0');		
		$('#intNav dd#currentLocation').css('border-bottom', '1px solid #d2d2d2');
		$('#intNav > dl').css('background-position', 'top left');		
        $('#intNav dd:not(#currentLocation)').css('display', 'none');
    });
}

function locationSelectorTabOrdering()
{
    var all = $("a");
    var inside = $("#intNav a");
    
    var firstIndex = all.index(inside[0]);     
    var lastIndex = all.index(inside[inside.length - 1]);
    
    var ranges = all.slice(firstIndex-1, lastIndex+2);

    $('#intNav #currentLocation a').focus(function() {
		$('#intNav').css('width', '855px');
		$('#intNav > dl').css('height', '215px');
		$('#intNav dd#currentLocation').css('border', '1px solid #d2d2d2');
		$('#intNav dd#currentLocation').css('border-top', '0');
		$('#intNav > dl').css('background-position', 'bottom left');
        $('#intNav dd:not(#currentLocation)').css('display', 'block');
    });

    $(ranges[0]).focus(function(){
        $('#intNav dd:not(#currentLocation)').css('display', 'none');
    });

    $(ranges[ranges.length-1]).focus(function(){
        $('#intNav dd:not(#currentLocation)').css('display', 'none');
    });
    
}

function iniGeoIP(){
	try{
		geoipRepeater('row1',window.locationList.slice(0, 7));
        geoipRepeater('row2',window.locationList.slice(7, 14));
        geoipRepeater('row3',window.locationList.slice(14, 21));
		geoipRepeater('row4',window.locationList.slice(21, 28));
		
	}catch(e){
		//fail silently
	}
	
}

function geoipRepeater(loc, list){
	for (var i = 0; i < list.length; i++) {
		$('.geoIPPopup dl.list dd.' + loc).append(list[i]);
	}
}

$(iniLocationSelector);
