
$(document).ready(function() {

  /*
   *  DEFAULT STYLE IF COOKIE EXISTS
   */
  if (favstyle = getCookie('hhl_favstyle')) {
    ActivateStyle(favstyle);
  }

  /*
   *  STYLE SWITCHER
   */
  $('.styleswitcher').click(function() {
    if ($("link[title='switchablecss']").attr('href') == 'styles/default.css')
      ActivateStyle('basic');
    else
      ActivateStyle('default');
    return false;
  });

  $("#activepoll").load('includes/poll.php');


  /*
   *  DOCUMENT VIEWER
   */
  $("#pageturner").click(function(e) {
    e.preventDefault();    
  });


  $(".thickbox").colorbox({width:820, height:570, iframe:true });


  /*
   *  MAIN MENU (top level)
   *  Only works if default css is active
   */

  // each dropdown has its own timeout, named after the parent li class
  var hideTimer = new Array();

  $("#mainmenu > li").hover(
    function () {

      if ($("link[title='switchablecss']").attr('href') == 'styles/basic.css')
        return false;

      $(this).find("a:first").addClass('active');
      var submenu = $(this).find("ul");

      // 1. find the position, size and margin for the parent li
      var pos = $(this).position();
      var width = $(this).width();
      var height = $(this).height();
      var leftmargin = $(this).css("margin-left").replace('px', '');
      var topMargin = $(this).css("margin-top").replace('px', '');
      //alert(width);
      //var x = pos.left + width + parseInt(leftmargin);
      var x = pos.left + 161;
      var y = pos.top + parseInt(topMargin);

      // 2. position the popup
      submenu.css('left', x+'px');
      submenu.css('top', y+'px');

      $(this).find("li:last").css('border-bottom', '1px solid #999');

      // 3. cancel any pending hide operations and show the menu
      var intervalKey = $(this).attr('class');
      clearInterval(hideTimer[intervalKey]);
      submenu.fadeIn(300);
      submenu.find("ul").css('display', 'none');  // don't show anything past the 2nd level (can be overidden below)
    },
    function () {

      if ($("link[title='switchablecss']").attr('href') == 'styles/basic.css')
        return false;

      // insert a short delay before hiding
      $(this).find("a:first").removeClass('active');
      var submenu = $(this).find("ul");
      var intervalKey = $(this).attr('class');
      hideTimer[intervalKey] = window.setTimeout(function() {
        submenu.hide();
      }, 250);
    }
  );


  /*
   *  SUB MENU
   */
  $(".anotherlevel").hover(
    function () {
      $(this).find("a:first").addClass('active');
      var submenu = $(this).find("ul");

      // 1. find the position, size and margin for the parent li
      var pos = $(this).position();
      var width = $(this).width();
      var height = $(this).height();
      var leftmargin = $(this).css("margin-left").replace('px', '');
      var topMargin = $(this).css("margin-top").replace('px', '');
      //alert(width);
      var x = pos.left + width + parseInt(leftmargin);
      var y = pos.top + parseInt(topMargin);

      // 2. position the popup
      submenu.css('left', x+'px');
      submenu.css('top', y+'px');

      $(this).find("li:last").css('border-bottom', '1px solid #999');

      // 3. cancel any pending hide operations and show the menu
      var intervalKey = $(this).attr('class');
      clearInterval(hideTimer[intervalKey]);
      submenu.fadeIn(300);
      submenu.find("ul").css('display', 'none');  // don't show anything past the 3rd level
    },
    function () {
      // insert a short delay before hiding
      $(this).find("a:first").removeClass('active');
      var submenu = $(this).find("ul");
      var intervalKey = $(this).attr('class');
      hideTimer[intervalKey] = window.setTimeout(function() {
        submenu.hide();
      }, 250);
    }
  );


  /*
   *  APP FORM
   */

  $(".hiddentab_content").css('display', 'none');

  $.jtabber({
    mainLinkTag: "#tabs li", // much like a css selector, you must have a 'title' attribute that links to the div id name
    activeLinkClass: "selectedtab", // class that is applied to the tab once it's clicked
    hiddenContentClass: "hiddentab_content", // the class of the content you are hiding until the tab is clicked
    showDefaultTab: 1, // 1 will open the first tab, 2 will open the second etc.  null will open nothing by default
    showErrors: false, // true/false - if you want errors to be alerted to you
    effect: 'fade', // null, 'slide' or 'fade' - do you want your content to fade in or slide in?
    effectSpeed: 'medium' // 'slow', 'medium' or 'fast' - the speed of the effect
  });

  $(".pastjobs:not(:first)").hide();
  $(".pastjobs:last").after('<p><a href="#" id="show_next_job">+ Add another entry...</a></p>');
  var nextJob = 2;
  $("#show_next_job").click(function(e) {
    e.preventDefault();
    if (nextJob <= 9)
      $("#pastjob_"+nextJob).slideDown();
    else
      alert("You are limited to 9 entries.");
    nextJob++;
  });

  // if we're correcting validation errors we need to display any completed entries
  var lastJob = $(".pastjobs input[value!='']:last").val();
  if (lastJob != undefined) {
    lastJob = $(".pastjobs input[value!='']:last").parent().parent().attr('id');
    lastJob = lastJob.substring(8,9);
    for (var i=2; i<=lastJob; i++)
      $("#pastjob_"+i).show();
  }

  $(".pastedu:not(:first)").hide();
  $(".pastedu:last").after('<p><a href="#" id="show_next_edu">+ Add another entry...</a></p>');
  var nextEdu = 2;
  $("#show_next_edu").click(function(e) {
    e.preventDefault();
    if (nextEdu <= 9)
      $("#pastedu_"+nextEdu).slideDown();
    else
      alert("You are limited to 9 entries.");
    nextEdu++;
  });

  // if we're correcting validation errors we need to display any completed entries
  var lastEdu = $(".pastedu input[value!='']:last").val();
  if (lastEdu != undefined) {
    lastEdu = $(".pastedu input[value!='']:last").parent().parent().attr('id');
    lastEdu = lastEdu.substring(8,9);
    for (var i=2; i<=lastEdu; i++)
      $("#pastedu_"+i).show();
  }

  $("#submitbutton").click(function() {
    if (confirm('Please confirm that you have checked your application and that you\'re ready to submit it.'))
      return true;
    else
      return false;
  });


});


