var detectableWithVB = false;

function JS3DMLWObject(containerId, source, width, height, version, npdisplayType, noplugindata){
    this._source = source;
    this._width = width;
    this._height = height;
    this._noplugindata = noplugindata;
    this._containerId = containerId;
    this._id = '';
    this.container = undefined;
    this._npdisplayType = npdisplayType;
    this.downloadURL = 'http://www.3dmlw.com/download/';
    this.npImageURL = 'no_plugin.png';
	this.os = 'windows';
	this.userAgent = '';
	this._version = version;
    
    this.writeEmbed = function(){
	    var html = '<embed id="'+this._id+'" width="'+this._width+'" height="'+this._height+'" source="'+this._source+'" type="text/3dmlw" pluginspage="'+this.getDownloadURL()+'" style="z-index: 0;"></embed>';
		if(this.container)
			this.container.innerHTML = html;
    }
    
    this.display = function(){
	    if(this.canDetectPlugins()){
			if(this.detect3DMLW()){
				this.writeEmbed();
			}
			else {
				this.getSpec();
				this.writeNoPlugin();
				this.checkIfIsInstalledLoop();
			}
		} else {
			this.writeEmbed();
		}
	}
	
	this.getSpec = function (){
		this.os = this.getOS();
		this.userAgent = this.getUserAgent();
	}
	
	this.getOS = function (){
		var OSName="Unknown OS";
		if (navigator.appVersion.indexOf("Win")!=-1) OSName="windows";
		if (navigator.appVersion.indexOf("Mac")!=-1) OSName="macos";
		if (navigator.appVersion.indexOf("X11")!=-1) OSName="unix";
		if (navigator.appVersion.indexOf("Linux")!=-1) OSName="linux";
		
		return OSName;
	}
	
	this.getUserAgent = function (){
		var sBrowser = navigator.userAgent;
		if (sBrowser.toLowerCase().indexOf('msie') > -1) this.userAgent = 'ie';
		else if (sBrowser.toLowerCase().indexOf('firefox') > -1) this.userAgent = 'firefox';
		else this.userAgent = 'mozilla';
	}
	
	this.writeNoPlugin = function (){
		switch(this._npdisplayType){
			case JS3DMLWObject.displayType.DefaultScreenshot:
				this.writeDefaultScreenshot();
			break;
			case JS3DMLWObject.displayType.HiddenObject:
				this.showHiddenObject();
			break;
			case JS3DMLWObject.displayType.SimpleHTML:
				if(this.container)
					this.container.innerHTML = this._noplugindata;
			break;
			default:
				this.writeDefaultScreenshot();
		}
	}
	
	this.getDownloadURL = function(){
		if(this._version > 0){
			return this.downloadURL + this.os + "/" + this._version + "/" ;
		}
		else {
			return this.downloadURL + this.os + "/latest/" ;
		}
	}
	this.writeDefaultScreenshot = function(){
		var npImage = new Image();
		npImage.src = this.npImageURL;
		var html = '<div style="width: '+this._width+'px; height: '+this._height+'px; background-image: url('+this._noplugindata+'); background-repeat: no-repeat;">';

			//if the image isn't loaded yet
			var count = 500000;
			while((!npImage.complete || npImage.width <= 0) && count > 0){
				count = count - 1;
			}
			
			if(!npImage.complete || npImage.width <= 0){
				html += '<a href="'+(this.getDownloadURL())+'"><div style="font-size: 14px; color: blue; font-family: arial; font-weight: bold; background-color: white; text-align: center; padding-top: 10px; left: '+((width/2)-(302/2))+'px; width: 302px; cursor: pointer; position: relative; top: '+((this._height/2)-(17))+'px; height: 24px;">Click here to install the 3DMLW plug-in</div></a>';
			} else {
				html += '<a href="'+(this.getDownloadURL())+'"><div style="left: '+((width/2)-(npImage.width/2))+'px; background-image: url('+this.npImageURL+'); width: '+npImage.width+'px; cursor: pointer; position: relative; top: '+((this._height/2)-(npImage.height/2))+'px; height: '+npImage.height+'px;" title="Click image to install the 3DMLW plug-in"></div></a>';
			}
		html += '</div>';
		if(this.container)
			this.container.innerHTML = html;
	}
	
	this.showHiddenObject = function(){
		var hiddenObject = this.getStyleObject(this._noplugindata);
		if(hiddenObject != null && hiddenObject){
			var newobject = hiddenObject.cloneNode(true);
			
			if(newobject.style.display == "none")
				newobject.style.display = "block";
				
			if(newobject.style.visibility == "hidden")
				newobject.style.visibility="visible";
				
			this.container.appendChild(newobject);
		}
	}
	
	
	this.getStyleObject = function(objectId) {
		if(document.getElementById && document.getElementById(objectId)) {
			use_gebi = true;
			return document.getElementById(objectId);
		} else if (document.all && document.all(objectId)) {
			use_css = true;
			return document.all(objectId).style;
		} else if (document.layers && document.layers[objectId]) {
			use_layers = true;
			return document.layers[objectId];
		} else {
			return false;
		}
	}
	
	this.canDetectPlugins = function () {
	    if(detectableWithVB || (navigator.plugins && navigator.plugins.length > 0) ) {
			return true;
	    } else {
			return false;
	    }
	}
	
	this.detect3DMLW = function () {
		var pluginFound = this.detectPlugin('3DMLW Plugin'); 
		if(!pluginFound && navigator.userAgent.indexOf('MSIE') != -1) {
			pluginFound = detectActiveXControl('3DMLW.IEPlugin');
		}
		return pluginFound;
	}
	
	this.setId = function (newId){
		this._id = newId;
	}
	
	this.setNoPluginImage = function (newImageUrl){
		this.npImageURL = newImageUrl;
		if (document.images){
			preimg = new Image(10,10); 
			preimg.src = this.newImageUrl; 
		}
	}
	
	this.initialize = function (){
	    this.container = this.getStyleObject(this._containerId);
		
		if (document.images){
			preimg = new Image(10,10); 
			preimg.src = this.npImageURL; 
		}

    }
	
    this.initialize();
	
	this.detectPlugin = function (pluginName) {
		var pluginFound = false;
		// if plugins array is there and not fake
		if (navigator.plugins && navigator.plugins.length > 0) {
			var pluginsArrayLength = navigator.plugins.length;
			for (pluginsArrayCounter=0; pluginsArrayCounter < pluginsArrayLength; pluginsArrayCounter++ ) {
				if( (navigator.plugins[pluginsArrayCounter].name.indexOf(pluginName) >= 0) || 
				  (navigator.plugins[pluginsArrayCounter].description.indexOf(pluginName) >= 0) ) {
					pluginFound = true;
				}   
			}
		}
		return pluginFound;
	}

	this.checkIfIsInstalledLoop = function() {
		navigator.plugins.refresh(false);
		if (this.detect3DMLW()) {
			if (this.os.indexOf('windows') >= 0) {
				document.location.hash = 'do3DMLW=SUCCESS';
			} else if (this.os.indexOf('macos') >= 0) {
				if (this.userAgent.indexOf('firefox') >= 0) {
					document.location.hash = 'do3DMLW=SUCCESS';
				} else {
					document.location.hash = 'do3DMLW=RESTART';
				}
			}
			window.location.reload(false);
		} else {
			var self = this;
			setTimeout(function(){self.checkIfIsInstalledLoop();}, 2000);
		}
	} 
	
// Here we write out the VBScript block for MSIE Windows
if ((navigator.userAgent.indexOf('MSIE') != -1) && (navigator.userAgent.indexOf('Win') != -1)) {
    document.writeln('<script language="VBscript">');

    document.writeln('\'do a one-time test for a version of VBScript that can handle this code');
    document.writeln('detectableWithVB = False');
    document.writeln('If ScriptEngineMajorVersion >= 2 then');
    document.writeln('  detectableWithVB = True');
    document.writeln('End If');

    document.writeln('\'this next function will detect most plugins');
    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>');
}
}

JS3DMLWObject.displayType = { DefaultScreenshot:0, HiddenObject:1, SimpleHTML:2 };


