
//======================== Generic load event function. Allows numerous function to be loaded on the same page without breaking anything
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}


//======================== W3C compliant Popup script
function popupLauncher()
{
  if( document && document.body && document.getElementById && document.getElementsByTagName ) //check to see if browser understands the methods
   {
     var alinks = document.getElementsByTagName('A'); // get all the links

     for( var i=0; i < alinks.length; i++ ) // store then all one by one
     {
      if( alinks[i].rel == "external" ) // check each one to see if it has the rel attribute set to external
       { 
         try
         {
          alinks[i].target = "_blank";  // if it does open new window
         }
         catch(e)
          {}
       }
     }
   }
}


//======================== IE6 antiflicker (stops the ie flicker on background images
try {
  document.execCommand('BackgroundImageCache', false, true);
} catch(e) {}

//======================== Function to check is javascript is activated. If it is the class js-active is assigned to html tag
	var newclassname = 'js-active'; 				// define class 
	var ob = document.getElementsByTagName('HTML');// get the html tag 
	if (ob.length > 0){
	  ob = ob[0];									// check if there is already a class applied to html tag
	  if (ob.className == ''){
	      ob.className = newclassname;				// if there isn't then add class
	  } else {
	     ob.className += ' ' + newclassname;		// if there is a class already present add class js-active after it
	  }
	}



//======================== search form (used to clear and reset all input fields)
function resetFields(whichform) {
  for (var i=0; i<whichform.elements.length; i++) {// get all the element tags within a form
    var element = whichform.elements[i];
    if (element.type == "submit") continue;       // check if element is a submit button
    if (!element.defaultValue) continue;			// check if element doesn't have some default text and stop if it doesn't
    element.onfocus = function() {					// function for if element is in focus
    if (this.value == this.defaultValue) {		// check if the elements text is the default when page was loaded
      this.value = "";								// if it is delete it
     }
    }
    element.onblur = function() {					// function for if element this element no-longer in focus
      if (this.value == "") {						// check if the elements text is empty
        this.value = this.defaultValue;			// if the elements text is empty put the default value back
      }
    }
  }
}
//======================== Function used by the reset form function. Allows the reset form function to be used for all input fields, not matter how many there are
function prepareForms() {
  for (var i=0; i<document.forms.length; i++) {   // get all the forms
    var thisform = document.forms[i];
    resetFields(thisform);							// apply reset form function to all the forms
  }
}

$(function(){
		  // alert("yeap");
		});   
//======================== hover effect
$(function(){
	if ($("body").hasClass("home")){	
		var $cta = $(".section.cta");
		var $zoom = $cta.find("a.image");
		var $extraInfo = $cta.find("span");
		if ($zoom){
			var images = $zoom.find("img");
			var w = images.width();
			var wIn = w / 100 ;
			var h = images.height();
			var hIn = h / 100;
		
			var normalView = {
				height: "100%",
				width: "100%",
				top: 0,
				left: 0
			};
			var zoomView = {
				height: "110%",
				width: "110%",
				top: '-' + (hIn / 2),
				left: '-' + (wIn / 2)
			};
			
			$(images).css(normalView);
			var hoverIntent = null;
			$($cta).hover(
				function () {
					$($cta).addClass("hover");	
					clearTimeout(hoverIntent);
					var el = this;
					hoverIntent = setTimeout(function () {
						images.stop().animate(zoomView);
					}, 200);
					//$($extraInfo).stop().css("z-index","100").fadeIn(2000);
		/* 			$($extraInfo).queue(function () {
						$(this).stop().css("z-index","100").fadeIn(2000);
						$(this).dequeue();
					}); */

				},
				function () {
					clearTimeout(hoverIntent);
					images.stop().animate(normalView);
					$($cta).removeClass("hover");
					//$($extraInfo).stop().fadeOut(2000).css("z-index","0");
				}
			);
		}

	}
});



//======================== no JavaScript email "fix"
$(function () {
	if (document.getElementsByClassName) {
		elements = document.getElementsByClassName('obfuscated_email'); 
		for(i=0;i<elements.length;i++)
			elements[i].style.display = 'none';
	} else {
		i = 0;
		a = document.getElementsByTagName("a");
		while (element = a[i++]) {
			if (element.className.indexOf("obfuscated_email") > -1)
				a[i-1].style.display = 'none';
		}
	}
});


addLoadEvent(popupLauncher);
addLoadEvent(prepareForms);