/* *********************************** */
/* begin Video Player/Playlist scripts */
/* *********************************** */


var loadedChannels = new Array();
var nowPlayingText = ' - Now Playing';
var nextUpText = ' - Next Up';
var videoStateText; // either Now Playing or Next Up
var selectedPlaylistEntry = {};


$(document).ready(function() {		
  	$.getScript(scriptPath + siteFAJAX); // pre-load Fandango ajax

  	// determine signed in state to enable functions  	
  	if (signedIn() == true) {
  		
  		// enable behaviors
  		enableRatings();
  		enableFavorites();	  		  	
  	} else {  		  		
  		disableRatingsFavorites();
  	}
  	
  	/* global functions to load */
  	
  	// initialize and close all header items
	closeAllHeaders();	
	
	// enable header hovers and clicks
	enablePlaylistHeader();	
  	
  	// focus on nth item	    
  	onloadPlaylistItem(preQueuedVideo); //preQueuedVideo defined in the markup
  	
/*  	This section is used only for testing 
  	// video player expand
  	toggleVideoPlayer("#tvp");
  	
  	// sample test for playlist next
  	$("a#simulatePreRoll").click(function() {
  		simulatePreRoll();
  		return false;
  	});  	
  	
  	$("a#lnv").click(function() {
  		loadNextVideo();	
  		return false;
  	});  	
  	
  	$("a#lad").click(function() {
  		toggleAdvertisement();	
  		return false;
  	});  
  	// end test
*/	

});

/* 
	TESTING: check for signed in 
*/
function signedIn() {		
	return signInState;
}

/*
    Check to see if this information has already been loaded
*/
function hasBeenLoaded(elementNumber)
{
    // First, let's see how many ajax calls were made for this item (favorites, rating)
    var beenLoaded = Boolean(loadedElements[elementNumber]);
    loadedElements[elementNumber] = true;
    return beenLoaded;
}

/*
	function to load specific item in playlist - starts at 1
*/
function onloadPlaylistItem(plistId) {
	$("div.header").slice(plistId-1,plistId).click();  
}

/* 
	hide all and reinitialize the state
*/
function closeAllHeaders() {	
	$('div#scrollable>div.header').removeClass('selected');
	$('div#scrollable>div.header .plist_thumb').show();
	$('div#scrollable>div.header').find(".subheader span").text("");
	$('div#scrollable>div.list_item').hide();	
}

/*
	work around for IE pseudo class
*/
function addHeaderHover() {
	$('div#scrollable>div.header').hover(function() {
  		if ($(this).hasClass('selected')) {
  			// do nothing
  		} else {
  			$(this).toggleClass('hover');
  		}
  	}, function() {
  		$(this).removeClass('hover');
  	});
  	
  	// click handling for playlist item  	
  	$('div.actions').click(function(ev) {
  		var $target = $(ev.target);
  		
  		if ($target.is("a.add, a.subtract")) {
  			if ($(this).siblings('.details').css('display') =='block') {
	  			$(this).siblings('.details').slideUp("fast");
	  			$($target).removeClass('subtract');
	  			// add attribute name="&lid=VID_Playlist&lpos=VID_PL_DetExpand" (TT6371)
				$($target).attr('name', '&lid=VID_Playlist&lpos=VID_PL_DetExpand');
	  		} else {
	  			$('.list_item .details').hide();
		  		$(this).siblings('.details').slideToggle("fast");
		  		$($target).addClass('subtract');
	  			// add attribute name="&lid=VID_Playlist&lpos=VID_PL_DetShrink"  (TT6371)
				$($target).attr('name', '&lid=VID_Playlist&lpos=VID_PL_DetShrink');
	  		}
  			return false;
  		} else {  			
			// do nothing and allow link
  			// add attribute name="&lid=VID_Playlist&lpos=VID_PL_DetShrink"  (TT6371)
			$($target).attr('name', '&lid=VID_Playlist&lpos=VID_PL_DetShrink');
   			return true;	  	
  		}
  	});
}

/* 
	add click behavior to header area
*/
function enablePlaylistHeader() {	
	addHeaderHover();	
	
  	$('div#scrollable>div.header').click(function(ev) {  		
  		if ($(this).hasClass("selected")) {
  			// do nothing 
  		} else {  		
	  		// if already visible, hide the list and unselect
			if ($('div.list_item'+'#'+$(this).attr("rel")).css('display') == 'block') {
				$('div.list_item'+'#'+$(this).attr("rel")).hide("fast");
				$(this).removeClass('selected').removeClass('hover');
			} else {				
			// else close all the other headers, move (and load)					
				closeAllHeaders(this);				
				itemFocus($(this));											
			}
			
			// load the video clip
			playVideo($(this).attr("rel") - 1);
		}
  	});
}

