/* This files controls the ainit */

$(document).ready(function() {

    $("#initContainer").show();

    var numItems = $(".menuItem").length;
    if (numItems > 5) { numItems = 5; }

    var counter = 0;
    var _interval;
    var _itemPaused;
    var _currentVideoItem;


    // Hide Every item but the first one.
    $(".itemContainer").hide();
    $($(".itemContainer")[0]).show();

    $(".itemContainer").click(function() {
        if ($(this).children(".videoInit").length != 0) {
            //load video.
            if (typeof interval !== "undefined") { clearInterval(interval); }
            _itemPaused = true;
            _currentVideoItem = this;

            $(this).children().hide();
            $(this).children(".videoPlayerContainer").show().html("<embed src='http://media.mtvnservices.com/mgid:cms:item:nick.com:" + stripID("itemContainer", this.id) + "' width='360' height='301' type='application/x-shockwave-flash' allowFullScreen='true' allowScriptAccess='always' flashvars='configParams=clickableVideoScreen%3Dfalse%26site%3Dnick%26type%3Dnormal%26playerName%3DpenguinPlayer%26cmsid%3D" + stripID("itemContainer", this.id) + "'/></embed>");
        }
    });

    $(".itemContainer").hover(function() { if (typeof interval !== "undefined") { clearInterval(interval); } }, function() { if (!_itemPaused) { resetInterval(); } });

    // Set the next item as selected.
    nextItem = function() {

        if (counter === numItems) {
            counter = 0;
        }

        // Display the proper image container.
        $(".itemContainer").hide();
        $($(".itemContainer")[counter]).show();

        // Set the proper state for the menu items.
        $(".menuItem").removeClass("menuItemSelected");
        $($(".menuItem")[counter]).addClass("menuItemSelected");

        // Increment
        counter = counter + 1;
    }

    // Display the specified item.
    displayItem = function(item) {
        if (_itemPaused) {
            resetVideoItem();
        }

        $(".itemContainer").hide();
        $(item).show();

        counter = $(".itemContainer").index($(item));

        $(".menuItem").removeClass("menuItemSelected");
        $($(".menuItem")[counter]).addClass("menuItemSelected");

        resetInterval();
    }

    // Remove the player and set the thumbnail to visible.
    resetVideoItem = function() {
        if (typeof _currentVideoItem != "undefined") {
            $(_currentVideoItem).children().show();
            $(_currentVideoItem).children(".videoPlayerContainer").html("").hide();
            _itemPaused = false;
        }
    }


    // Utils
    // Grab the cmsid of the current item.
    stripID = function(name, value) {
        var id = value;
        id = id.substring(name.length, id.length);
        return id;
    }

    // Teset the interval for the menu cycling
    resetInterval = function() {
        if (typeof interval !== "undefined") { clearInterval(interval); }
        interval = setInterval("nextItem()", 5000);
    }

    // Display First Item, then set interval between items.
    nextItem();
    interval = setInterval("nextItem()", 5000);

    // Move all of the menu items into the menu div.
    $("#items").append($(".menuItem"));

    // Handle Mouse events for menu items.
    $(".menuItem").click(function() {
        displayItem("#itemContainer" + stripID("menuItem", this.id));
    });

    $(".menuItem").hover(function() { $(this).addClass("menuItemOver"); }, function() { $(this).removeClass("menuItemOver"); });

});
