// ## ##################################################################### ##
// ##						InjectEolasObject.class.js						##
// ## ©1998-2006 Copyright Scheepens reclame adviseurs Tilburg Netherlands	##
// ##																		##
// ## 		     i:http://www.scheepens.nl  e:info@scheepens.nl 		  	##
// ## Scheepens Reclame Adviseurs; p.o.box 954; 5000AZ Tilburg; Netherlands ##
// ## ##################################################################### ##
// ## 22.05.2006 versie 1 door Huub											##
// ## ##################################################################### ##
// ## __CHANGES__															##
// ## ##################################################################### ##
// ## __TODO__																##
// ## 	* De originele patch verbeteren zodat er een goede patch onstaat	##
// ## ##################################################################### ##

/* ## ##################################################################### ##

Initialisatie:
	
	new EolasObjectInjection;
	
	
Methoden:
	
	Snelle Creatie van objecten (shortcuts):
	
		EolasObject.createFlashObject( string file, integer width, integer height, string quality, string version );
			Creeert een FlashObject in de pagina
		
		EolasObject.createMediaPlayerObject( string file, integer width, integer height );
			Creeert een MediaPlayerObject in de pagina
		
		EolasObject.createQuickTimePlayerObject( string file, integer width, integer height );
			Creeert een QuicktimePlayer Object in de pagina
		
		EolasObject.createRealPlayerObject( string file, integer width, integer height );
			Creeert een RealPlayerobject Object in de pagina
		
	
	Uitgebreid:
	
		EolasObject.setObjectAttribute( string atribuutnaam, string attribuutwaarde );
			Stelt een attribuut naam en waarde in, alleen als er geen "shortcuts" worden gebruikt. 
			Zovaak aanroepen als nodig is
			
		EolasObject.setObjectParameter( string paramnaam, string paramwaarde );
			Stelt een param naam en waarde in, alleen als er geen "shortcuts" worden gebruikt.
			Zovaak aanroepen als nodig is
		
		EolasObject.setEmbedAttribute( string atribuutnaam, string attribuutwaarde );
			Stelt een Embed atribuut naam en waarde in, alleen als er geen "shortcuts" worden gebruikt.
			Zovaak aanroepen als nodig is
		
		EolasObject.setPlaceHolder( string placeholder id );
			Indien er gebruik gemaakt wordt van een placeholder div, kan er gebruik worden gemaakt van deze functie. 
			Zo niet wordt de output rechtstreeks naar de browser geschreven op de plaats waar deze wordt aangeroepen.
		
		EolasObject.RenderObject();
			Maakt het object aan en zet deze of in de DocumentObjectModel of schrijft deze rechtstreeks naar de browser.
			
		EolasObject.applyPatch();
			Niet gebruiken, originele patch gebruiken.
	
   ## ##################################################################### ##
   
voorbeelden:

	Invoegen via addEvent ( script wordt in de head van de pagina geplaats )
	
	<head>
	<script type="text/javascript">
		function loadEolas(){
			var oFlash = new EolasObjectInjection;
		
			oFlash.createFlashObject( "swffile.swf", width, height, "quality", "versionstring" );
			oF.setPlaceHolder( DOM id );
			oF.RenderObject();
		}
		
		addEvent( window, load, loadEolas, false );
	</script>
	</head>
	
	
	Invoegen op de plaats waar deze hoort.. Zorgt dat er een flashobject wordt toegevoegd op de plaats waar het script staat
	
		<script type="text/javascript">
			var f = new EolasObjectInjection;
			f.setObjectAttribute( "attribuutnaam", "attribuutwaarde" );
			f.setObjectParam( "paramnaam", "paramwaarde" );
			f.setEmbedAttribute( "embedattribuutnaam", "embedattribuutwaarde" );
			f.RenderObject();
		</script>
	
   ## ##################################################################### ##
*/