/* 
	disabled Ratings / Favorites - prompt to login
	pass url for sign in link
*/
function disableRatingsFavorites(url) {
	/*
	$("#videoplayer .rating_app li div a, #videoplayer #actions li.rating_text").hover(function() {	
		$("#videoplayer #actions li.rating_text").html("<a href='"+url+"'>Sign In</a> To Rate");
	}, function() {			
		$("#videoplayer #actions li.rating_text").text("Rate This Video");
	});
	
	$(".list_item .rating li div a, .list_item .rating h5").hover(function() {		
		$(this).parents(".rating").find("h5").html("<a href='"+url+"'>Sign In</a> To Rate");
	}, function() {
		$(this).parents(".rating").find("h5").text("Rate This Video:");
	});
	*/
	
	// disable clicking
	$("#videoplayer .rating_app li div a, .list_item .rating li div a, #link_addToFavorites, #fs_addToFavorites").click(function() {
		return false;
	});
}

/* 
	enable Ratings App to change descriptions and to be clickable.
*/
function enableRatings() {	
	$(".rating_app li div").hover(function() {				
		$(this).find("a").addClass("hover");
		$(this).parent().parent().siblings("h5").children("span").text($(this).children("a").attr("title"));
		$("#videoplayer").find("li.rating_text span").text($(this).children("a").attr("title"));		
		
	}, function() {				
		// on mouseOut check if anything has been selected			
		var anchorGroup = $(this).parent().parent().children();		
		
		if ($(anchorGroup).find("a").hasClass("selected")) {			
			// if something in the group is selected, change the title to that selection			
			$(this).parent().parent().siblings("h5").children("span").text($(anchorGroup).find("a.selected").attr("title"));	
			$("#videoplayer").find("li.rating_text span").text($(anchorGroup).find("a.selected").attr("title"));
		} else {			
			// else, leave it blank
			$(this).parent().parent().siblings("h5").children("span").text("");
			$("#videoplayer").find("li.rating_text span").text($("#videoplayer").find("li.rating_text").attr("title"));
		}	
		
		$(this).find("a").removeClass("hover");	
	});
	
		// video player rating app
	  	$(".rating_app div a").click(function() {
	
	  		if ($(this).hasClass("selected")) {
	    		// do nothing
	    		return false;	
	    	} else {  		
	    		// remove selections
	  			$(this).parents("ul.rating_app").find("div > a").removeClass("selected");
	  			// select
	  			$(this).toggleClass("selected");  			
	  			
	  			// find the div that's selected
	  			var ctest = $(this).parent().attr("class"); 
	  			var selectedVideo = $("#scrollable > div.selected").attr("rel");		
	  			$("div#"+selectedVideo+" .rating_app div a").removeClass("selected");
	  			$("div#"+selectedVideo+" .rating_app div."+ctest+" a").toggleClass("selected");
	  			$("div#"+selectedVideo).find(".rating h5 span").text($(this).attr("title"));
	  			
	  			// pass to the video player
	  			passPlaylistRating(this, selectedVideo);
	  			return false;
			}		
	  	}); 
}

