if (typeof PWCC == "undefined" || !PWCC) { //create PWCC namespace if it hasn't been done already PWCC = new Object(); } /* Functions: Domready, readyState, init Replaces window.onload. runs once the DOM is ready, rather than waiting for the entire page to load. As a result it runs earlier as it doesn't need wait for images/flash to load. Credit Due: Geek Daily, www.geekdaily.net Content taken from http://www.geekdaily.net/2007/07/27/javascript-windowonload-is-bad-mkay/ CHANGE LOG moved to PWCC name space Changed way that onReady() is passed to DomReady() - passed call to function rather than the function, this allows me to change onReady using addDomEvent (below) */ //Setup the event PWCC.DomReady = function(fn) { var _this = this; //W3C if(document.addEventListener) { document.addEventListener("DOMContentLoaded", fn, false); } //IE else { document.onreadystatechange = function(){PWCC.readyState(fn)} } } //IE execute function PWCC.readyState = function (fn) { //dom is ready for interaction if(document.readyState == "interactive") { fn(); } } //create onDomReady Event window.onDomReady = PWCC.DomReady; PWCC.onReady = function () { //called on Dom Initialisation } window.onDomReady(function() { PWCC.onReady(); }); /* Functions addLoadEvent, addDomEvent Adds an event to the window.onload event addLoadEvent should only be used for functions that require a knowledge of images, otherwise addDomEvent should be used Credit Due: Simon Willison, simonwillison.net Taken from: http://simonwillison.net/2004/May/26/addLoadEvent/ CHANGE LOG changed to PWCC name space addDomEvent is a straight copy of addLoad Event, works with DomInit rather that window.onload */ PWCC.addLoadEvent = function(func) { var oldonload = window.onload; if (typeof window.onload != 'function') { window.onload = func; } else { window.onload = function() { if (oldonload) { oldonload(); } func(); } } } PWCC.addDomEvent = function(func) { var oldDomInit = this.onReady; var _this = this; if (typeof oldDomInit != 'function') { _this.onReady = func; } else { _this.onReady = function() { if (oldDomInit) { oldDomInit(); } func(); } } } // Two options for running addLoadEvent /* PWCC.addLoadEvent(nameOfSomeFunctionToRunOnPageLoad); PWCC.addLoadEvent(function() { // more code to run on page load }); */ /* functions hasClass addClass removeClass checks for, adds and removes classes from an element Credit due: Sitepoint - Kevin Yank, Cameron Adams Book: Simply Javascript - http://www.sitepoint.com/books/javascript1/ CHANGE LOG changed to PWCC namespace */ PWCC.hasClass = function(target, theClass) { var pattern = new RegExp("(^| )" + theClass + "( |$)"); if (pattern.test(target.className)) { return true; } return false; } PWCC.addClass = function(target, theClass) { if (!this.hasClass(target, theClass)) { if (target.className == "") { target.className = theClass; } else { target.className += " " + theClass; } } } PWCC.removeClass = function(target, theClass) { var pattern = new RegExp("(^| )" + theClass + "( |$)"); target.className = target.className.replace(pattern, "$1"); target.className = target.className.replace(/ $/, ""); } //http://javascript.internet.com/snippets/getelementsbyclass.html // swapped tag and node around. // added lines var i & var j to avoid globals // moved to PWCC name space PWCC.getElementsByClass = function(searchClass,tag,node) { var classElements = new Array(); if (node == null) node = document.getElementsByTagName("body")[0]; if (tag == null) tag = '*'; var els = node.getElementsByTagName(tag); var elsLen = els.length; var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)"); for (var i = 0, j = 0; i < elsLen; i++) { if (pattern.test(els[i].className) ) { classElements[j] = els[i]; j++; } } return classElements; }