/* Global Functions for OLN Affiliate */

/*************************************************************************************
* DOM scripting by brothercake -- http://www.brothercake.com/
* GNU Lesser General Public License -- http://www.gnu.org/licenses/lgpl.html
* allows other DOM scripting to run before window.onload;
**************************************************************************************/
function domFunction(f, a)	{
	var n = 0;
	var t = setInterval(function() {
		var c = true;
		n++;
		if(typeof document.getElementsByTagName != 'undefined' && (document.getElementsByTagName('body')[0] != null || document.body != null)) {
			c = false;
			if(typeof a == 'object') {
				for(var i in a) {
					if((a[i] == 'id' && document.getElementById(i) == null) || (a[i] == 'tag' && document.getElementsByTagName(i).length < 1))	{ 
						c = true; 
						break; 
					}
				}
			}
			if(!c) { f(); clearInterval(t); }
		}
		if(n >= 60) { clearInterval(t);}
	}, 250);
};


/*************************************************************************************
* Very simple pop up window script.  For use in other scripts
**************************************************************************************/
function basePopUp(url, target, params) {
	if(params) {
		var p = params;
	} else {
		 var p = "width=370,height=400,scrollbars=yes,resizable=yes";
	}

	newWin = window.open(url,target,p);
	newWin.focus();
	return false;
}

// Post a form to pop-up
function postFormToPopUp(myform, windowname, params) {
	if (! window.focus) { return true };
	window.open('', windowname, params);
	myform.target = windowname;
	return true;
}


/*************************************************************************************
* Replace the masthead title with the parameter passed
**************************************************************************************/
function setMastheadTitle(sTitle) {
	var oMastheadTitle = document.getElementById('mastheadTitle');
	
	if(!oMastheadTitle.hasChildNodes()) {
		oMastheadTitle.appendChild(document.createTextNode(sTitle));
		return true;
	} else{
		oMastheadTitle.firstChild.nodeValue = sTitle;
	} 
}


/*************************************************************************************
* Get all elements by class name
  Written by Jonathan Snook, http://www.snook.ca/jonathan
  Add-ons by Robert Nyman, http://www.robertnyman.com
**************************************************************************************/
function getElementsByClassName(oElm, strTagName, oClassNames){
    var arrElements = (strTagName == "*" && document.all)? document.all : 
    oElm.getElementsByTagName(strTagName);
    var arrReturnElements = new Array();
    var arrRegExpClassNames = new Array();
    if(typeof oClassNames == "object"){
        for(var i=0; i<oClassNames.length; i++){
            arrRegExpClassNames.push(new RegExp("(^|\\s)" + 
            oClassNames[i].replace(/\-/g, "\\-") + "(\\s|$)"));
        }
    }
    else{
        arrRegExpClassNames.push(new RegExp("(^|\\s)" + 
        oClassNames.replace(/\-/g, "\\-") + "(\\s|$)"));
    }
    var oElement;
    var bMatchesAll;
    for(var j=0; j<arrElements.length; j++){
        oElement = arrElements[j];
        bMatchesAll = true;
        for(var k=0; k<arrRegExpClassNames.length; k++){
            if(!arrRegExpClassNames[k].test(oElement.className)){
                bMatchesAll = false;
                break;                      
            }
        }
        if(bMatchesAll){
            arrReturnElements.push(oElement);
        }
    }
    return (arrReturnElements)
}


/******************************************
* Add an event onload - by Simon Willison
*******************************************/
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}
/* example of code needed to run a function on page load: */ 
/* addLoadEvent(function() {
}); */


/*************************************************************************************
* Make class="material_guidelines" anchors pop up to new window. 
**************************************************************************************/
function attachMaterialGuidelinesPopup() {
	var o = getElementsByClassName(document,'a','material_guidelines');

	if(o) {
		for (i=0;i<o.length;i++) {
			o[i].onclick = function() {
				basePopUp('/misc/material_guidelines_pop.php','_blank','width=370,height=400,scrollbars=yes,resizable=yes');
			}

		}
	}
}
new domFunction(attachMaterialGuidelinesPopup);


/*************************************************************************************
* Loops through all imgs looking for "rollover" class and assigns the appropriate functions.
* Images must be named as image_on.* and image_off.*
**************************************************************************************/
function attachRollOvers() {
	// Get all images and input buttons with class name "rollover"
	var oElements = getElementsByClassName(document,'img','rollover');
		
	for(var i=0; i<oElements.length; i++) {
		// Preload the rollover image!
		var tmpimg = new Image();
		tmpimg.src = oElements[i].src.replace("_off.","_on.");
		
		oElements[i].onmouseover = function() {
      	this.src = this.src.replace("_off.","_on.");
			if(this.runtimeStyle) { this.runtimeStyle.filter = this.runtimeStyle.filter.toString().replace("_off.","_on."); }
		}
		oElements[i].onmouseout= function() {
      	this.src = this.src.replace("_on.","_off.");
			if(this.runtimeStyle) { this.runtimeStyle.filter = this.runtimeStyle.filter.toString().replace("_on.","_off."); }
		}
 	}
}

new domFunction(attachRollOvers);
	

/* Object detection */
var isW3C = (document.getElementById) ? true : false;
var isAll = (document.all) ? true : false;

//	take the ID of an HTML element and returns an object reference
function getObj(elemID) {
	var theObj  = (isAll) ? document.all[elemID] : ((isW3C) ? document.getElementById(elemID) : null);
	return theObj;
}