/*
	enable Favorites to be clickable
*/
function enableFavorites() {
	$("#link_addToFavorites, #fs_addToFavorites").click(function() {	  				  		
  		// get currently selected video number
  		var relId = $('div#scrollable>div.selected').attr("rel");
  		if ($("#link_addToFavorites, #fs_addToFavorites").hasClass("selected")) {
			$("#"+relId+" input").slice(0).attr("class","");
		} else {
			$("#"+relId+" input").slice(0).attr("class","selected");
		}
  		
  		if ($(this).attr("id") == "link_addToFavorites") {
  			$(this).toggleClass("selected");
  			$("#fs_addToFavorites").toggleClass("selected");
		} else if ($(this).attr("id") == "fs_addToFavorites") {
			$(this).toggleClass("selected");
			$("#link_addToFavorites").toggleClass("selected");	
		}		  		
        
        // We need to see if we have a favorite action first
		var action = $(this).attr("favAction");
		if (action == "")
		{
		    var selected = $(this).attr("class");
		    if (selected == "")
		        action = "add";
		}
		
		if (signedIn() == true)
		{
		    // Let's make a call to update the favorite
	        var newUrl = $(this).attr("ajaxHref") + "&updatefavs=" + action;
	        
	        if (typeof("AjaxRequest") == 'function') {
        		new AjaxRequest(newUrl, null, null).run();
    		} else {
        		$.getScript(scriptPath + siteFAJAX, function() {
            		new AjaxRequest(newUrl, null, null).run();
        		});
    		}	        
	    }
	    
	    ReportToWss(this);
	    
	    if (action == "remove")
	    {
	        $(this).attr("favAction", "add");
	        $(this).attr("name","&lid=VID_AddFavorites&lpos=VID_PL_AddFavorites");
	        //_hbLink("VID_RemoveFavorites", "VID_PL_RemoveFavorites");
	    }
	    else
	    {
	        $(this).attr("favAction", "remove");
	        $(this).attr("name","&lid=VID_RemoveFavorites&lpos=VID_PL_RemoveFavorites");
	        //_hbLink("VID_AddFavorites", "VID_PL_AddFavorites");
	    }
	    
	    
		
  		return false;
	});
}

/*
	function itemFocus(thisEl)
	thisEl = the item that is clicked
*/
function itemFocus(thisEl) {		
	var divOffset = $('#scrollable').offset().top;
	var pOffset = $(thisEl).offset().top;
	var pScroll = pOffset - divOffset;

	$('#scrollable').animate({scrollTop: '+=' + pScroll + 'px'}, 200, function() {
		$('div.list_item'+'#'+$(thisEl).attr("rel")).slideToggle("fast");	
		$(thisEl).addClass('selected');
		$(thisEl).find(".subheader span").text(nowPlayingText);				
	});	
	// hide thumbnail
	$(thisEl).find(".plist_thumb").hide();
	
	// update h1 header
	$("#content h2.playing").text($(thisEl).find(".videoplaylist_item_title").text());	
	
	// load favorites, e-mail and ticketing links 		
	var relId = $(thisEl).attr("rel");
	
	// setup data for sharethis controls and attach the sharethis controls
	createSelectedPlaylistEntry(relId);
	attachShare("ShareThisLink");
	attachShare("ShareThisLinkExpanded");
	
	// load favorite or not
	if ($("#"+relId+" input").slice(0).attr("class") == "selected") {		
		$("#link_addToFavorites, #fs_addToFavorites").removeClass().addClass("selected");
		$("#link_addToFavorites, #fs_addToFavorites").attr("favaction", "remove");
	} else {		
		$("#link_addToFavorites, #fs_addToFavorites").removeClass();
		$("#link_addToFavorites, #fs_addToFavorites").attr("favaction", "add");
	}
	
	// load favorites link
	$("#link_addToFavorites, #fs_addToFavorites").attr("ajaxhref",$("#"+relId+" input").slice(0).attr("ajaxhref"));
	$("#actions li.options a").slice(1,2).attr("ajaxhref",$("#"+relId+" input").slice(0).attr("ajaxhref"));
	
	// load favorites WSS information
	$("#link_addToFavorites, #fs_addToFavorites").attr("name",$("#"+relId+" input").slice(0).attr("name"));
	$("#actions li.options a").slice(1,2).attr("name",$("#"+relId+" input").slice(0).attr("name"));
	
	// load e-mail
	$("#link_eMail").attr("href", "javascript:popWin('" + $("#"+relId+" input").slice(1).attr("name") + "');");
	$("#actions li.options#action_email a").attr("href","javascript:popWin('" + $("#"+relId+" input").slice(1).attr("name") + "');");
	
	// load "sign in" link
	if (signedIn() == false)
	{
	    var signInUrl = $("#"+relId+" .rating h5 a").attr("href");
	    $("#signin").attr("href", signInUrl);
	}
	
	// load tickets 
	if ((typeof($("#"+relId+" input").slice(2).attr("ajaxhref"))=='undefined') ||
        	($("#"+relId+" input").slice(2).attr("ajaxhref").length == 0)) {                    
		hideTicketsLink();
	} else {
		passTicketUrl(thisEl);
	}
	
	getPlaylistRating(thisEl);
	
	passPlayListRatingLinks(thisEl);
}

