/**
 * When the open event is called, this function will be used to 'open'
 * the overlay, container and data portions of the modal dialog.
 *
 * onOpen callbacks need to handle 'opening' the overlay, container
 * and data.
 */
function modalOpen (dialog) {		
	dialog.overlay.fadeIn('fast', function () {
		$("#simplemodal-overlay").css("height",$(document).height()); //fix for IE 'no' height issue
		dialog.container.fadeIn('slow', function () {
			dialog.data.hide().fadeIn('fast');	 
		});
	});
	
	// add click event for WSS reporting on clicking close box
	$("a.modalCloseImg").click(function(){_hbLink("lid=VIDHD_Player_Close", "lpos=VIDHD_Player_Close_X");});
}
/**
 * After the dialog is show, this callback will bind some effects
 * to the data when the 'button' button is clicked.
 *
 * This callback is completely user based; SimpleModal does not have
 * a matching function.
 */
function modalShow (dialog) {
	dialog.data.find('input.animate').one('click', function () {
		dialog.data.slideUp('slow', function () {
			dialog.data.slideDown('slow');
		});
	});
}
/**
 * When the close event is called, this function will be used to 'close'
 * the overlay, container and data portions of the modal dialog.
 *
 * The SimpleModal close function will still perform some actions that
 * don't need to be handled here.
 *
 * onClose callbacks need to handle 'closing' the overlay, container
 * data and iframe.
 */
function modalClose (dialog) {
	dialog.data.fadeOut('fast', function () {
		dialog.container.fadeOut('fast', function () {
			dialog.overlay.fadeOut('fast', function () {
				$.modal.close();
				revealAds();
			});
		});
	});
	
	jsHDVideo.endPlayback();				
} 

/**
 * Temporarily disable ads
 */ 
function hideAds() {
	embeds = document.getElementsByTagName('iframe');
	for(i = 0; i < embeds.length; i++) {
	    embeds[i].style.visibility = 'hidden';
	}
	$(".AdUnit").css("visibility","hidden");
}

function revealAds() {
	for(i = 0; i < embeds.length; i++) {
	    embeds[i].style.visibility = 'visible';
	}
	$(".AdUnit").css("visibility","visible");
}

/**
 * When the use clicks on a movie in the browse section this function will be invoked.
 * It will extract all relevant info it need from the html tags that makeup the movie 
 * info on the page; it will update the dialog then it will play the movie.
 */
function hdsetup(thisEl)
{
	var obj = parseRelString($(thisEl).attr("rel"));
	
	// get the movie title
	var title = $(thisEl).children("span").children("span").children("strong").text();
	
	// need to add the title (from thePlatform) to the obj object just in case there is no movieTitle (the Fandango movie title)
	obj["title"] = title;
	
	// add info to the popup dialog
	$("#player_content h2").text(title);
	setMovieDetailsLink(obj);
	
	// start playing the movie
	jsHDVideo.play(obj);
}

/**		SECTION I    ********************************************************************
 * NOTE: Because the info on the HTML page is layed out slightly differently for each of
 * layout areas displaying HD movie trailer images, titles and links several functions are
 * needed to extract the info that must be passed to the popup dialog.
 *
 * Each of the functions in this section are variations on the hdsetup() function to achieve
 * the same result for the popup dialog 
 */
function hdSetFeaturedImageLink(thisEl)
{
	var obj = parseRelString($(thisEl).attr("rel"));
	
	// extract the title:
	var title = $(thisEl).children("img").attr("alt");
	
	// the title in the image alt attribute has a suffix " HD Trailer" which should be removed
	title = title.replace(" HD Trailer", "");
	
	// need to add the title (from thePlatform) to the obj object just in case there is no movieTitle (the Fandango movie title)
	obj["title"] = title;
	
	trace("title = " + title);
	$("#player_content h2").text(title);
	setMovieDetailsLink(obj);
	
	jsHDVideo.play(obj);
}

function hdSetFeaturedTitleLink(thisEl)
{
	var obj = parseRelString($(thisEl).attr("rel"));
	
	// extract the title:
	var title = $(thisEl).text();
	
	// need to add the title (from thePlatform) to the obj object just in case there is no movieTitle (the Fandango movie title)
	obj["title"] = title;
	
	trace("title = " + title);
	$("#player_content h2").text(title);
	setMovieDetailsLink(obj);
	
	jsHDVideo.play(obj);
}

function hdSetFeaturedPlayable(thisEl)
{
	var obj = parseRelString($(thisEl).attr("rel"));
	
	// extract the title:
	var title = $(thisEl).text();
	
	// need to add the title (from thePlatform) to the obj object just in case there is no movieTitle (the Fandango movie title)
	obj["title"] = title;
	
	trace("title = " + title);
	$("#player_content h2").text(title);
	setMovieDetailsLink(obj);
	
	jsHDVideo.play(obj);
}

/**
 * This function either sets the href to a valid url or sets the style.visibility attribute to hidden
 *
 */