function EolasObjectInjection(){
	
	this.version = {
		major: 1,
		minor: 0,
		build: 0
	}
	
	this.versionstring = this.version.major + "." + this.version.minor + "." + this.version.build;
	
	this.objAttributes = Array();
	this.objParameters = Array();
	this.embedAtrributes = Array();
	this.objPlaceholder;
	
	this.createFlashObject = function( sFile, iWidth, iHeight, sQuality, sVersion ){
		if( sVersion = "" ){
			sVersion = "7,0,19,0";
		}
	
		this.setObjectAttribute( "classid", "clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" );
		this.setObjectAttribute( "codebase", "http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=" + sVersion );
		
		this.setEmbedAttribute( "pluginspage", "http://www.macromedia.com/go/getflashplayer" );
		this.setEmbedAttribute( "type", "application/x-shockwave-flash" );
		
		this.setObjectAttribute( "width", iWidth );
		this.setObjectAttribute( "height", iHeight );
		
		//this.setObjectParam( "Play", "1" );
		this.setObjectParam( "Movie", sFile );
		this.setObjectParam( "Quality", sQuality );
		
		this.setEmbedAttribute( "src", sFile );
		this.setEmbedAttribute( "quality", sQuality );
		this.setEmbedAttribute( "width", iWidth );
		this.setEmbedAttribute( "height", iHeight );
	}
	
	this.createMediaPlayerObject = function( sFile, iWidth, iHeight ){
		
		this.setObjectAttribute( "classid", "clsid:22d6f312-b0f6-11d0-94ab-0080c74c7e95" );
		
		this.setObjectParam( "AutoStart", "true" );
		this.setObjectParam( "ShowControls", "true" );
		this.setObjectParam( "ShowStatusBar", "false" );
		this.setObjectParam( "ShowDisplay", "false" );
		this.setObjectParam( "AutoRewind", "false" );
		this.setObjectParam( "Wmode", "transparent" );
		
		this.setEmbedAttribute( "type", "application/x-mplayer2" );
		this.setEmbedAttribute( "pluginspage", "http://www.microsoft.com/Windows/Downloads/Contents/MediaPlayer/" );
		
		this.setEmbedAttribute( "autostart", "true" );
		this.setEmbedAttribute( "showcontrols", "true" );
		this.setEmbedAttribute( "showstatusbar", "false" );
		this.setEmbedAttribute( "showdisplay", "false" );
		this.setEmbedAttribute( "autorRewind", "false" );
		this.setEmbedAttribute( "wmode", "transparent" );
		
		this.setObjectAttribute( "width", iWidth );
		this.setObjectAttribute( "height", ( iHeight + 46 ));
		
		this.setObjectParam( "filename", sFile );
		
		this.setEmbedAttribute( "src", sFile );
		this.setEmbedAttribute( "width", iWidth );
		this.setEmbedAttribute( "height", ( iHeight + 46 ) ); 
	}
	
	this.createQuickTimePlayerObject = function( sFile, iWidth, iHeight ){
		
		this.setObjectAttribute( "classid", "clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" );
		this.setObjectAttribute( "codebase", "http://www.apple.com/qtactivex/qtplugin.cab" );
		
		this.setObjectParam( "autoplay", "true" );
		this.setObjectParam( "loop", "false" );
		this.setObjectParam( "controller", "true" );
		
		this.setEmbedAttribute( "type", "video/quicktime" );
		this.setEmbedAttribute( "pluginspage", "http://www.apple.com/quicktime/download/" );
		
		this.setEmbedAttribute( "autoplay", "true" );
		this.setEmbedAttribute( "loop", "false" );
		this.setEmbedAttribute( "controller", "true" );
		
		this.setObjectAttribute( "width", iWidth );
		this.setObjectAttribute( "height", ( iHeight + 13 ));
		
		this.setObjectParam( "src", sFile );
		
		this.setEmbedAttribute( "src", sFile );
		this.setEmbedAttribute( "width", iWidth );
		this.setEmbedAttribute( "height", ( iHeight + 13 ) ); 
	}
	
	this.createRealPlayerObject = function( sFile, iWidth, iHeight ){
		
		this.setObjectAttribute( "classid", "clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA" );
		
		//this.setObjectParam( "CONTROLS", "ControlPanel" );
		//this.setObjectParam( "CONTROLS", "ImageWindow" );
		this.setObjectParam( "autostart", "true" );
		
		this.setEmbedAttribute( "type", "audio/x-pn-realaudio-plugin" );
		//this.setEmbedAttribute( "CONTROLS", "ControlPanel" );
		//this.setEmbedAttribute( "CONTROLS", "ImageWindow" );
		//this.setEmbedAttribute( "CONTROLS", "Status" );
		this.setEmbedAttribute( "autostart", "true" );
		
		this.setObjectAttribute( "width", iWidth );
		this.setObjectAttribute( "height", ( iHeight + 13 ));
		
		this.setObjectParam( "src", sFile );
		
		this.setEmbedAttribute( "src", sFile );
		this.setEmbedAttribute( "width", iWidth );
		this.setEmbedAttribute( "height", ( iHeight + 13 ) ); 
	}
	
	this.setObjectAttribute = function( name, value ){
		this.objAttributes[this.objAttributes.length] = Array( name, value );
	}
	
	this.setObjectParam = function ( name, value ){
		this.objParameters[this.objParameters.length] = Array( name, value );
	}
	
	this.setEmbedAttribute = function ( name, value ){
		this.embedAtrributes[this.embedAtrributes.length] = Array( name, value );
	}
	
	this.setPlaceHolder = function( sPlaceHolder ){
		this.objPlaceholder = document.getElementById( sPlaceHolder );
	}
	
	this.RenderObject = function(){
			
		if( this.objPlaceholder ){
			this.__DomInsertion();
		}
		else{
			this.__writeObjectToPage();
		}
	}

	this.applyPatch = function(){
		
		if( window.navigator.appVersion.search( /MSIE/gi ) != -1 ){
	 		alert( "DEBUG:: Do not use this method, please use original patch!" );
			return;
			
			aObjectsInPage = document.getElementsByTagName( "object" );
			
			for( var i=0; i < aObjectsInPage.length; i++ ){
				
				// Original Patch:
				oParent = aObjectsInPage[i].parentNode;
				
				sInsertHTML = oParent.innerHTML;
				oParent.removeChild( aObjectsInPage[i] );
				oParent.innerHTML = sInsertHTML;
				
				/*
				// new Patch <-- Doesn't work!!
				
				this.objAttributes = null;
				this.objParameters = null;
				this.embedAtrributes = null;
				
				this.objAttributes = Array();
				this.objParameters = Array();
				this.embedAtrributes = Array();

				var thisObject = aObjectsInPage[i];
				
				var aAttributes = thisObject.attributes;
				for( j=0; j< aAttributes.length; j++ ){
					if( aAttributes[j].value != "" && aAttributes[j].value != null && aAttributes[j].value != "null" ){
						this.objAttributes[this.objAttributes.length] = Array( aAttributes[j].name, aAttributes[j].value );
					}
				}
				
				var aParams = thisObject.getElementsByTagName( "param" );
				for( k=0; k < aParams.length; k++ ){
					this.objParameters[this.objParameters.length] = Array( aParams[k].name, aParams[k].value );
				}
				
				this.objParameters[this.objParameters.length] = Array( "Play", "1" );
				
				oParent = aObjectsInPage[i].parentNode
				oNewElement = this.__returnObject();
				
				oParent.replaceChild( oNewElement, aObjectsInPage[i] );
				*/
			}
		}
	}
	
	
// ____ PRIVATE FUNCTIONS ______________________________________
	this.__writeObjectToPage = function(){
		
		document.writeln( "<object " );
		for( var i in this.objAttributes ){
			document.writeln( this.objAttributes[i][0] + "=\"" + this.objAttributes[i][1] + "\" ");
		}
		document.writeln( " >" )
		
		for( var i in this.objParameters ){
			document.writeln( "<param name=\"" + this.objParameters[i][0] + "\" value=\"" + this.objParameters[i][1] + "\" /> ");
		}
		
		document.writeln( "<embed " );
		for( var i in this.embedAtrributes ){
			document.writeln( this.embedAtrributes[i][0] + "=\"" + this.embedAtrributes[i][1] + "\" ");
		}
		document.writeln( " /> " );
		
		document.writeln("</object>");
		
	}
	
	this.__DomInsertion = function(){
		
		if( window.navigator.appVersion.search( /MSIE/gi ) == -1 ){
			// Not Internet Explorer, so perform complete DHTML insertion
			// IE can't append EMBED element to the OBJECT element
			
			// Create Object Element and append it to the placeholder
			var oEolasObject = document.createElement( "object" );
			oEolasObject.setAttribute( "id", "EOLAS_OBJECT_"+ Math.floor( Math.random() * 100 ) );
			this.objPlaceholder.appendChild( oEolasObject );
	
			// Adding attributes to the object element
			for( var i in this.objAttributes ){
				oEolasObject.setAttribute( this.objAttributes[i][0], this.objAttributes[i][1] );
			}
	
			// Creating, setting attributes and Appending PARAM element(s)
			for( var i in this.objParameters ){
				var oParam = document.createElement( "param" );
				oParam.setAttribute( "name", this.objParameters[i][0] );
				oParam.setAttribute( "value", this.objParameters[i][1] );
				oEolasObject.appendChild( oParam );
			}
		
			// Create EMBED element and append it to the Object Element
			var oEolasEmbed = document.createElement( "embed" );
			
			// Adding attributes to the Embed element
			oEolasEmbed.setAttribute( "id", "EMB_"+ Math.floor( Math.random() * 100 ) );
			for( var i in this.embedAtrributes ){
				oEolasEmbed.setAttribute( this.embedAtrributes[i][0], this.embedAtrributes[i][1] );
			}
			
			// Append EMBED element to OBJECT element
			oEolasObject.appendChild( oEolasEmbed );
		}
		
		else{
			// So IE can't append EMBED element so just replace innerHTML
			sObjectElement = "<object ";
			for( var i in this.objAttributes ){
				sObjectElement += this.objAttributes[i][0] + "=\"" + this.objAttributes[i][1] + "\" ";
			}
			sObjectElement += " > ";
			
			for( var i in this.objParameters ){
				sObjectElement += "<param name=\"" + this.objParameters[i][0] + "\" value=\"" + this.objParameters[i][1] + "\" /> ";
			}
			
			sObjectElement += "<embed ";
			for( var i in this.embedAtrributes ){
				sObjectElement += this.embedAtrributes[i][0] + "=\"" + this.embedAtrributes[i][1] + "\" ";
			}
			sObjectElement += " /> ";
			
			sObjectElement += "</object>";
			
			this.objPlaceholder.innerHTML = sObjectElement;
		}
	}
}

