function mapHighlight() {

	$("#map-wrapper li a").css({ 'text-decoration' : 'none' });

		$("area").hover(

		function(){
			var region = $(this).attr("class");
			$(".map").attr({ src: "/images/interface/map/" + region + ".gif" });
			$("#"+region+" a").css({ 'color' : '#214232' });

		}, function($this){
			var region = $(this).attr("class");
			$(".map").attr({ src: "/images/interface/map/null.gif" });
			$("#"+region+" a").css({ 'color' : '' });

		});

		$(".region-list li").hover(

		function(){
			var region = $(this).attr("id");
			$(".map").attr({ src: "/images/interface/map/" + region + ".gif" });
			$(this).css({ 'color' : '#214232' });

		}, function($this){
			var region = $(this).attr("id");
			$(".map").attr({ src: "/images/interface/map/null.gif" });
			$(this).css({ 'color' : '' });

		});

};

function matchHeight(element) {

	var tallest = 0;

	element.each( function() {

		var thisHeight = $(this).height();

		if( thisHeight > tallest) {
            tallest = thisHeight;
        }
    });

    element.height(tallest);
};

function weatherFunc() {

	$('h2.national, h3.national, .tabbed-content .heading-link').hide();

	var tabContainers = $('ul.primary ~ div');
    $('ul.primary a').click(function () {
        tabContainers.hide().filter(this.hash).show();

        $('ul.primary a').removeClass('selected');
        $(this).addClass('selected');

        return false;
    }).filter(':first').click();


	var outlook = $('#outlook-tabs ~ img');
    $('#outlook-tabs a').click(function () {
        outlook.hide().filter(this.hash).show();

        $('#outlook-tabs a').removeClass('selected');
        $(this).addClass('selected');

        return false;
    }).filter(':first').click();


	var satellite = $('#satellite-tabs ~ img:not(.weather-key-pic)');
    $('#satellite-tabs a').click(function () {
        satellite.hide().filter(this.hash).show();

        $('#satellite-tabs a').removeClass('selected');
        $(this).addClass('selected');

        return false;
    }).filter(':first').click();


	var rainRadar = $('#rainfall-radar img:not(.weather-key-pic)');
    $('#rainfall-radar .btn').click(function () {
		var showRadar = $('#radar-region').val();
        rainRadar.hide().filter('#' + showRadar).show();

        return false;
    }).filter(':first').click();


	$('#warning-title ~ .heading-link').show();
	$('#weather-warning').hide();

	$('#warning-toggle').click(function () {
		$('#weather-warning').toggle();
		$('#warning-toggle').toggleClass("close");

		var warningStatus = $('#weather-warning').css("display");
		if (warningStatus == "block") {
			$('#warning-toggle').text("Close");
		} else {
			$('#warning-toggle').text("View");
		}

		return false;
	});


	$('#international-title').show();
	$('#international-title a, #world-map-title, div.international-region').hide();

	$('#world-map area').click(function () {
		var continent = $(this).attr('class');

		$('#international-title a, div#' + continent + '-forecast').show();
		var continentTitle = $('div#' + continent + '-forecast h2').text();
		$('#international-title h2').text(continentTitle);
		$('div#world-map-wrapper, div.international-region h2, div.international-region a').hide();

		return false;
	});

	$('#world-map-wrapper li').click(function () {
		var continent = $(this).attr('id');

		$('#international-title a, div#' + continent + '-forecast').show();
		var continentTitle = $('div#' + continent + '-forecast h2').text();
		$('#international-title h2').text(continentTitle);
		$('div#world-map-wrapper, div.international-region h2, div.international-region a').hide();

		return false;
	});

	$('a.world-map-return').click(function () {
		$('#international-title h2').text('World Map');
		$('#international-title a, div.international-region').hide();
		$('div.#world-map-wrapper').show();

		return false;
	});
};

