post_plate_html = false;

function divx_video(fileURL, width, height, content_id, instance, is_s6) {
	this.is_s6 = is_s6;
	this.plugin_id = 'divx-plugin' + content_id;
	this.content_id = content_id;
	this.width = width;
	this.height = height;
	//this.file_url = VIDEOS_URL + this.content_id + '/.divx'; // have to add the faked extension for IE
	this.file_url = fileURL;
	this.thumb = IMAGES_URL + 'videos/' + this.content_id + '.jpg';
	this.tracking_url = 'http://' + TRACKER_DOMAIN + '/video/tracker-ajax';
	this.instance = instance;
	this.post_plate = 'http://' + TRACKER_DOMAIN + '/video/post-plates/default';
	this.post_callback = null;
	this.downloadable = true;
	this.params = new Array();
	this.min_version = false;
	// set the initial params of the plugin
	this.add_param('showPostPlaybackAd', 'false');
	this.add_param('statusCallback', this.instance + '.status_callback');
	this.add_param('customMode', 'Stage6');
	
	// set div ids (note: these correspond to CSS)
	this.class_container = 'divx-container';
	this.class_overlay = 'divx-overlay';
	this.class_options = 'divx-options';
	this.class_video = 'divx-video';
	this.class_controls = 'divx-controls';
	this.class_thumb = 'divx-thumb';
	this.class_post_plate = 'divx-post-plate';
	this.class_image = 'divx-image';
	
	this.div_container = 'divx-container' + content_id;
	this.div_overlay = 'divx-overlay' + content_id;
	this.div_options = 'divx-options' + content_id;
	this.div_video = 'divx-video' + content_id;
	this.div_controls = 'divx-controls' + content_id;
	this.div_thumb = 'divx-thumb' + content_id;
	this.div_post_plate = 'divx-post-plate' + content_id;
	this.div_upgrade = 'divx-uphrade' + content_id;
	this.img_image = 'divx-image' + content_id;
	
	// initialize the divx_plugin framework
	this.p = new divx_plugin(this.plugin_id, this.plugin_id, 'player');
	this.plugin = this.p.detect_plugin();
	this.os = this.p.os;
	this.is_ie = this.p.is_ie;
	this.is_ff = this.p.is_ff;
	this.version = this.p.version;
	this.use_src_param = this.p.older_version('1.4.0', this.version);
	
	// set the default chrome
	this.chrome_height = 26;
	this.chrome_left_image = ASSETS_URL + 'images/video/video_player_left.gif';
	this.chrome_bg_image = ASSETS_URL + 'images/video/video_player_middle.gif';
	// 1.3 -> 1.4 skin change
	this.chrome_right_image = this.use_src_param ? ASSETS_URL + 'images/video/video_player_right.gif' : ASSETS_URL + 'images/video/video_player_right_new.gif';
}

divx_video.prototype.set_min_version = function(version) {
	this.min_version = version;
};

divx_video.prototype.add_param = function(name, value) {
	if (this.params.length) {
		for (i = 0; i < this.params.length; i++) {
			if (this.params[i][0].toLowerCase() == name.toLowerCase()) {
				this.params[i][1] = value;
				return true;
			}
		}
	}
	this.params.push(new Array(name, value));
};

divx_video.prototype.rpc = function(status) {
	var script_filename = this.tracking_url + '?' + this.set_tracking_status(status);
	this.rpc_core(script_filename);
	if (this.is_s6) {
		var eventId = status == 'play' ? 8 : (status == 'completed' ? 9 : 10);
		omniture_event(eventId, 5, this.content_id, 4, this.content_id);
	}
};

divx_video.prototype.rpc_core = function(script_filename) {
	var html_doc = document.getElementsByTagName('head').item(0);
	var js = document.createElement('script');
	js.setAttribute('language', 'javascript');
	js.setAttribute('type', 'text/javascript');
	js.setAttribute('src', script_filename);
	html_doc.appendChild(js);
	return false;
};

divx_video.prototype.set_post_plate = function(ajax_url) {
	//this.post_plate = ajax_url;
	this.post_plate = 'http://' + TRACKER_DOMAIN + '/video/post-plates/' + ajax_url;
};

