function toolTip(sSelector, sTip) {

	$(sSelector).live("mouseover",(function(e) {

			$("body").append('<div id="tooltip" class="tip">' + sTip + '</div>');
			$("#tooltip")
				.css("top",(e.pageY + 20) + "px")
				.css("left",(e.pageX + 5) + "px")
	}));

	$(sSelector).live("mouseout",(function(e) {
		$("#tooltip").remove();
	}));
}


function formatDetailCriteriaList(sLabel, sList) {
	if (sList.length > 0){
		return "&" + sLabel + "=" + escape(sList.substring(0, sList.length - 1));
    }else {
		return "";
	}

}

function getSearchDetailTerms() {
var sSearch		= "";
var sCategory	= "";
var sMaterial	= "";
var sDateAdded	= "";
var sFormat		= "";
var sRight		= "";
var sLength		= "";
var sLanguage	= "";
var sSupplier	= "";
var sYear		= "";
var sBrand		= "";
var sCatalog	= "";

	$("#nav .selected1").each(function(index){

		switch ($(this).attr('type'))
		{
			case 'category':
				sCategory += escape($(this).attr('name')) + ",";
				break;
			case 'material':
				sMaterial += $(this).attr('name') + ",";
				break;
			case 'dateAdded':
				sDateAdded += $(this).attr('name') + ",";
				break;
			case 'format':
				sFormat += $(this).attr('name') + ",";
				break;
			case 'right':
				sRight += $(this).attr('name') + ",";
				break;
			case 'length':
				sLength += $(this).attr('name') + ",";
				break;
			case 'language':
				sLanguage += $(this).attr('name') + ",";
				break;
			case 'supplier':
				sSupplier += $(this).attr('name') + ",";
				break;
			case 'year':
				sYear += $(this).attr('name') + ",";
				break;
			case 'brand':
				sBrand += $(this).attr('name') + ",";
				break;
			case 'catalog':
				sCatalog += $(this).attr('name') + ",";
				break;
			default:
				break;
		}
	});

	sCategory	= formatDetailCriteriaList("category",	sCategory);
	sMaterial	= formatDetailCriteriaList("material",	sMaterial);
	sDateAdded	= formatDetailCriteriaList("dateAdded",	sDateAdded);
	sFormat		= formatDetailCriteriaList("format",		sFormat);
	sRight		= formatDetailCriteriaList("right",		sRight);
	sLength		= formatDetailCriteriaList("length",		sLength);
	sLanguage	= formatDetailCriteriaList("language",	sLanguage);
	sSupplier	= formatDetailCriteriaList("supplier",	sSupplier);
	sYear		= formatDetailCriteriaList("year",		sYear);
	sBrand		= formatDetailCriteriaList("brand",		sBrand);
	sCatalog	= formatDetailCriteriaList("catalog",		sCatalog);

	sSearch = sCategory + sMaterial + sDateAdded + sFormat + sRight + sLength + sLanguage + sSupplier + sYear + sBrand + sCatalog;
	//alert(sSearch);
	return sSearch;
}


function toggleTab() {
	  var sSearch=getSearchDetailTerms();
	  var keywordValue=escape(document.getElementById("searchbox").value);
	  var episode_id=escape(document.frmresults.episode_id.value);
	  var series_id=escape(document.frmresults.series_id.value);

//Changes the 'Tab' from 'Results' to 'Playlists' and back again.
	$(".tab").click(function() {
			  if ($(this).hasClass("selected") == false && $(this).attr("id") != "tab_details"){
				$(".tab").removeClass("selected"); //removes the 'selected' class from all tabs
				$(this).addClass("selected"); 

				$("#tooltip").remove();

				if ($(this).attr("id") == "tab_playlists") {
					$("#series").hide();
					$("#episodes").hide();
					$("#images").hide();
					$("#playlists").show();
				}
				if ($(this).attr("id") == "tab_results") {
					if(!isNaN(episode_id) && episode_id.length>1){
						$("#series").hide();
					}else{ 
					    $("#series").show();
					}
					if(!isNaN(series_id) && series_id.length>1){
							$("#episodes").hide();
					}else{
    					$("#episodes").show();
					}
					$("#images").show();
					$("#playlists").hide();
				}
			}
	});

	$(".tab").ready(function() {
	 
	  if(isNaN(keywordValue)&&keywordValue.length>1&&!keywordValue=="Search"){ // returns true if the variable does NOT contain a valid number
	   $("#details").load("details.jsp?search=1&keyword="+keywordValue+sSearch).bind("ajaxComplete", function(){	$("#details").show();	$("#details").unbind("ajaxComplete");});
		  if($("#tab_playlists").hasClass("selected")){
					$("#series").hide();
					$("#episodes").hide();
					$("#images").hide();
					$("#playlists").show();
		   } 
	  }else if(!isNaN(episode_id)||!isNaN(series_id)){
		if(episode_id.length>1)
		{  
			$("#details").load("details.jsp?search=0&episodeID="+episode_id).bind("ajaxComplete", function(){	$("#details").show();	 $("#details").unbind("ajaxComplete");});
			$("#series").hide();
			  if($("#tab_playlists").hasClass("selected"))
			  {
					$("#series").hide();
					$("#episodes").hide();
					$("#images").hide();
					$("#playlists").show();
		
	    	   } 
		 } else if (series_id.length>1)
		 {
			$("#details").load("detailSeries.jsp?search=0&seriesID="+series_id).bind("ajaxComplete", function(){	$("#details").show();	 $("#details").unbind("ajaxComplete");});
			$("#episodes").hide();
			  if($("#tab_playlists").hasClass("selected"))
			  {
					$("#series").hide();
					$("#episodes").hide();
					$("#images").hide();
					$("#playlists").show();
		
	    	   } 
		 } else {
			$("#details").load("details.jsp?search=1&keyword="+keywordValue+sSearch).bind("ajaxComplete", function(){	$("#details").show();	$("#details").unbind("ajaxComplete");});
			if($("#tab_playlists").hasClass("selected")){
					$("#series").hide();
					$("#episodes").hide();
					$("#images").hide();
					$("#playlists").show();
					
					
			
			} 
		 }
	   } 
	});
}		


