/*
-----------------------------------------------
Wheel: Standard scripts
Version: $Id: mainscript.js,v 1.6 2006/07/26 12:43:51 heleno Exp $
----------------------------------------------- */

/* Load events
---------------------------------------*/
/* addLoadEvent(doPopups) use advanced 'getNewWindowLinks' instead*/
addLoadEvent(getNewWindowLinks)
//addLoadEvent(startSWFObjects)
addLoadEvent(checkForAnchor)
addLoadEvent(doHelpLinks)

/* Basic library functions
---------------------------------------*/
document.getElementsByClassName = function(oElm, strTagName, strClassName){
    var arrElements = (strTagName == "*" && document.all)? document.all : oElm.getElementsByTagName(strTagName);
    var arrReturnElements = new Array();
    strClassName = strClassName.replace(/\-/g, "\\-");
    var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
    var oElement;
    for(var i=0; i<arrElements.length; i++){
        oElement = arrElements[i];      
        if(oRegExp.test(oElement.className)){
            arrReturnElements.push(oElement);
        }   
    }
    arrElements = null;
    strClassName = null;
    oRegExp = null;
    oElement = null;
    return (arrReturnElements)
}
/* Global onLoad Event function handler
---------------------------------------*/
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
	  		oldonload();
	  		func();
		}
	}
}
/* Event handlers
---------------------------------------*/
function addEvent( obj, type, fn )
{
	if (obj.addEventListener)
		obj.addEventListener( type, fn, false );
	else if (obj.attachEvent)
	{
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
		obj.attachEvent( "on"+type, obj[type+fn] );
	}
}
function removeEvent( obj, type, fn )
{
	if (obj.removeEventListener)
		obj.removeEventListener( type, fn, false );
	else if (obj.detachEvent)
	{
		obj.detachEvent( "on"+type, obj[type+fn] );
		obj[type+fn] = null;
		obj["e"+type+fn] = null;
	}
}

/* Advanced popup functions
---------------------------------------*/
/* Create the new window */
function openInNewWindow(e) {
	var event;
	if (!e) event = window.event;
	else event = e;
	// Abort if a modifier key is pressed
	if (event.shiftKey || event.altKey || event.ctrlKey || event.metaKey) {
		return true;
	}
	else {
		// Change "_blank" to something like "newWindow" to load all links in the same new window
	    var newWindow = window.open(this.getAttribute('href'), '_blank');
		if (newWindow) {
			if (newWindow.focus) {
				newWindow.focus();
			}
			return false;
		}
		return true;
	}
}
/*
Add the openInNewWindow function to the onclick event of links with a specified class name
*/
function getNewWindowLinks() {
	// Check that the browser is DOM compliant
	if (document.getElementById && document.createElement && document.appendChild) {
		// Change this to the text you want to use to alert the user that a new window will be opened
		var strNewWindowAlert = " (opens in a new window)";
		// Find all links
		var links = document.getElementsByTagName('a');
		var objWarningText;
		var link;
		for (var i = 0; i < links.length; i++) {
			link = links[i];
			// Find all links with a class name of "newwin"
			if (/\bnewwin\b/.test(link.className)) {
				// Create an em element containing the new window warning text and insert it after the link text
				//objWarningText = document.createElement("em");
				//objWarningText.appendChild(document.createTextNode(strNewWindowAlert));
				//objWarningImg = document.createElement("img");
				//objWarningImg.src = "/assets/images/newwin.gif";
				//objWarningImg.width = "15";
				//objWarningImg.height = "12";
				//objWarningImg.alt = "This link will open in a new window";
				//objWarningImg.title = "This link will open in a new window";
				//link.appendChild(document.createTextNode(" "));
				//link.appendChild(objWarningImg);
				link.onclick = openInNewWindow;
				link.title = "This link will open in a new window";
			}
			if (/\bpopwin\b/.test(link.className)) {
				// Create an em element containing the new window warning text and insert it after the link text
				link.onclick = openInNewWindow;
				link.title = "This link will open in a new window";
			}
		}
		objWarningText = null;
	}
}

