/** 
 * Load the full playlist
 */
function loadPlayList(requestURL, defaultImage) {
	// read playlist from cookie
	var sPlaylist = readCookie('mediaroom_playlist');
	var nextVideoId = "";
	
	if (sPlaylist != null) {
		var videoArray = sPlaylist.split(",");
		videoArray.pop();  
		
		if (videoArray.length > 0) {
			hideInstructionImage();	
			
			for (i in videoArray) {
				var contentId = videoArray[i];
				
				document.getElementById("playlist").innerHTML = document.getElementById("playlist").innerHTML + "<div id='playlistContent" + contentId + "'></div>";
				
				loadPlaylistDoc(requestURL + "?contentId=" + contentId, "playlistContent" + contentId);
			}
			updatePlaylistNumber();
			
		} else {
			showInstructionImage();				
		}
	} else {
		showInstructionImage();		
	}
	
	updatePlaylistNext();
}

/**
 * Update the next item in the playlist
 */
function updatePlaylistNext() {
	var sPlaylist = readCookie('mediaroom_playlist');
	var nextVideoId = "";
	
	if (sPlaylist != null) {
		var videoArray = sPlaylist.split(",");
		videoArray.pop();  
		
		if (videoArray.length > 0) {		
			for (i in videoArray) {
				var contentId = videoArray[i];
					
				if (window.parent.playFromPlaylist && window.parent.currentPlaylistVideo == contentId) {
					nextVideoId = videoArray[new Number(i) + 1];
				}
				
				if (nextVideoId == contentId) {			
					document.getElementById("playlist-table-" + contentId).className = "playingNext";
					document.getElementById("playlist-next-" + contentId).style.display = "block";
					document.getElementById("playlist-hr-" + contentId).style.display = "none";
				} else {
					document.getElementById("playlist-table-" + contentId).className = "";
					document.getElementById("playlist-next-" + contentId).style.display = "none";
					document.getElementById("playlist-hr-" + contentId).style.display = "block";
				}
			}
		}
		if (nextVideoId == "" && window.parent.playFromPlaylist) {
			contentId = videoArray[0];
			document.getElementById("playlist-table-" + contentId).className = "playingNext";
			document.getElementById("playlist-next-" + contentId).style.display = "block";
			document.getElementById("playlist-hr-" + contentId).style.display = "none";
		}
	}
}

/** 
 * Remove all the items from the playlist
 */
function removeAll() {
	createCookie('mediaroom_playlist', "", 365);
	showInstructionImage();
	document.getElementById("playlist").innerHTML = "";
}

/**
 * Remove an item from the playlist
 */
function removeItem(contentId) {
	// remove from cookie
	var sPlaylist = readCookie('mediaroom_playlist');
	if (sPlaylist.indexOf(contentId + ",") != -1) {
		sPlaylist = sPlaylist.replace(contentId + ",", "");
		createCookie('mediaroom_playlist', sPlaylist, 365);
	}
	
	// hide the div layer
	var playlistDiv = document.getElementById("playlist");
	var childDiv = document.getElementById("playlistContent" + contentId);
	playlistDiv.removeChild(childDiv);
	updatePlaylistNext();
	
	// check playlist length and display instruction image if nothing in playlist
	sPlaylist = readCookie('mediaroom_playlist')
	if (sPlaylist == null || sPlaylist.length == 0) {
		showInstructionImage();
	} else {
		updatePlaylistNumber();
	}
}

/**
 * Add an item to the playlist. Change the "Add to Playlist" text to "View Playlist".  
 * Set up "View Playlist" link so it calls jumpToPlaylist with necessary info (playlist url, 
 * frame to load, image to change for highlighting tab, and img tag name)
 * 
 * @param requestURL the url to request for video content
 * @param contentId the content id of video being added to playlist
 * @param playlistURL url of the playlist page
 * @param playlistFrame frame to load playlist page
 * @param playlistImage the image that shows playlist tab highlighting
 * @param playlistTabName the <img> id to load playlistImage into
 */