function toggleSeriesSection() {
//Colapse/Expand the Series section:
var sSelector1 = "div#series > div.level1 > div.left";
var sSelector2 = "div#series > div.allSeriesWrapper";

	$(sSelector1).live("click",(function(e) {
//		alert('toggleSeriesSection()'); //DWR 8/13

		//Close the Episode Section when you open the Series Section:
		if ($(this).hasClass("closed") == true && $('div#episodes  > div.level1  > div.left').hasClass("open") == true ) {
			$('div#episodes > div.episodesWrapper').hide();
			$('div#episodes  > div.level1  > div.left').removeClass("open");
			$('div#episodes  > div.level1  > div.left').addClass("closed");
		}

		if ($(this).hasClass("open") == true ) {
			$(this).parents().find(sSelector2).hide();
			$(this).removeClass("open");
			$(this).addClass("closed");
		} else {
			$(this).parents().find(sSelector2).show();
			$(this).removeClass("closed");
			$(this).addClass("open");
		}	
	}));
}

function toggleEpisodeSection() {
//Colapse/Expand the Episode section:
var sSelector1 = "div#episodes  > div.level1  > div.left";
var sSelector2 = "div#episodes > div.episodesWrapper";

	$(sSelector1).live("click",(function(e) {

		
		//Close the Series Section when you open the Episode Section:
		if ($(this).hasClass("closed") == true && $('div#series > div.level1 > div.left').hasClass("open") == true ) {
			$('div#series > div.allSeriesWrapper').hide();
			$('div#series > div.level1 > div.left').removeClass("open");
			$('div#series > div.level1 > div.left').addClass("closed");
		}
		
		if ($(this).hasClass("open") == true ) {
			$(this).parents().find(sSelector2).hide();
			$(this).removeClass("open");
			$(this).addClass("closed");
		} else {
			$(this).parents().find(sSelector2).show();
			$(this).removeClass("closed");
			$(this).addClass("open");
		}	
	}));
}

function togglePlaylistSection() {
//Colapse/Expand the Playlist section:
var sSelector1 = "div#playlists  > div.level1  > div.left";
var sSelector2 = "div#playlists > div.playlistWrapper";

	$(sSelector1).live("click",(function(e) {
		if ($(this).hasClass("open") == true ) {
			$(this).parents().find(sSelector2).hide();
			$(this).removeClass("open");
			$(this).addClass("closed");
		} else {
			$(this).parents().find(sSelector2).show();
			$(this).removeClass("closed");
			$(this).addClass("open");
		}	
	}));
}