function hideTicketsLink()
{
    // remove anchor and replace with span
	$("ul#videoplayerlinks_list li").slice(0,1).hide();
	$("ul#actions li#action_tickets").hide();
}

/*
    This function will make an ajax call to see if the buy tickets button should be displayed
*/
function passTicketUrl(thisEl)
{
    if (typeof("AjaxRequest") == 'function') {
        var relId = $(thisEl).attr("rel");
        new AjaxRequest($("#"+relId+" input").slice(2), null, SetTicketVisibility).run();
    } else {
        $.getScript(scriptPath + siteFAJAX, function() {
            var relId = $(thisEl).attr("rel");
            new AjaxRequest($("#"+relId+" input").slice(2), null, SetTicketVisibility).run();
        });
    }
}

function createAjaxRequest(obj, preRequest, callback)
{
    if (signedIn() == false)
        return;
    if (typeof("AjaxRequest") == 'function') {    
    	new AjaxRequest(obj, preRequest, callback).run();
    } else {
        $.getScript(scriptPath + siteFAJAX, function() {
        	new AjaxRequest(obj, preRequest, callback).run();	
    	});
    }    		
}

/*
    Callback function used to show and hide the "buy tickets" link as specified by the response
*/
function SetTicketVisibility(sender, status, response)
{
    if (response == "fanalert")
        sender = $(sender).parent().find("input").slice(4,5);
        
    if (response == "none") {
        hideTicketsLink();
    } else {
        var ticketsLink = $("ul#videoplayerlinks_list li").slice(0,1).find("a");
        $(ticketsLink).attr("href", $(sender).attr("href"));
        $(ticketsLink).attr("name", $(sender).attr("name"));
        $(ticketsLink).html($(sender).attr("value"));
        $(ticketsLink).attr("title", $(sender).attr("title"));
        $(ticketsLink).attr("id", $(sender).attr("id"));
    
        var fullScreenTicketsLink = $("#actions li#action_tickets a");
        $(fullScreenTicketsLink).attr("href", $(sender).attr("href"));
        $(fullScreenTicketsLink).attr("name", $(sender).attr("name"));
        $(fullScreenTicketsLink).attr("title", $(sender).attr("title"));
        $(fullScreenTicketsLink).html($(sender).attr("value"));
        $(fullScreenTicketsLink).attr("class", $(sender).attr("class"));
        
        $("ul#videoplayerlinks_list li").slice(0,1).show();
	    $("ul#actions li#action_tickets").show();
    }
}

/*
    passes the rating links over to the video player
*/
function passPlayListRatingLinks(thisEl){
    // get selected header
	if (typeof(thisEl)=='undefined') {
		var thisEl = $('div#scrollable>div.selected');
	}
	
	var indexSelected;
	var cId = $(thisEl).attr("rel");
	var passIndex = $("div#"+cId+" .rating_app div a");
		
	$(passIndex).each(function (i) {
	    i = i+1;
	    var vidPlayerRatingBubble = $("#videoplayer ul.rating_app div.rating_" + i + " a");
	    $(vidPlayerRatingBubble).attr("ajaxhref",$(this).attr("ajaxhref"));
	    $(vidPlayerRatingBubble).attr("href",$(this).attr("href"));
	    
	    // this is strange, for some reason it is evaluating the onclick event instead of copying it over.
	    //$(vidPlayerRatingBubble).attr("onclick",$(this).attr("onclick"));
    });
}

/*
    Make an ajax call to get the rating for this video, then copy it over to the video player
*/
function getPlaylistRating(thisEl){
    // get selected header
	if (typeof(thisEl)=='undefined') {
		var thisEl = $('div#scrollable>div.selected');
	}
	
	var cId = $(thisEl).attr("rel");
	
	// let's get the user's rating for this video first
	if ((signedIn() == true) && (hasBeenLoaded(cId) == false))
	{
	    var getRatingUrl = $("#"+cId+" input").slice(3);
	    
	    if (typeof("AjaxRequest") == 'function') {    
	    	new AjaxRequest(getRatingUrl, null, loadRating).run();
	    } else {
	        $.getScript(scriptPath + siteFAJAX, function() {
	        	new AjaxRequest(getRatingUrl, null, loadRating).run();	
	    	});
	    }   
	}
	else if (signedIn() == true)
	{
	    passPlaylistRating(thisEl, cId);
	}
}