if( window.navigator.appVersion.search( /MSIE/gi ) != -1 ){
	if( !addEvent ){
		function addEvent(elm, evType, fn, useCapture){
			// cross-browser event handling for IE5+, NS6+ and Mozilla 
			// By Scott Andrew 
			if (elm.addEventListener) {
				elm.addEventListener(evType, fn, useCapture); 
				return true; 
			} 
			else if (elm.attachEvent) { 
				var r = elm.attachEvent('on' + evType, fn); 
				return r; 
			} 
			else {
				elm['on' + evType] = fn;
			}
		}
	}
}

//v1.0
//Copyright 2006 Adobe Systems, Inc. All rights reserved.
function AC_AddExtension(src, ext)
{
  if (src.indexOf('?') != -1)
    return src.replace(/\?/, ext+'?'); 
  else
    return src + ext;
}

function AC_Generateobj(objAttrs, params, embedAttrs) 
{ 
  var str = '<object ';
  for (var i in objAttrs)
    str += i + '="' + objAttrs[i] + '" ';
  str += '>';
  for (var i in params)
    str += '<param name="' + i + '" value="' + params[i] + '" /> ';
  str += '<embed ';
  for (var i in embedAttrs)
    str += i + '="' + embedAttrs[i] + '" ';
  str += ' ></embed></object>';

  document.write(str);
}