function toggleSeriesSubSection() {
//Show/Hide the "All Episodes" AND "Seasons" sub-sections under a SPECIFIC Series on the Series Display
var sSelector1  = "div#series > div.allSeriesWrapper > div.seriesWrapper > div.level2 > div.left";
var sSelector2 = "div#series > div.allSeriesWrapper > div.seriesWrapper > div.episodeWrapper";

	$(sSelector1).live("click",(function(e) {
//		alert('toggleSeriesSubSection()'); //DWR 8/13

		//Close the Episode Section when you open the Series Section:
		if ($(this).hasClass("closed") == true && $('div#episodes  > div.level1  > div.left').hasClass("open") == true ) {
			$('div#episodes > div.episodesWrapper').hide();
			$('div#episodes  > div.level1  > div.left').removeClass("open");
			$('div#episodes  > div.level1  > div.left').addClass("closed");
		}

		var iSeriesId = $(this).attr('seriesId');

		if ($(this).hasClass("open") == true ) {

			$(sSelector2).hide();
			$(this).removeClass("open");
			$(this).addClass("closed");
		} else {
			if ($(sSelector1).hasClass("open") == true ) {
				$(sSelector1).removeClass("open");
				$(sSelector1).addClass("closed");
				$(sSelector2).hide();	
			}
			$(this).removeClass("closed");
			$(this).addClass("open");

			sSelector2 = "div#series > div.allSeriesWrapper > div.seriesWrapper > div.episodeWrapper[seriesId=" + iSeriesId + "]";
			$(sSelector2).show();

			if ($(this).parent().parent().find("div.episodeWrapper div.level4 > div").hasClass('left') == false)
			{
				$(this).parent().parent().find("div.episodeWrapper div.level4").append('<div class="left loading"><!--comment --></div>');
				$(this).parent().parent().find("div.episodeWrapper div.level4").append('<div class="middle"><!--comment --></div>');
				$(this).parent().parent().find("div.episodeWrapper div.level4").append('<div class="right"><!--comment --></div>');
			}
			$(this).parent().parent().find("div.episodeWrapper div.level4").show();

			if ($(this).parent().parent().find("div.episodeWrapper div.level4 div").hasClass(".loading") == true)
				$(this).parent().parent().find("div.episodeWrapper").load("doSeriesLevelTwo.jsp?seriesId=" + iSeriesId);
		}	
	}));
}


function toggleSeriesAllEpisodesSubSection() {
//Show/Hide the Episodes under a SPECIFIC "All Episodes" listing on the Series Display
var sSelector1  = "div.episode > div.left";
var sSelector2 = "";

	//All Episodes:
	$(sSelector1).live("click",(function(e) {

		var iSeriesId = $(this).attr('seriesId');
		var iPlaylistId = $(this).attr('playlistId');



			if ( iPlaylistId == undefined)
			{
				iPlaylistId = 0;
			}


//alert("toggleSeriesAllEpisodesSubSection() - iPlaylistId: " + iPlaylistId);

		sSelector2 = "div.allEpisodesWrapper[seriesId=" + iSeriesId + "]";

		if ($(this).hasClass("open") == true ) {

			$(sSelector2).hide();
			$(this).removeClass("open");
			$(this).addClass("closed");
		
		} else {

			//Close all other all Seasons:
			if ($("div.season > div.left").hasClass("open") == true) {
				$("div.season > div.left").removeClass("open");			
				$("div.season > div.left").addClass("closed");			
			}
			$("div.seasonWrapper").hide();

			//Close all other all Episodes:
			if ($("div.episode > div.left").hasClass("open") == true) {
				$("div.episode > div.left").removeClass("open");			
				$("div.episode > div.left").addClass("closed");			
			}
			$("div.allEpisodesWrapper").hide();


			if ($(sSelector1).hasClass("open") == true ) {
				$(sSelector1).removeClass("open");
				$(sSelector1).addClass("closed");
				$(sSelector2).hide();	
			}
			$(this).removeClass("closed");
			$(this).addClass("open");

			$(sSelector2).show();

			if (iPlaylistId == 0)
			{
				$(this).parent().parent().parent().find("div.allEpisodesWrapper[seriesId=" + iSeriesId + "]").load("doSeriesEpisodes.jsp?seriesId=" + iSeriesId);
			} else {
				$(this).parent().parent().parent().find("div.allEpisodesWrapper[seriesId=" + iSeriesId + "]").load("doPlaylistSeriesEpisodes.jsp?seriesId=" + iSeriesId + "&playlistId=" + iPlaylistId);
			}

		}	
	}));
}


