// site specific javascript

var ANIM_FADE_RUNNING = false;

function onOutletEvent(e) {
	if(e.type == "outletInited")
	{
		var embeddedPlayer = Outlet.getOutletExtension("embeddedPlayer");
		embeddedPlayer.addEventListener("Player.end", endClipHandler);
		// Set the number of target val calls by the ad engine to 0.

		if (embeddedPlayer == null) {
			alert("Embedded Player not found.  Please ensure name matches id in video player config.");
			return;
		}
	}
}

var continuousPlay = 0;
function endClipHandler() {
	var playNext = 0;
	var objImageItems = $('div.video_list_vertical ul.image_items li');
	var objPagination = $('div.video_list_vertical ul.pagination li');
	if (objImageItems.length) {
		objImageItems.each(function() {
			if (playNext) {
				playNext = 0;				
				continuousPlay = 1;
				loadVideo($(this));
				return false;
			}
			if ($(this).hasClass('selected')) {
				playNext = 1;	
			}
		});
	}
}

function loadVideo(curr) {
        var hr=$('a',curr).attr('href');
        $('div.video_list_vertical ul.image_items li.tooltip_block').removeClass('selected');
       	$(curr).addClass('selected');
        videoID=hr.replace(/.*v(\d+)$/,'$1');
	if(window['Outlet']!==undefined && Outlet && typeof(Outlet) =='object'){
        	Outlet.getOutletExtension('embeddedPlayer').playVideo(videoID);
        }
}


if(!$.browser.safari)
{
	$(document).ready(function(){
		behavior_binder();
	});
}
else
{
	$(window).load(function(){
		behavior_binder();
	});
}


