﻿var ie6 = false;
var isHomepage = false;
var controller;
var player;
var hideSky = false;
var speed = 3000;
var promoSliderSpeed = 5000;
var superfootOpen=true;
var showBanner = false;
 
/*////////////////// Hover Intent - for tooltips /////////////////////*/
var hoverConfig = {    
     sensitivity: 4, // number = sensitivity threshold (must be 1 or higher)    
     interval: 100, // number = milliseconds for onMouseOver polling interval    
     over: showToolTip, // function = onMouseOver callback (REQUIRED)    
     timeout: 200, // number = milliseconds delay before onMouseOut    
     out: hideToolTip // function = onMouseOut callback (REQUIRED)    
};
function showToolTip() 
{
    $("#tooltip").html($(this).children("span").html());
    var pos = $(this).offset();
    $("#tooltip").css({left: (pos.left-10), top: (pos.top-30)});
    $("#tooltip").show();    
}
function hideToolTip() 
{
    $("#tooltip").fadeOut(300)
}

/*jQuery Timer plugin v0.1 || Matt Schmidt [http://www.mattptr.net] || Licensed under the BSD License: http://mattptr.net/license/license.txt */
jQuery.timer = function (interval, callback) {
    var tinterval = tinterval || 100;
    if (!callback) return false;
    _timer = function (tinterval, callback) 
    {
	    this.stop = function () {
		    clearInterval(self.id);
	    };	
	    this.internalCallback = function () {
		    callback(self);
	    };	
	    this.reset = function (val) {
		    if (self.id)
			    clearInterval(self.id);
    		
		    var val = val || 100;
		    this.id = setInterval(this.internalCallback, val);
	    };
	    this.tinterval = tinterval;
	    this.id = setInterval(this.internalCallback, this.tinterval);
	    var self = this;
    };
    return new _timer(tinterval, callback);
};
/*////////////////// Document ready //////////////////////////////////*/
jQuery(document).ready(function() 
{
    $("#showCarousel").removeClass("noscript-carousel");
    $(".carousel-prev,.carousel-next").show();
    $(function() {
        $("#carousel").jCarouselLite({
            btnNext: ".carousel-next",
            btnPrev: ".carousel-prev",
            mouseWheel: true,
            visible: 9,
            speed: 300
        });
    });
    $("#showCarousel a").hoverIntent(hoverConfig);
    
    /*//////////// Tune In messages ///////////////*/
    var numTuneIns = $("#tuneIn img").length;
    var tuneInCount = numTuneIns;
    $.timer(speed, function (timer) {
        $("#tuneIn"+tuneInCount).fadeOut(400);
        if(tuneInCount>1) tuneInCount--; 
        else {
            $("#tuneIn img").fadeIn(400);
            tuneInCount=numTuneIns;
        }
        timer.reset(speed);
    });
    
    
    /*////////// Promo image slider //////////////////*/
    $("#mainPromo-controls").show();
    var count = 2;
    var stopped = false;
    $.timer(promoSliderSpeed, function (timer) {
        if(!stopped) {
            $("#mainPromo-controls a").removeClass("on");
            $("#mainPromo-controls"+count).addClass("on");
            
            $("#mainPromo"+count).css({left: "356px"});
            $("#mainPromo"+count).show();
            $("#mainPromo"+count).animate({left: "0px"},500);
            if(count==1)
            {
                $("#mainPromo3").animate({left: "-356px"},500,function() {        
                    $("#"+this.id).hide();
                });
            } else {
                $("#mainPromo"+(count-1)).animate({left: "-356px"},500,function() {        
                    $("#"+this.id).hide();
                });
            }
            
            if(count<3) count++; 
            else {        
                count=1;
            }
            timer.reset(promoSliderSpeed);
        }
    });
    
    /*/////// Promo controls - click /////////*/
    $("#mainPromo-controls a").click(function(){
        var num = Number(this.id.substr(18,19));
        stopped = true;
        
        $("#mainPromo-controls a").removeClass("on");
        $("#mainPromo-controls"+num).addClass("on");
        
        if(((count-1)==num || (count==1 && num==3)) || $('#mainPromo a:animated').size() > 0)
        { 
            stopped = false;
        } 
        else 
        {
            if(count==1) {
                $("#mainPromo3").animate({left: "-356px"},500,function() {
                    $("#"+this.id).hide();
                });
            } else {
                $("#mainPromo"+(count-1)).animate({left: "-356px"},500,function() {        
                    $("#"+this.id).hide();
                });
            }
            $("#mainPromo"+num).hide();
            $("#mainPromo"+num).css({left: "356px"});
            $("#mainPromo"+num).show();
            $("#mainPromo"+num).animate({left: "0px"},500);
            if(num<3) count=num+1;
            else count=1;
        }
        return false;            
    });
    
    isViewportWide();
    
    $("#mainPromo1,#mainPromo2,#mainPromo3").click(function() { 
        trackFeatured("main3promos:"+this.href,"nickelodeonukprod",this);
        return true;
    });
    $("#sixPromos a").click(function() { 
        trackFeatured("sixPromos:"+this.href,"nickelodeonukprod",this);
        return true;
    });
    
    
    
    /*/// super footer ///*/
    var superFooterMaxHeight=517;
    
    if($("#superfooter")) {
        
        //track superfooter clicks:
        $("#superfooter a").click(function(){
            var game = $(this).html();
            trackFeatured("superfooter:"+game,"nickelodeonukprod",this);
            return false;
        });
        
        var t=0;
        var l=30;
        var tallestH=0;
        for(var i=0; i<99; i++){
            if(t>superFooterMaxHeight) { t=0; l+=187; }
            $("#show"+i).css({"top":t+"px"});
            $("#show"+i).css({"left":l+"px"});
            t += $("#show"+i).height();
            if($("#show"+i).height() > tallestH) { tallestH=$("#show"+i).height(); }
        }
        $("span.superfootB").show();
        $("#superfooter").height(tallestH+80)
        $("#superfooter div").css({"position":"absolute","margin-left":"0px"});
        $(".letter").show();
        toggleFooter();
        $(".showSuperfooter").click(function(){ toggleFooter(); })
    }
});