function toggleSeriesSeasonSubSection() {
//Show/Hide the Episodes under a SPECIFIC "Season" listing on the Series Display
var sSelector1  = "div.season > div.left";
var sSelector2 = ""; //"div#series > div.allSeriesWrapper > div.seriesWrapper > div.episodeWrapper";
var sSelector3 = "";


	//Seasons:
	$(sSelector1).live("click",(function(e) {
//		alert('toggleSeriesSeasonSubSection()'); //DWR 8/13

		var iSeriesId = $(this).attr('seriesId');
		var iSeason = $(this).attr('season');


		var iPlaylistId = $(this).attr('playlistId');



			if ( iPlaylistId == undefined)
			{
				iPlaylistId = 0;
			}

//alert("toggleSeriesSeasonSubSection() - iPlaylistId: " + iPlaylistId);

		sSelector2 = "div.seasonWrapper[seriesId=" + iSeriesId + "]";

		sSelector3 = "div.seasonWrapper[seriesId=" + iSeriesId + "][season=" + iSeason + "]";

		if ($(this).hasClass("open") == true ) {
			$(sSelector3).hide();
			$(this).removeClass("open");
			$(this).addClass("closed");
		} else {
			//Close all Seasons:
			if ($("div.season > div.left").hasClass("open") == true) {
				$("div.season > div.left").removeClass("open");			
				$("div.season > div.left").addClass("closed");			
			}
			$("div.seasonWrapper").hide();

			//Close all Episodes:
			if ($("div.episode > div.left").hasClass("open") == true) {
				$("div.episode > div.left").removeClass("open");			
				$("div.episode > div.left").addClass("closed");			
			}
			$("div.allEpisodesWrapper").hide();

			//Open this season:
			$(this).removeClass("closed");
			$(this).addClass("open");


			if (iPlaylistId == 0)
			{

				$(this).parent().parent().parent().find("div.seasonWrapper[seriesId=" + iSeriesId + "][season=" + iSeason + "]").load("doSeriesSeasonEpisodes.jsp?seriesId=" + iSeriesId + "&season=" + iSeason);

				$(this).parent().parent().parent().find("div.seasonWrapper[seriesId=" + iSeriesId + "][season=" + iSeason + "]").show();
			} else {
				$(this).parent().parent().parent().find("div.seasonWrapper[seriesId=" + iSeriesId + "][season=" + iSeason + "]").load("doPlaylistSeriesSeasonEpisodes.jsp?seriesId=" + iSeriesId + "&season=" + iSeason + "&playlistId=" + iPlaylistId);
				$(this).parent().parent().parent().find("div.seasonWrapper[seriesId=" + iSeriesId + "][season=" + iSeason + "][playlistId=" + iPlaylistId + "]").show();

			}
			


		}	
	}));

}


function togglePlaylistSubSection() {
//Show/Hide the Episodes under a SPECIFIC Playlist on the Playlist Display
var sSelector  = "div#playlists > div.playlistWrapper > div.level2 > div.left";
var sSelector2 = "div#playlists > div.playlistWrapper > div.playlistLevelTwoWrapper";


	$(sSelector).live("click",(function(e) {
//	$(sSelector).click(function(e) {

//		alert('togglePlaylistSubSection()');

		var iPlaylistId = $(this).attr('playlistId');


		if ($(this).hasClass("open") == true ) {
			$(sSelector2).hide();
			$(this).removeClass("open");
			$(this).addClass("closed");
		} else {
			if ($(sSelector).hasClass("open") == true ) {
				$(sSelector).removeClass("open");
				$(sSelector).addClass("closed");
				$(sSelector2).hide();	
			}
			$(this).removeClass("closed");
			$(this).addClass("open");

			if ($(this).parent().parent().find("div.playlistLevelTwoWrapper > div").hasClass('left') == false)
			{
				$(this).parent().parent().find("div.playlistLevelTwoWrapper").replaceWith('<div class="playlistLevelTwoWrapper"><div class="left loading"><!--comment --></div></div>');
				$(this).parent().parent().find("div.playlistLevelTwoWrapper").append('<div class="middle"><!--comment--><!--comment --></div>');
				$(this).parent().parent().find("div.playlistLevelTwoWrapper").append('<div class="right"><!--comment--><!--comment --></div>');
			}
			$(this).parent().parent().find("div.playlistLevelTwoWrapper").show();


			if ($(this).parent().parent().find("div.playlistLevelTwoWrapper div").hasClass(".loading") == true)
				$(this).parent().parent().find("div.playlistLevelTwoWrapper").load("doPlaylistsLevelTwo.jsp?playlistId=" + iPlaylistId);
		}	
	}));
}


function togglePlaylistAllEpisodes() {
//Show/Hide "All Episodes" section under a specific Playlist
var sSelector1  = "div#playlists > div.playlistWrapper > div.playlistLevelTwoWrapper > div.level2 > div.left";
var sSelector2 = "div#playlists > div.playlistWrapper > div.playlistLevelTwoWrapper > div.playlistAllEpisodesWrapper";

$(sSelector1).live("click",(function(e) {


		var iPlaylistId = $(this).attr('playlistId');

//		alert('togglePlaylistAllEpisodes() - iPlaylistId:' + iPlaylistId); //DWR 


		//Hide the other Series sub-sections under this playlist
		var sSelector3  = "div#playlists > div.playlistWrapper > div.playlistLevelTwoWrapper div.playlistAllSeriesWrapper > div.seriesWrapper > div.level2 > div.left";
		var sSelector4 = "div#playlists > div.playlistWrapper > div.playlistLevelTwoWrapper div.playlistAllSeriesWrapper > div.seriesWrapper > div.episodeWrapper";
		if ($(sSelector3).hasClass("open") == true ) {
			$(sSelector3).removeClass("open");
			$(sSelector3).addClass("closed");
			$(sSelector4).hide();	
		}

		if ($(this).hasClass("open") == true ) 
		{
			$(sSelector2).hide();
			$(this).removeClass("open");
			$(this).addClass("closed");
		} else {
			


			if ($(sSelector1).hasClass("open") == true ) 
			{
				$(this).removeClass("open");
				$(this).addClass("closed");
				$(sSelector2).hide();	
			}

			
			$(this).removeClass("closed");
			$(this).addClass("open");

			if ($(this).parent().parent().find("div.playlistAllEpisodesWrapper > div").hasClass('left') == false)
			{
				$(this).parent().parent().find("div.playlistAllEpisodesWrapper").append('<div class="left loading"><!--comment --></div>');
				$(this).parent().parent().find("div.playlistAllEpisodesWrapper").append('<div class="middle"><!--comment --></div>');
				$(this).parent().parent().find("div.playlistAllEpisodesWrapper").append('<div class="right"><!--comment --></div>');
			}
			
			$(this).parent().parent().find("div.playlistAllEpisodesWrapper").show();

			if ($(this).parent().parent().find("div.playlistAllEpisodesWrapper div").hasClass(".loading") == true)
				$(this).parent().parent().find("div.playlistAllEpisodesWrapper").load("doPlaylistEpisodes.jsp?playlistId=" + iPlaylistId);
		
		}	

	}));

}


