// SYFY
function onOutletEvent(e)
{
	if(e.type == "outletInited")
	{
		loadShareMenuEvents();
		// Set the number of target val calls by the ad engine to 0.
		var embeddedPlayer = Outlet.getOutletExtension("embeddedPlayer");
		if (embeddedPlayer == null) {
			alert("Embedded Player not found.  Please ensure name matches id in video player config.");
			return;
		}
		var adData = { numberOfAdCalls:0};
		embeddedPlayer.updateAdData(adData);
		
	}
}
// site specific javascript
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(){

	$('.module_title:not(#player_title)').drawFont();
	$('.module_title#player_title span.main').drawFont();
	$('.module_title#player_title span.sub').drawFont();
	$('.module_title#player_title').css('visibility','visible');
	$('.module_tabs li a').drawFont();
	$('#find_it .module_content h3').drawFont(function(){adjust_find_it();});
	// $('.comments_header').drawFont();
	$('ul.star_rating').talkPodRate();
	$('#featured_video_module p.featured_video_comments a').talkPodGetNumComments();
	$('#search_results .image_item_comments span').talkPodGetNumComments();
	$('div.tP_commenting').talkPodCommenting();
	$('#featured_video_module p.featured_video_comments a').toggle(
		function(e){
			e.preventDefault();
			$('div.tP_commenting').slideDown('fast',function(){$('div.tP_commenting form').slideDown();});
			$('#featured_video_module p.featured_video_comments a').addClass('open');
		},
		function(e){
			e.preventDefault();
			$('div.tP_commenting').slideUp('fast',function(){$('div.tP_commenting').css('display','none');});
			$('#featured_video_module p.featured_video_comments a').removeClass('open');
		}
		);
	$('form#search_form').submit(function(e){
		if($('input#search_input').val()==''){
			e.preventDefault();
			alert('Please enter a search term');
		}
		else{
			$('form#search_form').attr('action','/search/q/'+escape($('input#search_input').val()));
		}
	});
	set_internal_tabs();
	
}
function adjust_find_it(){
	var heights=[];
	var total_heights=[];
	var total_height=0;
	if($('div#find_it').length){
		$('div#find_it .module_content .find_it_content *').each(function(){
			if($(this).is('h3') || $(this).is('li')){
				var h=$(this).outerHeight();
				heights.push(h);
				total_height+=h;
				total_heights.push(total_height);
			}
			
		});
		var mid=Math.floor(total_height/2);

		var left='';
		var right='';
		var i=0;
		var in_li=false;
		var midpoint=false;
		$('div#find_it .module_content .find_it_content *').each(function(){
			if($(this).is('h3') || $(this).is('li')){
				var prepend='';
				var curr=$(this).html();
				if($(this).is('li')){
					if(!in_li){
						in_li=true;
						prepend='<ul>';
					}
					curr='<li>'+curr+'</li>';
				}
				if($(this).is('h3')){
					if(in_li){
						in_li=false;
						prepend='</ul>';
					}
					curr='<h3>'+curr+'</h3>';

				}
				curr=prepend+curr;
				if(total_heights[i]<mid){
					left+=curr;
				}
				else{
					if(midpoint===false){
						midpoint=true;
						left+=curr;
						if(in_li){
							right="<ul>";
						}
					}
					else{
						right+=curr;
					}
					
				}
				i++;
			}
			
		});
		$('div#find_it .module_content .find_it_content').html('<div id="left_find" class="find_it_column">'+left+'</div><div id="right_find" class="find_it_column">'+right+'</div>');
		$('div#find_it .module_content .find_it_content h3').css('visibility','visible');
		
	}
}
var AT_INTERNAL_PAGE=0;
var AT_INTERNAL_PIXEL=0;
var PAGE_WIDTH=0;
var PAGES_START=[0];
var PIXELS_START=[0];
var INTERNAL_TAB_WIDTHS=[];
function set_internal_tabs(){
	AT_INTERNAL_PAGE=0;
	AT_INTERNAL_PIXEL=0;
	PAGE_WIDTH=0;
	PAGES_START=[0];
	PIXELS_START=[0];
	INTERNAL_TAB_WIDTHS=[];
	if($('#internal_tab_holder li').length){
		$('#internal_tab_holder').css('height',25);
		PAGE_WIDTH=$('#internal_tab_holder').innerWidth()-60;
		if(PAGE_WIDTH<0){
			return;
		}
		var i=0;
		var ww=0;
		var selected=0;
		var selected_pixel=0;
		var tab_total=0;
		$('#internal_tab_holder li').each(function(){
			if($(this).hasClass('ui-tabs-selected')){
				selected=i;
			}

			INTERNAL_TAB_WIDTHS[i]=parseInt($(this).outerWidth());
			ww+=INTERNAL_TAB_WIDTHS[i];
			if(ww>=PAGE_WIDTH){
				if((i-1)>0){
					PAGES_START.push(i-1);
					PIXELS_START.push(tab_total)
					ww=INTERNAL_TAB_WIDTHS[i];
				}
			}
			
			tab_total+=INTERNAL_TAB_WIDTHS[i];
			
			i++;
		});
		if(ww>PAGE_WIDTH){
			if((i-1)>0){
				PAGES_START.push(i-1);
				PIXELS_START.push(tab_total-INTERNAL_TAB_WIDTHS[i-1])
				ww=0;
			}
		}
		if(PAGES_START.length>1){
			if(selected>0){
				for(i=0;i<PAGES_START.length;i++){
					if(selected<PAGES_START[i]){
						break;
					}
				}
				if(i-1>0){
					AT_INTERNAL_PAGE=i-1;
					$('ul.internal_tabs').css('left',-PIXELS_START[AT_INTERNAL_PAGE]);
				}
			}
			
			$('#internal_tab_holder ul.internal_tabs').css('width',tab_total+10);
			$('#internal_tab_holder').append('<a class="go_left">Left</a><a class="go_right">Right</a>');
			$('#internal_tab_holder a.go_left').bind('click',function(e){shift_tabs_left(e);});
			$('#internal_tab_holder a.go_right').bind('click',function(e){shift_tabs_right(e);});
			var arrow_w=$('#internal_tab_holder a.go_left').width();
			$('.internal_tabs').css('margin-left',arrow_w-18);
			set_arrow_status();
		}
	}
}
function shift_tabs_left(e){
	e.preventDefault();
	if(AT_INTERNAL_PAGE>0){
		AT_INTERNAL_PAGE--;
		set_arrow_status();
		$('ul.internal_tabs').animate({left:-PIXELS_START[AT_INTERNAL_PAGE]});
		
	}
}
function shift_tabs_right(e){
	e.preventDefault();
	if(AT_INTERNAL_PAGE<(PAGES_START.length-1)){
		AT_INTERNAL_PAGE++;
		set_arrow_status();
		$('ul.internal_tabs').animate({left:-PIXELS_START[AT_INTERNAL_PAGE]});
		
	}
}
function set_arrow_status(){
	$('#internal_tab_holder a.go_left, #internal_tab_holder a.go_right').css('opacity',1);
	if(AT_INTERNAL_PAGE==0){
		$('#internal_tab_holder a.go_left').css('opacity',0.2);
	}
	if(AT_INTERNAL_PAGE==(PAGES_START.length-1) ){
		$('#internal_tab_holder a.go_right').css('opacity',0.2);
	}
}