divx_video.prototype.set_tracking_status = function(status) {
	var url = encodeURIComponent(window.location.toString());
	url = url.replace(/'/ig, "%39");
	return 'url=' + url + '&video_id=' + this.content_id + '&status=' + status + '&source=javascript';
};

divx_video.prototype.post_plate_params = function() {
	var url = encodeURIComponent(window.location.toString());
	url = url.replace(/'/ig, "%39");
	return 'url=' + url + '&video_id=' + this.content_id + '&wxh=' + this.width + 'x' + this.height;
}

divx_video.prototype.status_callback = function(event) {
	if (event == 1) {
		// the video is done buffering (e.g. no issues)
		this.rpc('play');
	}
	if (event == 2) {
		this.p.get().GoEmbedded();
		if (this.tracking_url) {
			this.rpc('completed');
		}
		if (this.post_callback) {
			this.post_callback();
		}
		else if (this.post_plate) {
			set_size(this.plugin_id, 1, 1);
			set_size(this.div_video, 1, 1);
			set_size(this.div_post_plate, this.width, this.height);
			show(this.div_post_plate);
			this.rpc_core(this.post_plate + '?' + this.post_plate_params());
			this.interval = setInterval(this.instance + '.rpc_listener()', 25);
			$(this.div_controls).style.top = this.height + 'px';
			show(this.div_controls);
		}
	}
};

divx_video.prototype.rpc_listener = function() {
	if (post_plate_html) {
		clearInterval(this.interval);
		this.render_post_plate(post_plate_html);
	}
};

divx_video.prototype.render = function() {
	// initialize the video element
	if (!$(this.div_container)) {
		document.write('<div class="' + this.class_container + '" id="' + this.div_container + '"></div>');
	}
	var v = $(this.div_container);
	set_size(this.div_container, this.width, this.height + this.chrome_height);
	v.innerHTML = '<div id="' + this.div_upgrade + '" onclick="' + this.instance + '.initiate_upgrade(' + "'in-site'" + ')" class="divx-upgrade-player" style="display:none"></div>';
	v.innerHTML += '<div class="' + this.class_overlay + '" id="' + this.div_overlay + '"></div><div class="' + this.class_post_plate + '" id="' + this.div_post_plate + '" style="display:none"></div>';
	set_size(this.div_overlay, this.width, this.height);
	$(this.div_overlay).style.lineHeight = this.height + 'px';
	if (this.thumb) {
		v.innerHTML += '<div class="' + this.class_thumb + '" id="' + this.div_thumb + '"><img class="' + this.class_image + '" style="float:none" src="' + this.thumb + '" width="' + this.width + '" height="' + this.height + '" /></div>';
	}
	v.innerHTML += '<div class="' + this.class_controls + '" onclick="' + this.instance + '.render_video()" id="' + this.div_controls + '"></div>';
	var w = $(this.div_controls);
	set_size(this.div_controls, this.width, this.chrome_height);
	w.style.top = this.thumb ? 0 + 'px' : this.height + 'px';
	w.style.background = 'url(' + this.chrome_bg_image + ') bottom left repeat-x';
	w.innerHTML = '<img class="' + this.class_image + '" src="' + this.chrome_left_image + '" style="float:left" /><img class="' + this.class_image + '" src="' + this.chrome_right_image + '" style="float:right" />';
	
	// decide how to proceed with rendering
	if (this.plugin) {
		var render_upgrade = false;
		if (this.is_s6 && this.min_version) {
			if (this.p.force_version(this.min_version)) {
				// omniture impression of in-page ad
				omniture_event(16, 8, 'in-siteOffer', 11, 'in-siteOffer');
				setTimeout(this.instance + '.show_upgrade()', 1);
				var opted_out = read_cookie('onefour_optout');
				if (opted_out == null) {
					// omniture impression of overlay ad
					omniture_event(16, 8, 'overlayOffer', 11, 'overlayOffer');
					add_onload(suggest_wp_update);
					create_cookie('onefour_optout', 'true', 3);
				}
				render_upgrade = true;
			}
		}
		this.render_options(render_upgrade);
	}
	else if (this.os == 'Lin') {
		$(this.div_overlay).innerHTML = 'For Linux support try Mplayer.';
	}
	else {
		this.render_install_prompt();
	}
};

divx_video.prototype.render_options = function(show_upgrade) {
	var o = $(this.div_overlay);
	o.innerHTML = '<div class="' + this.class_options + '" id="' + this.div_options + '"></div>';
	var c = $(this.div_options);
	c.innerHTML = '<a title="Play Video" class="divx-play" onclick="' + this.instance + '.render_video()"></a>';
	if (this.downloadable) { 
		c.innerHTML += '<a title="Download Video" class="divx-download" onclick="' + (this.tracking_url ? this.instance + '.rpc(\'download\');' : '') + '" href="' + this.file_url + '"></a>';
	}
	if (show_upgrade) {
		c.innerHTML += '<a class="divx-14" onclick="' + this.instance + '.initiate_upgrade(' + "'in-site'" + ')"></a>';
	}
	c.style.marginTop = ((this.height - c.getHeight()) / 2) + 'px';
};

divx_video.prototype.render_video = function() {
	hide(this.div_post_plate);
	hide(this.div_controls);
	hide(this.div_overlay);
	if (this.thumb) {
		hide(this.div_thumb);
	}
	// if it was already instantiated just re-size it
	if ($(this.plugin_id)) {
		set_size(this.plugin_id, this.width, this.height + this.chrome_height);
		set_size(this.div_video, this.width, this.height + this.chrome_height);
		$(this.plugin_id).Play();
		return;
	}
	$(this.div_container).innerHTML += '<div class="' + this.class_video + '" id="' + this.div_video + '"></div>';
	set_size(this.div_video, this.width, this.height + this.chrome_height);
	var param = this.use_src_param ? 'src' : 'video';
	if (this.is_ie) {
		var code = "<object id='" + this.plugin_id + "' classid='clsid:67DABFBF-D0AB-41fa-9C46-CC0F21721616' width='" + this.width + "' height='" + (this.height + this.chrome_height) + "' codebase='" + WIN_CAB + "'><param name='" + param + "' value='" + this.file_url + "' />" + this.render_params() + "</object>";
	}
	else {
		var code = "<embed id='"+ this.plugin_id +"' " + param + "='" + this.file_url + "' type='video/divx' width='" + this.width + "' height='" + (this.height + this.chrome_height) + "' pluginspage='http://go.divx.com/plugin/download/'" + this.render_params() + "></embed>";
	}
	$(this.div_video).innerHTML = code;
	/* if (this.is_s6 && this.min_version) {
		if (this.p.force_version(this.min_version)) {
			// setTimeout(this.instance + '.show_upgrade()', 3000);
			// this.instance + '.show_upgrade()';
		}
	}
	*/
	/* try {
		if (!this.is_ie) {
			$(this.plugin_id).Open(this.file_url);
		}
	}
	catch (e) {
	} */
};

divx_video.prototype.render_install_prompt = function() {
	if (this.is_ff || this.is_ie) {
	// if (this.is_ff) {
		$(this.div_container).innerHTML += '<div class="shaded" style="margin-bottom:6px"><span class="grey">The DivX Web Player is required by Stage6 to playback high quality video.</span><br /><b style="font-size:18px">Click below to install the DivX Web Player ' + (this.is_ie ? 'ActiveX control ' : '') + '&darr;</b></div>';
		this.render_video();
		if (this.is_ie) {
			// listener
			this.interval = setInterval(this.instance + '.ie_is_installed()', 100);
		}
	}
	else {
		if (this.is_s6) {
			Modalbox.show(INSTALL_PLUGIN_LANGUAGE, '/download/install-plugin-ajax/player/', {width: 400, height:225, loadingString:''});
		}
		$(this.div_overlay).innerHTML = '<a class="prompt" onclick="' + this.instance + '.render_install_post()" href="' + (this.os == 'Win' ? WIN_PLAYER : MAC_PLAYER) + '"><b>(!)</b> The DivX Web Player is required to view this video.<br /><br /><b>Click here to install.</b></a>'
	}
};

divx_video.prototype.ie_is_installed = function() {
	try {
		if ($(this.plugin_id).GetVersion()) {
			history.go(0);
		}
	}
	catch (e) {
	}
};

divx_video.prototype.show_upgrade = function() {
	if (this.os != 'Lin') {
		// $(this.div_upgrade).innerHTML = '<span style="line-height:9px"><br /></span><small style="font-size:12px">UPGRADE AVAILABLE<br /></small>DivX Web Player 1.4.0 released!';
		set_size(this.div_container, this.width, this.height + this.chrome_height + 54);
		show(this.div_upgrade);
	}
};

divx_video.prototype.initiate_upgrade = function(source) {
	// omniture click thru with source variable
	omniture_event(16, 8, source + 'Accept', 11, source + 'Accept');
	if ($(this.div_video)) {
		hide(this.div_video);
	}
	$(this.div_upgrade).className = 'divx-upgrade-post';
	if (this.os == 'Win') {
		var filename = 'DivXWebPlayerInstaller.exe';
		document.location = this.p.install_win;
	}
	else {
		var filename = 'DivXWebPlayer.dmg';
		document.location = this.p.install_mac;
	}
	$(this.div_upgrade).onclick = '';
	if (this.os == 'Win' && this.is_ie) {
		$(this.div_upgrade).innerHTML = 'Click <b>Run</b> when prompted. &nbsp; <a href="#" onclick="history.go()">After installation click here.</a>';
	}
	else {
		$(this.div_upgrade).innerHTML = 'Choose <b>Save File</b> and then <b>Open</b> the installer. &nbsp; <a href="' + document.location + '">After installation click here.</a>';
	}
};

divx_video.prototype.render_install_post = function() {
	if (this.os == 'Win' && this.is_ie) {
		$(this.div_overlay).innerHTML = '<a class="prompt" href="#" onclick="history.go()">Click here to complete installation after you have run the installer.</a>';
	}
	else {
		$(this.div_overlay).innerHTML = '<a class="prompt" href="#" onclick="navigator.plugins.refresh(true);history.go()">Click here to complete installation after you have run the installer.</a>';
	}
};

divx_video.prototype.render_post_plate = function(r) {
	$(this.div_post_plate).innerHTML = r;
	var p = $(this.div_post_plate).childNodes[0].id;
	$(p).style.margin = 'auto';
	$(p).style.marginTop = ((this.height - $(p).getHeight()) / 2) + 'px';
};

divx_video.prototype.render_params = function() {
	var output = '';
	if (this.params.length) {
		for (i = 0; i < this.params.length; i++) {
			if (this.is_ie) {
				output += '<param name="' + this.params[i][0] + '" value="' + this.params[i][1] + '" />';
			}
			else {
				output += ' ' + this.params[i][0] + '="' + this.params[i][1] + '"';
			}
		}
	}
	return output;
};
