﻿/* Global window[] object */
var _gmctvoc = {
	swfobjectloading:false,
	jqueryloading:false,
	modernizrloading:false,
	playheadInterval:null,
	videoNetworkState:null
};

/* Main Openchannel 4 constructor */
multichanneltv_openchannel = function(config) {
	
	// Maiun object properties
	this.listenerProxies 						= [];
	this.id 												= this.guid();
	this.JSON              				  = {};
	this.chapters										= {};
	this.currentChapter 						= null;
	this.intelligentVideos					= {};
	this.currentIntelligentVideo		= null;
	this.config 										= config;
	this.type												= null;
	this.loader											= null;
	this.currentPlayheadTime				= -1;
	this.seekOnPlayback							= -1;
	this.html5DOM										= null;
	this.videoDiv										= config.div + this.guid();
	this.uid												= this.config.uid = config.uid ? config.uid : 9999;
	this.disableFlash								= this.config.disableFlash ? true : false;
	this.subtitleLang								= "";
	this.subtitles									= {};
	this.html5SubtitlesDOM					= null;
	this.hasSubtitles								= false;
	this.currentSubtitle						= null;
	this.protocol										= 'https:' == document.location.protocol ? "https" : "http";
	
	// Cloudfront location - should be set to Amazon name is using SSL
	this.cf 												= 'https:' == document.location.protocol ? 
																				'https://d17d9oit8zaeus.cloudfront.net' : 
																				'http://cdn3.multichanneltv.com'
	this.playStatus									= "paused"; // Set to pause until video is started.

	// Custom handlers
	this.customOnChapterChange			= null;
	this.onPause										= null;
	this.onPlay											= null;
	this.onComplete									= null;
	this.onInit											= null;

	if (config.onChapterChange != undefined) {
		if (typeof config.onChapterChange === "function") {
			this.customOnChapterChange = config.onChapterChange;	
		}
		delete config.onChapterChange;
	}
	
	if (config.onInit != undefined) {
		if (typeof config.onInit === "function") {
			this.onInit = config.onInit;	
		}
		delete config.onInit;
	}
	
	if (config.onPause != undefined) {
		if (typeof config.onPause === "function") {
			this.onPause = config.onPause;
		}
		delete config.onPause;	
	}	
	if (config.onPlay != undefined) {
		if (typeof config.onPlay === "function") {
			this.onPlay = config.onPlay;	
		}
		delete config.onPlay;
	}
	if (config.onComplete != undefined) {
		if (typeof config.onComplete === "function") {
			this.onComplete = config.onComplete;
		}
		delete config.onComplete;	
	}
	
	if (_gmctvoc.playheadInterval != null) {
		clearInterval(_gmctvoc.playheadInterval);
		_gmctvoc.playheadInterval = null
	}
	
	if (_gmctvoc.videoNetworkState != null) {
		clearInterval(_gmctvoc.videoNetworkState);
		_gmctvoc.videoNetworkState = null
	}
	
	if(!window._openchannelModules) 
		window._openchannelModules = {};	
	
	window._openchannelModules["_openchannelModule_"+this.id] = this;
	
	// COnfigure the video container div, and add the core div
	document.getElementById(this.config.div).style.lineHeight = "1px";
	document.getElementById(this.config.div).innerHTML = "";
	document.getElementById(config.div).appendChild(this.createNode({
			type:"div",
			styles:{ 
				width:config.width + "px",
				height:config.height + "px",
				marginBottom:"0",
				position:"relative",
				lineHeight:"1"
			},
			attributes: {
				id:this.videoDiv
			}
		})
	);
	
	this.config.id = this.id;
	
	/* Test to make sure all libraries are loaded */
	if (!this.testEnvironment()) {
		this.loader = new multichanneltv_set_environment(this.id);	
		this.loader.process();
		return this;
	} else {
		this.pluginsLoaded();
	}
	
	return this;

}


