/**
 * 
 *
 */

self.onerror = null; // suppress all js errors

// browser detection
sys_av = navigator.appVersion.toLowerCase();
sys_ua = navigator.userAgent.toLowerCase();
sys_mac = (sys_av.indexOf('mac')>=0);
sys_osx = (sys_ua.indexOf('osx')>=0) || (sys_ua.indexOf('os x')>=0);
sys_win = (sys_av.indexOf('win')>=0);
sys_opera = (sys_ua.indexOf('opera')>=0)?parseFloat(sys_ua.slice(sys_ua.indexOf('opera')+6)):0;
sys_gecko = ((sys_ua.indexOf('gecko')>=0) && (!sys_opera))?parseFloat(sys_ua.slice(sys_ua.indexOf('rv:')+3)):0;
sys_safari = (sys_ua.indexOf('applewebkit')>=0)?parseFloat(sys_ua.slice(sys_ua.indexOf('applewebkit')+12)):0;
sys_camino = (sys_ua.indexOf('camino')>=0)?1:0;
sys_ie	= ((sys_ua.indexOf('msie')>=0) && (!sys_opera))?parseFloat(sys_ua.slice(sys_ua.indexOf('msie')+5)):0;
sys_iewin = (sys_win)?sys_ie:0;
sys_ns	= (document.layers)?parseFloat(sys_av):0;

// treat safari as gecko
if (sys_safari > 85) sys_gecko = 1.7; 
else if (sys_safari > 1) sys_gecko = 1;

// flash detection
sys_flash=0;
sys_flashsub=0;
sys_maxflashver=8;
if (sys_ie && sys_win) {
	for (sys_i=2;sys_i<=sys_maxflashver;sys_i++) { eval('var sys_fl'+sys_i+'=false'); }
	document.write('<SCR'+'IPT LANGUAGE=VBScript\>\n');
	document.write('on error resume next \n');
	for (sys_i=2;sys_i<=sys_maxflashver;sys_i++) { document.write('sys_fl'+sys_i+'=(IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.'+sys_i+'")))\n'); }
	document.write('</SCR'+'IPT\>\n');
	for (sys_i=2;sys_i<=sys_maxflashver;sys_i++) { if (eval('sys_fl'+sys_i)==true) sys_flash=sys_i; }
} else if (navigator.mimeTypes && navigator.mimeTypes['application/x-shockwave-flash'] && navigator.mimeTypes['application/x-shockwave-flash'].enabledPlugin) {
	var sys_plugname=navigator.plugins['Shockwave Flash'].description;
	sys_flash=parseInt(sys_plugname.substring(sys_plugname.indexOf('.')-1));
	sys_flashsub=parseInt(sys_plugname.substring(sys_plugname.indexOf('r')+1));
}

sys_flash=parseFloat(sys_flash+'.'+sys_flashsub);

/**
 * Capabilities object.
 *
 * @author	Ruben Gersons
 * @version 0.1 [2005.09.28]
 * @param	
 */  
sys_cap = new Object();

sys_cap.flash = sys_flash;
// Object tag
if (sys_iewin>=4) sys_cap.object = 1;
// Flash transparency
if (sys_cap.object || sys_opera>=8 || ((sys_osx || sys_win) && sys_gecko>=1 && sys_cap.flash>=6.65)) sys_cap.trans = 1;
// Support stacked flash movies
if (sys_osx && sys_gecko>1.5) {
	if (sys_camino==0) sys_cap.stackswf = 1;
} else if (sys_cap.css && !(sys_opera > 0 && sys_opera < 8) && !(sys_opera && sys_mac)) sys_cap.stackswf = 1;
// Support flash clipping
if (sys_cap.css && sys_win && !(sys_opera > 0)) sys_cap.clip = 1;
// Javascript to flash communication
// TODO: check browser jstoflash support
if (sys_iewin>=4 || sys_opera>=6 || sys_gecko>1.4 ) sys_cap.jstoflash = 1;
// FSCommand support
if (sys_cap.object) sys_cap.fscommand = 1;
// Support createElement
if (sys_gecko) sys_cap.create = 1;
// Support insertAdjacent
if (sys_iewin >= 4 || sys_opera >= 6) sys_cap.adjacent = 1;
// Dynamically insert HTML elements
if (sys_cap.create || sys_cap.adjacent) sys_cap.insert = 1;
// Strict mode in ie6
if (sys_iewin && document.compatMode && document.compatMode != "BackCompat") sys_cap.compat = 1;
// Event caps
if (sys_gecko) sys_cap.event_listener=1;
if (sys_iewin>=5 || sys_opera>=8) sys_cap.event_attach=1;
if (sys_opera>=7 || sys_gecko || sys_ie>=6) sys_cap.setatttributes=1;