function setMovieDetailsLink(obj)
{
	var details = document.getElementById("goToMovieDetails");
	
	// setup the movie details link
	var detailsUrl = obj["gotoMovie"];
	detailsUrl = detailsUrl.replace(/^\s+|\s+$/g, '');
	
	if (!detailsUrl || detailsUrl == '' || detailsUrl == '#')
	{
		details.style.display = "none";
	}
	else
	{
		details.style.display = "inline";
		details.setAttribute("href", detailsUrl);
	}
	
	// setup the link caption
	// first remove the current text node
	if (details.childNodes.length > 0)
	{
		details.removeChild(details.childNodes[0]);
	}
	
	var caption = "Go to";
	if (obj["movieTitle"])
	{
		caption += " " + obj["movieTitle"] + " ";
	}
	else
	{
		caption += " ";
	}
	caption += "Movie Details";
	
	details.appendChild(document.createTextNode(caption));
}

/**
 * The function extracts values from a string where the values in the string are delimited by '^'
 * It returns an object containing name/value pairs where the names have been predefined by an array
 * called fields (internal to the function) 
 *
 * TT 8771 - 9/10/2009 - rrobinson - added two optional fields: optContentCategory and optPageName to pass the
 * WSS content category and page name values to the HDFandangoPlayer.aspx when a play is triggered
 * by the HDFandangoPuppetTrailers.aspx page.
 *
 * NOTE: since optContentCategory and optPageName are optional they should always be at the end of the
 * array; only optional fields should be appended after them.
 **/
function parseRelString(str)
{
	var result = {};
	
	var arry = str.split("^");
	var fields = ["gotoMovie", "guid", "movieCategory", "movieTitle", "movieID", "optContentCategory", "optPageName"];

	for (var i = 0; i < arry.length; i++)
	{
		result[fields[i]] = arry[i];
	}

	return result;
}
/********************************************************************
 **		END OF SECTION I    
 ********************************************************************/
/**
 * name: removeSortClass
 * description: This function removes the style class value 'ascending' or 'descending'
 * from the class attribute for the element with the given id.
 *
 * param: id	dom element id
 **/
function removeSortClass(id)
{
	if ($(id).hasClass("ascending"))
	{
		$(id).removeClass("ascending");
	}
	else if ($(id).hasClass("descending"))
	{
		$(id).removeClass("descending");
	}	
}

function flipSortClass(id, defaultClass)
{
	if ($(id).hasClass("ascending"))
	{
		$(id).removeClass("ascending");
		$(id).addClass("descending");
	}
	else if ($(id).hasClass("descending"))
	{
		$(id).removeClass("descending");
		$(id).addClass("ascending");
	}
	else
	{
		$(id).addClass(defaultClass);
	}
}

$(document).ready(function() {
	/* modify click behavior of the 2 sort fields */
	$("#HDTrailersContent_SortTitle").click(function(){
		/* remove ascending/descending class */
		removeSortClass("#HDTrailersContent_SortDate");
		flipSortClass("#HDTrailersContent_SortTitle", "ascending");
	});
	
	$("#HDTrailersContent_SortDate").click(function(){
		/* remove ascending/descending class */
		removeSortClass("#HDTrailersContent_SortTitle");
		flipSortClass("#HDTrailersContent_SortDate", "descending");
	});
	
	/* add class playable to items that can trigger the player */    	
	$("a.playable").click(function(e) {
		e.preventDefault(); // keep anchor effect from happening
		
		$('#modalPlayer').modal({
			opacity: (70),
			zIndex: (1000000), 
			onOpen: modalOpen,
			onClose: modalClose, 
			persist: true			
		});
		hideAds();
		hdsetup(this);
	});

	$("#hdFeaturedImageLink").click(function(e) {
		e.preventDefault(); // keep anchor effect from happening
		
		$('#modalPlayer').modal({
			opacity: (70),
			zIndex: (1000000), 
			onOpen: modalOpen,
			onClose: modalClose, 
			persist: true			
		});
		hideAds();
		hdSetFeaturedImageLink(this);
	});
	
	$("#hdFeaturedTitleLink").click(function(e) {
		e.preventDefault(); // keep anchor effect from happening
		
		$('#modalPlayer').modal({
			opacity: (70),
			zIndex: (1000000), 
			onOpen: modalOpen,
			onClose: modalClose, 
			persist: true			
		});
		hideAds();
		hdSetFeaturedTitleLink(this);
	});
	
	$("a.featured_playable").click(function(e){
		e.preventDefault(); // keep anchor effect from happening
		
		$('#modalPlayer').modal({
			opacity: (70),
			zIndex: (1000000), 
			onOpen: modalOpen,
			onClose: modalClose, 
			persist: true			
		});
		hideAds();
		hdSetFeaturedPlayable(this);
	
	});
	
	$(document).keydown(function(e){
		if( e.which == 27 )
			$.modal.close();
	});
	
	$("#other_featured a.image").hover(function() {
		$(this).siblings(".covermask").show();
	}, function() {
		$("#other_featured .covermask").hover(function() {
			$(this).show();
		}, function() {
			$(this).hide();
		});
		$(this).siblings(".covermask").hide();
	});
});