/* Create node function for all dynamically created div's, etc */
multichanneltv_openchannel.prototype.createNode = function(props) {
	var e = document.createElement(props.type);
	
	if (typeof props.classes != "undefined")
		e.className = props.classes;
	
	if (typeof props.styles != "undefined") 
		for (var i in props.styles) 
			e.style[i] = props.styles[i];
		
	if (typeof props.attributes != "undefined")
		for (var ii in props.attributes) 
			e.setAttribute(ii,props.attributes[ii]);
	
	if (typeof props.properties != "undefined")
		for (var iii in props.properties) 
			e[iii] = props.properties[iii];
			
	if (typeof props.content != "undefined")
		e.innerHTML = props.content;
		
	return e;
}

/* Once all libraries are loaded, start building the openchannel */
multichanneltv_openchannel.prototype.pluginsLoaded = function() {
	if (swfobject.hasFlashPlayerVersion('10') && !this.disableFlash) {
			this.buildFlash();
		}	else if (Modernizr.video) {
			this.buildHTML5();
		} else {
			this.showUpgrade();
		}
}

// If no flash OR html5 is present, then show flash upgrade message
multichanneltv_openchannel.prototype.showUpgrade = function() {
	document.getElementById(this.videoDiv).innerHTML = '<div style="line-height:14px;margin:5px auto 0 auto; font-family:Verdana, Geneva, sans-serif; color:#000; background-color:#FFF; border:1px solid #000; width:85%; padding:10px;"><p style="font-size:10px;margin:0; padding:0;">We have detected that you are not seeing the video. Please <a style="color:#000;" href="http://www.multichanneltv.com/client_checks" target="_blank">click here</a> to to do a system check.</p><p style="margin:0; padding:0;">&nbsp;</p><p style="font-size:10px;margin:0; padding:0;">You might not currently have the correct version of Flash Player installed on your system.</p><p style="margin:0; padding:0;">&nbsp;</p><p style="font-size:10px;margin:0; padding:0;"><b>This site requires the use of Flash Player 10.2 or above.</strong></p><p style="margin:0; padding:0;">&nbsp;</p><p style="font-size:10px;margin:0; padding:0;">Flash Player is a widely used free product from Adobe, which will enhance your experience accross millions of websites. In order to view this programme you must install Flash Player.</p><p style="margin:0; padding:0;">&nbsp;</p><p style="font-size:10px;margin:0; padding:0;">To install Flash Player, click the image below and we will take you to the official download.</p><p style="margin:0; padding:0;">&nbsp;</p><p style="margin:0; padding:0;"><a href="http://www.adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a></p></div>';
}

// If we are building HTML5, load the JSON instance properties, then call setJSON method.
multichanneltv_openchannel.prototype.buildHTML5 = function() {
	this.type = "html5";
	$.ajax({
		url: "http://api.multichanneltv.com/openchannel/v4/instance.api?iid=" + this.config.iid + "&jsonp=" + this.id,
		dataType: "jsonp",
		success:function(responseData, textStatus, XMLHttpRequest) {
			window._openchannelModules["_openchannelModule_"  + responseData.openchannel_guid].setJSON(responseData, true);
		}
	});	
};

// If we are building Flash, then embed the swf
multichanneltv_openchannel.prototype.buildFlash = function() {
	this.type = "flash";
	var params = {menu: "false",scale: "noScale", allowFullscreen: "true",allowScriptAccess: "always",bgcolor: "#000000",wmode:"transparent",align:"TL",salign:"TL"}; 						 
	var d = new Date();
	swfobject.embedSWF(this.protocol + "://openchannel.multichanneltv.com/core/v4/oc.swf?__c=" + d.getTime(), this.videoDiv, this.config.width, this.config.height, "9.0.0", "expressInstall.swf", this.config, params, {}, multichanneltv_delegate(this, this.onFlashEmbed));
}