function GetObj(id){return $(id);}

/**
 * Little helper function to output an object.
 *
 * @param	object	An object.   
 */   
function printr(oElement)
{	
	var str = "";
	for (var e in oElement)
		str += "e: " +e +" this[e]: " +oElement[e] +"\n";		
	alert(str);
}

/**
 * function to check if a variable exists, is set, is not null and is not empty
 *
 * @param	anything	Any kind of type  
 */ 
function IsSet(anything)
{
	return !Object.isUndefined(anything);
}

/**
 * Add arguments to a URL. Check if the URL already has arguments
 *
 * @param	sUrl		String
 * @parem	sArgs	String
 */ 
function AddArgs( sUrl, sArgs ) {	
	if (sUrl.indexOf('?')>0) return(sUrl+'&'+sArgs); else return(sUrl+'?'+sArgs); 
}

/**
 * Add eventlistener wrapper so we can add events in IE and 
 * engines which implement the standard of JS, such as Gecko.
 * 
 * @param		string	sID			The ID of the HTML-object
 * @param		string	sType		The event to catch, i.e.:
 *  
 * - change
 * - click
 * - mouseover
 * 								 	NOTE: The sType is the event
 *
 * @param		string	sFunction	Function to call (not between quotes)
 * @example:
 * 
 *  AddEventListener(
 *  	  'image_select_6d58f2'
 *  	  ,'change'
 *  	  ,ShowSelectedImage
 *  	  ,false)   								 	           
 */
 
function AddEventListener(sId, sName, oFunction, bUseCapture, aParams)
{
	Event.observe($(sId), sName, oFunction, bUseCapture);
   	
	// Create event parameter array.
	if (!document.maEventParams) document.maEventParams = new Array();

	// TODO: check if this works and is still needed (Lucas)
	// Add event parameters
	if (aParams)
	{
		document.maEventParams[document.maEventParams.length] =
		{
			 "s_id"		: sId
			,"s_type"	: sName
			,"a_params"	: aParams
		}
	}
}

// @tmp to be removed Diederick Huijbers <diederick@rhinocreations.com>	
function GetEventParams(sID, sType)
{
	if (!document.maEventParams) return; // Safety check.
	for (var i in document.maEventParams)
	{
		if (document.maEventParams[i].s_id == sID 
			&& document.maEventParams[i].s_type == sType)
			return (document.maEventParams[i]);
	}
}

/**
 * Use this function together with the AddEventListener to get
 * the object which caused the event.   
 *
 * @param		object	oEvent					A event object.
 * @return		object							The DOM (X/HTML object)		
 */
function EventToObj(oEvent)
{
	if (oEvent.srcElement)
		return (oEvent);
	else if (oEvent.target)
		return (oEvent.target);
}

/**
 * Set the height/fold of an object. Use this to scale flash movies
 *
 * @param	sDiv	String
 * @parem	iHeight	Integer
*/
function setFold( sDiv, iHeight) {
	if (sys_gecko) {
		GetObj(sDiv).style.height = iHeight+'px';
	} else {
		GetObj(sDiv).style.pixelHeight = iHeight;
	}
}