/*
	This function passes the playlist rating to the video player.
*/
function passPlaylistRating(thisEl, cId) {
	var indexSelected;
	var passIndex = $("div#"+cId+" .rating_app div a");
	
	$(passIndex).each(function (i) {
		if (!($(this).hasClass("selected"))) {
			i = i+1;
		} else {
			indexSelected = i;
			return indexSelected;
		}
	});
	
	if (typeof(indexSelected)=='undefined' && signedIn() == true) {
		$("#videoplayer ul.rating_app").find("div > a").removeClass("selected");
		$("#videoplayer").find(".rating_text span").text($("#videoplayer .rating_text").attr("title"));
	} else {
		$("#videoplayer ul.rating_app").find("div > a").removeClass("selected");
		// pass the selection to video player
		$("#videoplayer ul.rating_app").find("div > a:eq("+indexSelected+")").toggleClass("selected");
		
		$("#videoplayer").find(".rating_text span").text($("#videoplayer ul.rating_app").find("div > a.selected").attr("title"));
	}
}

/*
    Callback function used to set the selected rating value
*/
function loadRating(sender, status, response)
{
    $(sender).parent().find("ul.rating_app div.rating_" + response + " a").attr("class", "selected");
    passPlaylistRating(sender, $(sender).parent().prev().attr("rel"));
}

function toggleVideoPlayer(thisEl) {
	$(thisEl).click(function() {
		if ($("#videoplayer").hasClass("large")) {
  			$("#videoplayer").toggleClass("large");
  			$(this).toggleClass("full");		  				
  			$("#videoplayer").children('#actions').hide();  			
  		} else {  			
			$("#videoplayer").toggleClass("large");
			toggleVideoPlayerActions("#videoplayer");				
		}
		return false;
	});
}

function toggleVideoPlayerActions(thisEl) {
	$(thisEl).children('#actions').show();
}

function loadNextVideo() {
	// grab all headers
	var thisSelect = $('div#scrollable > div.header');
	var selectedIndex;
	
	// determine what is currently selected
	$(thisSelect).each(function(i) {
		if ($(this).hasClass("selected")) {
			selectedIndex = i + 1;
			return selectedIndex;
		}
	});
	
	// set item focus to next entry
	closeAllHeaders();	
	itemFocus($('div#scrollable > div.header:eq('+selectedIndex+')'));
}

/*
	covers play area 
*/
function toggleAdvertisement() {	
	$("#ad_announce").slideToggle();
	$("#holder_mask").toggle();
}

/*
	determine what was the last item selected and return item after that (if available)
*/
function loadNextHeader() {		
	var thisSelect = $('div#scrollable > div.header');
	var selectedIndex;		
	
	$(thisSelect).each(function(i) {
		if ($(this).hasClass("selected")) {
			selectedIndex = i + 1;
			return selectedIndex;
		}
	});
	
	if (selectedIndex <= thisSelect.size()) {
		$("#content h2.playing").text("Next Up: " + $("div.header:eq("+selectedIndex+")").find(".videoplaylist_item_title").text());
	} else {
		// don't do anything
	}
}

/*
	determine what is the current video
*/
function loadSelectedHeader() {		
	var thisSelect = $('div#scrollable > div.header');
	var selectedIndex;		
	
	$(thisSelect).each(function(i) {
		if ($(this).hasClass("selected")) {
			selectedIndex = i;
			return selectedIndex;
		}
	});
	
	var text = $("#content h2.playing").text();
	if (text.substring(0,8) == "Next Up:"){
		$("#content h2.playing").text($("div.header:eq("+selectedIndex+")").find(".videoplaylist_item_title").text());
		$("div.header:eq("+selectedIndex+")").find(".subheader span").text(nowPlayingText);
	}else {
		$("#content h2.playing").text("Next Up: " + $("div.header:eq("+selectedIndex+")").find(".videoplaylist_item_title").text());
		$("div.header:eq("+selectedIndex+")").find(".subheader span").text(nextUpText);
	}
}

function simulatePreRoll() {
	toggleAdvertisement();
	loadNextHeader();

	var t=setTimeout("toggleAdvertisement(); loadNextVideo();",3000);
}