function addItem(requestURL, contentId, playlistURL, playlistFrame, playlistImage, playlistTabName, newImage) {
	// add the content id to the cookie	if it is already not in the list
	var sPlaylist = readCookie('mediaroom_playlist'); 
	if (sPlaylist == null) {
		createCookie('mediaroom_playlist', contentId + ",", 365);
	} else {
		if (sPlaylist.indexOf(contentId + ",") == -1) {
			createCookie('mediaroom_playlist', sPlaylist + contentId + ",", 365);
		}
	}

	// change link text to "View Playlist"
	if (document.getElementById("playlistLink-" + contentId)) {
		if (newImage == undefined) {
			document.getElementById("playlistLink-" + contentId).innerHTML = "<a href=\"javascript:jumpToPlaylist('" + playlistURL + "', '" + playlistFrame + "', '" + playlistImage + "', '" + playlistTabName + "');\" style=\"cursor:hand\" class=\"inplaylist\">IN PLAYLIST</a>";	
		} else {
			document.getElementById("playlistLink-" + contentId).innerHTML = "<a href=\"javascript:jumpToPlaylist('" + playlistURL + "', '" + playlistFrame + "', '" + playlistImage + "', '" + playlistTabName + "');\">"
							+ "<img src=\"" + newImage + "\" border=\"0\" /></a>";	
		}
	}

	if (document.getElementById("playlist")) {
		// add the div layer and fill in with content
		var playlistDiv = document.getElementById("playlist");
		
		hideInstructionImage();
		
		playlistDiv.innerHTML = playlistDiv.innerHTML 
			+ "<div id='playlistContent" + contentId + "'></div>";
			
		loadPlaylistDoc(requestURL + "?contentId=" + contentId, "playlistContent" + contentId);
		
		// update number in playlist 	
		updatePlaylistNumber();

		// set indicator to play from the playlist
		if (!window.parent.playFromPlaylist) {
			window.parent.playFromPlaylist = true;	
			window.parent.playFromTodaysTheme = false;
			window.parent.updateTodaysThemeNext();
		}		
			
		// update next to play		
		updatePlaylistNext();
		
	} else if (top.leftFrame.document.getElementById("playlist")) {
		// request was made from sibling frame, tell playlist frame to load the content
		top.leftFrame.addItem(requestURL, contentId, playlistURL, playlistFrame, playlistImage, playlistTabName);
		
	} else {
		// set indicator to play from the playlist
		if (!window.parent.playFromPlaylist) {
			window.parent.playFromPlaylist = true;	
			window.parent.playFromTodaysTheme = false;
			window.parent.updateTodaysThemeNext();
		}			
	
		// switch the tab image so playlist is highlighted
		window.parent.switchImage(playlistTabName, playlistImage);
	
		// load the playlist page in the frame
		window.parent.document.getElementById(playlistFrame).src = playlistURL;
	}	
}

/**
 * Switch frame to playlist and move so playlist is visible
 */
function jumpToPlaylist(playlistURL, playlistFrame, playlistImage, playlistTabName) {
	// switch the tab image so playlist is highlighted
	window.parent.switchImage(playlistTabName, playlistImage);
	
	// load the playlist page in the frame
	window.parent.document.getElementById(playlistFrame).src = playlistURL;
	
	// move so the playlist is visible in the page
	var href = window.parent.location.href.split("#");
	window.parent.location.href = href[0] + "#playlist_area";
}

/** 
 * Move the playlist item down
 */
function moveDown(contentId, requestURL) {
	var sPlaylist = readCookie('mediaroom_playlist');
	var index = 0;
	var newIndex = 0;
	
	// put the playlist into an array
	if (sPlaylist != null) {
		var playlistArray = sPlaylist.split(",");
		playlistArray.pop();  
		
		for (i in playlistArray) {	
			if (playlistArray[i] == contentId) {
				index = i;
			}
		}
	
		// move the video into its new position
		if (index < (playlistArray.length - 1)) {
			newIndex = new Number(index) + 1;
			var tempItem = playlistArray[newIndex];
			playlistArray[newIndex] = playlistArray[index];
			playlistArray[index] = tempItem;
		}
		
		// store the new reordered playlist
		createCookie('mediaroom_playlist', playlistArray.toString() + ",", 365);
	}
	
	// reload the playlist
	document.getElementById("playlist").innerHTML = "";
	loadPlayList(requestURL);
}

