// JavaScript Document
/* 
******************************************************************
FILE NAME: common_javascript.php		  DATE CREATED:
******************************************************************
OUTSTANDING ISSUES: None.
******************************************************************
MODIFICATION HISTORY: DATE, NAME, REASON, MODIFICATION

******************************************************************
*/

	
(function($) {
  var cache = []; //create cache
  // Arguments are image paths relative to the current page.
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
})(jQuery);


	//jQuery function for setting the class on the seach box when it is focussed and conditionally resetting it
	$(document).ready(function() {
		$("#query, .contactquery, .messagequery").focus(function(){ 
			$(this).addClass('active'); //Adds active class when focussed
		});
		$("#query, .contactquery, .messagequery").blur(function(){ 
			if($(this).attr('value') == "" || $(this).attr('value') == null) { //if the searchbox is not empty then...
				$(this).removeClass('active'); //Removes active class on blur
			}
		});
	});
	
	//jQuery function for creating tabbed content
	$(document).ready(function() {
		//Set up initial tab view:
		$(".tab_content").hide(); //Hide all content in class .tab_cobtent
		$("ul.tabs li:first").addClass('active').show(); //Activate first tab
		$(".tab_content:first").show(); //Show first tab content
	
		//When a new tab is clicked:
		$("ul.tabs li").click(function() {
			$("ul.tabs li").removeClass('active'); //Remove the front tab by removing 'active' class
			$(this).addClass('active'); //Show newly selected tab by adding 'active' class
			$(".tab_content").hide(); //Hide all tab content in class .tab_contnent
			
			//Fade in the new contnent
			var activeTab = $(this).find('a').attr('href'); //Find the href attribute value to identify the active tab + content
			$(activeTab).fadeIn(); //Fade in the active ID content
			return false;
		});
	
	});	
	
	//jQuery image gallery for elegantly fading out and in images by clicking on the numbers
	$(document).ready(function() {
		//Trigger the image change
		$(".number").click(function(){
			var ttl = $(this).attr('id'); //Collect the new image name stored in the number's ID tag
			$("#mainpic").fadeOut("normal", function(){ //Fade out the original image
				$("#mainpic").attr("src", ttl); //Set the src for the new image
			});		
			$("#mainpic").fadeIn("normal"); //Fade in the new image
			//Clean up the down states on the numbers
			$(this).removeClass('hover normal'); //Remove hover and normal states from the clicked button
			$(this).addClass('down'); //Add down state to clicked button
			$(this).siblings().removeClass('hover down'); //Remove hover and down states from all the other buttons
			$(this).siblings().addClass('normal'); // Add the normal state for all the other buttons
		});
		//Hover state for the numbers
		$(".number").hover(function(){
			$(this).addClass('hover'); //Add 'hover' class on mouseover
			}, function(){
			$(this).removeClass('hover'); //Reomove 'hover' class on mouseout	
		
		});
	});
		
	//jQuery image gallery for elegantly fading out and in images by clicking on the current image
	$(document).ready(function() {
		//Set some vars
		var counter = 2; //counter for knowing which pic to select
		var total = $("#numbers > div").size();//total number of images available
		
		if(total > 1) { $("#mainpic").addClass('pointer') }; // If there is more than one image then show it is clickable by adding a pointer on mouseover

		//Trigger the image change
		$("#mainpic").click(function(){
						
			var numb = $('#numbers > div:nth-child('+counter+')'); //Get the currrent number
			var ttl = numb.attr('id'); //Collect the new image name stored in the number's ID tag
			$("#mainpic").fadeOut("normal", function(){ //Fade out the original image
				$("#mainpic").attr("src", ttl); //Set the src for the new image
			});		
			$("#mainpic").fadeIn("normal"); //Fade in the new image
			//Clean up the down states on the numbers
			$(numb).removeClass('hover normal'); //Remove hover and normal states from the clicked button
			$(numb).addClass('down'); //Add down state to clicked button
			$(numb).siblings().removeClass('hover down'); //Remove hover and down states from all the other buttons
			$(numb).siblings().addClass('normal'); // Add the normal state for all the other buttons
			
			counter = counter + 1; //increment counter
			if(counter > total) { counter = 1 }//check counter and reset if necessary
		});
	});
	
	
	
	// jQuery function for hover text on tertiary navigation
	$(document).ready(function() {
		$("#tertiarynavigation li img, #tertiarynavshort li img").hover(function(){ 
			$("#terttext").text($(this).attr('title')); //Adds new text taken from the image title class on mouseover
			}, function(){
			$("#terttext").text(origtext);	//Replaces with the original text
		
		});
	});
	
	//Show of hide text with more/less button
	$(document).ready(function() {
		$('.more').hide();
		$(".morebutton").click(function(){
			$(this).prev().slideToggle("normal", function() {
				if($(this).is(":visible")) {
					$(this).next().val('Less...');
				}
				else {
					$(this).next().val('More...');
				}
			});
		});
	});

	
	//jQuery function for setting the class on the admin pages inputs
	$(document).ready(function() {
		$("#adminform input, #adminform select, #adminform textarea").focus(function(){ 
			$(this).addClass('adminactive'); //Adds active class when focussed
		});
		$("#adminform input, #adminform select, #adminform textarea").blur(function(){ 
			//if($(this).attr('value') == "" || $(this).attr('value') == null) { //if the input is not empty then...
				$(this).removeClass('adminactive'); //Removes active class on blur
			//}
		});
	});


function openwindow(path, x, y) {
	if(!x) x = 400;
	if(!y) y = 800;
  	open(path,'windowname','width='+x+',height='+y+',scrollbars,resizable,toolbar,status');
	document.windowname.focus();
}


/////////////////////////////////////////////////////////////////////
// GOOGLE ANALYTICS
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-6989714-1']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();
/////////////////////////////////////////////////////////////////////

