runScripts = function() {
  if (document.getElementById) {
    // Call general functions on window load
	fnDivFunctions();
	dynPopUp()
  }
}
window.onload = runScripts;

/*
  General functions
  ****************************************************
*/

/*
  Sets the colour of the sifr text based on the background div's classname
*/
fnDetermineSection = function() {
  if (document.getElementById("shell")){
    varClassName = document.getElementById("shell").className;
    switch(varClassName){
	case "section_15": // Corporate Info
      color = "#6caec8";
      return [varClassName,color];
	  break;
	case "section_21": // Championing Cities
      color = "#cf0076";
      return [varClassName,color];
	  break;
	case "section_20": // Third Places
      color = "#66ffff";
      return [varClassName,color];
	  break;
	case "section_45": // Projects
      color = "#99cc00";
      return [varClassName,color];
	  break;
	case "section_22": // News
      color = "#fc962a";
      return [varClassName,color];
	  break;
	case "section_56": // Miscellaneous pages/Secure section
      color = "#f4ff4d";
      return [varClassName,color];
	  break;
    default:
      color = "#f4ff4d"; // Default
      return [varClassName,color]
	}
  }
}



/*
  Replace the module icons with the icons from that section based on the background div's classname
*/
fnDivFunctions = function(){
    
	// Get section name
	section = fnDetermineSection();
	sectionName = section[0];
	
	divAry = document.getElementsByTagName('DIV');
	for (i=0;i<divAry.length;i++){
	  
	  // Replace images
      if (divAry[i].className == "blocks"){
        imgAry = divAry[i].getElementsByTagName('IMG');
	    for (x=0;x<imgAry.length;x++){
          if (imgAry[x].parentNode.tagName == "DIV"){
            newImgSrc = imgAry[x].src
            newImgSrc = newImgSrc.replace("Icons", "Section");
            newImgSrc = newImgSrc.replace(".gif", "-"+sectionName+".gif");
            imgAry[x].src = newImgSrc;
          }
        }
      }
	
	// IE6 hack to assign imgThumb class to .block  if a thumb image is present
	if (divAry[i].className == "block"){
	  imgAry = divAry[i].getElementsByTagName('IMG');
	  for (x=0;x<imgAry.length;x++){
		  
		// Detect default blocks with thumbnail images and apply rounded corners
	    if (imgAry[x].parentNode.className == "block"){
		  divAry[i].className = divAry[i].className + " imgThumb";
		  roundedCorners(imgAry[x]);
		}
		// Detect video blocks and apply rounded corners
		if (imgAry[x].parentNode.parentNode.className.indexOf("blockVideo") != -1){
		  roundedCorners(imgAry[x]);
		}
	  }
	}
  }	
}
// Apply rounded corners function
roundedCorners = function(object){
	createSpan = document.createElement('span');
	newSpan = object.parentNode.insertBefore(createSpan,object);
	newSpan.className = "imgCorners";
		  
	newSpan.appendChild(object);
	
	createPNG = document.createElement('span');
	newSpan.appendChild(createPNG);
}


popUp = function(URL) {
  var newwindow = window.open(URL, 'Pop_Box', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=320,height=270,left=300,top=200');
}
popUp2 = function(URL) {
  var newwindow = window.open(URL, 'Pop_Box', 'toolbar=0,scrollbars=1,location=0,statusbar=1,menubar=0,resizable=0,width=800,height=500,left=50,top=50');
}

dynPopUp = function() {
  if (document.getElementById) {
    aryLinks = document.getElementsByTagName("a")
	for (i=0; i<aryLinks.length; i++) {
      aryLinks[i].onclick = function() {
		if (this.className.indexOf('popUp2') != -1) {
	      popUp2(this.href)
		  return false
		}
	    if (this.className.indexOf('popUp') != -1) {
	      popUp(this.href)
		  return false
		}
	  }
	}
  }
}