/*
 * Global JavaScript (requires jQuery v1.3.2)
 * 
 * Original Author: Chris Martin, Webexpectations.com
 * Last updated: 2009/09/16
 *
 */
 
/* - Initialisation (runs functions common to all pages) - */

$(document).ready(function() {
  enchanceButtons();
  attachNavbarHovers();
  theRotator();
  attachLinkHovers();

});

// Add a class to the anchor of the home page li when on home page
$(document).ready(function() {
	$(".page-item-22.current_page_item").children("a").addClass("home-selected");
	$('#a2apage_show_more_less').removeAttr('onmouseover');
	$('#a2apage_show_more_less').removeAttr('onmouseout');
	$('#a2apage_show_more_less').addClass('more');
	$('#a2apage_show_more_less').html('More');
	
	$('#a2apage_show_more_less').click(function(){
		
		if($(this).hasClass('more')){
			$(this).removeClass('more');
			$(this).addClass('less');
			$(this).html('Less');
		}
		else{
			$(this).removeClass('less');
			$(this).addClass('more');
			$(this).html('More');
		}
	});
	
	 /*used for adding additional classes to navigation menu*/
 	$('.navmenu ul').each(function(){
 		
 		$(this).find('li:last').addClass('last');
 		$(this).children().find('li:last').addClass('last');
 		$(this).find('li:last-child').addClass('last');
		if($(this).hasClass('last') && $(this).hasClass('current_page_item')){ $(this).addClass('current_item_last');}
	});
	/*
	$('.navmenu ul li:not(:has(ul)) a').each(function(){
	    
		var el = $(this).is(':has(ul li ul a)');
		var elHTML = $('<span>').append(el.clone()).remove().html();
		el.replaceWith('<span>' + elHTML + '</span>');
	});
	*/
	});

function attachLinkHovers(){
	
	$('.orange-bullet a').each(function(){
		var emTag = $(this).next('em').html();
		$(this).next('em').html('<div class="popup-wrapper"><div class="popup-top"><div class="popup-tl">' + emTag + 
	  '</div><div class="popup-tr"></div></div><div class="popup-bottom"><div class="popup-bl"></div><div class="popup-br"></div></div></div>');
	});
	
	$('.tick a').each(function(){
		var emTag = $(this).next('em').html();
		$(this).next('em').html('<div class="popup-wrapper"><div class="popup-top"><div class="popup-tl">' + emTag + 
	  '</div><div class="popup-tr"></div></div><div class="popup-bottom"><div class="popup-bl"></div><div class="popup-br"></div></div></div>');
	});

	$('.orange-bullet a').hover(function() {
		/*uses the parent li top position as the starting point'*/
		var parentPos = Math.round(parseFloat($(this).parent('li').position().top));
		/*as we are using the bottom css style we need to work in reverse and set it as negative integer, an additional integer is then added to account for the space*/
		$(this).next('em').css('bottom', -parentPos + 225);
		$(this).next('em').css('display','block');
		$('div#navbar > div > ul').css('position','static');
		$('div#navbar > div > ul > li').css('position','static');
	  //$(this).next("em").animate({opacity: "show", bottom: Math.round($(this).position().bottom) - 75}, "slow");
	}, function() {
		$(this).next('em').css('display','none');
		$('div#navbar > div > ul').css('position','relative');
		$('div#navbar > div > ul > li').css('position','relative');
	  //$(this).next("em").animate({opacity: "hide", bottom: '15'}, "fast");
	});
		
	$('.tick a').hover(function() {
		var parentPos = Math.round(parseFloat($(this).parent('li').position().top));
		$(this).next('em').css('bottom', -parentPos + 225);
		$(this).next('em').css('display','block');
		$('div#navbar > div > ul').css('position','static');
		$('div#navbar > div > ul > li').css('position','static');
	  //$(this).next("em").animate({opacity: "show", bottom: Math.round($(this).position().bottom) - 75}, "slow");
	}, function() {
		$(this).next('em').css('display','none');
		$('div#navbar > div > ul').css('position','relative');
		$('div#navbar > div > ul > li').css('position','relative');
	  //$(this).next("em").animate({opacity: "hide", bottom: '15'}, "fast");
	});
	
	
	
	

}

