﻿$(document).ready(function() {

    $("#header div.fc").pngFix();

    if ($(".popup").length > 0) {
        SetupPopups();
    }

    SetupCyclePager();

    $('ul.panelRotate').cycle({
        fx: 'fade',
        speed: 1000,
        timeout: 7000,
        slideExpr: 'li.slide',
        after: SetPagerStatus
    });

    if ($(".featureItem").length > 0 && $("#mapOuter").length > 0 && $("#tabs").length > 0) {
        SetupTabs();
    }

    if (IsInImmediacyEditor()) {
        setTimeout("$('#mtaareatopslider').css('width','560px').css('margin-left','10px').css('float','left');", 1000);
        setTimeout("$('#panelRotateContainer').css('padding-bottom','3px');", 1000);
        setTimeout("$('#mtaareasubright').css('margin-left','20px');", 1000);

    }

    $(".buttonPaging input").hover(function() { $(this).addClass("hover"); }, function() { $(this).removeClass("hover"); });

    SetupExpandingInterests();

    if ($("#regionSelect h2").length > 0) {
        SetupImageMapSelection();
    }

    if ($("#print").length > 0) {
        $(window).ready(function() { window.print(); });
    }

    if ($("head link[href$='Winter.css']").length > 0 && $("#imageMap img").length > 0) {
        var newMapSrc = $("#imageMap img").attr("src").replace("build_images", "build_images/winter");

        $("#imageMap img").attr("src", newMapSrc);
    }

    if ($("#offerDetails").length > 0) {
        $("#content .col_170fl").prepend("<a id='back' href='javascript:history.back();'>Return to search results</a>");
    }
});

function SetupImageMapSelection() {
    $("#regionSelect h2").after('<div id="mapContent"><p>Refine by clicking on the map or by using the list of regions below</p></div>'
                + '<img src="' + imageMapPath + '" alt="Map of Scotland" usemap="#map" />'
                + '<map name="map" id="map">'
                    + '<area onclick="SetRegionStatus(this);return false;" shape="poly" coords="152,42,164,64,173,42,173,21" href="#shetland" title="Shetland." alt="Shetland." />'
                    + '<area onclick="SetRegionStatus(this);return false;" shape="poly" coords="137,76,120,78,114,97,125,105" href="#orkney" title="Orkney." alt="Orkney." />'
                    + '<area onclick="SetRegionStatus(this);return false;" shape="poly" coords="47,111,26,126,12,151,9,181,13,181,44,134" href="#hebrides" title="The Outer Hebrides." alt="The Outer Hebrides." />'
                    + '<area onclick="SetRegionStatus(this);return false;" shape="poly" coords="118,107,77,109,55,153,43,147,35,156,47,171,47,190,57,197,67,195,85,188,116,169,127,150,105,143,121,120" href="#highlands" title="Highlands and Moray." alt="Highlands and Moray." />'
                    + '<area onclick="SetRegionStatus(this);return false;" shape="poly" coords="149,148,139,188,127,179,112,184,99,180,127,151" href="#aberdeen" title="Aberdeen City and Shire." alt="Aberdeen City and Shire."/>'
                    + '<area onclick="SetRegionStatus(this);return false;" shape="poly" coords="44,189,26,197,41,207,36,234,53,247,74,224,93,221,108,217,82,189" href="#argyll" title="Argyll, the Isles, Loch Lomond, Stirling and Trossachs." alt="Argyll, the Isles, Loch Lomond, Stirling and Trossachs."/>'
                    + '<area onclick="SetRegionStatus(this);return false;" shape="poly" coords="99,181,112,186,118,203,116,215,111,216,86,190" href="#perthshire" title="Perthshire." alt="Perthshire." />'
                    + '<area onclick="SetRegionStatus(this);return false;" shape="poly" coords="112,184,128,180,139,188,127,204,119,206" href="#angus" title="Angus and Dundee." alt="Angus and Dundee." />'
                    + '<area onclick="SetRegionStatus(this);return false;" shape="poly" coords="106,222,103,229,110,234,138,228,131,222" href="#edinburgh" title="Edinburgh and the Lothians." alt="Edinburgh and the Lothians." />'
                    + '<area onclick="SetRegionStatus(this);return false;" shape="poly" coords="140,227,145,232,140,241,141,248,126,257,109,248,111,235" href="#borders" title="Scottish Borders." alt="Scottish Borders." />'
                    + '<area onclick="SetRegionStatus(this);return false;" shape="poly" coords="119,206,127,206,132,211,110,222" href="#fife" title="The Kingdom of Fife." alt="The Kingdom of Fife." />'
                    + '<area onclick="SetRegionStatus(this);return false;" shape="poly" coords="78,225,95,222,110,235,106,251" href="#glasgow" title="Glasgow and the Clyde Valley." alt="Glasgow and the Clyde Valley." />'
                    + '<area onclick="SetRegionStatus(this);return false;" shape="poly" coords="75,227,80,244,70,266,97,245,97,245" href="#ayrshire" title="Ayrshire and Arran." alt="Ayrshire and Arran." />'
                    + '<area onclick="SetRegionStatus(this);return false;" shape="poly" coords="67,268,81,282,126,261,105,249" href="#dumfries" title="Dumfries and Galloway." alt="Dumfries and Galloway." />'
                + '</map>');
}

