
var castBios = new Array();
var currentChar = 0;

$(function() {
	
	// Logo Customization Utils
	
	// Iterate through li's in the Watch Video module. Append Full Episode where needed
	$('.level_one_video #olListing li.vidlist_dehigh ul').each( function() {
		if( $(this).children('li.list-ct').html() == 'Full Episode') {
			$(this).children('li.list-title').prepend('<span id="fullContentFlag">FULL EPISODE</span>');
		}
		// Remove Type column
		$(this).children('li.list-ct').remove();
	});
	// Remove Type column header
	$('ol.vidlist.list-header').children('li.list-ct').remove();
	// Rename Episodes column header
	$('ol.vidlist.list-header').children('li.list-ep').html('Ep');
	// Rename Date column header
	$('ol.vidlist.list-header').children('li.list-date').html('Date Posted');
	
	// Dynamic addition of the play button overlay to the thumbnails in the Episodes module
	$('.level_one_episodes .title1').each( function() {
		if( $(this).find('a img').length != 0 ) {
			link = $(this).children('a').attr('href');
			$(this).append('<a class="imgOverlay playOverlayOff" href="' + link + '"></a>');
		}
	});
	
	// Mouseover behavior to the overlayed play button in Episodes
	$('.level_one_episodes .title1 .imgOverlay').mouseover( function() {
		$(this).addClass('playOverlayOn');
	}).mouseout( function() {
		$(this).removeClass('playOverlayOn');
	});
	
	// Update all flipbook links to include skin then apply fancybox iFrame
	$('.flipbooks .title2 a').each( function() {
		var FlipbookSrc = $(this).attr('href');
		$(this).attr('href', FlipbookSrc+flipbookSkin);
	}).fancybox({
		'iFrame'				:	true,
		'iFrameScrolling'		:	'no',
		'frameWidth'			:	720,
		'frameHeight'			:	540,
		'hideOnContentClick'	:	false,
		'centerOnScroll'		:	true,
		'overlayShow'			:	true,
		'overlayOpacity'		:	0.75,
		'overlayColor'			:	'#000'
	});
	
	// Apply iFrame pop up to video links in Videos and Episodes modules
	/*
	$('div.level_one_video a, div.level_one_episodes a').fancybox({
		'iFrame'				:	true,
		'iFrameScrolling'		:	'no',
		'frameWidth'			:	870,
		'frameHeight'			:	375,
		'hideOnContentClick'	:	false,
		'centerOnScroll'		:	true,
		'overlayShow'			:	true,
		'overlayOpacity'		:	0.75,
		'overlayColor'			:	'#000'
	});
	*/ 
	
	// Add tooltips to Watch Videos | Meet the Cast modules
	MTVN.VidPopup.init();
	castBioToolTip();
	
});

function castBioToolTip() {
	
	$('.mdl_cast li p').each( function() {
		castBios.push( $(this).text() );
		$(this).html( $(this).html().split('-')[0] );
		$(this).parent().attr('popupid', currentChar);
		currentChar++;
	});
	
	$('div.mdl_cast div.title3 a').mouseover( function() {
	
		popUpContent = castBios[$(this).parent().parent().attr('popupid')].split('-');
		popUpContent = '<strong>' + popUpContent[0] + '</strong><br />' + castBios[$(this).parent().parent().attr('popupid')].substr(popUpContent[0].length + 2, castBios[$(this).parent().parent().attr('popupid')].length);
	
		$popUpTitle = $('div#popupCastDynamic div.title1 a');
		$popUpContent = $('div#popupCastDynamic p.deck');
		
		offset = $(this).parent().parent().offset();
		
		$('div#popupCastDynamic div.title1 a').text( $(this).text().trim() );
		$('div#popupCastDynamic p.deck').html( popUpContent );
		$('div#popupCastDynamic').css({ display: 'block', top: (offset.top - ( $('div#popupCastDynamic').height() ) ), left: offset.left });
	}).mouseout( function() {
		$('div#popupCastDynamic').css({ display: 'none' });
	});
	
	if( $('div#popupCastDynamic').length == 0 ) {
		$('body').append('<div id="popupCastDynamic" class="popup_div"><div class="popup_div_body"><div class="body-container"><div class="title1"><a href="#"></a></div><p class="deck"></p></div><div class="clr"><span></span></div></div><div class="popup_div_bottom"></div></div>');
	}
}

