var INSTALL_PLUGIN_LANGUAGE = 'Install Missing Plugin';
var UPGRADE_PLUGIN_LANGUAGE = 'Upgrade Plugin';
var MAC_UPLOADER = 'http://download.divx.com/divx/stage6/DivXStage6Installer.dmg';
var MAC_PLAYER = 'http://download.divx.com/player/DivXWebPlayer.dmg';
var WIN_UPLOADER = 'http://download.divx.com/player/DivXContentUploaderInstaller.exe';
var WIN_PLAYER = 'http://download.divx.com/player/stage6/DivXWebPlayerInstaller.exe';
var ASSETS_URL = 'http://www.bilbox.net/';
var TRACKER_DOMAIN = 'www.bilbox.net';

post_plate_html = false;

// Useful JS functions that were created because IE and prototype don't get along
function set_size (id, width, height) {
	$(id).style.width = width + 'px';
	$(id).style.height = height + 'px';
}

function hide(id) {
	$(id).style.display = 'none';
}
function show(id) {
	$(id).style.display = '';
}

function divx_video(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 = this.content_id; // have to add the faked extension for IE
	//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 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';
	this.chrome_right_image = ASSETS_URL + 'images/video/video_player_right.gif';
	
	// 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.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;
}

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);
};

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;
};

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 == 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 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) {
		this.render_options();
	}
	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() {
	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.tracking_url ? this.instance + '.rpc(\'play\');' : '') + 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>';
	}
	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);
	}
	$(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);
	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='http://go.divx.com/plugin/DivXBrowserPlugin.cab'><param name='src' value='" + this.file_url + "' />" + this.render_params() + "</object>";
	}
	else {
		var code = "<embed id='"+ this.plugin_id +"' type='video/divx' src='" + this.file_url + "' 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) {
		this.p.force_version(this.min_version);
	}
};

divx_video.prototype.render_install_prompt = function() {
	if (this.is_ff) {
		this.render_video();
	}
	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.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;
};