multichanneltv_openchannel.prototype.remove = function () {
	if (_gmctvoc.playheadInterval != null) {
		clearInterval(_gmctvoc.playheadInterval);
		_gmctvoc.playheadInterval = null
	}
	if (this.type == "flash") {
		swfobject.removeSWF(this.videoDiv);
	} else {
		this.html5DOM.innerHTML = "";
	}
}

// Called once the video player is ready.  
// Incorprates 0.1 second delay as workaround to asynchrous processing
multichanneltv_openchannel.prototype.init = function() {
	setTimeout(multichanneltv_delegate(this, this.oninit), 100);
};

// Main initialistion function - adds video event listeners and processes other instance options (non video)
multichanneltv_openchannel.prototype.oninit = function() {
	_gmctvoc.playheadInterval = setInterval(multichanneltv_delegate(this,this.playheadCheck), 800);  
	
	if (this.type == "flash") {
		this.addEventListener("playheadPlaying", multichanneltv_delegate(this, this.integratedOnPlay));
		this.addEventListener("playheadPaused", multichanneltv_delegate(this, this.integratedOnPause));
		if (this.onPlay != null) {
			this.addEventListener("playheadPlaying", multichanneltv_delegate(this, this.onPlay));
		}
		if (this.onPause != null) {
			this.addEventListener("playheadPaused", multichanneltv_delegate(this, this.onPause));		
		}
		if (this.onComplete != null) {
			this.addEventListener("playheadFinished", multichanneltv_delegate(this, this.onComplete));		
		}
	} else {
		this.html5DOM.addEventListener("playing", multichanneltv_delegate(this, this.integratedOnPlay));
		this.html5DOM.addEventListener("pause", multichanneltv_delegate(this, this.integratedOnPause));			
		if (this.onPlay != null) {
			this.html5DOM.addEventListener("playing", multichanneltv_delegate(this, this.onPlay));
		}
		if (this.onPause != null) {
			this.html5DOM.addEventListener("pause", multichanneltv_delegate(this, this.onPause));
		}
		if (this.onComplete != null) {
			this.html5DOM.addEventListener("ended", multichanneltv_delegate(this, this.onComplete));		
		}
	}
			
	// If subtitles
	if (this.json.subtitles.length > 0)
		this.doSubtitles();
	
	// If social div
	if (this.json.social == "true") 
		this.doSocial();
	
	// Custom init
	if (this.onInit != null) 
		this.onInit.call();
		
	// Legacy from OC3
	try {customCoreInit();} catch (e) {}	
}

