var flashCarousel, ie, flashVideoPlayer;

function init(){
	var ie = navigator.appName.indexOf("FlashObject") != -1;
	flashCarousel = (ie) ? window['videoPlayer'] : document['videoPlayer'];	
	flashVideoPlayer = (ie) ? window['videoPlayer'] : document['videoPlayer'];	
}

function playVideo( s ) {
	flashVideoPlayer.playVideo( s );
	flashVideoPlayer.playVideo( s );
}

function pauseVideo() {
	flashVideoPlayer.pauseVideo();
}


function resumeVideo() {
	flashVideoPlayer.resumeVideo();
}

function showText( s ) {
	episode.showReadAlongText(s).bind(episode);
}

// gets called when the video is finished playing.
function gotoNextChapter() {
	flashCarousel.nextChapter();
}



episode = {
	settings : {
		videoPathID:"chapter-first-video",
		episodeCarousel:"episode-carousel",
		urlPath:"/?q=node/",
		popupWidth:480,
		popupHeight:280,
		popupTitle:'chapterText',
		centerPopup:false,
		popupSettings:'resizable=1,menubar=0,toolbar=0,personalbar=0,status=0,scrollbars=1'
	},
	posX:0,
	posY:0,
	episodeID:0,
	popupWindow:null,
	init : function(){
		if (hasFlash){
			$("horizontal-tear").removeClassName("hide");
			episode.episodeID = $("chapter-episode-id").innerHTML;
			episode.posX = (screen.width-episode.settings.popupWidth)/2;
			episode.posY = (screen.height-episode.settings.popupHeight)/2;
			episode.loadChapterSWF();
			episode.verticalAlignProductInfo();
			flashCarousel = $("MediaCarousel");
			flashVideoPlayer = $("videoPlayer");
			playVideo($(episode.settings.videoPathID).innerHTML);
			$("channel-logo").setStyle({
				"height":($("channel-logo-image").getHeight())+"px"
			});
		}
	},
	loadChapterSWF : function(){	
		if (hasFlash){
			var numChapters = parseInt($("chapter-count").innerHTML);
			var chapterHeight = 177 + 30*numChapters;
			var height = $("episode-carousel").getHeight() - $("channel-logo-image").getHeight()+17;
			height = chapterHeight>height? chapterHeight:height;
			var chapterSwf = new SWFObject("/sites/all/themes/borders_media/assets/swf/BordersMediaCarousel.swf", "MediaCarousel", "253", height, "9", "#000", "high" , "" );
			chapterSwf.addParam("allowScriptAccess","always");
			chapterSwf.addParam("allowFullScreen","true");
			chapterSwf.addParam("wmode","opaque");
			chapterSwf.addParam("scale","noscale");
			chapterSwf.addParam("salign","top");
			chapterSwf.addVariable("dataSource",escape("/?episode="+episode.episodeID+"&type=xml"));
			chapterSwf.write("ChapterDiv");
			$("ChapterDiv").setStyle({
				"height":(height)+"px"
			});
		}	
	},
	loadMainSWF : function(){
		if (hasFlash){
			var swfObj = new SWFObject("/sites/all/themes/borders_media/assets/swf/BordersMediaPlayerAkamai.swf", "videoPlayer", "482", "290", "9", "#000000", "high" , "" );
			swfObj.addParam("allowScriptAccess","always");
			swfObj.addParam("allowFullScreen","true");
			swfObj.addParam("scale","noscale");
			swfObj.write("FlashDiv");
		} else {
			$("episode-carousel").addClassName("hide");
			$("HomeNoFlashCarousel").removeClassName("hide");
		}
	},
	verticalAlignProductInfo : function(){
		var thumb = $("episode-carousel").getElementsByClassName("product-thumbnail")[0];	
		var descr = $("episode-carousel").getElementsByClassName("product-description")[0];
		diff = thumb.getHeight() - descr.getHeight();
		if (diff>0){
			descr.setStyle({
				"padding-top":Math.round(diff/2)+"px"
			});				
		}
	},
	showReadAlongText : function (chapterID){
		this.popupWindow = window.open(this.settings.urlPath+chapterID,this.settings.popupTitle,'width='+this.settings.popupWidth+',height='+this.settings.popupHeight+','+this.settings.popupSettings);
		if (this.popupWindow){
			this.popupWindow.focus();
		} else {
			alert('Please disable your popup blocker for this site.');
		}
	}
}

window.onload = episode.init;