﻿$(function () {
    // Set up the media gallery
    if (jQuery.jcarousel) {
        // Show the list once everything has loaded.
        $('#promosList').show();

        // Init the jcarousel plugin
        $('#promosList').jcarousel({
            scroll: 5
        });

        // Register the thumbnail click handler
        $('.jcarousel-item a').click(function (e) {
            e.preventDefault();
            activateGalleryItem(this);
        });

        // Pre select the first item in the gallery list
        $('.jcarousel-item').first().addClass('current');
        activateGalleryItem($('.jcarousel-item a').first());
    }

    $("ul.tabs").tabs("div.panes > div", {
        onBeforeClick: function (event, tabIndex) {
            var tabPanes = this.getPanes();
            //alert(tabIndex);
            if (tabIndex == 0) {
                $("#news .tabs li.leftCorner .filler").hide();
            } else {
                $("#news .tabs li.leftCorner .filler").show();
            }

            if (tabIndex == ($("ul.tabs li").length - 3)) {
                $("#news .tabs li.rightCorner .filler").hide();
            } else {
                $("#news .tabs li.rightCorner .filler").show();
            }
        }
    });

    $("ul.tabs li").first().addClass("first");
    $("ul.tabs li").last().addClass("last");
});

// Displays the item that was clicked in the gallery list
function activateGalleryItem(activeLink) {
    // un-highlight the old selected thumbnail
    $('.jcarousel-item .current').removeClass('current');
    // highlight the newly clicked thumbnail
    $(activeLink).parent('.jcarousel-item').addClass("current");

    var curLink = $(activeLink);
    var actionString = curLink.attr("href");
    var actionArray = [];
    var actionInfo = new Array();

    if (actionString == null) {
        return false;
    }

    // Strip the ? from the start of the string
    actionString = actionString.substring(1);

    // Split the action string info into sets
    actionArray = actionString.split('|');

    // Loop the action string info sets
    $.each(actionArray, function () {
        // Split the info set
        var tempArray = this.split('~');
        // Assign the info set key and value to an associative array
        actionInfo[tempArray[0]] = tempArray[1];
    });

    $("#promos .largePromo .photo").attr("href", actionInfo["linkurl"]);
    $("#promos .largePromo .photo").attr("target", actionInfo["target"]);
    $("#promos .largePromo .photo").removeClass("videoLink");
    $("#promos .largePromo .photo img").attr("src", "/!userfiles/media_gallery/" + actionInfo["imgurl"] + "?height=245&width=400&crop=auto");
    $("#promos .largePromo .photo").attr("title", curLink.attr("title"));
    $("#promos .largePromo .photo img").attr("alt", curLink.attr("title"));
    $("#promos .largePromo .photo").show();
}