function SetRegionStatus(area) {
    var checkboxSelectId = $(area).attr("href");

    var input = $(checkboxSelectId + " input");

    if (input.attr("checked")) {
        input.attr("checked", "");
    }
    else {
        input.attr("checked", "checked");
    }
    
}

function SetupExpandingInterests() {
    if ($("#interests ul input[checked]").length == 0) {
        $("#interests .innerContent").hide();
        $("#interests h2 a").addClass("closed");
    }
    $("#interests h2 a").toggle(
        function() {
            $("#interests .innerContent").slideDown();
            $("#interests h2 a").removeClass("closed");
        },
        function() {
            $("#interests .innerContent").slideUp();
            $("#interests h2 a").addClass("closed");
        });

}

function SetupTabs() {    
    $("#list, #mapOuter").wrapAll('<div id="tabContent"/>'); // Wrapp all list items and map in the tab content div
        
    // Hide the map by default
    $("#mapOuter").hide();

    // Add the click events to switch between the tab content
    $(".tabLink").click(function() { ShowTabContent(this); return false; });

}

function ShowTabContent(selectedTab) {
    var currentContentContainerId = $("#tabs .current a").attr("href"); // Get the current showing container Id
    $("#tabs .current").removeClass("current"); // Remove the current class
    
    var tab = $(selectedTab); // Make the selectedTab a jQuery object
    tab.parents("li").addClass("current"); // Add a class to it's containing li to make it the current tab

    var contentContainerId = tab.attr("href"); // Get the containerId of the element to show
    
    // Hide the current before showing the next
    $(currentContentContainerId).fadeOut("slow", function() { $(contentContainerId).fadeIn("slow"); });
}


function SetPagerStatus() {

    var currentPagerItem = $("#thumbPager li.current");

    if (currentPagerItem.length > 0) {
        currentPagerItem.removeClass("current");
    }
}

function SetupCyclePager() {
    var thumbs = $('ul.panelRotate li img.thumbnail');

    if (thumbs.length > 0) {
        var pagerLinks;
        thumbs.each(function(i) {
            $(this).addClass("hide");
            var thumbnailSrc = $(this).attr("src");
            var thumbnailAlt = $(this).attr("alt");
            pagerLinks += '<li><a href="#" onclick="ResetCycle(' + i + '); return false;"><img src="' + thumbnailSrc + '" alt="' + thumbnailAlt + '"/></a></li>';
        });

        $('<ul id="thumbPager" class="fc"/>').insertAfter('ul.panelRotate');
        $(pagerLinks).appendTo("#thumbPager");
    }
}

function ResetCycle(startIndex) {
    $("#thumbPager li.current").removeClass("current");
    $("#thumbPager li:eq(" + startIndex + ")").addClass("current");
    
    $('ul.panelRotate').cycle({
        fx: 'fade',
        speed: 1000,
        timeout: 7000,
        slideExpr: 'li.slide',
        startingSlide: startIndex,
        after: SetPagerStatus
    });
}

function IsInImmediacyEditor() {
    return top.Support;
}

function SetupPopups(rel) {  
    var popupClone = $("div.popup");
    $('<div id="popupOverlay"/>').appendTo($("body"));
    
    //popupClone.appendTo($("body"));
    
    for (var ii = 0; ii < popupClone.length; ii++) {
        var clone = $(popupClone[ii]);
        clone.wrap('<div class="popupContainer"/>');  
    }


    $("a.popup").click(function() {
        // Setup the overlay
        var height = $(document).height();
        var width = $(document).width();

        $('#popupOverlay').css({
            opacity: 0.6,
            height: height,
            width: width,
            display: "block"
        })

        // Position container
        var scrollTop = $(window).scrollTop() + 20;
        $(".popupContainer").css("top", scrollTop + "px");
        var popupId = "#" + $(this).attr("rel");
        return ShowPopup(popupId);


    });

    SetupPopupClose();
}

function ShowPopup(popupId) {

    
        
    var popup = $(popupId).show(); // The main container
    var height = $(popupId + " .popupContent").height(); // The contents width (Set through CSS)
    var width = $(popupId + " .popupContent").width() + 10; // The contents width (Set through CSS) + 10 is there to generate space for the shadow

    // Animate the container to be the width first then height of the content inside
    popup.animate({ width: width, height: height }, 1200, function() {
        $(popupId + " .popupContent").show();
        thisMovie('videoPlayer').playPauseVideo();
    });

    return false;
}

function SetupPopupClose() {
    $(".close").click(function() {
    thisMovie('videoPlayer').playPauseVideo();
    
        $("div.popupContent").fadeOut("slow", function() {
            $("div.popup").animate({ height: 0, width: 0 }, 1200, function() { $("#popupOverlay").hide(); });
        });
        return false;
    });
}