/**
 * Querystring handling from http://seattlesoftware.wordpress.com/2008/01/16/javascript-query-string/
 */
location.querystring = (function() {
	// The return is a collection of key/value pairs
	var queryStringDictionary = {};
	// Gets the query string, starts with '?'
	var querystring = decodeURI(location.search);
	// document.location.search is empty if no query string
	if (!querystring) {
		return {};
	}
	// Remove the '?' via substring(1)
	querystring = querystring.substring(1);
	// '&' seperates key/value pairs
	var pairs = querystring.split("&");
	// Load the key/values of the return collection	 
	for (var i = 0; i < pairs.length; i++) {
		var keyValuePair = pairs[i].split("=");
		queryStringDictionary[keyValuePair[0]]
				= keyValuePair[1];
	}
	// toString() returns the key/value pairs concatenated
	queryStringDictionary.toString = function() {
		if (queryStringDictionary.length == 0) {
			return "";
		}
		var toString = "?";
		for (var key in queryStringDictionary) {
			toString += key + "=" +
				queryStringDictionary[key];
		}
		return toString;
	};
	// Return the key/value dictionary
	return queryStringDictionary;
})();

jQuery(document).ready(function() {
    /**
    * Show map markers with tooltips when store names are hovered over or get focus.
    * Open activated store pages in the opening window.
    */
    var stores = {
        '209': 1,
        '126': 1,
        '213': 1,
        '130': 1,
        '217': 1,
        '133': 1,
        '219': 1,
        '134': 1,
        '221': 1,
        '166': 1,
        '222': 1,
        '167': 1,
        '168': 1,
        '227': 1,
        '173': 1,
        '228': 1,
        '174': 1,
        '229': 1,
        '175': 1,
        '176': 1,
        '231': 1,
        '177': 1,
        '232': 1,
        '233': 1,
        '179': 1,
        '234': 1,
        '181': 1,
        '236': 1,
        '240': 1,
        '184': 1,
        '241': 1,
        '242': 1,
        '201': 1,
        '244': 1,
        '202': 1,
        '204': 1,
        '246': 1,
        '205': 1,
        '247': 1,
        '206': 1,
        '249': 1,
        '207': 1,
        '254': 1,
        '251': 1,
        '95': 1,
        '94': 1,
        '93': 1,
        '208': 2,
        '125': 2,
        '210': 2,
        '127': 2,
        '211': 2,
        '42': 2,
        '212': 2,
        '128': 2,
        '214': 2,
        '215': 2,
        '131': 2,
        '216': 2,
        '132': 2,
        '218': 2,
        '220': 2,
        '165': 2,
        '224': 2,
        '169': 2,
        '225': 2,
        '171': 2,
        '226': 2,
        '172': 2,
        '230': 2,
        '235': 2,
        '237': 2,
        '238': 2,
        '439': 2,
        '494': 2,
        '239': 2,
        '183': 2,
        '200': 2,
        '243': 2,
        '203': 2,
        '245': 2,
        '248': 2,
        '253': 2,
        '252': 2,
        '359': 2,
        '375': 2
    };
    var eventAreas = { '1': 1,
        '2': 1,
        '3': 1,
        '4': 2,
        '5': 1
    };
    if ($('#content.store-maps').length) {
        $('#content.store-maps').append('<span id="tooltip"></span>');
        $('<div><a href="#">Visa plan 2</a></div>').appendTo('#floor-1 .figure');
        $('#floor-1 .figure a').click(function() {
            $('#floor-1').addClass('hidden');
            $('#floor-2').removeClass('hidden');
        });
        $('<div><a href="#">Visa plan 1</a></div>').appendTo('#floor-2 .figure');
        $('#floor-2 .figure a').click(function() {
            $('#floor-2').addClass('hidden');
            $('#floor-1').removeClass('hidden');
        });
        $('.store-list a, .poi-list .poi').bind('mouseover focus', function() {
            if (highlightedStore && highlightedStore.length) {
                $(highlightedStore).removeClass('active');
            }
            var markerPosition = $(this).find('.marker:first').position();
            $('#tooltip').css({ 'top': markerPosition.top + 20 + 'px', 'left': markerPosition.left + 20 + 'px' });
            $('#tooltip').text($(this).find('span.name:first').text()).addClass('active').removeClass('hidden');
            $(this).find('.marker:first').css({ 'z-index': 2 });
        });
        $('.store-list a, .poi-list .poi').bind('mouseout blur', function() {
            var marker = $(this).find('.marker:first');
            $('#tooltip').addClass('hidden').removeClass('active');
            $('#tooltip').text('');
            $(this).find('.marker:first').css({ 'z-index': 1 });
        });
        $('.store-list a').click(function(e) {
            window.opener.location.href = $(this).attr('href');
            window.opener.focus();
            window.close();
            return false;
        });
        // Get storeid if supplied
        var storeID = window.location.querystring['storeid'];
        // Get event area id if supplied
        var eventAreaID = window.location.querystring['eventareaid'];
        // Get floorid if supplied
        var floorID = window.location.querystring['floorid'];
        // If a store ID is supplied, highlight the matching store
        if (storeID != undefined) {
            var highlightedStore = $('a#s' + storeID + ':first');
            if (highlightedStore.length) {
                if (stores['' + storeID] == 1) {
                    $('#floor-2').addClass('hidden');
                } else if (stores['' + storeID] == 2) {
                    $('#floor-1').addClass('hidden');
                    $('#floor-2').removeClass('hidden');
                }
                $(highlightedStore).addClass('active');
                var markerPosition = $(highlightedStore).find('.marker:first').position();
                $('#tooltip').css({ 'top': markerPosition.top + 20 + 'px', 'left': markerPosition.left + 20 + 'px' });
                $('#tooltip').text($(highlightedStore).find('span.name:first').text()).addClass('active').removeClass('hidden');
            }
        }
        else if (eventAreaID != undefined) {
            var highlightedEventArea = $('a#e' + eventAreaID + ':first');
            if (highlightedEventArea.length) {
                if (eventAreas['' + eventAreaID] == 1) {
                    $('#floor-2').addClass('hidden');
                } else if (eventAreas['' + eventAreaID] == 2) {
                    $('#floor-1').addClass('hidden');
                    $('#floor-2').removeClass('hidden');
                }
                $(highlightedEventArea).addClass('active');
                var markerPosition = $(highlightedEventArea).find('.marker:first').position();
                $('#tooltip').css({ 'top': markerPosition.top + 20 + 'px', 'left': markerPosition.left + 20 + 'px' });
                $('#tooltip').text($(highlightedEventArea).find('span.name:first').text()).addClass('active').removeClass('hidden');
            }
        }
        // If floor ID 2 is supplied, hide the other floor
        else if ((floorID != undefined) && (floorID == '2')) {
            $('#floor-1').addClass('hidden');
        }
        // No shop or floor supplied, so show floor 1
        else {
            $('#floor-2').addClass('hidden');
        }
    }
    window.focus();
});