/**
 * Writes a flash tag to the page, if needed with javascript/fscommand connector
 *
 * @param	sRefstr		String	Unique ref strings, if empty ('') this function generates one
 * @param	sSrc		String	Flash src
 * @parem	sArgs		String	Name values argument string (no ?)
 * @param	iWidth		Int		width
 * @param	iHeight		Int
 * @param	sWmode		String
 * @param	sScale		String
 * @param	sSalign		String
 * @param	sFunction	String	Name of a function that is called by the flashmovie
 */ 
 
function swfWrite( sRefstr, sSrc, sArgs, iWidth, iHeight, sWmode, sScale, sSalign, sFunction ) {
	if (!IsSet(sWmode)) sWmode='opaque';
	if (!IsSet(sScale)) sScale='noScale';
	if (!IsSet(sSalign)) sSalign='LT';
	
	if (!IsSet(sRefstr)) sRefstr='ref'+(new Date()).getTime();
	
	if (IsSet(sArgs)) sArgs+='&'; else sArgs='';
	sArgs+='sys_host='+location.host;
	sArgs+='&sys_method='+((sys_cap.fscommand)?'fs':'js');
	sArgs+='&sys_refstr='+sRefstr;
	
	// write javascript incomming communicator
	if (!sys_cap.fscommand && IsSet(sFunction)) eval(sRefstr+'_command=function(sAction,sArgs){'+sFunction+'(sAction,sArgs);}');

	var s_tag = '';

	if (sys_cap.object) {
		s_tag+='<object id="'+sRefstr+'" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=4,0,2,0" width="'+iWidth+'" height="'+iHeight+'">';
		if (sys_cap.flash >= 6) {
			s_tag+='<param name="movie" value="'+sSrc+'"><param name="flashvars" value="'+sArgs+'">';
		} else {
			s_tag+='<param name="movie" value="'+AddArgs(sSrc,sArgs)+'">';
		}
		s_tag+='<param name="menu" value="false"><param name="quality" value="high"><param name="wmode" value="'+sWmode+'"><param name="salign" value="'+sSalign+'"><param name="scale" value="'+sScale+'"></object>';
		
		// write IE fscommand incomming cummunicator
		if (sys_cap.fscommand && IsSet(sFunction)) s_tag+='<scr'+'ipt for="'+sRefstr+'" event="FSCommand(sAction,sArgs)">'+sFunction+'(sAction,sArgs);</scr'+'ipt>';
	} else {
		if (sys_cap.flash >= 6) {
			s_tag+='<embed src="'+sSrc+'" flashvars="'+sArgs+'"';
		} else {
			s_tag+='<embed src="'+AddArgs(sSrc,sArgs)+'"';
		}
		//TODO: check all mozilla version for order of scale and salign parameters
		//difference in 1.6 and 1.7 versions
		if (sys_gecko==1.6) {
			s_tag+=' salign="'+sSalign+'" scale="'+sScale+'"';
		} else {
			s_tag+=' scale="'+sScale+'" salign="'+sSalign+'"';
		}
		s_tag+=' quality="high" wmode="'+sWmode+'" width="'+iWidth+'" height="'+iHeight+'" id="'+sRefstr+'" name="'+sRefstr+'" swLiveConnect="true" menu="false" type="application/x-shockwave-flash"></embed>';
		
		// write non IE fscommand incomming cummunicator
		if (sys_cap.fscommand && IsSet(sFunction)) eval(sRefstr+'_DoFSCommand=function(sAction,sArgs){'+sFunction+'(sAction,sArgs);}');
	}
	document.write(s_tag);
}

/**
 * This function is used with the FireBug extension of
 * FireFox to log messages to the console of FireBug! (very nice) 
 *
 */  
function printfire()
{
    if (document.createEvent)
    {
        printfire.args = arguments;
        var ev = document.createEvent("Events");
        ev.initEvent("printfire", false, true);
        dispatchEvent(ev);
    }
}