// Override MTV.VidPopup from global shows.js
// Enables Custom Tooltip locations

MTVN.VidPopup = new function()
{
	this.init = function(ajaxstatus){
		
		obj = $j('.vidlist_dehigh');

		var i = 1;

		obj.each(function(){			
			var posX = 0;
			var posY = 0;
			var stepback = i-1;
			var triggerParentId = 'vidlist_'+i;
            var trigger = $j('#innerVidlist_'+i);
			var triggerParent = $j('#'+triggerParentId);
			var pagination = triggerParent.attr("pagination");
			var timeout = 50;
			
			var id = 'popup_'+i;
            var inb = $j('#'+inb);	
		
			
			triggerParent.css({cursor: 'pointer'});
			
			var getViews = "";
			if(triggerParent.attr("mainviews")!='')
			{
				getViews = "     <span class='views'>"+triggerParent.attr("mainviews")+"</span>";	
			}
			
			var getMainTitle = triggerParent.attr("maintitle");
			var getImg =  document.getElementById('img_'+i).innerHTML;
			var getScript = "&nbsp;<span id='scriptPopupWidget_"+i+"'></span>";
			var getTitle = "<div class='title1'><a href='#'>"+getImg+getMainTitle+"</a></div>";
			var getTContent = "<p class='deck'>"+triggerParent.attr("maincontent")+"</p>";			
			var getPosted = "<span class='posted'>Posted "+triggerParent.attr("mainposted")+"</span>";
			var getLink = "<p class='usage'>"+getPosted+getViews+"</p>";
			
			var construct = "<div class='popup_div_body'><div>"+getTitle+getTContent+getLink+"</div><div class='clr'>"+getScript+"</div></div><div class='popup_div_bottom' id=\"popUpBottom_"+i+"\"> </div>";				
			dynaPopupShow(id,triggerParentId,construct,'popup_div');
			
			var oldWidgetId = 'FW_Usage_'+stepback;
			var widgetId = $j('#scriptPopupWidget_'+i);	
			var scriptId = $j('#script_'+i);	
			var info = $j('#popup_'+i);
			
			widgetId.html(scriptId.html());
			if(ajaxstatus==true)
			{
				if(triggerParent.attr("mainuri")!='')
				{
					scriptId.remove();
					MTVN.Flux.Widgets.loadEach('#ModuleVideoList_'+i);
				}
			}
		
			i += 1;
			trigger.hover(
			  function (e) 
			  {
				triggerParent.addClass('vidlist_high');				
				var inbOffset  = triggerParent.offset();
				var initPosX = inbOffset.left;
				var initPosY = inbOffset.top;
							
				var scriptInfo = scriptId;
				
				if(scriptInfo!=null)
				{
					if(ajaxstatus!=true)
					{
							widgetId.html(scriptId.html());
					}
					/*posX = parseInt(initPosX) + ((triggerParent.width()-info.width())/2);*/
					posX = parseInt(initPosX) + (triggerParent.width() - (info.width()/2));
					posY = parseInt(initPosY) - info.height();
					
					info.css({
							  left: posX+'px',
							  top: posY+'px',
							  display: 'block'
							 });
				}
			  }, 
			  function (e) {
				triggerParent.removeClass('vidlist_high');
				info.css({
						  display: 'none'
						 });
                return false;
			});
			trigger.click(function () {
				document.location.href = triggerParent.find('ul li.list-title a').attr('href');
			});
			 
		});		
	}
}