/*
 * This function scrapes data from the entry in the page's videoplayerplaylist_container that currently
 * has focus, i.e. the layout that is rendered for the currently playing trailer.
 * This data is saved in a js object that will be used by the sharethis controls on the page.
 *
 * @param relId - string containg the value of the rel attribute which is a (1 based) index of the
 *                the currently selected entry in the playlist
*/
function createSelectedPlaylistEntry(relId)
{
	var idx = Number(relId) - 1;
	
	// selectedPlaylistEntry is a global (yuck!) variable in this script module (see top of module)
	selectedPlaylistEntry = {
		"title": $("div.header:eq("+idx+")").find(".videoplaylist_item_title").text(),
		"icon": $("div.header:eq("+idx+")").find(".plist_thumb img:first").attr("src"),
		"url": $("div.header:eq("+idx+")").find(".subheader a:first").attr("href"),
		"summary": $("div#"+relId).find(".data").html()
	};
}

/*
 * This function attaches the sharethis control to an element on the page. It also populates the
 * control with data from the selected playlist entry.
 *
 * @param elId - string containing the id of the element to attach the sharethis control to
 */
function attachShare(elId)
{
	var el = document.getElementById(elId);
	var obj = SHARETHIS.addEntry(selectedPlaylistEntry, {button:false, onmouseover:false});
	obj.attachButton(el);
}
/* ********************************* */
/* end Video Player/Playlist scripts */
/* ********************************* */




/* ********************** */
/* begin Channels scripts */
/* ********************** */

/*
    click on video channel tabs
*/
$(function () {
	//var subtabContent = $('div#tabContent_container > div'); // all subtabs and subtab content
	var subtabContent = $('div#tabContent_container > div.tabContent'); // all subtabs and subtab content

	$('div#tabContent_container ul.rounded_tabs_dynamic li a').click(function () { // on click of orange tab

        // tab backgrounds
		$('div#tabContent_container ul.rounded_tabs_dynamic li.current').removeClass('current'); // remove class 'current' from all tabs that have a class of 'current'
		$(this).parents('li').addClass('current');// add class 'current' to active subnav list item
        
        // tabs content
        subtabContent.removeClass('current'); // remove class 'current' from tab content divs
        subtabContent.addClass('hidden'); // add class 'hidden' to tab content divs
        subtabContent.filter('#' + $(this).attr('rel')).removeClass('hidden'); // remove class 'hidden' from tab content div
        subtabContent.filter('#' + $(this).attr('rel')).addClass('current'); // add class 'current' to tab content div

        var subtabLinks = subtabContent.filter('#' + $(this).attr('rel')).find('ul.rounded_tabs_dynamic_subnav li'); // current subtab links
        var thisSubtab = $('#' + $(this).attr('rel')).find('div.channellist_container'); // current subtab content

        /* ***************************************************** */
        /* begin uncomment to REMEMBER LAST MOVIE info selection */
        /*
        
        // subtab links
        var hasVisitedSubtab = 'no'; // sets visited to 'no'
        subtabLinks.each(function() { // loops through each subtab links
            if ($(this).hasClass('current')) { hasVisitedSubtab = 'yes'; } // if any subtab link has class of 'current', set visited to 'yes'
        });

        // subtab content divs
        if (hasVisitedSubtab == 'no') { // if tab has not been clicked
            subtabLinks.parent().find('li:eq(0)').addClass('current'); // if no subtab links have class 'current', set class of 1st subtab link to 'current'
            $('div#tabContent_container > div').filter(this.hash).find('div.channellist_container:eq(0)').addClass('current'); // displays first subtab content div
        }
        
        */
        /* end uncomment to REMEMBER LAST MOVIE info selection */
        /* *************************************************** */


        /* *************************************************** */
        /* begin uncomment to FORGET LAST MOVIE info selection */
        /**/
        
        // subtab links
        if (subtabLinks.length != 0) { // if there are any subtabs
            subtabLinks.parent().find('li').removeClass('current'); // remove class 'current' from subtab links
            subtabLinks.parent().find('li:eq(0)').addClass('current'); // set class of 1st subtab link to 'current'
        }

        // subtab content divs
    	thisSubtab.removeClass('current'); // hides subtab content div
    	thisSubtab.filter('div:eq(0)').addClass('current'); // displays first subtab content div

        // default movie info paragraph
    	thisSubtab.children('ul').hide(); // hides all movie info
    	thisSubtab.filter('div:eq(0)').find('ul:first').css({visibility:'visible'}); // shows default movie info paragraph in 1st subnav
    	thisSubtab.filter('div:eq(0)').find('ul:first').show(); // shows default movie info paragraph in 1st subnav
        
        // scrolling thumbnail div
    	$('div.channellist_scroller').animate({scrollTop:0},1); // scrolls thumbnail div to top
        
        /**/
        /* end uncomment to FORGET LAST MOVIE info selection */
        /* ************************************************* */

		return false; // keeps page from jumping to anchor
	});
});

