/*
    Written by Jonathan Snook, http://www.snook.ca/jonathan
    Add-ons by Robert Nyman, http://www.robertnyman.com
*/2

function getElementsByClassName(oElm, strTagName, strClassName){
	//if (oElm.getElementsByClassName) return oElm.getElementsByClassName(strClassName);
    var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
    var arrReturnElements = new Array();
    strClassName = strClassName.replace(/\-/g, "\\-");
    var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
    var oElement;
    for(var i=0; i<arrElements.length; i++){
        oElement = arrElements[i];      
        if(oRegExp.test(oElement.className)){
            arrReturnElements.push(oElement);
        }   
    }
    return (arrReturnElements)
}


// Plus & Minus / Open & Close 
// script created by Ed Merritt

if (document.getElementById && 
document.createTextNode && document.createElement){canDOM=true}
function plusminusicon(n){
 if(canDOM){
  u=n;
  b=n.parentNode;
  if(u){u.style.backgroundImage=(u.style.backgroundImage=='url(/images/tminus.gif)'||u.style.backgroundImage=='')?'url(/images/tplus.gif)':'url(/images/tminus.gif)';}
 }
}

if (document.getElementById && 
document.createTextNode && document.createElement){canDOM=true}
function plusminusform(n){
 if(canDOM){
  u=n.parentNode.getElementsByTagName('form')[0];
  if(u){u.style.display=(u.style.display=='block'||u.style.display=='')?'none':'block';}
  if(u){u.style.backgroundImage=(u.style.backgroundImage=='url(/images/tagsbox.gif)'||u.style.backgroundImage=='')?'':'url(/images/tagsbox.gif)';}
 }
}


function hideBoxes() {
	var divs = document.getElementsByClassName("plusminus");
   	for (var i=0; i< divs.length; i++) {
		var pluIcon = divs[i].getElementsByTagName("h4");
		plusminusicon(pluIcon[0]);		
		plusminusform(pluIcon[0]);
	}
}


// Clear Search field on focus

window.onload = function() {
	doOnload();
};

function doOnload()
{
	hideBoxes();
	if (!document.getElementById) return false;	
	var e = document.getElementById("sField");
	e.onfocus = function() {clearSearchBox(e);}
	makePopupLinks();
	try
	{
		var f = document.getElementById("sField2");
		if(f!=null)	f.onfocus = function() {clearSearchBox(f);}
	}
	catch(ex)
	{
	}
			
}

function clearSearchBox(e)
{
		if (e.value == "Enter your keywords here." || e.value == "Placename or postcode.") e.value = "";
		return false;
}


function clearPostcodeBox()
{
	var g = document.getElementById('mapField');
	g.onfocus = function() {clearSearchBox(g);};
}


// Image captions

function captionsLeft() {
   wrapImagesInDiv( 'captionleft', [], [ 'float' ] );
}

if(typeof window.addEventListener != 'undefined') {
   window.addEventListener('load', captionsLeft, false);
}
else if(typeof document.addEventListener != 'undefined') {
   document.addEventListener('load', captionsLeft, false);
}
else if(typeof window.attachEvent != 'undefined') {
   window.attachEvent('onload', captionsLeft);
}
function captionsRight() {
   wrapImagesInDiv( 'captionright', [], [ 'float' ] );
}

if(typeof window.addEventListener != 'undefined') {
   window.addEventListener('load', captionsRight, false);
}
else if(typeof document.addEventListener != 'undefined') {
   document.addEventListener('load', captionsRight, false);
}
else if(typeof window.attachEvent != 'undefined') {
   window.attachEvent('onload', captionsRight);
}

function wrapImagesInDiv( className, attributes, styles ) {
    try {
    if (document.getElementById("poidetail")) { return false };
   var images = document.getElementsByClassName(className);
   for ( var i = 0; i < images.length; ++i ) {
      var img = images[i];
      // Lift the image out of the page and insert a div under it.
      var parent = img.parentNode;
      var frame = document.createElement('div');
      var para = document.createElement('p');
      var txt = document.createTextNode(img.getAttribute('title'));
      parent.insertBefore(frame, img);
      parent.removeChild(img);
      frame.appendChild(img);
      frame.appendChild(para);
      para.appendChild(txt);
      // These are special cases.  We always copy these from the image to the
      // div.
      frame.style.width = img.getAttribute('width') + 'px';
      para.style.width = img.getAttribute('width') + 'px';
      frame.className = img.className;
      // Copy specified attributes and style properties from the image to the
      // div.
      for ( var j = 0; j < attributes.length; ++j ) {
         frame.setAttribute(attributes[j], img.getAttribute(attributes[j]));
      }
      for ( var j = 0; j < styles.length; ++j ) {
         frame.style[styles[j]] = img.style[styles[j]];
      }
   }
   } catch(e) {}
}

function makePopupLinks()
{
	var ps = getElementsByClassName(document,'a','popup');
	for (var i=0;i<ps.length;i++)
	{
		ps[i].onclick= function() {window.open(this.href);return false;};
	}

}

jQuery(document).ready(
	function() {
	    gaTracking();
	}
);

//Add tracking events to downloads
function gaTracking() {
    var filetypes = [".pdf", ".doc", ".docx", ".xls", ".xlsx", ".ppt", ".pptx"];
    for (i = 0; i <= (filetypes.length - 1); i++)
    {
        jQuery('a[href$=' + filetypes[i] + ']').click(function() {
            pageTracker._trackEvent('downloads', 'click', jQuery(this).attr('href'));
        });
    }
};
