var neoarchaic;
if (neoarchaic == undefined){
	neoarchaic = {}
}


neoarchaic.ObjectSwap = function(){

	var ie = (document.defaultCharset && document.getElementById && !window.home);
	var opera9 = false;
	if (ie){

		var ver=navigator.appVersion.split("MSIE")
		ver=parseFloat(ver[1])
		ie = (ver >=6)
	}else if (navigator.userAgent.indexOf("Opera")!=-1) {

		var versionindex=navigator.userAgent.indexOf("Opera")+6
		var verint=parseInt(navigator.userAgent.charAt(versionindex));
		if (verint==9 || verint == 1){
			opera9 = true;
		}
	}

	this.oswap = (ie || opera9);
	this.ie = ie;
	

	if (this.oswap){
		document.write ("<style id='hideObject'> object{display:none;} </style>");
	}
	this.addLoadEvents()
}


neoarchaic.ObjectSwap.prototype.addLoadEvents = function(){
	if (document.addEventListener){

		this.addEvent(document, "DOMContentLoaded", "init")
	}else{

		this.addEvent(document, "readystatechange", "init")
	}

	this.addEvent(window, "load", "init")
}


neoarchaic.ObjectSwap.prototype.addEvent = function(obj, evType, fn, listener){ 

	 if (listener == undefined){
	 	listener = this;
	 }
	
	 var e = {target: obj, type:evType}
	 this["handle"+fn] = function(){
		 listener[fn](e); 
	 }	 
	  var handlefn = this["handle"+fn]
	 if (obj.addEventListener){

	   obj.addEventListener(evType, handlefn, false); 
	   return true; 
	 } else if (obj.attachEvent){

	   var r = obj.attachEvent("on"+evType, handlefn); 
	   return r; 
	 } else { 
	   return false; 
	 } 
}


neoarchaic.ObjectSwap.prototype.init = function(){

	if (this.isInit) return;
	this.isInit = true;
	if (!document.getElementsByTagName){
		return;
	}

	var stripQueue = [];

	var objects = document.getElementsByTagName('object');
	for (var i=0; i<objects.length; i++){			
		var o = objects[i];	
		var h = o.outerHTML;

		var params = "";
		this.hasFlash = true;
		for (var j = 0; j<o.childNodes.length; j++) {
			var p = o.childNodes[j];
			if (p.tagName == "PARAM"){
				
				if (p.name == "flashVersion"){
					this.hasFlash = this.detectFlash(p.value);
					if (!this.hasFlash){
						
						o.id = (o.id == "") ? ("stripFlash"+i) : o.id;
						stripQueue.push(o.id);
						break;
					}
				} 
				params += p.outerHTML;		       
			}
		}	
		if (!this.hasFlash){
			continue;
		}		

		if (!this.oswap){
			continue;
		} 
		//Avoid specified objects, marked with a "noswap" classname
		if (o.className.toLowerCase().indexOf ("noswap") != -1){
			continue;
		}		
		//Get the tag and attributes part of the outer html of the object
		var tag = h.split(">")[0] + ">";			
		//Add up the various bits that comprise the object:
		//The tag with the attributes, the params and it's inner html
		var newObject = tag + params + o.innerHTML + " </OBJECT>";	
		//And rewrite the outer html of the tag 
		o.outerHTML = newObject;
	}
	//Strip flash objects
	if (stripQueue.length) {
		this.stripFlash(stripQueue);
	}
	//Make the objects visible again
	if (this.oswap){
		document.getElementById("hideObject").disabled = true;
	}
}

neoarchaic.ObjectSwap.prototype.detectFlash = function(version){
	if(navigator.plugins && navigator.plugins.length){
		//Non-IE flash detection.
		var plugin = navigator.plugins["Shockwave Flash"];
		if (plugin == undefined){
			return false;
		}
		var ver = navigator.plugins["Shockwave Flash"].description.split(" ")[2];
		return (Number(ver) >= Number(version))
	} else if (this.ie && typeof (ActiveXObject) == "function"){
	//IE flash detection.
		//Try 3 versions higher than specified	
		var maxCount = Number(version) + 3;
		for(var i=version; i<=maxCount; i++){
			try{
				var flash = new ActiveXObject("ShockwaveFlash.ShockwaveFlash." + i);
				return true;
			}
			catch(e){
				//continue			
			}
		}
		return false;
	}
	//Catchall - skip detection
	return true;
}

//Loop through an array of ids to strip
//Replace the object by a div tag containing the same innerHTML.
//To display an alternative image, message for the user or a link to the flash installation page, place it inside the object tag.  
//For the usual object/embed pairs it needs to be enclosed in comments to hide from gecko based browsers.
neoarchaic.ObjectSwap.prototype.stripFlash = function (stripQueue){
	if (!document.createElement){
		return;
	}
	for (var i=0; i<stripQueue.length; i++){
		var o = document.getElementById(stripQueue[i]);
		var newHTML = o.innerHTML;	
		//Strip the comments
		newHTML = newHTML.replace(/<!--\s/g, "");
		newHTML = newHTML.replace(/\s-->/g, "");
		//Neutralise the embed tag
		newHTML = newHTML.replace(/<embed/gi, "<span");		
		//Create a new div element with properties from the object
		var d = document.createElement("div");
		d.innerHTML = newHTML;
		d.className = o.className;
		d.id = o.id;
		//And swap the object with the new div
		o.parentNode.replaceChild(d, o);
	}
}

if (neoarchaic.objswap == undefined){
	neoarchaic.objswap = new neoarchaic.ObjectSwap();
}