/* Basic popup functions
---------------------------------------*/
function openLink(oururl,features,winname) { // SP2 friendly popups hopefully
	if (oururl) {
		if(!features) { features="" }
		if(!winname) { winname="" }
		try	{		    
			window.open(oururl,winname,features);		
			return false;
		} catch (e)	{
			return true; // Just use normal link if popup cannot open
		}
	}
}
function doPopups() { // Open links .'class' in new windows
	if (!document.getElementsByTagName) return false;		
	
	var links = document.getElementsByTagName("a");  
	for (var i=0; i < links.length; i++) {			
			
		if (links[i].className.match("newwin")) { // Open new window
			links[i].onclick = function() {
				openLink(this.href);
				return false;
			}
			links[i].title = "This link will open in a new window";
			
		} else if (links[i].className.match("popup")) {	// Open popup 
			links[i].onclick = function() {
//			Generic popup
//				openLink(this.href,"width=550,height=490,resizable=1,scrollbars=1");
//			Fully specified features - No toolbars etc
				openLink(this.href,"width=550,height=490,resizable=0,scrollbars=1,navigation=0,menubar=0,location=0,status=0,toolbar=0");
//			Specify window name to prevent duplicates
//				openLink(this.href,"width=550,height=490,resizable=1,scrollbars=1","windowNameHere");
				return false;
			}
			links[i].title = "This link will open in a new window";			
				
		}
		
	}  
}

/*  Flash Objects
----------------------------------------------- */
function startSWFObjects() {
	if (!document.getElementById) return false;

/* ** example use **
	if (document.getElementById("flashholder-1")) {
		var so = new SWFObject("images/static.swf", "statictest", "229", "190",  "8", "#fefcf5", true);	// source, name&id, width, height, Flash version, background colour, useExpressInstall, quality, redirectUrl, detectKey
		//so.addParam("scale", "noscale"); // param
		//so.addVariable("container", "browser"); // flashvar
		so.write("flashholder-1"); // element to write flash into
	}
** end example use ** */

}

/* FAQ highlighter functions
-----------------------------------*/
function doHelpLinks() { // setup highlighting from links
	if (!document.getElementById) return false;
	if (document.getElementById("faqs")) {
		var links = document.getElementById("faqs").getElementsByTagName("a")
		for (var i=0; i < links.length; i++) {
			links[i].onclick = function() {
			jump(this.href);
			}
		}
	} else {
		return false;
	}
}

var theID = ""; /* I'm the heading ID to style on jump */
function highlighter(styleloop) {
	if (!document.getElementById) return false;
	var limit = 6;
	if (styleloop == 1) {
		document.getElementById(theID).className = "faq-step" + styleloop.toString();
		setTimeout("highlighter(" + (styleloop + 1) + ")", 1000); /* Pause on initial colour */
	} else if (styleloop <= limit) {
		document.getElementById(theID).className = "faq-step" + styleloop.toString();
		setTimeout("highlighter(" + (styleloop + 1) + ")", 33);
	} else {
		document.getElementById(theID).className = "faq-step0";
	}
}

function jump(target) {
	if (!document.getElementById) return false;
	if (target)	{
		if ( target.indexOf("#") >= 0 ) { //Check for anchorness
			
			if ( (theID != "") && document.getElementById(theID) ) { //Clear and check for previous instance
				document.getElementById(theID).className = "faq_off";
			}
			
			theID = target.substring(target.indexOf("#")+1);
			
			if (document.getElementById(theID)) {
				highlighter(0);
			} else {
				//alert("not found: "+theID);
			}
			
		}
	}
}

function checkForAnchor() {
	if (!document.getElementById("faqs")) return false;
	if (location.hash && location.hash!="#content") {
		jump(location.hash);
	}
}