function calendarPopUp(startDate,endDate) {

	// initialise the "Select date" link


	$('#date-pick')
		.datePicker(
			// associate the link with a date picker
			{
				createButton:false,
				startDate:startDate,
				endDate:endDate
			}
		).bind(
			// when the link is clicked display the date picker
			'click',
			function()
			{
				updateSelects($(this).dpGetSelected()[0]);
				$(this).dpDisplay();
				return false;
			}
		).bind(
			// when a date is selected update the SELECTs
			'dateSelected',
			function(e, selectedDate, $td, state)
			{
				updateSelects(selectedDate);
			}
		).bind(
			'dpClosed',
			function(e, selected)
			{
				updateSelects(selected[0]);
			}
		);

	var updateSelects = function (selectedDate)
	{
		var selectedDate = new Date(selectedDate);
		$('#date-day option[value=' + selectedDate.getDate() + ']').attr('selected', 'selected');
		$('#date-month option[value=' + (selectedDate.getMonth()+1) + ']').attr('selected', 'selected');
		$('#date-year option[value=' + (selectedDate.getFullYear()) + ']').attr('selected', 'selected');
	}
	// listen for when the selects are changed and update the picker
	$('#date-day, #date-month, #date-year')
		.bind(
			'change',
			function()
			{
				var d = new Date(
							$('#date-year').val(),
							$('#date-month').val()-1,
							$('#date-day').val()
						);
				$('#date-pick').dpSetSelected(d.asString());
			}
		);

	// default the position of the selects to today
	var today = new Date();
	updateSelects(today.getTime());

	// and update the datePicker to reflect it...
	$('#date-day').trigger('change');
};

var geocoder;
var map;

function getMap() {
	geocoder = new google.maps.Geocoder();
	var latlng = new google.maps.LatLng(-37.785897,175.281936);
	var myOptions = {
        zoom: 15,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
	var mapCanvas = document.getElementById("store-map-canvas");
    if (mapCanvas == null) mapCanvas = document.getElementById("admin-map-canvas");
    if (mapCanvas == null) mapCanvas = document.getElementById("order-map-canvas");
	map = new google.maps.Map(mapCanvas, myOptions);
}

function codeAddress(storeAddress) {
	var address = (storeAddress);
	if (geocoder) {
		geocoder.geocode( { 'address': address}, function(results, status) {
			if (status == google.maps.GeocoderStatus.OK) {
				map.setCenter(results[0].geometry.location);
				var marker = new google.maps.Marker({
					map: map,
					position: results[0].geometry.location
				});
			} else {
				//alert("Geocode was not successful for the following reason: " + status);
			}
		});
	}
}

var Cart = {
    Frontend: (function () {
        function AddToCart(elm) {
            elm.each(function () {
                var i = $(this);
                i.click(function () {
                    cart.addToCart(i.attr("data-article"), i.attr("data-promo"));
                    ShowNotice(i);
                    UpdateCartPreview(Cart.Server.GetCartInformation());
                });
            });
        }

        function AddNotice(elm) {
            var html = '<div class="cart-update abs bg-tan-dark">'
                + '	<p>This product has now been added to your cart</p>'
                + '	<ul>'
                + '		<li class="left"><a class="btn view" href="/cart">View Cart</a></li>'
                + '		<li class="right"><a class="btn browse margin-none" href="#">Continue Browsing</a></li>'
                + '	</ul>'
                + '</div>';
            var target = $('body').prepend(html);
            var i = $('body > .cart-update');
            i.hide();
            i.find('.browse').click(function () {
                HideNotice();
                return false;
            });
        }

        function ShowNotice(elm) {
            HideNotice();
            if ($('body > .cart-update').length < 1) {
                AddNotice(elm);
            }
            var notice = $('body > .cart-update');
            var x = elm.offset().left - notice.outerWidth() / 2 + (elm.outerWidth() / 2);
            var y = elm.offset().top - (notice.outerHeight() + 5);
            notice.css({ left: x + 'px', top: y + 'px' });
            notice.fadeIn();
        }

        function HideNotice() {
            $('body > .cart-update').hide();
        }

        return {
            AddToCart: AddToCart
        }
    })(),
    Server: (function () {
            function GetCartInformation() {
            var cartInfo;
            $.ajax({
                url: '/cart-totals',
                dataType: 'json',
                async: false,
                success: function(data) {
                    cartInfo = data;
                }
            });
            return cartInfo;
        }

        return {
            GetCartInformation: GetCartInformation
        }
    })()
};

function UpdateCartPreview(cartInfo) {
    var target = $('.cart-preview');
    var itemText = cartInfo.numberOfItems != 1 ? ' items' : ' item';
    target.find('dd.total').text('$ '+ format2dp(cartInfo.totalCost));
    target.find('dd.items a').text(cartInfo.numberOfItems + itemText);
}