function enchanceButtons() {
  $('input.form-button').each(function(){
    $(this).addClass('hide');
    
    var a = $("<a></a>").click(function(){
      // $('form').submit();
      $(this).closest('form').submit();
    });
    
    a.addClass('form-button');
    
    var span1 = $('<span></span>');
    span1.addClass('button-top');
    var span2 = $('<span></span>');
    span2.addClass('button-bottom');
    span2.append($(this).val());
    span1.append(span2);
    a.append(span1);
    
    $(this).after(a);
  });
}

function attachNavbarHovers() {
  $('ul.navbar li').each(function(){
    $(this).hover(
      function () {
        $(this).addClass('over');
      }, 
      function () {
        $(this).removeClass('over');
      }
    );
  });
  
  $('ul.navbar li:first').each(function(){
    $(this).hover(
      function () {
        $(this).addClass('firsthover');
      }, 
      function () {
        $(this).removeClass('firsthover');
      }
    );
  });
  }
//Document Text Resizer script (May 14th, 08'): By JavaScript Kit: http://www.javascriptkit.com

var documenttextsizer = {
	
//remember the last selected control 
prevcontrol: '', 
existingclasses: '',

setpageclass:function(control, newclass){
	if (this.prevcontrol!='')
//un-select previous control, by removing class 'selectedtoggler'
			this.css(this.prevcontrol, 'selectedtoggler', 'remove')
//apply new class to document
	document.documentElement.className=this.existingclasses+' '+newclass 
	this.css(control, 'selectedtoggler', 'add') 
//remember new class added to document
	this.setCookie('pagesetting', newclass, 5) 
	this.prevcontrol=control
},

css:function(el, targetclass, action){
	var needle=new RegExp("(^|\\s+)"+targetclass+"($|\\s+)", "ig")
	if (action=="check")
		return needle.test(el.className)
	else if (action=="remove")
		el.className=el.className.replace(needle, "")
	else if (action=="add")
		el.className+=" "+targetclass
},

getCookie:function(Name){ 
	var re=new RegExp(Name+"=[^;]+", "i"); 
	if (document.cookie.match(re)) 
		return document.cookie.match(re)[0].split("=")[1] 
	return null
},

setCookie:function(name, value, days){
	if (typeof days!="undefined"){ 
		var expireDate = new Date()
		var expstring=expireDate.setDate(expireDate.getDate()+days)
		document.cookie = name+"="+value+"; path=/; expires="+expireDate.toGMTString()
	}
	else 
		document.cookie = name+"="+value
},

setup:function(targetclass){
	this.existingclasses=document.documentElement.className 
	var persistedsetting=this.getCookie('pagesetting')
	var alllinks=document.getElementsByTagName("a")
	for (var i=0; i<alllinks.length; i++){
		if (this.css(alllinks[i], targetclass, "check")){
			if (alllinks[i].getAttribute("rel")==persistedsetting) 
				this.setpageclass(alllinks[i], alllinks[i].getAttribute("rel"))
			alllinks[i].onclick=function(){
				documenttextsizer.setpageclass(this, this.getAttribute("rel"))
				return false
			}
		}
	}
}

}


function theRotator() {
	/*call to rotate function, switching the current image every 6 seconds*/
	$('table#rotator td img').css({opacity: 0.0});
	//Get the first image and display it (gets set to full opacity)
	$('table#rotator td img:first-child').css({opacity: 1.0});
	/*only call the rotator if the number of images is more than 5*/
	if($('table#rotator td img').length > 5)
	{
		setInterval('rotate()', 6000);
	}
	
}

function rotate() {	
	//Get the first image/child
	var curImage = ($('table#rotator img.show')?  $('table#rotator img.show') : $('table#rotator td img:first-child'));

	//Get next image, when it reaches the end, rotate it back to the first image
	var nextImage = ((curImage.next().length) ? ((curImage.next().hasClass('show')) ? $('table#rotator td img:first-child') :curImage.next()) : $('table#rotator td img:first-child'));	
	
	//Set the fade in effect for the next image, the show class has higher z-index
	nextImage.css({opacity: 0.0})
	.addClass('show')
	.animate({opacity: 1.0}, 1000);

	//Hide the current image
	curImage.animate({opacity: 0.0}, 1000)
	.removeClass('show');
	
};