/*
 *  CUSTOMISE LOOK IN LOW-GRAPHICS MODE
 */

function FontFace(req) {
  if (req == 'monospace')
    $("body").css('font-family', '"Courier New", Courier, monospace');
  else if (req == 'sans-serif')
    $("body").css('font-family', 'Verdana, Arial, Helvetica, sans-serif');
  else
    $("body").css('font-family', '"Times New Roman", Times, serif');
  setCookie('hhl_basicstyle_font', req, 90);
  return false;
}

function FontSize(req) {
  var size = $("body").css('font-size').replace('px', '');
  var sizeInt = parseInt(size);
  if (req == 'smaller')
    sizeInt = sizeInt - 2;
  else if (req == 'larger')
    sizeInt = sizeInt + 2;
  else
    sizeInt = req;                 // allow sizes to me input direct (e.g. from cookie value)
  if (sizeInt < 10) {
    sizeInt = 10;
    alert ('You have reached the minimum size.');
  }
  if (sizeInt > 30) {
    sizeInt = 30;
    alert ('You have reached the maximum size.');
  }
  size = sizeInt + 'px';
  $("body").css('font-size', size);
  setCookie('hhl_basicstyle_size', sizeInt, 90);
  return false;
}

function ColourScheme(req) {
  if (req == 'black') {
    $("body").css('background-color', '#000000');
    $("body").css('color', '#FFFF00');
    $("a").css('color', '#FFFFFF');
  } else if (req == 'blue') {
    $("body").css('background-color', '#0033CC');
    $("body").css('color', '#FFFFFF');
    $("a").css('color', '#FFFF00');
  } else if (req == 'cream') {
    $("body").css('background-color', '#FFFDD1');
    $("body").css('color', '#000000');
    $("a").css('color', '#0000CC');
  } else if (req == 'grey') {
    $("body").css('background-color', '#CCCCCC');
    $("body").css('color', '#000033');
    $("a").css('color', '#0000CC');
  } else {
    $("body").css('background-color', '#FFFFFF');
    $("body").css('color', '#000000');
    $("a").css('color', '#0000CC');
  }
  setCookie('hhl_basicstyle_color', req, 90);
  return false;
}



function DefaultSize(req) {
  /* 13 -> 16 -> 20 */

  if (req == 'm')
    size = '13px';
  else if (req == 'l')
    size = '16px';
  else if (req == 'xl')
    size = '20px';
  else
    size = req;

  $("body").css('font-size', size);
  setCookie('hhl_defaultstyle_size', size, 90);
  return false;
}


function ActivateStyle(req) {
  if (req == 'basic') {
    $("link[title='switchablecss']").attr('href', 'styles/basic.css');
    if (getCookie('hhl_basicstyle_font'))
      FontFace(getCookie('hhl_basicstyle_font'));
    if (getCookie('hhl_basicstyle_size'))
      FontSize(getCookie('hhl_basicstyle_size'));
    if (getCookie('hhl_basicstyle_color'))
      ColourScheme(getCookie('hhl_basicstyle_color'));
    setCookie('hhl_favstyle', 'basic', 90);
  } else {
    $("link[title='switchablecss']").attr('href', 'styles/default.css');
    $("body").css('font-family', '');
    $("body").css('background-color', '');
    $("body").css('color', '');
    $("a").css('color', '');
    if (getCookie('hhl_defaultstyle_size'))
      DefaultSize(getCookie('hhl_defaultstyle_size'));
    setCookie('hhl_favstyle', 'default', 90);
  }
}


function getCookie(NameOfCookie) {
  if (document.cookie.length > 0) {
    begin = document.cookie.indexOf(NameOfCookie+"=");
    if (begin != -1) {
      begin += NameOfCookie.length+1;
      end = document.cookie.indexOf(";", begin);
      if (end == -1)
        end = document.cookie.length;
      return unescape(document.cookie.substring(begin, end));
    }
  }
  return null;
}