function togglePlaylistSeriesSubSection() {
//Show/Hide the "All Episodes" AND "Seasons" sub-sections under a SPECIFIC Series on the Playlist Display
var sSelector1  = "div#playlists > div.playlistWrapper > div.playlistLevelTwoWrapper div.playlistAllSeriesWrapper > div.seriesWrapper > div.level2 > div.left";
var sSelector2 = "div#playlists > div.playlistWrapper > div.playlistLevelTwoWrapper div.playlistAllSeriesWrapper > div.seriesWrapper > div.episodeWrapper";



	$(sSelector1).live("click",(function(e) {
//		alert('togglePlaylistSeriesSubSection()'); //DWR 8/13


		var iSeriesId = $(this).attr('seriesId');
		var iPlaylistId = $(this).attr('playlistId');

			if ( iPlaylistId == undefined)
			{
				iPlaylistId = 0;
			}

//alert("togglePlaylistSeriesSubSection - iPlaylistId: " + iPlaylistId);

		//Hide the other All Episodes sub-sections under this playlist
		var sSelector3  = "div#playlists > div.playlistWrapper > div.playlistLevelTwoWrapper > div.level2 > div.left";
		var sSelector4 = "div#playlists > div.playlistWrapper > div.playlistLevelTwoWrapper > div.playlistAllEpisodesWrapper";
		if ($(sSelector3).hasClass("open") == true ) {
			$(sSelector3).removeClass("open");
			$(sSelector3).addClass("closed");
			$(sSelector4).hide();	
		}

		if ($(this).hasClass("open") == true ) {

			$(sSelector2).hide();
			$(this).removeClass("open");
			$(this).addClass("closed");
		} else {
			if ($(sSelector1).hasClass("open") == true ) {
				$(sSelector1).removeClass("open");
				$(sSelector1).addClass("closed");
				$(sSelector2).hide();	
			}
			$(this).removeClass("closed");
			$(this).addClass("open");

			sSelector2 = "div#playlists > div.playlistWrapper > div.playlistLevelTwoWrapper div.playlistAllSeriesWrapper > div.seriesWrapper > div.episodeWrapper[seriesId=" + iSeriesId + "]";

			$(sSelector2).show();

			if ($(this).parent().parent().find("div.episodeWrapper div.level4 > div").hasClass('left') == false)
			{
				$(this).parent().parent().find("div.episodeWrapper div.level4").append('<div class="left loading"><!--comment --></div>');
				$(this).parent().parent().find("div.episodeWrapper div.level4").append('<div class="middle"><!--comment --></div>');
				$(this).parent().parent().find("div.episodeWrapper div.level4").append('<div class="right"><!--comment --></div>');
			}
			$(this).parent().parent().find("div.episodeWrapper div.level4").show();

			if ($(this).parent().parent().find("div.episodeWrapper div.level4 div").hasClass(".loading") == true)
				$(this).parent().parent().find("div.episodeWrapper").load("doPlaylistsLevelThree.jsp?seriesId=" + iSeriesId + "&playlistId=" + iPlaylistId);
		}	
	}));
}

