$(document).ready(function() {
   //Stuff to run when this mother loads
   breakNav();
   hideRightcolImg();
   applyAsterix();
   applyEmptyAClasses();
   if ($("#content-tabs").length){
      iniTabs();
   }
        
   //Check if there is any content in the alert div, if none then hide
   var alertContent = $('.alert').html();
   if(alertContent.length < 5){
      $('.alert').hide();
   }
   // wrap form submit buttons in submit div
   $('#contact-form input[type="submit"]').wrap('<div class="submit" />');
   $('#sq_commit_button').wrap('<div class="submit" />');
   // adds class to first td in every table row
   $("#contact-form tr").find("td:first").addClass("left-col-td");
   applyRowClasses();
   //Initialise drop down js menu
   iniMenu();

if( $.browser.safari ) $("#matrix-content").css("padding-left", "1px" );

});

//Initialize js menu for delaying drop down effect by 1 second
function iniMenu(){
	$("#nav li.first-level").hoverIntent({
		interval: 200, 
		over: drops_show, 
		timeout: 300, 
		out: drops_hide
	});
	$("#nav li.first-level").addClass('with-js');
}
//function drops_show(){ $(this).addClass('show'); $(this).removeClass('with-js'); }
//function drops_hide(){ $(this).removeClass('show'); $(this).addClass('with-js'); }

function drops_show(){ $(this).children('ul').fadeIn('fast'); $(this).removeClass('with-js'); }
function drops_hide(){ $(this).children('ul').hide(); $(this).addClass('with-js'); }

// Adds odd class to every second table row
function applyRowClasses(){
   $("#matrix-content div>table>tbody>tr").removeClass("odd");
   $("#matrix-content div>table>tbody>tr:visible:odd").addClass("odd");
   $("#matrix-content form>table>tbody>tr:visible:odd").addClass("odd");
   $('#matrix-content #custom-form .form-item:odd').addClass('odd');
}

function fixLayout(){
   //Below 2 lines are defenders of IE evil wizardry
   $('#white-left-bg').css('position','static').css('position','absolute');
   $('#footer').css('position','absolute').css('position','relative');
}

function breakNav(){
    $('.nav-2').each(function(){
        $(this).children('li:eq(4)').after('<li class="break"></li>');
    });
};


// checks for right column thumbnail image and hides if not present
function hideRightcolImg(){
var getImg = $('#thumbnail img').length;
   if (getImg == 0){
    $('#thumbnail').hide();
   };
};


//Functions to put a span tag around asterix symbols in forms
function replaceAsterix(origString){
  var inChar = '*';
  var outChar = '<span class="error">*</span>';
  var newString = origString.split(inChar);
  newString = newString.join(outChar);
  return newString;
};
function applyAsterix(){
   $("#left-col form td:contains('*')").each(function(){
      var tempString = this.innerHTML;
      this.innerHTML = replaceAsterix(tempString);
   });
};

function stafLoader(){
// apply click function to load send to a friend popup lightbox

        $('#staf').show();
        $('<iframe />', {"name":"staf","id":"staf-iframe","src":"/home/send-to-a-friend?SQ_DESIGN_NAME=blank_design"}).prependTo('#staf');
	$('#lightbox-bg').show();
};

//Adds a clas to all A tags that don't have a href attribute
function applyEmptyAClasses(){
   $('#matrix-content a').each(function(){
      if(this.href == '' || this.href == null){
         $(this).addClass('anchor');
      }
   });
}

//Wherever there are tabs being used on the site, this function is used for initialising them and hiding/showing the necessary tabs, it also uses cookies to save the tab that you last had open on each individual page that has tabs
function iniTabs(){
var tabCookie = $("#matrix-content").attr('class') + 'tab';
var cookieVal = '';
$('#content-tabs').show();

//Find the content tab A tags and add ids and hide their relative divs that they are targeted for
$('#content-tabs a').each(function(i){
   $(this).attr('id',('tab-' + (i + 1)));
   var rel = '#' + $(this).attr('rel');
   if($(this).is('.selected')) {
      $(rel).show();
   }else{
      $(rel).hide();
   }
});

//Add onclicks to each A tag in the LI tags
$('#content-tabs a').click(function(){
   $('#content-tabs a').removeClass('selected');
   $(this).addClass('selected');
   $.cookie(tabCookie, '#' + this.id);
   $('#content-tabs a').each(function(){
      var rel = '#' + $(this).attr('rel');
      if($(this).is('.selected')) {
         $(rel).show();
      }else{
         $(rel).hide();
      }
   });
   fixLayout();
   return false;
});

//If a cookie is available, then activate the tab that the cookie value is for
if($.cookie(tabCookie) != '' || $.cookie(tabCookie) != null){
   cookieVal = $.cookie(tabCookie);
   $(cookieVal).trigger('click');
}

//If there is a hash tag available for auto opening a tab, open it
var tagHash = window.location.hash
$(window.location.hash).trigger('click');

}


/**
* hoverIntent r5 // 2007.03.27 // jQuery 1.1.2+
* <http://cherne.net/brian/resources/jquery.hoverIntent.html>
* 
* @param  f  onMouseOver function || An object with configuration options
* @param  g  onMouseOut function  || Nothing (use configuration options object)
* @author    Brian Cherne <brian@cherne.net>
*/
(function($){$.fn.hoverIntent=function(f,g){var cfg={sensitivity:7,interval:100,timeout:0};cfg=$.extend(cfg,g?{over:f,out:g}:f);var cX,cY,pX,pY;var track=function(ev){cX=ev.pageX;cY=ev.pageY;};var compare=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);if((Math.abs(pX-cX)+Math.abs(pY-cY))<cfg.sensitivity){$(ob).unbind("mousemove",track);ob.hoverIntent_s=1;return cfg.over.apply(ob,[ev]);}else{pX=cX;pY=cY;ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}};var delay=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);ob.hoverIntent_s=0;return cfg.out.apply(ob,[ev]);};var handleHover=function(e){var p=(e.type=="mouseover"?e.fromElement:e.toElement)||e.relatedTarget;while(p&&p!=this){try{p=p.parentNode;}catch(e){p=this;}}if(p==this){return false;}var ev=jQuery.extend({},e);var ob=this;if(ob.hoverIntent_t){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);}if(e.type=="mouseover"){pX=ev.pageX;pY=ev.pageY;$(ob).bind("mousemove",track);if(ob.hoverIntent_s!=1){ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}}else{$(ob).unbind("mousemove",track);if(ob.hoverIntent_s==1){ob.hoverIntent_t=setTimeout(function(){delay(ev,ob);},cfg.timeout);}}};return this.mouseover(handleHover).mouseout(handleHover);};})(jQuery);



$.extend({
  getUrlVars: function(){
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
      hash = hashes[i].split('=');
      vars.push(hash[0]);
      vars[hash[0]] = hash[1];
    }
    return vars;
  },
  getUrlVar: function(name){
    return $.getUrlVars()[name];
  }
});


function calcHeight(theId){
  //find the height of the internal page
  var the_height = document.getElementById(theId).contentWindow.document.body.scrollHeight;

  //change the height of the iframe
  document.getElementById(theId).height = the_height + 50;
}
