function isIE8() {
	// use JQuery, previously loaded.
	return $.browser.msie && $.browser.version[0] == '8';
}

function myAddListener(obj, evt, handler, captures) {
	if ( document.addEventListener )
		obj.addEventListener(evt, handler, captures);
	else {
		// IE
		obj.attachEvent('on' + evt, handler);
	}
}



function selectResolution(button, width, height) {
	// Disable this for now, go to the page.
	return true;
	
	if (isIE8()) {
		// Has bug with the javascript video player. Use link's href instead.
		return true;
	} else {
		window.selectedResolution.width = width;
		window.selectedResolution.height = height;
		
		$("ul.videosizes a").removeClass("selected");
		button.className = "selected";
		
		playVideo();
		
		return false;
	}
}

function videoEnded() {
	//alert("Video Ended");
	if (window.nextVideoURL)
		document.location.href = window.nextVideoURL + window.selectedResolution.height + "/#videoplayer" ;
}

function playVideo() {
	// Disable this for now, just go to the URL.
	return true;
	
	if (isIE8()) {
		// Has bug with the javascript video player. Use link's href instead.
		return true;
	} else {
	
		width = window.selectedResolution.width;
		height = window.selectedResolution.height;
		videoDiv = $("#video");
		videoObjects = $("object", videoDiv)
		if (videoObjects.length) {
			videoObject = videoObjects[0];
			videoObject.parentNode.removeChild(videoObject);
		}
		
		var playbutton = $("#playbutton");
		if (playbutton.length) {
			playbutton.html = "";
			playbutton[0].parentNode.removeChild(playbutton[0]);
		}	
		
		videoDiv.media( { 
			src:					"videofiles/"+window.showSlug+"-"+window.episodeSlug+"-"+height+".mov",
			width: 					width, 
			height: 				height+16,
			bgColor: 				'black',
			autoplay: 				true, 
			params:					{	qtsrcdontusebrowser:	'true',
										controller:				'true',
										kioskmode:				'true'
									}
		} );
		
		var episodeListWidth = 160;
		var episodeListMargin = 10;
		
		// Keep the video from dropping off the left edge of the window.
		var minLeftMargin = 0 - parseInt($("#content").css("left"));
		
		var containerWidth = parseInt($("#content").css("width"));
		var leftMargin = (containerWidth - (width + episodeListWidth + episodeListMargin))/2;
		if (leftMargin < minLeftMargin)
			leftMargin = minLeftMargin;
		var episodeListLeft = width + episodeListMargin + leftMargin;
		
		$("#videoplayer").css({	"width":width+"px",
						"height":height+"px",
						"margin-left":leftMargin+"px"		});
		$("#episode-list").animate({ "left": episodeListLeft+"px"});
		
		// Update href
		
		
		// Add Listener
		addVideoListeners();
		
		return false;
	}
}

function addVideoListeners() {
	var videoNode = document.getElementById('videoObj');
	if (videoNode)
		myAddListener(videoNode, 'qt_ended', videoEnded, false);
}


$(document).ready(function() {
	var nextURL = $('link[rel="next"]').attr("href");
	if (nextURL)
		window.nextVideoURL = nextURL;
	
	addVideoListeners();
	//$("#video object").listen('qt_ended', '', videoEnded)
});