// Subtitles DIV builder
multichanneltv_openchannel.prototype.doSubtitles = function() {
	this.hasSubtitles = true;
	
	var buttonSize = navigator && navigator.platform && navigator.platform == "iPad" ? "big" : "small";
	
	var subtitlesDiv = this.createNode({
		type:"div",
		attributes:{
			id:"openchannel_subtitles_" + this.id
		},
		styles: {
			backgroundColor:"#000000",
			height:"20px",
			textAlign:"right",
			marginTop:"0",
			padding:"10px 8px 2px 8px"
		}
	});
	
	var subtitleSpan = this.createNode({
		type:"div",
		styles: {
			backgroundColor:"#000000",
			padding:"5px 8px 5px 8px",
			display:"inline-block",
			fontSize:"11px",
			fontFamily:"Verdana, Arial, Helvetica",
			color:"#444",
			marginRight:"7px",
			position:"relative",
			top:"-1px"
		},
		content: "subtitles"
	});
	
	subtitlesDiv.appendChild(subtitleSpan);
	
	// Language options array
	var langs = [];
	
	this.subtitleLang = this.json.subtitle_lang;
	
	// For each subtitles, if the language is not yet in langs[] array, then add a flag.	
	for (var l in this.json.subtitles) {
		var p = false;
		for(var i = 0; i < langs.length; i++) { 
			if (langs[i] == this.json.subtitles[l].lang) {
				p = true;
				break;
			}
		}
		if (!p) {
			langs.push(this.json.subtitles[l].lang);
			var flagDiv = this.createNode({
				type:"div",
				styles:{
					cursor:"pointer",
					marginRight:"10px",
					textAlign:"center",
					width:"14px",
					height:"18px",
					position:"relative",
					color:"#FFFFFF",
					fontWeight:"bold",
					paddingTop:"0",
					display:"inline-block",
					background:"url(" + this.cf + '/libs/img/icons/openchannel-assets/arrow.png) ' + (this.json.subtitle_lang == this.json.subtitles[l].lang ? '2px 11px' : '60px 60px') + ' no-repeat'
					
				},
				properties:{
					onclick:multichanneltv_delegate(this, this.changeSubtitle, this.json.subtitles[l].lang)
				},
				attributes: {
					lang:this.json.subtitles[l].lang,
					alt:this.json.subtitles[l].lang,
					title:this.json.subtitles[l].lang,
					id:"mctv-subtitle-flag-" + this.json.subtitles[l].lang
				},
				content:'<img src="' + this.cf + '/libs/img/icons/flags-14x9/' + this.json.subtitles[l].lang + '.png" />',
				classes: "mctv-subtitle-flag"
			});
			subtitlesDiv.appendChild(flagDiv);
		}
	}
	
	if (this.type == "html5") {
		//html5SubtitlesDOM
		var subtitlesDOM = this.createNode({
			type:"div",
			styles:{
				position:"absolute",
				padding:"10px 0",
				bottom:"40px",
				height:"30px",
				color:"#FFFFFF",
				fontFamily:"Verdana, Arial, Helvetica",
				lineHeight:"13px",
				fontSize:"11px",
				textAlign:"center",
				width:this.config.width + "px",
				zIndex:"100",
				backgroundImage:"url(" + this.cf + "/libs/img/backgrounds/black-40.png)"
			}
		});
		this.html5SubtitlesDOM = subtitlesDOM;
		
		document.getElementById(this.videoDiv).appendChild(subtitlesDOM);
		
		for(i in this.json.subtitles)
			this.subtitles[i]=this.json.subtitles[i];
	}
	document.getElementById(this.config.div).appendChild(subtitlesDiv);
}

multichanneltv_openchannel.prototype.changeSubtitle = function(e,lang) {
	$(".mctv-subtitle-flag").css("background-position","60px 60px");
	this.subtitleLang = lang == this.subtitleLang ? '' : lang;
	if (this.type == "flash") {
		this.callCoreMethod("changeLanguage",this.subtitleLang);	
	} else {
		this.html5SubtitlesDOM.style.display = "none";
		this.currentSubtitle = null;
	}
	$("#mctv-subtitle-flag-" + this.subtitleLang).css("background-position","2px 11px");
}

multichanneltv_openchannel.prototype.doSocial = function() {
	var socialDiv = this.createNode({
		type:"div",
		attributes:{
			id:"openchannel_social_" + this.id
		},
		styles:{
			height:"28px",
			marginTop:"0",
			padding:"10px 0 0 8px",
			backgroundColor:this.json.social_background
		},
		content:'<div style="float:left; margin-right:10px; display:inline;"><a href="http://twitter.com/share" class="twitter-share-button" data-url="'+this.json.social_link+'" data-count="horizontal">Tweet</a></div>'
					 +'<div style="float:left; margin-right:1px; display:inline;"><g:plusone size="medium" href="'+this.json.social_link+'"></g:plusone></div>'
					 +'<div style="float:left; margin-right:10px; isplay:inline;"><iframe src="//www.facebook.com/plugins/like.php?href='+escape(this.json.social_link)+'&amp;send=false&amp;layout=button_count&amp;width=95&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font&amp;height=21" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:95px; height:21px;" allowTransparency="true"></iframe></div>'
	});
	document.getElementById(this.config.div).appendChild(socialDiv);
	
	document.getElementsByTagName("head")[0].appendChild(this.createNode({
		type:"script",
		attributes:{
			src:"http://platform.twitter.com/widgets.js",
			type:"text/javascript"
		}
	}));
	
	document.getElementsByTagName("head")[0].appendChild(this.createNode({
		type:"script",
		attributes:{
			src:"https://apis.google.com/js/plusone.js",
			type:"text/javascript"
		}
	}));
}