/* 
    click on video channel subtabs
*/
$(function () {
	$('div#tabContent_container div.tabContent ul.rounded_tabs_dynamic_subnav li a').click(function () { // on click of subtab

        // subtab links
        var subtabList = $(this.parentNode);
    	subtabList.siblings('li').removeClass('current'); // removes class 'current' from active subtab link items
        subtabList.addClass('current'); // adds class 'current' to subtab link

        // subtab content divs - displays last movie info
        var subtabContent = $(this.parentNode.parentNode.parentNode).children('div.channellist_container');
    	subtabContent.removeClass('current'); // hide all subtab content of current tab
    	subtabContent.filter('#' + $(this).attr('rel')).addClass('current'); // show subtab content of current tab;

        var subtabContent2 = $('#tabContent_container > div.channellist_container');
    	subtabContent2.css("visibility","visible"); // hide all subtab content of current tab
    	subtabContent2.hide(); // hide all subtab content of current tab
    	subtabContent2.removeClass('current'); // hide all subtab content of current tab

        /* *************************************************** */
        /* begin uncomment to FORGET LAST MOVIE info selection */
        /**/
        
        // subtab content divs - displays default movie info. 
    	$('div.channellist_container').children('ul').hide(); // hides movie info paragraphs
    	subtabContent.filter('#' + $(this).attr('rel')).children('ul').filter(':first').show(); // shows default movie info paragraph

        // scrolling thumbnail div
    	$('div.channellist_scroller').animate({scrollTop:0},1); // scrolls thumbnail div to top
        
        /**/
        /* end uncomment to FORGET LAST MOVIE info selection */
        /* ************************************************* */

		return false; // keeps page from jumping to anchor
	});
});


function initChannelRollovers() { // initializes channel thumbnail rollovers
    /*
        video thumbnail rollover play button div
    */
    $(function() {
        $('div.channellist_scroller a.trailer').hover(function() {
            $(this).parents('ul li').children('.trailerMask').show();
        }, function() {
            $(this).parents('ul li').children('.trailerMask').hide();
        });
    });
    /* keep mask layer open while hovering */
    $(function() {
        $('div.channellist_scroller .trailerMask').hover(function() {
            $(this).show();
        }, function() {
            $(this).hide();
        });
    });

    /*
        video info div
    */
    $(function () {
	    $('div.channellist_container div.channellist_scroller ul a.trailer').mouseover(function () { // mouse over video thumbnail
	        var getThisParentSubtab = $(this).parents('div.channellist_container'); // finds the subnav content li
	        var thumbContainers = getThisParentSubtab.children('ul').not($("li div.trailerMask")); // finds correct movie info li
            var thumbCount = (getThisParentSubtab.find('div.channellist_scroller ul > li').children().index(this)+1)/2-1; // gets number of video thumbs on current this subtab
	        thumbContainers.hide(); // hides all movie info divs within the current subtab
	        thumbContainers.filter('ul:eq('+(thumbCount+1)+')').show(); // shows movie info div
	        thumbContainers.filter('ul:eq('+(thumbCount+1)+')').find('div').show(); // shows movie info div
	    });
    });
}

$(document).ready(function() {
    $('div#tabContent_container > div.tabContent > div.current > ul').filter(':first').show(); // shows default movie info paragraph in 1st subnav
    initChannelRollovers();
});

function LoadChannel(targetDiv, targetUrl, playid, lposSuffix)
{

	var channelAlreadyLoaded = false;	

	for (i=0;i< loadedChannels.length;i++)
	{
		
            if (loadedChannels[i] == playid)
            {
		channelAlreadyLoaded = true;
		break;
	    }
	}

	if (!channelAlreadyLoaded )
	{
	
		loadedChannels[loadedChannels.length] = playid;

	
		$("#" + targetDiv).load(
	    	targetUrl,{},
	    	function() {initChannelRollovers()}
			    
		);
	}
	
	//send wss click info	
	var lpid = "VID_Browse_Tab";
	var lpos = "VID_BC_Tab_" + lposSuffix;

	_hbLink(lpid,lpos);
}

/* ******************** */
/* end Channels scripts */
/* ******************** */