function AC_FL_RunContent(){
  var ret = 
    AC_GetArgs
    (  arguments, ".swf", "movie", "clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"
     , "application/x-shockwave-flash"
    );
  AC_Generateobj(ret.objAttrs, ret.params, ret.embedAttrs);
}

function AC_SW_RunContent(){
  var ret = 
    AC_GetArgs
    (  arguments, ".dcr", "src", "clsid:166B1BCA-3F9C-11CF-8075-444553540000"
     , null
    );
  AC_Generateobj(ret.objAttrs, ret.params, ret.embedAttrs);
}

function AC_GetArgs(args, ext, srcParamName, classid, mimeType){
  var ret = new Object();
  ret.embedAttrs = new Object();
  ret.params = new Object();
  ret.objAttrs = new Object();
  for (var i=0; i < args.length; i=i+2){
    var currArg = args[i].toLowerCase();    

    switch (currArg){	
      case "classid":
        break;
      case "pluginspage":
        ret.embedAttrs[args[i]] = args[i+1];
        break;
      case "src":
      case "movie":	
        args[i+1] = AC_AddExtension(args[i+1], ext);
        ret.embedAttrs["src"] = args[i+1];
        ret.params[srcParamName] = args[i+1];
        break;
      case "onafterupdate":
      case "onbeforeupdate":
      case "onblur":
      case "oncellchange":
      case "onclick":
      case "ondblClick":
      case "ondrag":
      case "ondragend":
      case "ondragenter":
      case "ondragleave":
      case "ondragover":
      case "ondrop":
      case "onfinish":
      case "onfocus":
      case "onhelp":
      case "onmousedown":
      case "onmouseup":
      case "onmouseover":
      case "onmousemove":
      case "onmouseout":
      case "onkeypress":
      case "onkeydown":
      case "onkeyup":
      case "onload":
      case "onlosecapture":
      case "onpropertychange":
      case "onreadystatechange":
      case "onrowsdelete":
      case "onrowenter":
      case "onrowexit":
      case "onrowsinserted":
      case "onstart":
      case "onscroll":
      case "onbeforeeditfocus":
      case "onactivate":
      case "onbeforedeactivate":
      case "ondeactivate":
      case "type":
      case "codebase":
        ret.objAttrs[args[i]] = args[i+1];
        break;
      case "width":
      case "height":
      case "align":
      case "vspace": 
      case "hspace":
      case "class":
      case "title":
      case "accesskey":
      case "name":
      case "id":
      case "tabindex":
        ret.embedAttrs[args[i]] = ret.objAttrs[args[i]] = args[i+1];
        break;
      default:
        ret.embedAttrs[args[i]] = ret.params[args[i]] = args[i+1];
    }
  }
  ret.objAttrs["classid"] = classid;
  if (mimeType) ret.embedAttrs["type"] = mimeType;
  return ret;
}