multichanneltv_openchannel.prototype.playheadCheck = function() {
	if (this.playStatus != "playing")
		return;
	var playheadTime =  this.type == "flash" ? 
		Math.floor(this.swfmodule._getPlayheadTime()) : 
		Math.floor(this.getHTML5PlayheadTime());
	
	if (playheadTime == this.currentPlayheadTime)
		return;
		
	this.currentPlayheadTime = playheadTime;
	this.onChapterChange();
	this.onIntelligentVideoChange();
	if (this.hasSubtitles && this.type =="html5")
		this.onSubtitlesChange();
		
}

multichanneltv_openchannel.prototype.goto = function(seconds) {
	if (this.type == "flash") {
		this.callCoreMethod('videoEvent',{method:"seekAndStart", seconds:seconds})
	} else {
		if (this.html5DOM.currentTime < 1) {
			this.seekOnPlayback = seconds;
			this.html5DOM.play();
			return;
		}
		this.html5DOM.currentTime = seconds;
		this.html5DOM.play();
	}
}

multichanneltv_openchannel.prototype.getHTML5PlayheadTime = function() {
	return this.html5DOM.currentTime;
}

multichanneltv_openchannel.prototype.trackChapterLoad = function(id) {
	if (this.type == "flash") {
		this.callCoreMethod("externalTracking", "chapter_loaded", id);
	} 
	// Todo: add HTML5 chapter tracking
}

multichanneltv_openchannel.prototype.onChapterChange = function(){
	for (var i in this.chapters) {
		i = parseInt(i);
		if (this.currentPlayheadTime >= this.chapters[i].start_point) {
			if (this.chapters[i+1]) {
				if (this.currentPlayheadTime < this.chapters[i+1].start_point) {
					if (this.current_chapter != i) {
						this.current_chapter = i;
						this.trackChapterLoad(this.chapters[i].chapter_id);
						if (this.customOnChapterChange) {
							try {
									this.customOnChapterChange(this.chapters[i]);
							} catch (e) {}
						} else {
							try {
								customChapterChange(this.chapters[i]);
							} catch(e){};
						}
					}
					break;
				}
			} else {
				if (this.current_chapter != i) {
					this.current_chapter = i;
					this.trackChapterLoad(this.chapters[i].chapter_id);
					if (this.customOnChapterChange) {
						try {
							this.customOnChapterChange(this.chapters[i]);
						} catch (e) {}
					} else {
						try {
							customChapterChange(this.chapters[i]);
						} catch(e){};
					}
				}
			}
		}
	}
};

multichanneltv_openchannel.prototype.onSubtitlesChange = function(){
	
	if (this.currentSubtitle != null) {
		if (this.currentPlayheadTime > this.subtitles[this.currentSubtitle].end_point || 
				this.currentPlayheadTime < this.subtitles[this.currentSubtitle].start_point) {
			this.html5SubtitlesDOM.innerHTML = "";
			this.html5SubtitlesDOM.style.display = "none";
			this.currentSubtitle = null;
		}
	}
			
	for (var i in this.subtitles) {
		i = parseInt(i);
		if (i === this.currentSubtitle) 
			continue;
		
		if (this.currentPlayheadTime >= this.subtitles[i].start_point && 
				this.currentPlayheadTime <= this.subtitles[i].end_point) {
			if (this.subtitleLang != this.subtitles[i].lang ||
					this.subtitles[i].subtitle == "") 
				continue;
				this.html5SubtitlesDOM.innerHTML = this.subtitles[i].subtitle;
				this.html5SubtitlesDOM.style.display = "block";
				this.currentSubtitle = i;
			}
		}
};