/*
Binds behaviors
** can be re-run whenever there are DOM changes
*/
function behavior_binder(){

    equalHeight($(".movie_info_stack"));
    equalHeight($(".two_column .two_column .module_stacked .module_content"));
	
		$('div.video_list').inlinePager({numItems:5, autoPage: false});
		//$('div.carousel_list').inlinePager({numItems:1,autoPage:7500,completeCallback:function(){alert('DEBUG /Volumes/NBCUXD/scottnath/ushe_portal/_js/site.js @ 27');}});
		$('div.carousel_list').inlinePager({numItems:1,autoPage:4000,buildCompleteCallback:function(){add_carousel_tooltips();}});
		$('div.upcoming_release_blocks').inlinePager({numItems:2, autoPage: false});
		
				
	
	$('#header_search').focus(function(){
		$('#header_search_label').hide();
	});
	if(!$('input#header_search').val()){
		$('#header_search_label').show();
	}
	$("#jquery_tabs").tabs();
	$(".subnav_tabs").tabs();
	$(".module_content_tabs").tabs();
	
	
	
	
	
	// VIDEO LIST VERTICAL / PHOTO GALLERY VERTICAL
	//photo gallery using inline pager for product pages
	$('div#product_photos div.media_list_vertical').inlinePager({slideDirection:'V',numItems:4,
		clickCallback:function(curr,e){
			e.preventDefault();
			if(ANIM_FADE_RUNNING){
				return;
			}
			ANIM_FADE_RUNNING = true;
			var at=$('div#product_photos span.vertical_list_target');
			$(at).css({position:'relative',overflow:'hidden',height:276});
			
			$('img.curr_image',at).css({position:'absolute',left:0,top:0,zIndex:1000,height:276});
			$(at).append('<img class="next_image" src="'+$('img.large_image',$(e.target).parent().parent()).attr('src')+'" />');
			$('img.next_image',at).css({position:'absolute',left:0,top:0,zIndex:1,height:276});
			$('img.curr_image',at).fadeOut(function(){
				$('img.curr_image',at).remove();
				$('img.next_image',at).addClass('curr_image');
				$('img.next_image',at).removeClass('next_image');
				
				ANIM_FADE_RUNNING = false;
			})
		}
	});
	$('div#movies_popular div.media_list_vertical').inlinePager({slideDirection:'V',numItems:4,
		clickCallback:function(curr,e){
			e.preventDefault();
			var hr=$(e.target).parent().parent().attr('href');
			var curr=e.target;
			$('div.video_list_vertical ul.image_items li.tooltip_block').removeClass('selected');
			while($(curr).parent().length && !$(curr).parent().hasClass('tooltip_block')){
				curr=$(curr).parent();
			}
			if($(curr).parent().hasClass('tooltip_block')){
				$(curr).parent().addClass('selected')
			}
			videoID=hr.replace(/.*v(\d+)$/,'$1');
	
			if(window['Outlet']!==undefined && Outlet && typeof(Outlet) =='object'){
				Outlet.getOutletExtension('embeddedPlayer').playVideo(videoID);
			}
			
		}
	});
	// TOOLTIP/SMALL POP-UP WITH CONTENT
	$('.tooltip_block').each(function(){
		// grab content for tooltip popup
		var this_tt_content = $(this).children('.tooltip_content').html();
		if(!this_tt_content){
			this_tt_content = "NO CONTENT FOR TOOLTIP INCLUDED";
		}
		//create the tooltip popup
		$(this).children('.tooltip_popup').qtip({
			show: 'mouseover',
			style: { 
				background: 'transparent',
				border: {
					 width: 0,
					 radius: 0,
					 color: 'transparent'
				  },
				width: 351
			   },
			content: { text: tooltip_start+this_tt_content+tooltip_end,
				prerender: true
				},
			position: {
				corner: {
					target: 'topRight',
					tooltip: 'bottomLeft'
				},
				adjust: { x: -32, y: 20, scroll: false, mouse: false }
			},
			show: { delay: 0, effect: "slide" },
			hide: { when: 'mouseout', fixed: true, effect: "none" },
			api: {
				onRender : function (){ 
					this.elements.content.find('.tooltip_close').click(this.hide);
				}
			}
		
		});
	});
	// TOOLTIP/SMALL POP-UP WITH CONTENT VIDEO POSTER VERSION
	$('.tooltip_block_view_only').each(function(){
		// grab content for tooltip popup
		var this_tt_content = $(this).children('.tooltip_content').html();
		if(!this_tt_content){
			this_tt_content = "NO CONTENT FOR VIDEO POSTER INCLUDED";
		}
		//create the tooltip popup
		$(this).children('.tooltip_popup').qtip({
			show: 'mouseover',
			style: { 
				background: 'transparent',
				border: {
					 width: 0,
					 radius: 0,
					 color: 'transparent'
				  }
			   },
			position: {
				corner: {
					target: 'topMiddle',
					tooltip: 'bottomMiddle'
				},
				adjust: { x: 0, y: 0, scroll: false, mouse: false }
			},
			content: { text: tooltip_start_vid_poster+this_tt_content+tooltip_end_vid_poster,
				prerender: true
				},
			show: { delay: 0, effect: "slide" },
			hide: { when: 'mouseout', fixed: false, effect: "none" }
		
		});
	});
	
	// for the video play list in video hub
	$('div#playlist_module ul.module_tabs li,div#playlist_module ul.internal_tabs li,div#playlist_module ul.pagination li').live('click',function(e){
	
		if(e.which!==undefined && e.which!=1){
			return;
		}
		if($(e.target).hasClass('disabled')){
			return;
		}
		e.preventDefault();
		$('div#playlist_module div.loading').remove();
		var link=$('a',this).attr('href');
		if(link){
			if(link.match(/^http/i)){
				link=link.replace(/^http:\/\/[^\/]+/,'');
			}
			$('div#playlist_module .module_content').prepend('<div class="loading"></div>');
			var w=$('div#playlist_module .module_content').width();
			var h=$('div#playlist_module .module_content').height();
			$('div#playlist_module .module_content .loading').css({width:w,height:h});
			$('div#playlist_module div.loading').fadeIn('slow');
			$('div.module_playlist').load('/video_list'+link+' div#playlist_module', function(responseText, textStatus, req){
				$('div#playlist_module .module_title:not(#player_title)').drawFont();
				$('.module_tabs li a').drawFont();
				$('.internal_tabs li a').drawFont();
				//set_internal_tabs();
				//set_module_tabs();
				set_omniture();
				set_hash(link);
			});
		}
		
	});
	$('img').each(function(){
		if($(this).width() === 80 && $(this).height()===45){
			$(this).wrap("<span class='play_button_wrapper'></span>");
			$(this).parent().prepend("<span class='play_button'></span>");
		}
	});
	fix_mt_pager();
}

