
//
// 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();
		};
	}
}

//
// addEvent()
// Adds event to an object
//
function addEvent(obj, evType, func)
{ 
 if (obj.addEventListener){ 
   obj.addEventListener(evType, func, false); 
   return true; 
 } else if (obj.attachEvent){ 
   var r = obj.attachEvent("on"+evType, func); 
   return r; 
 } else { 
   return false; 
 } 
}

//
// getElementsByAttribute()
// This script and many more are available free online at
// The JavaScript Source :: http://javascript.internet.com 
// Copyright Robert Nyman, http://www.robertnyman.com
// Free to use if this text is included */
//

function getElementsByAttribute(oElm, strTagName, strAttributeName, strAttributeValue)
{
    var arrElements = (strTagName == "*" && document.all)? document.all : oElm.getElementsByTagName(strTagName);
    var arrReturnElements = new Array();
    var oAttributeValue = (typeof strAttributeValue != "undefined")? new RegExp("(^|\\s)" + strAttributeValue + "(\\s|$)") : null;
    var oCurrent;
    var oAttribute;
    for(var i=0; i<arrElements.length; i++){
        oCurrent = arrElements[i];
        oAttribute = oCurrent.getAttribute(strAttributeName);
		if (oAttribute == null) {
			if(strAttributeName == 'className') {
				oAttribute = oCurrent.getAttribute('class');
			} else if(strAttributeName == 'class') {
				oAttribute = oCurrent.getAttribute('className');
			}
		}
        if(typeof oAttribute == "string" && oAttribute.length > 0){
            if(typeof strAttributeValue == "undefined" || (oAttributeValue && oAttributeValue.test(oAttribute))){
                arrReturnElements.push(oCurrent);
            }
        }
    }
    return arrReturnElements;
}


//
// rotatingImageForPage()
// Randomly pick up data from the attached image array to populate the given image.
// NOTE: 
//	1. Parameter 'images' is an array whose element is a map contains src and title of image;
//	2. Parameter 'oImage' is the image object which is the target of rotating, if a string is passed in, then it
//	   will the id of the image;
//
function rotatingImageForPage(images, oImage) 
{
	if (images.length <= 0) {
		return;
	}
	
	var rotatingimage;
	if (typeof oImage == 'string') {
		rotatingimage = document.getElementById(oImage);
	} else if (oImage != null && oImage.tagName == 'IMG') {
		rotatingimage = oImage;
	} else {
		return;
	}
	if (rotatingimage == null || rotatingimage.src == null) {
		return;
	}
	var index = Math.floor(Math.random() * images.length);
	rotatingimage.src = images[index].src;
	rotatingimage.title = unescapeXml(images[index].title);
	addClickEventToImage(oImage, images[index].url, images[index].target);
}

//
// addClickEventToImg()
// Attach click event to an image.
//
function addClickEventToImage(oImage, url, target)
{
	if (url != '') {
		if (target == '_blank') {
			addEvent(oImage, 'click', function() { window.open(unescapeXml(url), ""); } );
		} else {
			addEvent(oImage, 'click', function() { window.location = unescapeXml(url); } );
		}
		oImage.style.cursor = "hand";
		oImage.style.cursor = "pointer";
	}
}

//
// unescapeXml()
// unescapge xml data.
//
function unescapeXml(strXml)
{
	if (strXml == null || strXml.length <= 0) {
		return strXml;
	}
	var obj = document.createElement("div");
	obj.innerHTML = strXml;
	if (document.all) // IE?
		return obj.innerText;
	else
		return obj.textContent;
	
}