multichanneltv_openchannel.prototype.onIntelligentVideoChange = function(){
	if (this.currentIntelligentVideo){
		if (this.currentPlayheadTime >= this.intelligentVideos[this.currentIntelligentVideo].end_point || 
				this.currentPlayheadTime < this.intelligentVideos[this.currentIntelligentVideo].start_point){
			if (this.type == "flash") {
				this.callCoreMethod("_unloadIV");
			} 
			this.currentIntelligentVideo=false;
		}
	}
	for(i in this.intelligentVideos){
		if(this.currentPlayheadTime >= this.intelligentVideos[i].start_point &&
			 this.currentPlayheadTime < this.intelligentVideos[i].end_point){
			if (this.currentIntelligentVideo===i) continue;
			if (this.type == "flash") {
				this.callCoreMethod("_loadIV", this.intelligentVideos[i].swf, this.intelligentVideos[i].action, this.intelligentVideos[i].controls);
			} 
			this.currentIntelligentVideo=i;
		}
	}	
}

multichanneltv_openchannel.prototype.pause = function() {
	if (this.type == "flash") {
		this.callCoreMethod("videoEvent", {method:"pause"});
	} else {
		this.html5DOM.pause();
	}
}

multichanneltv_openchannel.prototype.play = function() {
	if (this.type == "flash") {
		this.callCoreMethod("videoEvent", {method:"play"});
	} else {
		this.html5DOM.play();
	}
}

multichanneltv_openchannel.prototype.integratedOnPlay = function() {
	this.playStatus = "playing";
	if (this.type == "html5" && this.seekOnPlayback > 0) {
		setTimeout(multichanneltv_delegate(this, this.goto, this.seekOnPlayback), 2000);
		this.seekOnPlayback = -1;
	} 
}

multichanneltv_openchannel.prototype.integratedOnPause = function() {
	this.playStatus = "paused";
}
	
multichanneltv_openchannel.prototype.setJSON = function(json, html5) {
	this.json = json;
	for(i in this.json.chapters)
		this.chapters[i]=this.json.chapters[i];
		
	for(i in this.json.intelligent_videos)
		this.intelligentVideos[i]=this.json.intelligent_videos[i];
		
	
	if (this.type == "flash")
		return;
				
	if (json.status == "error" || json.html5_fallback == "false" || !Modernizr.video) {
		var errorDiv = '<div style="width:' + this.config.width + 'px; height:' + this.config.height + 'px; background-color:#000; line-height:12px; color:#FFF; font-weight:bold; text-align:center;"><br /><br />This video cannot be viewed</div>';
		document.getElementById(this.config.div).innerHTML = errorDiv;
	} else {
		if (html5 != undefined) {
			if (navigator && navigator.platform && navigator.platform == "iPhone") {
				var iphone_video = this.json.download_location + "videos/"
												 + this.json.video_name + "_480_" 
												 + this.json.html5_iphone_bitrate + "_"
												 + this.json.package_id + ".mp4";
												 
				html5 = '<video id="multichanneltv_html5_movie" src="' + iphone_video + '" width="' + this.config.width + '" height=" ' + this.config.height + '" poster="' + this.json.preview_image + '"></video>';
			} else if (navigator && navigator.platform && navigator.platform == "iPad") {
				html5 = '<video id="multichanneltv_html5_movie" src="' + this.json.download_location + 'videos/' + this.json.video_name + '_' + this.config.width + '_' + this.json.html5_mp4_bitrate + "_" + this.json.package_id + '.mp4" width="' + this.config.width + '" height=" ' + this.config.height + ' " preload controls ' + (this.json.autoplay=="true" ? 'autoplay' : '') + ' poster="' + this.json.preview_image + '"></video>';
			} else {
					html5 = '<video id="multichanneltv_html5_movie" width="' + this.config.width + '" height="' + this.config.height + ' " preload controls ' + (this.json.autoplay=="true" ? 'autoplay' : '') + ' poster="' + this.json.preview_image + '">'
									+ '<source src="' + this.json.download_location + 'videos/' + this.json.video_name + '_' + this.config.width + '_' + this.json.html5_mp4_bitrate + "_" + this.json.package_id + '.mp4" />'
									+ '<source src="' + this.json.download_location + 'videos/' + this.json.video_name + '_' + this.config.width + '_' + this.json.html5_webm_bitrate + "_" + this.json.package_id + '.webm" />'
									+ '<source src="' + this.json.download_location + 'videos/' + this.json.video_name + '_' + this.config.width + '_' + this.json.html5_ogv_bitrate + "_" + this.json.package_id + '.ogv" />'
							  + '</video>';
			}
			
			var spinnerPos = {
				top:((this.config.height / 2)-25) + "0px;",
				left:((this.config.width / 2)-55) + "px;"
			}
			html5 += '<div id="multichanneltv_html5_movie_info" style="position:absolute;width:90px;height:40px;padding:10px;background-color:#000;color:#FFF;font-weight:bold;text-align:center;z-index:9999;font-family:Verdana, Arial, Helvetica;font-size:11px;top:'+spinnerPos.top + 'left:'+spinnerPos.left + 'border-radius:10px;"><img src="http://cdn3.multichanneltv.com/libs/img/loaders/oc4loading.gif" /><br />Buffering...</div>';
			
			this.doHTML5Track();
			
			document.getElementById(this.videoDiv).innerHTML = html5;
			this.html5DOM = document.getElementById("multichanneltv_html5_movie");
			_gmctvoc.videoNetworkState = setInterval(multichanneltv_delegate(this,this.cycleVideoState), 500); 

			this.init();
		}
	}
};

