// DivX Plugin Code Handler
function divx_plugin (object_id, embed_id, type) {
	this.is_ie = false;
	this.is_ff = false;
	this.os = this.get_env();
	
	if (!object_id || !embed_id) {
		return;
	}
	
	if (type == 'uploader') {
		// it's the content uploader
		this.regex = 'divx.*?(content upload)';
		this.activex = 'npUpload.DivXContentUploadPlugin.1';
		this.type = 'uploader';
		this.install_win = WIN_UPLOADER;
		this.install_mac = MAC_UPLOADER;
	}
	else {
		// otherwise assume it's the web player
		this.regex = 'divx.*?((web)|(browser))';
		this.activex = 'npdivx.DivXBrowserPlugin.1';
		this.type = 'player';
		this.install_win = WIN_PLAYER;
		this.install_mac = MAC_PLAYER;
	}
	this.object_id = object_id;
	this.embed_id = embed_id;
	this.installed = false;
}

// Core Functions ...
divx_plugin.prototype.force_version = function(force_version) {
	var version = this.get().GetVersion();
	var current = version.split('.');
	var forced = force_version.split('.');
	var force = false;
	for (var i = 0; i < 3; i++) {
		if (parseInt(forced[i]) > parseInt(current[i])) {
			force = true;
			break;
		}
		else if (parseInt(forced[i]) < parseInt(current[i])) {
			break;
		}
	}
	if (parseInt(forced[0]) == 5) {
		// because the old uploader is sometimes version 5...sigh.
		force = true;
	}
	if (force) {
		Modalbox.show(UPGRADE_PLUGIN_LANGUAGE, '/download/update-plugin-ajax/' + this.type + '/', {width: 400, height:225, loadingString:''});
	}
}

divx_plugin.prototype.get_env = function() {
	this.get_browser();
	if (navigator.appVersion.indexOf('Win') != -1) {
		return 'Win';
	}
	if (navigator.appVersion.indexOf('Mac') != -1) {
		return 'Mac';
	}
	if (navigator.userAgent.indexOf('Linux') != -1) {
		return 'Lin';
	}
};

divx_plugin.prototype.get_browser = function() {
	var agent = navigator.userAgent.toLowerCase();
	if (agent.indexOf('msie') != -1) {
		this.is_ie = true;
	}
	else if (agent.indexOf('firefox') != -1) {
		this.is_ff = true;
	}
};

divx_plugin.prototype.detect_plugin = function() {
	if (this.is_ie && this.os == 'Win') {
	     // IE/Windows - can't use navigator.plugins since it's empty
	     // must use VBScript instead to detect the plugin
	     document.writeln('<script language="VBscript">');
	     document.writeln('detectableWithVB = False');
	     document.writeln('If ScriptEngineMajorVersion >= 2 then');
	     document.writeln('  detectableWithVB = True');
	     document.writeln('End If');
	     document.writeln('Function detectActiveXControl(activeXControlName)');
	     document.writeln('  on error resume next');
	     document.writeln('  detectActiveXControl = False');
	     document.writeln('  If detectableWithVB Then');
	     document.writeln('     detectActiveXControl = IsObject(CreateObject(activeXControlName))');
	     document.writeln('  End If');
	     document.writeln('End Function');
	     document.writeln('</scr' + 'ipt>');
	     
	     // Now use the function
	     return detectActiveXControl(this.activex);
	}
	else {
	     // See if DivX Plugin is installed in navigator.plugins
	     navigator.plugins.refresh(false);
	     var reg_check = new RegExp(this.regex, 'i');
	     for (plugin = 0; plugin < navigator.plugins.length; plugin++) {
	        if (reg_check.test(navigator.plugins[plugin].name)) {
	            return navigator.plugins[plugin];
	        }
	     }
	}
	return false;
};

divx_plugin.prototype.get = function() {
	if (this.is_ie && this.os == 'Win') {
		return document.getElementById(this.object_id);
	}
	return document.getElementById(this.embed_id);
};
// ... end Core Functions