/* 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;


    $(".itemContainer").click(function() {
        if ($(this).children(".videoInit").length != 0) {
            //load video.
            if (typeof interval !== "undefined") { clearInterval(interval); }
            _itemPaused = true;
            _currentVideoItem = this;
            $(this).children().hide();
			$("#videoPlayer").show();
			doRegisterSwf('video-player','http://media.nick.com/mgid:cms:video:nick.com:' + stripID("itemContainer", this.id),'videoPlayer','9.0.124.0','324px','403px',{configParams:'clickable', VideoScreen:'false', site:'nick', type:'normal', playerName:'penguinPlayer', cmsid:stripID("itemContainer",this.id)},'false','false','/dynamo/javascript/swfobject/expressinstall.swf','/common/detect/get_flash.jhtml',{wmode:"opaque",salign:"TL",allowScriptAccess:"sameDomain",swliveconnect:"false"},{});
        }
    });

    $(".itemContainer").hover(function() { if (typeof interval !== "undefined") { clearInterval(interval); } }, function() { if (!_itemPaused) { resetInterval(); } });

    // Display the specified item.
    displayItem = function(item) {
		NICK.utils.doLog(item);
        if (_itemPaused) {
            resetVideoItem();
        }

        $(".itemContainer").removeClass('active last-active');
        $(item).addClass('active');

        counter = $(".itemContainer").index($(item));

        $(".menuItem").removeClass("active");
        $($(".menuItem")[counter]).addClass("active");

        resetInterval();
    }

    // Remove the player and set the thumbnail to visible.
    resetVideoItem = function() {
        if (typeof _currentVideoItem != "undefined") {
            $(_currentVideoItem).children().show();
            $("#videoPlayer").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;
    }

    // Test the interval for the menu cycling
    resetInterval = function() {
        if (typeof interval !== "undefined") { clearInterval(interval); }
        if($(".featured-player-navigation").length > 0){
        	interval = setInterval("slideSwitch()", 5000);
        }
    }

	if($(".featured-player-navigation").length > 0){
  	 	// Display First Item, then set interval between items.
		interval = setInterval( "slideSwitch()", 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").eq(0).addClass("active");
	$("#initContainer .itemContainer").eq(0).addClass("active");
	
    $(".menuItem").hover(function() { $(this).addClass("menuItemOver"); }, function() { $(this).removeClass("menuItemOver"); });

});
/*
 * You thought it was raining
 * But it was just my code making you cry
 * -Blaine
 */
function slideSwitch() {

    var $active = $('#initContainer DIV.active');
	var $menuActive = $("ul.featured-player-navigation li.active");
	
    if ( $active.length == 0 ) $active = $('#initContainer DIV.itemContainer:last');
    
    var $next =  $active.next().length ? $active.next()
        : $('#initContainer DIV.itemContainer:first');

	var $menuNext = $menuActive.next().length ? $menuActive.next()
		: $('ul.featured-player-navigation li.menuItem:first');

    $active.addClass('last-active');
	$menuActive.removeClass('active');
	$menuNext.addClass('active');
	
    $next.css({opacity: 0.0,position:"absolute",top:0,left:0})
        .addClass('active')
        .animate({opacity: 1.0}, 800, function() {
        	$active.removeClass('active last-active');
        	$(this).css("position","relative");
        });
        
}

