// 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();
	this.version = false;
	
	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) {
	if (!this.version) {
		try {
			var version = this.get().GetVersion();
		}
		catch (e) {
			return false;
		}
	}
	else {
		var version = this.version;
	}
	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 (this.type == 'uploader' && parseInt(forced[0]) == 5) {
		// because the old uploader is sometimes version 5...sigh.
		force = true;
	}
	if (force && this.type == 'uploader') {
		Modalbox.show(UPGRADE_PLUGIN_LANGUAGE, '/download/update-plugin-ajax/' + this.type + '/', {width: 400, height:225, loadingString:''});
	}
	return force;
};

divx_plugin.prototype.older_version = function(new_version, version) {
	if (version.split) {
		var current = version.split('.');
		var nv = new_version.split('.');
		var is_older = false;
		for (var i = 0; i < 3; i++) {
			if (parseInt(nv[i]) > parseInt(current[i])) {
				is_older = true;
				break;
			}
			else if (parseInt(nv[i]) < parseInt(current[i])) {
				break;
			}
		}
		return is_older;
	}
	return false;
};

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
	     var ret = detectActiveXControl(this.activex);
	     if (this.type == 'player' && ret) {
	     	document.writeln('<script language="VBScript">');
			document.writeln('Function VBGetDWPVersion()');
			document.writeln('	on error resume next');
			document.writeln('	Dim dwpControl, dwpVersion');
			document.writeln('	dwpVersion = false');
			document.writeln('	set dwpControl = CreateObject("npdivx.DivXBrowserPlugin.1")');
			document.writeln('	if (IsObject(dwpControl)) then');
			document.writeln('		dwpVersion = dwpControl.GetVersion()');
			document.writeln('	end if');
			document.writeln('	VBGetDWPVersion = dwpVersion');
			document.writeln('End Function');
			document.writeln('</scr' + 'ipt>');
	     	this.version = VBGetDWPVersion();
	     }
	     return ret;
	}
	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)) {
	        	if (this.type == 'player') {
		        	var dwpPlugin = navigator.plugins[plugin];
		        	var index = dwpPlugin.description.indexOf('version ');
					if (index != -1) {
			    		this.version = dwpPlugin.description.substring(index+8);
					}
					else {
						// Older than 1.4
				    	this.version = '1.0.0';
					}
	        	}
	            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