// tooltip common html container
var tooltip_start = '<div class="tooltip"><div class="tooltip_top"><div class="tooltip_topleft"></div><div class="tooltip_topright"><div class="tooltip_content"><a href="javascript:;" class="tooltip_close">X</a>';
var tooltip_end = '</div></div></div><div class="tooltip_bottom"><div class="tooltip_bottomleft"></div><div class="tooltip_bottomright"></div></div></div>';

// tooltip common html container - video poster version
var tooltip_start_vid_poster = '<div class="tooltip_view_only">';
var tooltip_end_vid_poster = '</div>';

// tooltip style
//$.fn.qtip.styles.ushe_tooltip = { // Last part is the name of the style
//   background: 'transparent',
//   border: 'none',
//}

function equalHeight(group) {
    var tallest = 0;
    group.each(function() {
        var thisHeight = $(this).height();
        if(thisHeight > tallest) {
            tallest = thisHeight;
        }
    });
    group.height(tallest);
}

function add_carousel_tooltips(){
	
		$('.carousel_list .image_item').each(function(){
			var thisTooltip = $(this).find('.image_item_tooltip_content').html();
			var thisElmNum = $('.carousel_list .image_item').index(this);
			var pagElmNum = thisElmNum+2;// because nth-child starts and 1 AND you need to skip the first 'prev' LI, you add 2 here
			$('div.carousel_list .pagination li:nth-child('+pagElmNum+')').addClass('tooltip_block_view_only');
			$('div.carousel_list .pagination li:nth-child('+pagElmNum+')').find('.pagination_page_num').addClass('tooltip_popup');
			$('div.carousel_list .pagination li:nth-child('+pagElmNum+')').append(thisTooltip);
		});
}

function fix_mt_pager(){
	if($('ul.pagedarchives li.mt_pager').length){
		var pages=[];
		var prev=false;
		var next=false;
		$('ul.pagedarchives li.mt_pager a').each(function(){
			if($(this).attr('rel')!=''){
				if($(this).attr('rel')=='prev'){
					prev=$(this).attr('href');
				}
				else{
					next=$(this).attr('href');
				}
			}
			else{
				if(parseInt($(this).html())){
					pages[parseInt($(this).html())]=$(this).attr('href');
				}
				
			}
			
		});
		$('ul.pagedarchives li.mt_pager b').each(function(){
			pages[parseInt($(this).html())]=false;
		});
		var list='';
		if(prev){
			list+='<li class="pagedarchives_prev"><a href="'+prev+'"><span class="angled_quotes">Previous</span></a></li>';
		}
		for(var i=0;i<pages.length;i++){
		if(pages[i]===false){
					marker=i-1;
					
				}

		}

		for(var i=0;i<pages.length;i++){
			if(pages[i]!==undefined){
				if(pages[i]===false){
					list+='<li class="selected"><a href="javascript:void();">'+i+'</a></li>';
					
				}
				else{
					if(i==pages.length-1){
						list+='<li class="lastNum"><a href="'+pages[i]+'">'+i+'</a></li>';
					}else if(i == marker){
						
						list+='<li class="prevNum"><a href="'+pages[i]+'">'+i+'</a></li>';
					}
					else {
					list+='<li><a href="'+pages[i]+'">'+i+'</a></li>';

					}
				}
			}
		}
		if(next){
			list+='<li class="pagedarchives_next"><a href="'+next+'"><span class="angled_quotes">Next</span></a></li>';
		}
		$('ul.pagedarchives').html(list);
	}
}

var randDARTNumber=0;
function genSetRandDARTNumber()
{
 randDARTNumber = Math.round(Math.random()*1000000000000);
}
genSetRandDARTNumber();

