/*

 Author: Jay Dobson, Hang Yuan
 Date: Dec. 5, 2008
 Description: Provides general helper methods

*/


// Returns trimmed version of a given string
String.prototype.trim = function() { return this.replace(/^\s+|\s+$/, ''); };

// Switches the language of the page by changing the URL
function switchLanguage() {
	
var url = document.location.href;
	var str = document.location.href;

	if(str.indexOf("/en/") >= 0){
		url = url.replace("/en/", "/fr/");
		document.location = url;
	}
	
	else if(str.indexOf("lang=en") >= 0){
		url = url.replace("lang=en", "lang=fr");
		document.location = url;
	}
	
	else if(str.indexOf("/fr/") >= 0){
		url = url.replace("/fr/", "/en/");
		document.location = url;
	}
	
	else if(str.indexOf("lang=fr") >= 0){
		url = url.replace("lang=fr", "lang=en");
		document.location = url;
	}
	
}

// Rotating Images based on image array generated by Rotating Image Manager
function rotatingImage(images, imageId) {
	if (typeof(images) == 'undefined' || images.length <= 0) {
		return;
	}
	var index = Math.floor(Math.random() * images.length);
	var rotatingimage = document.getElementById(imageId)
	if (rotatingimage != null && rotatingimage.src != null) {
		rotatingimage.src = images[index].src;
		rotatingimage.title = images[index].title;
	}
}



// Used for entering and leaving search textbox so the user doesn't have to clear the 'Search' text
function Search_Enter(searchTextbox) {
    if (searchTextbox.value.toLowerCase() == 'recherche' || searchTextbox.value.toLowerCase() == 'search')
        searchTextbox.value = '';
}

function Search_Leave(searchTextbox) {
    var url = document.location.href;

    if (url.indexOf("/en/") >= 0 && searchTextbox.value.trim() == '')
        searchTextbox.value = 'Search';

    if (url.indexOf("/fr/") >= 0 && searchTextbox.value.trim() == '')
        searchTextbox.value = 'Recherche';
}

//
// addOnloadEvent()
// Adds event to window.onload without overwriting currently assigned onload functions.
// Function found at Simon Willison's weblog - http://simon.incutio.com/
//
function addOnloadEvent(func)
{	
	var oldonload = window.onload;
	if (typeof window.onload != 'function'){
    	window.onload = func;
	} else {
		window.onload = function(){
			oldonload();
			func();
		};
	}

}