/**
 * Move the playlist item up
 */
function moveUp(contentId, requestURL) {
	var sPlaylist = readCookie('mediaroom_playlist');
	var index = 0;
	var newIndex = 0;
	
	// put the playlist into an array
	if (sPlaylist != null) {
		var playlistArray = sPlaylist.split(",");
		playlistArray.pop();  
		
		for (i in playlistArray) {	
			if (playlistArray[i] == contentId) {
				index = i;
			}
		}
	
		// move the video into its new position
		if (index > 0) {
			newIndex = new Number(index) - 1;
			
			var tempItem = playlistArray[newIndex];
			playlistArray[newIndex] = playlistArray[index];
			playlistArray[index] = tempItem;
		}
		
		// store the new reordered playlist
		createCookie('mediaroom_playlist', playlistArray.toString() + ",", 365);
	}
	
	// reload the playlist
	document.getElementById("playlist").innerHTML = "";
	loadPlayList(requestURL);
}

/** 
 * Update number in playlist count
 */
function updatePlaylistNumber() {
	var sPlaylist = readCookie('mediaroom_playlist');
	if (sPlaylist != null) {
		var playlistArray = sPlaylist.split(",");
		playlistArray.pop();  
		
		if (playlistArray.length == 1) {
			document.getElementById("playlistOptions").innerHTML = "1 item in your playlist " 
		} else {
			document.getElementById("playlistOptions").innerHTML = playlistArray.length + " items in your playlist " 
		}

		document.getElementById("playlistOptions").innerHTML = document.getElementById("playlistOptions").innerHTML +
		 	"<a href=\"javascript:removeAll();\" onClick=\"return confirm('Are you sure you want to empty your playlist?');\">(remove all)</a>&nbsp;&nbsp;";
	} else {
		document.getElementById("playlistOptions").style.display = "none";	
	}	
}

/** 
 * Show the instruction image, hide the instruction text and playlist options
 */
function showInstructionImage() {
	document.getElementById("instructionImage").style.display = "block";		
	document.getElementById("instructionText").style.display = "none";
	document.getElementById("playlistOptions").style.display = "none";		
}

/** 
 * Hide the instruction image, show the instruction text and playlist optioins
 */
function hideInstructionImage() {
	document.getElementById("instructionImage").style.display = "none";		
	document.getElementById("instructionText").style.display = "block";
	document.getElementById("playlistOptions").style.display = "block";		
}

/** 
 * HTTP Request call to load page behind the scenes
 */
function loadPlaylistDoc(url, divName) {
    if (window.XMLHttpRequest) {
        // for native XMLHttpRequest object
        req = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        // for IE/Windows ActiveX version
        req = new ActiveXObject("Microsoft.XMLHTTP");
    }
    if (req != null) {
		// make the requests synchronously so req.onreadystatechange is never called, instead call its event after send()
		req.open("POST", url, false);
        req.send(null);	        
        processPlaylistReqChange(req, divName);
    }
}

/** 
 * Handle the HTTP Request response
 */
function processPlaylistReqChange(req, divName) {
    // if req shows "loaded"
    if (req.readyState == 4) {
        // if req "OK"
        if (req.status == 200) {
            if(document.getElementById){	    
			   document.getElementById(divName).innerHTML = req.responseText; 
			} else if(document.all) {       
			   document.all[divName].innerHTML = req.responseText; 
			}
         } else {
            if(document.getElementById){	    
			   document.getElementById(divName).innerHTML = "There was a technical error retrieving the data.  Please try again later."; 
			} else if(document.all) {       
			   document.all[divName].innerHTML = "There was a technical error retrieving the data.  Please try again later."; 
			}
         }
    }
}