multichanneltv_openchannel.prototype.cycleVideoState = function() {
	if (this.html5DOM.networkState == 0 ||
	    (this.html5DOM.networkState == 2 && (this.html5DOM.readyState != 3 && this.html5DOM.readyState != 4))) {
		document.getElementById("multichanneltv_html5_movie_info").style.display = "block";
	} else {
		document.getElementById("multichanneltv_html5_movie_info").style.display = "none";
	}
}

multichanneltv_openchannel.prototype.addEventListener = function(type, delegate, id) {
	for (var i=0; i<this.listenerProxies.length; i++) {
		if(this.listenerProxies[i].callback == delegate && 
			 this.listenerProxies[i].type == type) {
			return null;
		}
	}
	var proxy_id = this.swfmodule.addJSEventListener(type, id);
	var newproxy = new multichanneltv_listener_proxy(type, delegate, proxy_id)
	this.listenerProxies.push(newproxy);
	window[proxy_id] = delegate;
};

multichanneltv_openchannel.prototype.doHTML5Track = function() {
	var html5_track_params = [];
		html5_track_params['type'] = "html5";
		html5_track_params['iid'] = this.json.instance_id;
		html5_track_params['w'] = screen.width;
		html5_track_params['h'] = screen.height;
		html5_track_params['uid'] = this.uid;
		var paramstr = "";
		for (var i in html5_track_params) {
			paramstr += i+"="+html5_track_params[i] + "&";
		}
		var html5_track_script = document.createElement('script');
		html5_track_script.setAttribute("type","text/javascript");
		html5_track_script.setAttribute("src", "http://api.multichanneltv.com/openchannel/v4/track.api?" + paramstr);
		document.getElementsByTagName("body")[0].appendChild(html5_track_script);
}

multichanneltv_openchannel.prototype.onFlashEmbed = function(e) {
	this.swfmodule = e.ref;
}

multichanneltv_openchannel.prototype.testEnvironment = function() {
	if (typeof(swfobject) == "undefined" || 
			typeof(Modernizr) == "undefined" ||
			typeof(jQuery) == "undefined") {
		return false;
	} else {
		return true;
	}
}

