/*
	Script created by Martin Kronstad for Siteman AS.
	www.siteman.no

	The purpose of this script is:
	1. Create flash-tags for IE6 (automaticaly activation, so we do not get ugly frames)
	2. Create flash-tags that support detection if the user has or needs a new plugin.
	3. Create flash-tags that support ALL browsers. (output different tags for different browsers)
	4. Centralize codebase for ALL <object> <embed> and other plugin-related code.
*/

/* Write an flashobject inside a div */
function writeFlash(height,width,movie,params){
	if(getBrowser() == 'Internet Explorer'){
		strFlash = createFlash_IE(height,width,movie,params);
		newid = CreateContainerID('flashdiv');
		writeNewDiv(newid);
		var newDiv = document.getElementById(newid);
		newDiv.innerHTML = strFlash;
	}
	else {
		objFlash = createFlash_NotIE(height,width,movie,params);
		writeNewDiv('flashdiv',objFlash);
	}
}

/* Create a flash object */
function createFlash_NotIE(height,width,movie,params){

	if(height && width){
		objObject = document.createElement('object');
		objObject.setAttribute('height',height);
		objObject.setAttribute('width',width);
		objObject.setAttribute('data',movie);
		objObject.setAttribute('type','application/x-shockwave-flash');
	}
	else {
		alert('Height, width AND movie is REQUIRED for createFlash (file: jsflash.js)!');
		return false;
	}

	/* add params if any */
	if(params){
		var newParams = '';
		var intNumParams = params.length;
		for(i=0;i<intNumParams;i++){
			var objParam = document.createElement('param');
			objParam.setAttribute('name',params[i][0]);
			objParam.setAttribute('value',params[i][1]);
			objObject.appendChild(objParam);
		}
	}
	return objObject;
}

/* Create a flash object */
function createFlash_IE(height,width,movie,params){

	if(height && width){
		strObject = '<object ';
		strObject += ' classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0"';
		strObject += ' height="' + height + '"';
		strObject += ' width="' + width + '"';
		strObject += ' data="' + movie + '"';
		strObject += ' type="application/x-shockwave-flash">';

	}
	else {
		alert('Height, width AND movie is REQUIRED for createFlash (file: jsflash.js)!');
		return false;
	}

	/* add params if any */
	var strParams = '';
	if(params){
		var intNumParams = params.length;
		for(i=0;i<intNumParams;i++){
			 strParams = strParams + '<param name="' + params[i][0] + '" value="' + params[i][1] + '" />';
		}
	}

	strObject += strParams;
	return strObject;
}

/* Testfunction for inserting images */
function writeImage(imageurl,imagealt){
	writeNewDiv('imagediv',createImage(imageurl,imagealt));
}

/* Testfunction for inserting text */
function writeText(text){
	writeNewDiv('textdiv',document.createTextNode(text));
}

/* ================================= INTERNAL FUNCTIONS ================================= */

/* for testing */
function createImage(imageurl,imagealt){
	var image = document.createElement('img');
	image.setAttribute('src',imageurl);
	if(imagealt) image.setAttribute('alt',imagealt);
	return image;
}

/*
	Calls CreateContainer, then fills the new div with an DOM-object.
*/
function writeNewDiv(id,obj){
	newid = CreateContainerID(id);
	/*
	var theBody = document.getElementsByTagName('body')[0];
	var newDiv = document.createElement('div');
	newDiv.id = newid;
	theBody.appendChild(newDiv);
	*/
	document.write('<div id="' + newid + '"></div>');
	if(obj){
		var container = document.getElementById(newid);
		container.appendChild(obj);
	}
}

/*
	Gets an uniqe ID and creatates an empty div.
*/
function CreateContainerID(ContainerID){

	if(document.getElementById(ContainerID)){
		return CreateContainerID(ContainerID + "_1");
	}
	else {
		return ContainerID;
	}

}

/* Browsercheck from http://www.quirksmode.org/js/detect.html */
var detect = navigator.userAgent.toLowerCase();
function getBrowser(){

	var browser,total,thestring;

	if (checkIt('konqueror'))
	{
		browser = "Konqueror";
		OS = "Linux";
	}
	else if (checkIt('safari')) browser = "Safari"
	else if (checkIt('omniweb')) browser = "OmniWeb"
	else if (checkIt('opera')) browser = "Opera"
	else if (checkIt('webtv')) browser = "WebTV";
	else if (checkIt('icab')) browser = "iCab"
	else if (checkIt('msie')) browser = "Internet Explorer"
	else if (!checkIt('compatible'))
	{
		browser = "Netscape Navigator"
		version = detect.charAt(8);
	}
	else browser = "An unknown browser";


	return browser;
}
function checkIt(string)
{
	place = window.detect.indexOf(string) + 1;
	thestring = string;
	return place;
}