//attach multiple events to onload
//simonwillison.net
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

//#rollover function, adds rollovers to all images with class rollover using filename_on.xxx as hover image
//#credit: http://onlinetools.org/articles/unobtrusivejavascript/
function findimg()
{
 var imgs,i;
 //#janine addition: array for preloading
 var rollovers = new Array();
// Loop through all images, check if they contain the class roll
 imgs=document.getElementsByTagName('img');
 for(i=0;i<imgs.length;i++)
 {
  if(/rollover/.test(imgs[i].className))
  {
	//#janine addition: preload rollover images
	rollovers[i] = new Image();
   	rollovers[i].src = imgs[i].src.replace('.jpg','_on.jpg');
	//#end addition
// add the function roll to the parent Element of the image
  imgs[i].parentNode.onmouseover=function(){roll(this);};
  imgs[i].parentNode.onmouseout=function(){roll(this);};
  imgs[i].parentNode.onfocus=function(){roll(this);};
  imgs[i].parentNode.onblur=function(){roll(this);};
  }
 }
}
function roll(o)
{
 var i,isnode,src,ftype,newsrc,nownode;
// loop through all childNodes
 for (i=0;i<o.childNodes.length;i++)
 {
  nownode=o.childNodes[i];
// if the node is an element and an IMG set the variable and exit the loop
  if(nownode.nodeType==1 && /img/i.test(nownode.nodeName))
  {
   isnode=i;
   break;
  }
 }
// check src and do the rollover
 src = o.childNodes[isnode].src;
 ftype = src.substring(src.lastIndexOf('.'), src.length);
 if(/_on/.test(src))
 {
  newsrc = src.replace('_on','');
 }else{
  newsrc = src.replace(ftype, '_on'+ftype);
 }
 o.childNodes[isnode].src=newsrc;
}

//#search box populate with value initially and clear it when focused
/*
 * Clear Default Text: functions for clearing and replacing default text in
 * <input> elements.
 *
 * by Ross Shannon, http://www.yourhtmlsource.com/
 */

addEventOn(window, 'load', populate, false);

function populate() {
    var formInputs = document.getElementsByTagName('input');
    for (var i = 0; i < formInputs.length; i++) {
        var theInput = formInputs[i];
        
        if (theInput.type == 'text' && theInput.className.match(/\bcleardefault\b/)) {  
            /* Add event handlers */          
            addEventOn(theInput, 'focus', clearDefaultText, false);
            addEventOn(theInput, 'blur', replaceDefaultText, false);
            
            /* Save the current value */
            if (theInput.value != '') {
                theInput.defaultText = theInput.value;
            }
        }
    }
}

function clearDefaultText(e) {
    var target = window.event ? window.event.srcElement : e ? e.target : null;
    if (!target) return;
    
    if (target.value == target.defaultText) {
        target.value = '';
    }
}

function replaceDefaultText(e) {
    var target = window.event ? window.event.srcElement : e ? e.target : null;
    if (!target) return;
    
    if (target.value == '' && target.defaultText) {
        target.value = target.defaultText;
    }
}

//#javascript resolution dependent layout script by colly
//#credit: http://www.colly.com/?/comments/redesign-notes-1-width-based-layout/

wraphandler = {
  init: function() {
    if (!document.getElementById) return;
    // set up the appropriate wrapper
    wraphandler.setWrapper();
    // and make sure it gets set up again if you resize the window
    wraphandler.addEvent(window,"resize",wraphandler.setWrapper);
  },

  setWrapper: function() {
    // width stuff from ppk's evolt.org/article/document_body_doctype_switching_and_more/17/30655/index.html
    var theWidth = 0;
    if (window.innerWidth) {
	theWidth = window.innerWidth
    } else if (document.documentElement &&
                document.documentElement.clientWidth) {
	theWidth = document.documentElement.clientWidth
    } else if (document.body) {
	theWidth = document.body.clientWidth
    }
    if (theWidth != 0) {
      if (theWidth < 850) {
		  if(document.getElementById('wrapperCatalogue'))
		  {
        	document.getElementById('wrapperCatalogue').className = 'wrapperThin';
		  }
		  else if(document.getElementById('wrapper'))
		  {
			//#home page is special case
			document.getElementById('wrapper').className = 'wrapperHomeThin';
		  }
      } else {
		  if(document.getElementById('wrapperCatalogue'))
		  {
        	document.getElementById('wrapperCatalogue').className = 'wrapperStandard';
		  }
		  else if(document.getElementById('wrapper'))
		  {
			//#home page is special case
			document.getElementById('wrapper').className = 'wrapperHomeStandard';
		  }
      }
    }
  },

// addEvent stuff from John Resig's ejohn.org/projects/flexible-javascript-events
  addEvent: function( obj, type, fn ) {
    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] );
    } else {
      obj.addEventListener( type, fn, false );
    }
  }
}

wraphandler.addEvent(window,"load",wraphandler.init);

	/* 
 * Cross-browser event handling, by Scott Andrew
 */
function addEventOn(element, eventType, lamdaFunction, useCapture) {
    if (element.addEventListener) {
        element.addEventListener(eventType, lamdaFunction, useCapture);
        return true;
    } else if (element.attachEvent) {
        var r = element.attachEvent('on' + eventType, lamdaFunction);
        return r;
    } else {
        return false;
    }
}
	
	//Run dynamicLayout function when page loads and when it resizes.
	//addEvent(window, 'load', dynamicLayout);
	//addEvent(window, 'resize', dynamicLayout);

	//#extra functionality for rollovers
	addEventOn(window, 'load', findimg, false);
	//#populate/clear search box