// $('.search_module ul.pagination li').live('click',function(e){
// 	e.preventDefault();
// 	$('.search_module div.loading').remove();
// 	var link=$('a',this).attr('href');
// 	if(link){
// 		$('.search_module').append('<div class="loading">Please  ..</div>');
// 		$('.search_module .loading').css('opacity','.5');
// 		$('.search_module div.loading').fadeIn('fast');
// 		$('.search_module').load(link+' .search_module > *',function(){
// 			$('.search_module .module_header h2').drawFont();
// 			$('.search_module #search_results .image_item_comments a').talkPodGetNumComments();
// 			$('.search_module ul.star_rating').talkPodRate();
// 		});
// 	}
// });
function set_hash(link){
	var l='/'+link.replace(/^(http:\/\/)?[^\/]+\//,'');
	var p=document.location.pathname;
	var r=new RegExp('^'+p);
	orig=l;
	l=l.replace(r,'');
	if(orig.length==l.length){
		document.location.hash='//'+l
	}
	else{
		document.location.hash=l;
	}
}
var OM_SHOW=null;
var OM_SECTION=null;
function set_omniture(){
	if(window['s']!==undefined && s && typeof(s)=='object' && s.t!==undefined && typeof(s.t)=='function'){
		///Shows: Ghost Hunters: Deleted Scenes: Season 5: Reveal (1090042)

		var om_page_name=[];
		if(s.pageName){
			om_page_name=s.pageName.split(/\s+:\s+/)
		}
		var om_section=[];
		if(s.prop4){
			om_section=s.prop4.split(/\s+:\s+/);
		}
		var folder='';
		var video_title='';
		var video_id='';
		if($('div#playlist_module ul.module_tabs li.ui-state-active').length){
			folder=$('div#playlist_module ul.module_tabs li.ui-state-active a img').attr('alt');
		}
		if($('div#playlist_module ul.internal_tabs li.ui-state-active').length){
			var subsection=$('div#playlist_module ul.internal_tabs li.ui-state-active a img').attr('alt');
			if(subsection){
				folder=subsection;
			}
		}
		if($('div#playlist_module ul.image_items li.now_playing').length){
			var curr=$('div#playlist_module ul.image_items li.now_playing');
			video_title=$('h3 a',curr).html();
			var vid=$('h3 a',curr).attr('href').replace(/^.*\D(\d+)$/,'$1');
			if(vid.length){
				video_id=vid;
			}
		}
		var section=OM_SECTION;
		if(section==null){
			section=om_page_name[1];
			OM_SECTION=section;
		}
		
		var show=OM_SHOW;
		if(show==null){
			show=om_section[om_section.length-1];
			OM_SHOW=show;
		}
		var pn=[];
		var p4=[];
		s.pageName='Syfy Video: ';
		if(section){
			pn.push(section);
			p4.push(section);
		}
		if(show){
			pn.push(show);
		}
		if(folder){
			pn.push(folder);
		}
		if(video_title){
			pn.push(video_title);
			p4.push(video_title);
		}
		if(folder){
			p4.push(folder);
		}
		s.pageName=pn.join(': ');
		s.prop4=p4.join(': ');
			
		s.t();
	}
}
$('div#playlist_module ul.image_items li.image_item').live('click',function(e){
	if(window.location.pathname==='/'){
		return;
	}
	if(e.which!==undefined && e.which!=1){
		return;
	}
	e.preventDefault();
	GATEWAY_MESSENGER.remove_internal_alertbox('#featured_video_player');
	$('div#playlist_module ul.image_items li').removeClass('now_playing');
	$('div#playlist_module ul.image_items li h2').remove();
	var link=$('a',this).attr('href');

	var video_id=link.replace(/^.*v(\d+)$/,'$1');
	if(window['videoID']!==undefined){
		videoID=""+video_id;
	}
	if(link){
		$(this).addClass('now_playing');
		var header_info=$('h3 a',this).html();
		if($('.image_item_subtitle',this).html()!=''){
			header_info+='<span class="subtitle">: '+$('.image_item_subtitle',this).html()+'</span>';
		}
		$('.image_item_text',this).prepend('<h2>Now Playing</h2>');
		$('div#featured_video_module div.featured_video_info h3').html(header_info);
		$('div#featured_video_module div.featured_video_info p').html($('.image_item_desc',this).html());
		// $.fn.talkPodRate.load_new('div#featured_video_module ul.star_rating',video_id);
		// $.fn.talkPodCommenting.load_new('.tP_commenting',video_id);
		load_new_fb(video_id,this);
		set_internal_tabs();
		// $('#featured_video_module p.featured_video_comments a').attr('item_guid',video_id);
		// $('#featured_video_module p.featured_video_comments a').talkPodGetNumComments();
		if(window['Outlet']!==undefined && Outlet && typeof(Outlet) =='object'){
			Outlet.getOutletExtension('embeddedPlayer').playVideo(video_id);
		}
		set_omniture();
		set_hash(link);
		$('div#loomia_recs div.module_content').loomia({itemGUID:video_id});
	}
});

function load_new_fb(video_id,curr){
	if(typeof(clientParams)==='object'){
		VA_CONTENTURL=$('a',curr).attr('href');
		if(!VA_CONTENTURL.match(/^http/i)){
			VA_CONTENTURL='http://'+window.location.hostname+VA_CONTENTURL;
		}
		VA_ID=video_id;
		VA_TITLE=$('h3 a',curr).html();
		VA_IMG=$('img',curr).attr('src');
		if(clientParams.content!==undefined){
			clientParams.content.contentTitle=VA_ID;
		}
		
		if(clientParams.feed!==undefined){
			if(clientParams.feed.comments!==undefined){
				clientParams.feed.comments.contentType=VA_TITLE;
				clientParams.feed.comments.contentURL=VA_CONTENTURL;
				clientParams.feed.comments.contentImage=VA_IMG;
			}
			if(clientParams.feed.rating!==undefined){
				clientParams.feed.rating.contentType=VA_TITLE;
				clientParams.feed.rating.contentURL=VA_CONTENTURL;
				clientParams.feed.rating.contentImage=VA_IMG;
			}

		}

		reloadFBComponents();
	}
}


$('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){
		$('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_tabs li a').drawFont();
			$('div#playlist_module .module_title:not(#player_title)').drawFont();
			set_internal_tabs();
			set_omniture();
			set_hash(link);
		});
	}
	
});


__setCookie = function(name,value,days,path) {
	if (!document.cookie || !window.location || !window.location.pathname){
		return;
	}
	if (!path){
		path = '/';
	}
	var cookie = name + "=" + value + ";";
	if (days) {
		var myDate=new Date();
		myDate.setTime(myDate.getTime()+(days*24*60*60*1000));
		cookie += " expires=" + myDate.toGMTString() + ";";
	}   
	cookie += " path="+path;
	document.cookie = cookie;
};
__eraseCookie = function(name,path){
	$.fn.entitlement.setCookie(name,'',-1,path);
};
__getCookie = function(name) {
	if (!document.cookie) return "";
	var nameEQ = name + "=";
	var ca = document.cookie.split(";");
	for(var i = 0; i < ca.length; i++) {
		var c = ca[i];
		while (c.charAt(0) == " ") c = c.substring(1, c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return undefined;
};