function createNewPlaylist() {


	$("#createNewPlaylist").live("keyup",(function(e) {
		if(e.keyCode == 13) {
			var sVal = URLEncode($(this).attr('value'));
			$('div.newPlaylistWrapper').parent().load("doPlaylistsLevelOne.jsp?playlistId=0&playlistNm=" + sVal);
		}
	}));

	$("a.playlistDelete").live("click",(function(e) {
			var iPlaylistId = URLEncode($(this).attr('playlistId'));
			var iPlayListName= $(this).attr('displayNm');
			// JIRA 72
			var retValPlayList=confirm("Playlist '"+iPlayListName+"' will be deleted. Do you want to continue?");
			if( retValPlayList == false ){
				  return false;
			 }else{
				 $(this).hide(); //DWR 9/18:
				//DWR 9/18: 
				$('div.newPlaylistWrapper').parent().load("doPlaylistsLevelOne.jsp?playlistId=" + iPlaylistId + "&action=deletePlaylist");
				
			 }
	}));


	$("a.icon_add").live("click",(function(e) {
			var iPlaylistId = URLEncode($(this).attr('playlistId'));
			var iEpisodeId = $(this).attr('episodeId');
			var iSeriesId = $(this).attr('seriesId');
			var iSeason = $(this).attr('season');
			var sDisplayNm = $(this).attr('displayNm');


			if ( iEpisodeId == undefined)
				iEpisodeId = 0;
			
			if (iSeriesId == undefined)
				iSeriesId = 0;

			if (iSeason == undefined)
				iSeason = 0;

			if (sDisplayNm == undefined)
				sDisplayNm = "sDisplayNm not defined in a.icon_add.";

			$("#menuWrapper").remove();
			$("body").append("<div id='menuWrapper'><!--comment --></div>");
			$("#menuWrapper").css("visible","none");
			
			//Update the Playlist section of the Detail Pane:		
			var sLoadFile= "doPlaylistMenu.jsp?random=" + new Date().getTime() + "&action=1&playlistId=" + iPlaylistId + "&episodeId=" + iEpisodeId + "&season=" + iSeason + "&seriesId=" + iSeriesId;	
			$("div.playlists").load(sLoadFile);
			alert(sDisplayNm + " has been added to your Playlist");

			sLoadFile = "doPlaylistsLevelOne.jsp?random=" + new Date().getTime();
			$('#playlists').load(sLoadFile);

	}));

	$("a.icon_delete").live("click",(function(e) {
			var iPlaylistId = URLEncode($(this).attr('playlistId'));
			var iEpisodeId = $(this).attr('episodeId');
			var iSeriesId = $(this).attr('seriesId');
			var iSeason = $(this).attr('season');
			var iSeriesName=  $(this).attr('seriesNm');
			var sDisplayNm = $(this).attr('displayNm');
			var playlistFlag=0; // 0 means it is not playlist , 1 means playlist
			
			
			if ( iEpisodeId == undefined)
				iEpisodeId = 0;
			
			if (iSeriesId == undefined){
				iSeriesId = 0;
				playlistFlag=1;
			}

			
			if (sDisplayNm == undefined)
				sDisplayNm = "sDisplayNm not defined in a.icon_delete.";

			if (iSeason == undefined)
				iSeason = 0;

			if (iSeriesName== undefined)
				iSeriesName= "";

			
			if(iSeason>0)
			   sDisplayNm=iSeriesName +" | " + sDisplayNm;	

			// JIRA 72
			if(playlistFlag==0){
			
		        var  retVal=confirm("'"+sDisplayNm+"'  will be deleted. Do you want to continue?");			
			 if( retVal == false ){
			 	return false;
			 }else{
				$("#menuWrapper").remove();
				$("body").append("<div id='menuWrapper'><!--comment --></div>");
				$("#menuWrapper").css("visible","none");
				//Update the Playlist section of the Detail Pane:	
				var sLoadFile= "doPlaylistMenu.jsp?random=" + new Date().getTime() + "&action=2&playlistId=" + iPlaylistId + "&episodeId=" + iEpisodeId + "&season=" + iSeason + "&seriesId=" + iSeriesId;	
				$("div.playlists").load(sLoadFile);
				$("div.episodeWrapper[playlistId=" + iPlaylistId + "][episodeId=" + iEpisodeId + "]").hide(); //DWR 9/18:
				alert(sDisplayNm + " has been removed from your Playlist");
				//DWR 9/18: 
				sLoadFile = "doPlaylistsLevelOne.jsp?random=" + new Date().getTime();
				//DWR 9/18: 
				$('#playlists').load(sLoadFile);
			 }
			}
		
				
			 
	}));

}

function URLEncode(saString) {
	// The Javascript escape and unescape functions do not correspond
	// with what browsers actually do...
	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";

	var plaintext = saString;
	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
		if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
			var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
			    alert( "Unicode Character '" 
                        + ch 
                        + "' cannot be encoded using standard URL encoding.\n" +
				          "(URL encoding only supports 8-bit characters.)\n" +
						  "A space (+) will be substituted." );
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for
	return encoded;
}

function toggleEpisodeSubset(e) {
//Show a different Episode 'bucket' in the Episodes section:
var sSelector1 = "a.toggle_episode[offset]";

	$(sSelector1).live("click",(function(e) {

//		alert('toggleEpisodeSubset()'); //DWR

		//Close the Series Section when you open the Episode Section:
		$('div#series > div.allSeriesWrapper').hide();
		$('div#series > div.level1 > div.left').removeClass("open");
		$('div#series > div.level1 > div.left').addClass("closed");

		var iOffset = $(this).attr('offset');
		$(this).bind("ajaxSend", function(e){
			$("div#episodes > div.episodesWrapper").replaceWith("<div class='episodesWrapper'><div id='loading' class='left loading'><img src='images/loading.gif' alt='Loading data, please wait...'>Loading...</div><!--comment --></div>");
			$(this).unbind("ajaxSend");
		});


		$(this).parents("div#episodes").load("doEpisodes.jsp?offset=" + iOffset + "&searchbox=" + URLEncode($("#searchbox").attr('value')) );
	}));
}

