/* 	Script for replacing titles with flashfiles 
*	for nicer fonts 
*/
/*
 * Adding some nice functions to the standard JS-API 
 */
if(!Array.prototype.in_array) {
	Array.prototype.in_array = function(value){
		for(i=0;i<this.length;i++) {
			if (this[i] == value) {
				return true;	
			}
		}
		return false;
	}
}
if(!String.prototype.urlencode) {
	String.prototype.urlencode = function(){
		var result = "";
		for (i = 0; i < this.length; i++) {
			if (this.charAt(i) == " ") result += "+";
			else result += this.charAt(i);
		}
		return escape(result);
	}
}

/* 
 * Including Flash detection 
 */
document.write('<script language="javascript" src="http://www.adcontact.se/js/flashdetect.js" ></script>');
document.write('<script language="VBScript" src="http://www.adcontact.se/js/flashmsdetect.js" ></script>');

/*
 * Prototyping the flash class
 */
function Flash() {
	this.swf = '';
	this.alt = ''; // Alternative image if swf fails
	this.width = '';
	this.height = '';
	this.id = 'myFlash';
	this.align = 'middle';
	this.properties = new Array();
	this.swfVersion = 6;
	this.useAlt = false;
	
	this.addProperty = function (prop, val) {
		this.properties[prop] = val;
	}
	
	this.disableCache = function() {
		var d = new Date();
		this.swf += "?"+d.getTime();	
	}
	
	this.setSize = function (sizeArray) {
		this.width = sizeArray[0];	
		this.height = sizeArray[1];			
	}
	
	this.toHTML = function() {
		var myParams = "";		
		var myAttr = "";
		for(var key in this.properties) {
			if (key == "in_array") { continue; }
			myParams 	+= '<param name="'+key+'" value="'+this.properties[key]+'"/>\n';
			myAttr 		+= key+'="'+this.properties[key]+'" ';			
		}
		
		var myHTML 	 = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" style="z-index: 500;" width="'+this.width+'" height="'+this.height+'" id="'+this.id+'" align="'+this.align+'">';
		myHTML 		+= '<param name="movie" value="'+this.swf+'" />';
		myHTML 		+= myParams;
		myHTML 		+= '<embed src="'+this.swf+'" width="'+this.width+'" height="'+this.height+'" name="'+this.id+'" align="'+this.align+'" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"'+myAttr+' /></object>';
		return myHTML;
	}
	
	this.toImage = function() {
		return '<img src="'+this.alt+'" width="'+this.width+'" height="'+this.height+'" id="'+this.id+'" alt="" />';	
	}
	
	this.render = function() {
		if (flashinstalled != 2 || (flashversion*1) < this.swfVersion) {
			if (this.useAlt == true) {
				document.write(this.toImage());
			} else {
				if (confirm('This site uses a newer version of flash than you currently have installed, would you like to proceed to macromedia.com and install it?')) {
					document.location = 'http://www.macromedia.com/flashplayer';
				} else {
					alert('Some functions on this site will be disabled until you upgrade your flash-player.');	
				}
			}
		} else {
			document.write(this.toHTML());
		}
	}
}


/*
 * Prototyping the title class
 */
function SXCT(parentContainer) {

	this.parentContainer = parentContainer;
	this.titles = new Array();
	this.replaceMentSwfs = new Array();
	this.replaceMentSwfVars = new Array();
	this.replaceMentSwfSize = new Array();
	/* Flash Settings */
	this.Flash = new Flash();
	this.defaultSwf = '';
	this.defaultSwfSize = new Array(200,100);
	this.requireFlashVersion = 6; // What version of flash to require
	this.passHrefs = true;
	this.where = new Array();
	
	/* Only change if tags arg is value */
	this.addWhere = function ( arg, value, newSwf ) {
		this.where.push(new Array(arg,value, newSwf));
	}
	
	this.addTitle = function ( myClass, myTag, myVars, mySwf, mySize ) {
		// Create array if it doesnt exists
		if (this.titles[myTag] == undefined) {
			this.titles[myTag] = new Array();
		}
		if (myVars == undefined) 	{ myVars = "string="; }
		if (mySwf == undefined) 	{ mySwf = this.defaultSwf; }
		if (mySize == undefined)	{ mySize = this.defaultSwfSize; }

		this.titles[myTag].push(myClass);
		// Populating the replacement array
		this.replaceMentSwfs[myClass] = mySwf;
		this.replaceMentSwfVars[myClass] = myVars;		
		this.replaceMentSwfSize[myClass] = mySize;				
	}
	
	this.changeContainer = function (newContainer) {
		this.parentContainer = 	newContainer;
	}
	
	this.executeWhere = function (tag) {
		if (this.where.length > 0) {
			if (this.where[0][0] == "id") {
				if (tag.id == this.where[0][1]) {
					return this.where[0][2];	
				}
			}
		} else {
			return false;
		}
	}
	
	/* To start the hunt from a certain ID, just set the parentcontainer */
	this.init = function (parentContainer) {

		if (flashinstalled != 2 || (flashversion*1) < this.requireFlashVersion) {
			return false;
		}
		if (this.parentContainer == undefined) {			
			var root = document;
		} else if (document.getElementById(this.parentContainer) != null) {
			var root = document.getElementById(this.parentContainer);
		} else {
			return false;
		}
		
		/* Traversing root to find titles */
		for (var key in this.titles) {
			tags = root.getElementsByTagName(key);	
			for(a=0; a<tags.length;a++) {
				/* Checking if I should replace this */
				if (this.titles[key].in_array(tags[a].className)) {
					text = new String(tags[a].innerHTML);
					// Look and see if this tag i affected by any where
					if (nSwf = this.executeWhere(tags[a])) {
						this.Flash.swf = nSwf;
					} else {
						this.Flash.swf = this.replaceMentSwfs[tags[a].className];
					}
					
					if (this.passHrefs == true && key == 'a') {
						flashVar = this.replaceMentSwfVars[tags[a].className]+text+'&uri='+tags[a].href;						
					} else {
						flashVar = this.replaceMentSwfVars[tags[a].className]+text;
					}
					this.Flash.addProperty('flashVars', flashVar);
					this.Flash.setSize(this.replaceMentSwfSize[tags[a].className]);
					tags[a].innerHTML = this.Flash.toHTML();
				}
			}
		}
		return true;
	}
}