multichanneltv_openchannel.prototype.callCoreMethod = function() {
	var methodname=arguments[0], params=[];
	for(var i=1;i<arguments.length;i++){
		params.push(arguments[i]);
	}
	if(params.length > 0){
		if(this.swfmodule[methodname]!=undefined){
			return this.swfmodule[methodname].apply(this.swfmodule,params);
		} else {
			return this.swfmodule.callCoreMethod.apply(this.swfmodule,arguments);
		}
	} else { 
		if(this.swfmodule[methodname]!=undefined){
			return this.swfmodule[methodname]();
		} else { 
			return this.swfmodule.callCoreMethod.apply(this.swfmodule,arguments);
		}
	}
}

multichanneltv_openchannel.prototype.guid = function() {
	var S4 = function() { 
  	return (((1+Math.random())*0x10000)|0).toString(16).substring(1); 
  }; 
  return (S4()+S4()+"_"+S4()+"_"+S4()+"_"+S4()+"_"+S4()+S4()+S4()); 
} 

multichanneltv_set_environment = function(id) {
	this.parentName = 'window._openchannelModules["_openchannelModule_'  + id + '"].loader';
	this.parentNode = window._openchannelModules["_openchannelModule_"  + id];
	this.scriptsReady = 0;
	
	this.cf = 'https:' == document.location.protocol ? 'https://d17d9oit8zaeus.cloudfront.net' : 'http://cdn3.multichanneltv.com';
	this.t = null;
	
	this.load_swfObject = function() {
		if (typeof(swfobject) === "undefined" && !_gmctvoc.swfobjectloading) {
			_gmctvoc.swfobjectloading = true;
			this.getScript(this.cf + "/libs/js/swfobject.js");
		}
	}

	this.load_jQuery = function() {
		if (typeof(jQuery) === "undefined" && !_gmctvoc.jqueryloading)  {
			_gmctvoc.jqueryloading = true;
			this.getScript(this.cf + "/libs/js/jquery-1.6.4.min.js");
		}
	}

	this.load_modernizr = function() {
		if (typeof(Modernizr) === "undefined" && !_gmctvoc.modernizrloading) {
			_gmctvoc.modernizrloading = true;
			this.getScript(this.cf + "/libs/js/modernizr-2.0.6.min.js");
		}
	}
	
	this.getScript = function(src) {
		var script = document.createElement('script');
		script.setAttribute("type","text/javascript");
		script.setAttribute("src", src);
		document.getElementsByTagName("head")[0].appendChild(script);
	}
	
	this.process = function() {
		this.load_jQuery();
		this.load_swfObject();
		this.load_modernizr();
		this.t = setTimeout(this.parentName + ".loophole()",200);
	}
	
	this.loophole = function() {
		clearTimeout(this.t);
		if (typeof(swfobject) == "undefined" || 
				typeof(Modernizr) == "undefined" ||
				typeof(jQuery) == "undefined") {
			this.t = setTimeout(this.parentName + ".loophole()",200);
		} else {
			this.parentNode.pluginsLoaded();			
		}
	}
}

function multichanneltv_delegate(target,method){
	if(!method || !target) return;
	var parameters = [];
	for(var i = 2; i < arguments.length; i++) {
		parameters.push(arguments[i]);
	}
	var proxy = function() {
		var actualParameters = [];
		for(var i = 0; i < arguments.length; i++) {
			actualParameters.push(arguments[i]);
		}
		return method.apply(target, actualParameters.concat(parameters));
	};
	return proxy;
}

function multichanneltv_listener_proxy(type_in, callback_in, id_in) {
	this.type = type_in;
	this.callback = callback_in;
	if (id_in == undefined) {
		this.id = "listenerProxy_" + type_in + "_" + guid();
	} else {
		this.id = id_in;
	}
};

multichanneltv_listener_proxy.prototype.dispatchProxy = function(e) {
	this.callback(e, this);
}