function toggleSeriesSubset(e) {
//Show a different Series 'bucket' in the Series section: 
var sSelector1 = "a.toggle_series[offset]";
var sSelector2 = "div#series > div.allSeriesWrapper > div.seriesWrapper > div.episodeWrapper > div.level4";

	$(sSelector1).live("click",(function(e) {
//		alert('toggleSeriesSubset(e)'); //DWR 8/13

		//Close the Episode Section when you open the Series Section:
		$('div#episodes > div.episodesWrapper').hide();
		$('div#episodes  > div.level1  > div.left').removeClass("open");
		$('div#episodes  > div.level1  > div.left').addClass("closed");

		var iOffset = $(this).attr('offset');		

		$(this).bind("ajaxSend", function(e){
			$("div#series > div.allSeriesWrapper").replaceWith("<div class='allSeriesWrapper'><div id='loading' class='left loading'><img src='images/loading.gif' alt='Loading data, please wait...'>Loading...</div><!--comment --></div>");

			$(this).unbind("ajaxSend");
		});

		//Load the other Series:
		$(this).parents("div#series").load("doSeriesLevelOne.jsp?offset=" + iOffset + "&searchbox=" + URLEncode($("#searchbox").attr('value')) ).bind("ajaxComplete", function(e){
			$(sSelector1).removeClass("open");
			$(sSelector1).addClass("closed");
			$(sSelector2).hide();	
//			$(this).unbind("ajaxComplete");
		});
		
	}));

}




function showDetails() {
//Refresh/Show the Details section (Episode OR Series):

// 1: Episode Details:
var sSelector1 = "a.episode";

	$(sSelector1).live("click",(function(e) {

//alert('showDetails');

		var iEpisodeId = $(this).attr('episodeId');
		var iMaterialsScreenerFlg = $(this).attr('iMaterialsScreenerFlg');



		$("#details").load("details.jsp?episodeID=" + iEpisodeId + "&iMaterialsScreenerFlg=" + iMaterialsScreenerFlg).bind("ajaxComplete", function(){
			$("#details").show();
			$("#details").unbind("ajaxComplete");
		});

	}));


// 2: Series Details:
	sSelector1 = "a.series";

	$(sSelector1).live("click",(function(e) {

//alert('showDetails2');

		var iSeriesId = $(this).attr('seriesId');

		$("#details").load("detailSeries.jsp?seriesID=" + iSeriesId).bind("ajaxComplete", function(){
			$("#details").show();
			$("#details").unbind("ajaxComplete");
		});

	}));

}

function toolTips() {
	// JIRA 71
	toolTip('.icon_new',									"New&nbsp;to&nbsp;Catalog");
	toolTip('a.icon_available:not(:.icon_ghost)',			"Available&nbsp;Now");
	toolTip('a.icon_hd:not(:.icon_ghost )',		"HD");
	toolTip('a.icon_shortform:not(:.icon_ghost)',			"Short&nbsp;Form");
	toolTip('a.icon_vod:not(:.icon_ghost, :#details a)',		"VOD");
	toolTip('a.icon_mobile:not(:.icon_ghost )',	"Mobile");
	toolTip('a.icon_web:not(:.icon_ghost)',					"Web");
	toolTip('a.icon_ad:not(:.icon_ghost)',				"Ad");
	toolTip('a.icon_credits:not(:.icon_ghost)',			"Credits");
	toolTip('a.icon_images:not(:.icon_ghost)',			"Images");
	toolTip('a.icon_logo:not(:.icon_ghost)',			"Logo");
	toolTip('a.icon_lowerthird:not(:.icon_ghost)',		"Lower&nbsp;Third");
	toolTip('a.icon_musiccues:not(:.icon_ghost)',		"Music&nbsp;Cues");
	toolTip('a.icon_onair:not(:.icon_ghost)',			"On-Air&nbsp;Materials");
	toolTip('a.icon_press:not(:.icon_ghost)',			"Press");
	toolTip('a.icon_screener:not(:.icon_ghost)',		"Screener");
	toolTip('a.icon_script:not(:.icon_ghost)',			"Script");
	toolTip('a.icon_synopsis:not(:.icon_ghost)',		"Synopsis");
	toolTip('a.icon_licensed:not(:.icon_ghost)',		"Licenses");
	toolTip('a.icon_publicdomain:not(:.icon_ghost)',	"Public&nbsp;Domain");
	toolTip('a.icon_royaltyfree:not(:.icon_ghost)',		"Royalty&nbsp;Free");		
	toolTip('a.icon_72:not(:.icon_ghost)',				"Low&nbsp;Resolution&nbsp;Image");	
	toolTip('a.icon_300:not(:.icon_ghost)',				"High&nbsp;Resolution&nbsp;Image");	
	toolTip('a.icon_download:not(:.icon_ghost)',		"Download&nbsp;Image");	
	//JIRA 73
	toolTip('a.icon_send:not(:.icon_ghost)',		"Send&nbsp; PlayList");	
	// JIRA 72
	toolTip('a.icon_delete:not(:.icon_ghost,:#details a)',		"Delete");	

 
	playlistMenu('a.playlistItem');
	
}