$(window).resize(function(){
    isViewportWide();
});

 
 
function isViewportWide()
{   
    $("#sideAdWrap").hide();
    
    
    if(showBanner) { // special case - show banner (eg stitchup)
        $("#sideAdWrap").show();
        sky_res=1;
        return;
    }
        
    var widthWindow = $(window).width();
    if(ie6 || !isHomepage || hideSky) 
    {
        $("#sideAdWrap").hide();
        return;        
    } 
    else 
    {
        if (widthWindow > 1250 && widthWindow < 1300)
        { 
            $("#nav").css({margin: "0 0 0 73px"});
            $("#contentArea").css({margin: "0 0 0 85px"});
            $("#sideAdWrap").show(); 
        }
        else if (widthWindow > 1300 && widthWindow < 1400)
        {
            $("#nav").css({margin: "0 0 0 108px"});
            $("#contentArea").css({margin: "0 0 0 120px"});
            $("#sideAdWrap").show();
        }
        else if (widthWindow > 1400 && widthWindow < 1550)
        { 
            $("#nav").css({margin: "0 0 0 168px"});
            $("#contentArea").css({margin: "0 0 0 180px"});
            $("#sideAdWrap").show();
        }
        else if (widthWindow > 1550)
        { 
            $("#nav").css({margin: "0 0 0 258px"});
            $("#contentArea").css({margin: "0 0 0 270px"});
            $("#sideAdWrap").show();
        }
        else 
        {
            $("#nav").css({margin: "0 auto"});
            $("#contentArea").css({margin: "0 auto"});
            $("#sideAdWrap").hide();
        }        
    }
}

function onPlayerLoaded(controller)
{
    player = controller.player;
    if(player.getVolume() > 0.5) player.setVolume(0.5);
}

function toggleFooter() 
{
    if(superfootOpen) {
        $("#superfooter").slideUp(200); 
        $(".showSuperfooter").removeClass("open");
        $(".superfootT").html("Show list of all games");
        superfootOpen=false;
    } else {
        $("#superfooter").slideDown(200); 
        $(".showSuperfooter").addClass("open");
        $(".superfootT").html("Hide list of all games");
        superfootOpen=true;
    }
}

/***********************************************
* Disable Text Selection script- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
***********************************************/
function disableSelection(target)
{
    if (typeof target.onselectstart!="undefined") target.onselectstart=function(){return false}
    else if (typeof target.style.MozUserSelect!="undefined") target.style.MozUserSelect="none"
    else target.onmousedown=function(){return false}
}


/***********************************************/
// set zone Override:    
var path = location.pathname.split("/");
var zone = "";
if(path[1]=="shows") 
{
    zone="shows";
}
if(path[1]=="games")
{
    zone="games";    
}
if(path[2]!="" && path[2]!="default.aspx")
{
    zone+="/"+path[2];
}
if(path[3] && path[3]!="" && path[3]!="default.aspx")
{
    var pathsplit = path[3].split(".");
    
    if(pathsplit[1]=="aspx") zone+="/"+pathsplit[0];
    else zone+="/"+path[3];
}
com.mtvi.ads.AdManager.setZoneOverride(zone);