function setCookie(NameOfCookie, value, expiredays) {
  var ExpireDate = new Date ();
  ExpireDate.setTime(ExpireDate.getTime() + (expiredays * 24 * 3600 * 1000));
  document.cookie = NameOfCookie + "=" + escape(value) + ((expiredays == null) ? "" : "; expires=" + ExpireDate.toGMTString());
}

function delCookie (NameOfCookie) {
  if (getCookie(NameOfCookie)) {
    document.cookie = NameOfCookie + "=" + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

/***********************************************
* ORPHANS ROLLOVER
* Ed Jeavons, July 2007
***********************************************/
/*
function addRollover(img, rolloverURL)
{
  if (typeof img == "string")
  {
    var id = img;
    img = null;

    if (document.getElementById)
      img = document.getElementById(id);
    else if (document.all)
      img = document.all[id];

    if(!img)
      img = document.images[id];

    if(!img)
      return;
  }

  if (img.tagName.toLowerCase() != "img")
    return;

  var baseURL = img.src;

  (new Image()).src = rolloverURL;

  img.onmouseover = function()
    img.src = rolloverURL;
  img.onmouseout = function()
    img.src = baseURL;
}

function initRollovers()
{
}
*/



/***********************************************
* ORPHANS MENU
* Ed Jeavons, July 2007
***********************************************/
/*
var clearPause;

function ShowSubmenu (objZone, objPages)
{
	clearInterval(clearPause);

	// don't do anything if already visible
	if (objPages.style.display == 'none')
	{
		// hide all others
		ClearSubmenus(objZone)
		
		// calc menu position
		var y = 0;
		for (var e = objZone; e; e = e.offsetParent)
			y += e.offsetTop;
		objPages.style.top = y + "px";
		
		// highlight active zone
		objZone.style.color = '#ffffff';
		objZone.style.backgroundColor = '#8B0B02';
		objZone.style.backgroundImage = 'none';

		// show menu
		Effect.Appear (objPages, {delay:0.2, duration:0.3, queue: {position:'end', scope:'menuscope'} });
	}
}

function ClearSubmenus(objIgnore)
{
	// empty the queue
	var queue = Effect.Queues.get('menuscope');
	queue.each(function(e) { e.cancel() });
	
	// clear the menu
	for (var i=0; i<zoneNames.length; i++)
	{
		tempObjZone = document.getElementById(zoneNames[i]);
		tempObjPages = document.getElementById(zonePages[i]);
		if (tempObjZone != objIgnore)
			HideSubmenu (tempObjZone, tempObjPages);
	}
}

function PauseThenClear()
{
	clearPause = window.setTimeout('ClearSubmenus()', 1500);
}

function HideSubmenu (objZone, objPages)
{

	// hide menu
	if (objPages.style.display != 'none')
		Effect.Fade(objPages, {duration:0.1, queue: {position:'front', scope:'menuscope'} });

	// highlight active zone
	if (objZone.style.color != '#666666')
	{
		objZone.style.color = '#666666';
		objZone.style.backgroundColor = '#ffffff';
		objZone.style.backgroundImage = 'url(images/site/menu-fade.jpg)';
	}
}
*/

/***********************************************
* Cross browser Marquee II- © Dynamic Drive (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit http://www.dynamicdrive.com/ for this script and 100s more.
***********************************************/

var delayb4scroll=2000 //Specify initial delay before marquee starts to scroll on page (2000=2 seconds)
var marqueespeed=1 //Specify marquee scroll speed (larger is faster 1-10)
var pauseit=1 //Pause marquee onMousever (0=no. 1=yes)?

////NO NEED TO EDIT BELOW THIS LINE////////////

var copyspeed=marqueespeed
var pausespeed=(pauseit==0)? copyspeed: 0
var actualheight=''

function scrollmarquee(){
if (parseInt(cross_marquee.style.top)>(actualheight*(-1)+8))
cross_marquee.style.top=parseInt(cross_marquee.style.top)-copyspeed+"px"
else
cross_marquee.style.top=parseInt(marqueeheight)+8+"px"
}

function initializemarquee(){
cross_marquee=document.getElementById("vmarquee")
cross_marquee.style.top=0
marqueeheight=document.getElementById("marqueecontainer").offsetHeight
actualheight=cross_marquee.offsetHeight
if (window.opera || navigator.userAgent.indexOf("Netscape/7")!=-1){ //if Opera or Netscape 7x, add scrollbars to scroll and exit
cross_marquee.style.height=marqueeheight+"px"
cross_marquee.style.overflow="scroll"
return
}
setTimeout('lefttime=setInterval("scrollmarquee()",30)', delayb4scroll)
}

if (window.addEventListener)
window.addEventListener("load", initializemarquee, false)
else if (window.attachEvent)
window.attachEvent("onload", initializemarquee)
else if (document.getElementById)
window.onload=initializemarquee