function playlistMenu(sSelector) {

	$(sSelector).live("mouseover",(function(e) {

		var  sTip = "";
		var iEpisodeId = $(this).attr('episodeId');
		var iSeriesId = $(this).attr('seriesId');
		var iSeason = $(this).attr('season');
		var sY = e.pageY;
		var sX = e.pageX;

		if ( iEpisodeId == undefined)
			iEpisodeId = 0;
		
		if (iSeriesId == undefined)
			iSeriesId = 0;

		if (iSeason == undefined)
			iSeason = 0;

		$("#menuWrapper").remove();
		$("#tip_add").remove();

		$("body").append("<div id='menuWrapper' style='display: none' ><!--comment --></div>");
		
		var sLoadFile = "doPlaylistMenu.jsp?episodeId=" + iEpisodeId + "&season=" + iSeason + "&seriesId=" + iSeriesId;	

		$("#menuWrapper").load(sLoadFile).bind("ajaxComplete", function(e){

			sTip = $("#menuWrapper").html();

			$("body").append(sTip);
			$("#tip_add").remove();

			$("#tip_add")
				.css("top",sY + "px")
				.css("left",sX + "px")	

			$("#tip_add").show();

			$(this).unbind("ajaxComplete");
		});
	}));

	$("body").live("click",(function(e) {
		$("#tip_add").remove();
		$("#tooltip").remove();
	}));

}

// BRIGHTCOVE HELPER FUNCTION:
// NOTE (RT): js/BrightcoveExperiences.js is required.

isPlayerAdded = false;

function addPlayer(VideoRef, playerName, playerDivID, pAutoStart) {


	if(isPlayerAdded == false) {
		isPlayerAdded = true; 
		var params = {};

		params.playerId = "4793323001";
		params.publisherID = "1320149064";
		params.videoId = "ref:" + VideoRef;
		params.playerTag = "AETN International";
		params.autoplay = false;
		params.autoStart = pAutoStart;

		params.width = 377;
		params.height = 319;
		params.lineupId = null;
		params.bgcolor = "#FFFFFF";
		params.preloadBackColor = "#cccccc";
		params.wmode = "opaque";
		params.backgroundColor = "#cccccc";
		params.isVid = null;

		var player = brightcove.createElement("object");
		player.id = playerName;
		var parameter;
		for (var i in params) {
			parameter = brightcove.createElement("param");
			parameter.name = i;
			parameter.value = params[i];
			player.appendChild(parameter);
		}
 
		var playerContainer = document.getElementById(playerDivID);
 
		brightcove.createExperience(player, playerContainer, true);
	}
}
 
function removePlayer(playerName) {
 
	if(isPlayerAdded == true) {
		isPlayerAdded = false;
 
		brightcove.removeExperience(playerName);
	}
 
}

      var GB_ANIMATION = true;




function sendTo(size) {
 $("a.greybox").live("click",(function(e) {
         
          var t = this.title || this.innerHTML || this.href;
          GB_show(t,this.href,500,640); //540 in FF
          return false;

	})); 

}
function cancelSendTo() {

  GB_hide();
}

/* JIRA 77*/
function imageAlert(episode_id,series_id){
	GB_show("AETN International - Images","images.jsp?id="+episode_id+"&series_id="+series_id,500,640); //540 in FF
	return false;
    

	
}


/* JIRA 157*/
function detailLinkAlert(episode_id,series_id){
	GB_show("AETN International","detailInfo.jsp?id="+episode_id+"&series_id="+series_id,500,640); //540 in FF
	return false;
}

function initEvents() {

	toggleSeriesSection();
	toggleEpisodeSection();
	togglePlaylistSection();
	toggleSeriesSubSection();
	toggleSeriesSubset();
	toggleSeriesAllEpisodesSubSection();
	toggleSeriesSeasonSubSection();
	togglePlaylistSubSection();
	togglePlaylistAllEpisodes();
	togglePlaylistSeriesSubSection();
	createNewPlaylist();
	toggleEpisodeSubset();
	showDetails();
	toggleTab();
	toolTips();
	sendTo();

}


$(document).ready(function() {initEvents();});
