/*-----------------------------------------------------------------------------
truTV Global Javascript

version:   2.0
author:	elbert oh
email:	 elbert.oh@avenuea-razorfish.com
-----------------------------------------------------------------------------*/				   
$(document).ready(function() {
    // activate carousels	
	initCarousels();
	// calculate showpage secondary nav width
    getSubNavWidth();
	// remove global search text on focus
    removeSrchTxt();
	// fix IE6 support for PNGs, :hover and Guillotine bug
    fixIE(); 
	// main navigation
    initMainNav();
	// add a css class to the first two dl tags
    truExclusivesFix();
	// reapplies the fixIE function on text resize
    textResize();
});

$.fn.hoverClass = function(c) {
    return this.each(function() {
        $(this).hover(
        function() {
            $(this).addClass(c);
        },
        function() {
            $(this).removeClass(c);

        });

    });

};

function getSubNavWidth() {
    var secNavWidth = $('#showNav').width()+5;
    if (secNavWidth < 884) {
        $('#showNav-c').css('width', secNavWidth);

    }

}

function removeSrchTxt() {
    var searchLabel = $('#glbSrch .glbSrchTxtLabel').remove().text();
    $('#glbSrchTxt').addClass('placeholder').val(searchLabel).focus(function() {
        if (this.value == searchLabel) {
            $(this).removeClass('placeholder').val('');

        };

    }).blur(function() {
        if (this.value == '') {
            $(this).addClass('placeholder').val(searchLabel);

        };

    });
    $('#glbSrch').submit(function() {
        if ($('#glbSrchTxt').val() == searchLabel) {
            $('#glbSrchTxt').val('');

        }

    });

}

function eCalendarCarousel_initCallback(carousel) {
    jQuery('.jcarousel-control a').bind('click', 
    function() {
        carousel.scroll(jQuery.jcarousel.intval(jQuery(this).text()));
        return false;

    });

    jQuery('.jcarousel-scroll select').bind('change', 
    function() {
        carousel.options.scroll = jQuery.jcarousel.intval(this.options[this.selectedIndex].value);
        return false;

    });

    jQuery('#eCalendarCarousel-next').bind('click', 
    function() {
        carousel.next();
        return false;

    });

    jQuery('#eCalendarCarousel-prev').bind('click', 
    function() {
        carousel.prev();
        return false;

    });

};

function mustWatchCarousel_initCallback(carousel) {
    jQuery('.jcarousel-control a').bind('click', 
    function() {
        carousel.scroll(jQuery.jcarousel.intval(jQuery(this).text()));
        return false;

    });

    jQuery('.jcarousel-scroll select').bind('change', 
    function() {
        carousel.options.scroll = jQuery.jcarousel.intval(this.options[this.selectedIndex].value);
        return false;

    });

    jQuery('#mustWatchCarousel-next').bind('click', 
    function() {
        carousel.next();
        return false;

    });

    jQuery('#mustWatchCarousel-prev').bind('click', 
    function() {
        carousel.prev();
        return false;

    });

};

function ffCarousel_initCallback(carousel) {
    jQuery('.jcarousel-control a').bind('click', 
    function() {
        carousel.scroll(jQuery.jcarousel.intval(jQuery(this).text()));
        return false;

    });

    jQuery('.jcarousel-scroll select').bind('change', 
    function() {
        carousel.options.scroll = jQuery.jcarousel.intval(this.options[this.selectedIndex].value);
        return false;

    });

    jQuery('#ffCarousel-next').bind('click', 
    function() {
        carousel.next();
        return false;

    });

    jQuery('#ffCarousel-prev').bind('click', 
    function() {
        carousel.prev();
        return false;

    });

};

function initCarousels() {
    $('#eCalendarCarousel').jcarousel({
        scroll: 3,
        initCallback: eCalendarCarousel_initCallback,
        buttonNextHTML: null,
        buttonPrevHTML: null

    });
    $('#mustWatchCarousel').jcarousel({
        scroll: 3,
        initCallback: mustWatchCarousel_initCallback,
        buttonNextHTML: null,
        buttonPrevHTML: null

    });
    $('#ffCarousel').jcarousel({
        scroll: 1,
        initCallback: ffCarousel_initCallback,
        buttonNextHTML: null,
        buttonPrevHTML: null

    });

}

function fixIE() {
    $('#glbSrchTxt, .relatedShows h3, .relatedShows h4, .sideBarAd, #oceanForce #mainContent h3, .submitStories dt, .flash-c a').ifixpng();
    $('#glbSrchSub, .jcarousel-next-horizontal, .jcarousel-next-disabled-horizontal, .jcarousel-prev-disabled-horizontal, .jcarousel-prev-horizontal').hoverClass("roll");
    if ($.browser.msie) {
        $(function() {
            $(".banner .inner-c iframe").css({
                zindex: "20",
                position: "relative",
                zoom: "1"
            });
            $('.gridA dl, .relatedShows dl').append("<dd style='clear: both;'></dd>");
            $('.gridA dl:last, .relatedShows dl:last').addClass("last");

        });

    }

}

function initMainNav() {
    $("#mainNav li").hover(
    function() {
        $("ul", this).fadeIn("fast");
    },
    function() {}
    );
    $("#mainNav li").hoverClass("sfHover");

}

function truExclusivesFix() {
    $('.truExclusives dl:lt(2)').addClass("first");

}

function textResize() {
    $.jqem.bind(function() {
        if ($.jqem.current() > $.jqem.previous()) {
            fixIE();

        } else if ($.jqem.current() < $.jqem.previous()) {
            fixIE();

        }

    });
}