// JavaScript Document

function setActiveTextStyleSheet(title) {
   var i, a, main;
   for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
     if(a.getAttribute("rel").indexOf("style") != -1
        && (a.getAttribute("title") == "text")) {
       a.disabled = true;
       if(a.getAttribute("id") == "link_"+title) a.disabled = false;
     }
   }
}

function setActiveContrastStyleSheet(title) {
   var i, a, main;
   for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
     if(a.getAttribute("rel").indexOf("style") != -1
        && (a.getAttribute("title") == "contrast")) {
       a.disabled = true;
       if(a.getAttribute("id") == "link_"+title) a.disabled = false;
     }
   }
}

this.image_resize = function(){
		
	maxh = 538;
	maxw = 700;	
	
	count = 0;
	
	$j("img").each(function() {
	
		if(this.getAttribute('height')) {
			seth = $j(this).attr('height');
		} else {
			seth = false;
		}
		if(this.getAttribute('width')) {
			setw = $j(this).attr('width');
		} else {
			setw = false;
		}
	
		if($j(this).hasClass('lightbox')) {
			
			count++;
			$j(this).wrap('<a id="lb_link_'+count+'" href="image.php?img='+$j(this).attr('src')+'&h='+maxh+'&w='+maxw+'" title="'+$j(this).attr('title')+'"></a>');
			//$j("body").append('<div id="lb_box_'+count+'"><img src="" alt=""/></div>');
			$j("#lb_link_"+count).fancybox(
			{		
			'overlayShow'			: false,
			'zoomSpeedIn'			: 500,
			'zoomSpeedOut'			: 500
			}
			);
	
		}
	
		if( $j(this).attr('src').indexOf('http://') == -1 && (seth != false || setw != false) ) {
		
			src = 'image.php?p=true&img='+$j(this).attr('src');
			
			if(seth != false) {
				src += '&h='+$j(this).attr('height');
			}
			if(setw != false) {
				src += '&w='+$j(this).attr('width');
			}
			
			$j(this).attr("src",src);
		
		}
	
	});
	
};

jQuery(document).ready(function() {
	
	//colorize required text in forms
	if(jQuery('.contact-us-heading').length != 0){
		jQuery(".contact-us-heading:contains('*')").css('color','red');
	}	

	//spacer box size adjustment
	if(jQuery('#background-spacer-box').length != 0){
		var height = 283 - jQuery('#background-spacer-box').position().top; //283 is position of divider line
		jQuery('#background-spacer-box').css('height',height+10); //set height and add 10px spacer
	}

	//image_resize();
	
	//Loads settings (color/text size) based on cookie settings
	textcookie = getCookie('text_size');
	contrastcookie = getCookie('contrast');
	if (textcookie == "" || textcookie == null)
	{
		clickTextSmall();
	} else
	{
		setActiveTextStyleSheet(textcookie);
	}
	if (contrastcookie == "" || contrastcookie == null)
	{
		clickContrastFull();
	} else
	{
		setActiveContrastStyleSheet(contrastcookie);
		changeContrastText(contrastcookie);
	}
	
	//Search "enter" key:
	jQuery('#ajaxSearch_form').keyup(function(e) {
		//alert(e.keyCode);
		if(e.keyCode == 13) {
			location.href = "/site-search-results?AS_search="+document.getElementById('ajaxSearch_input').value;
		}
	});
	
});

//Font Size Adjustors
function clickTextLarge()
{
	setActiveTextStyleSheet('text_l');
	setCookie('text_size','text_l','1');
}
function clickTextMedium()
{
	setActiveTextStyleSheet('text_m');
	setCookie('text_size','text_m','1');
}
function clickTextSmall()
{
	setActiveTextStyleSheet('text_s');
	setCookie('text_size','text_s','1');
}

//High Contrast Switch
function clickContrastHigh()
{
	setActiveContrastStyleSheet('contrast');
	changeContrastText('contrast');
	setCookie('contrast','contrast','1');
}

//Full Colour Switch
function clickContrastFull()
{
	setActiveContrastStyleSheet('full_colour');
	changeContrastText('full_colour');
	setCookie('contrast','full_colour','1');
}

function setCookie(name,value)
{
	document.cookie=name+"="+value+"; path=/;";
}

function getCookie(name)
{
	var search = name + "="
	var returnvalue = "";
	if (document.cookie.length > 0) {
	  offset = document.cookie.indexOf(search)
	  // if cookie exists
	  if (offset != -1) { 
		offset += search.length
		// set index of beginning of value
		end = document.cookie.indexOf(";", offset);
		// set index of end of cookie value
		if (end == -1) end = document.cookie.length;
		returnvalue=unescape(document.cookie.substring(offset, end))
		}
	 }
	return returnvalue;
}

function changeContrastText(contrast_switch)
{
	if(contrast_switch == 'contrast')
	{
		document.getElementById('contrast').style.display = "none";
		document.getElementById('full_colour').style.display = "inline";
	} else
	{
		document.getElementById('full_colour').style.display = "none";
		document.getElementById('contrast').style.display = "inline";
	}
}

function searchSiteNonAjax()
{
	location.href = "/site-search-results?AS_search="+document.getElementById('ajaxSearch_input').value;
}

function showJobDescr(id){
	el=document.getElementById("job_"+id);
	if(el.style.display=='block'){el.style.display='none';}
	else{el.style.display='block';}
}


last_event="";
	function hide_news(){
		document.getElementById(last_event).style.display='none';		
	}
	function display_news(thenews, el){
		if(last_event!=""){
			document.getElementById(last_event).style.display='none';		
		}
		last_event=thenews;
		//y=getOffset(el, 'y');
		//x=getOffset(el, 'x');
		document.getElementById(thenews).style.display='block';
		
		/*
y=el.offsetTop-(document.getElementById(thenews).offsetHeight/2)+(el.offsetHeight/2)+24;
		x=el.offsetLeft+15;
		alert("x="+x+" y="+y);
*/
		
		y1=jQuery(el).position().top-(document.getElementById(thenews).offsetHeight/2)+(el.offsetHeight/2)+24;
		x1=jQuery(el).position().left+15;
		
		//alert("x1="+x1+" y1="+y1);
		
		document.getElementById(thenews).style.top=y1+"px";
		document.getElementById(thenews).style.left=x1+"px";
		
	}
	
	function getOffset( oElement, coord )
	{
		var iReturnValue = 0;
		while( oElement != null ) {
			if(coord=='y'){iReturnValue += oElement.offsetTop;}
			else{iReturnValue += oElement.offsetLeft;}
			oElement = oElement.offsetParent;
		}
		return iReturnValue;
	}
	
	
/** 
#  * Copyright (c) 2008 Pasyuk Sergey (www.codeasily.com) 
#  * Licensed under the MIT License: 
#  * http://www.opensource.org/licenses/mit-license.php 
#  *  
#  * Splits a <ul>/<ol>-list into equal-sized columns. 
#  *  
#  * Requirements:  
#  * <ul> 
#  * <li>"ul" or "ol" element must be styled with margin</li> 
#  * </ul> 
#  *  
#  * @see http://www.codeasily.com/jquery/multi-column-list-with-jquery 
#  */  
jQuery.fn.makeacolumnlists = function(settings){
	settings = jQuery.extend({
		cols: 2,				// set number of columns
		colWidth: 0,			// set width for each column or leave 0 for auto width
		equalHeight: false, 	// can be false, 'ul', 'ol', 'li'
		startN: 1				// first number on your ordered list
	}, settings);

	if(jQuery('> li', this)) {
		this.each(function(y) {
			var y=jQuery('.li_container').size(),
		    	height = 0, 
		        maxHeight = 0,
				t = jQuery(this),
				classN = t.attr('class'),
				listsize = jQuery('> li', this).size(),
				percol = Math.ceil(listsize/settings.cols),
				contW = t.width(),
				bl = ( isNaN(parseInt(t.css('borderLeftWidth'),10)) ? 0 : parseInt(t.css('borderLeftWidth'),10) ),
				br = ( isNaN(parseInt(t.css('borderRightWidth'),10)) ? 0 : parseInt(t.css('borderRightWidth'),10) ),
				pl = parseInt(t.css('paddingLeft'),10),
				pr = parseInt(t.css('paddingRight'),10),
				ml = parseInt(t.css('marginLeft'),10),
				mr = parseInt(t.css('marginRight'),10),
				col_Width = Math.floor((contW - (settings.cols-1)*(bl+br+pl+pr+ml+mr))/settings.cols);
			if (settings.colWidth) {
				col_Width = settings.colWidth; 
			}
			var colnum=1,
				percol2=percol;
			jQuery(this).addClass('li_cont1').wrap('<div id="li_container' + (++y) + '" class="li_container"></div>');
			for (var i=0; i<=listsize; i++) {
				if(i>=percol2) { percol2+=percol; colnum++; }
				var eq = jQuery('> li:eq('+i+')',this);
				eq.addClass('li_col'+ colnum);
				if(jQuery(this).is('ol')){eq.attr('value', ''+(i+settings.startN))+'';}
			}
			jQuery(this).css({cssFloat:'left', width:''+col_Width+'px'});
			for (colnum=2; colnum<=settings.cols; colnum++) {
				if(jQuery(this).is('ol')) {
					jQuery('li.li_col'+ colnum, this).appendTo('#li_container' + y).wrapAll('<ol class="li_cont'+colnum +' ' + classN + '" style="float:left; width: '+col_Width+'px;"></ol>');
				} else {
					jQuery('li.li_col'+ colnum, this).appendTo('#li_container' + y).wrapAll('<ul class="li_cont'+colnum +' ' + classN + '" style="float:left; width: '+col_Width+'px;"></ul>');
				}
			}
			if (settings.equalHeight=='li') {
				for (colnum=1; colnum<=settings.cols; colnum++) {
				    jQuery('#li_container'+ y +' li').each(function() {
				        var e = jQuery(this);
				        var border_top = ( isNaN(parseInt(e.css('borderTopWidth'),10)) ? 0 : parseInt(e.css('borderTopWidth'),10) );
				        var border_bottom = ( isNaN(parseInt(e.css('borderBottomWidth'),10)) ? 0 : parseInt(e.css('borderBottomWidth'),10) );
				        height = e.height() + parseInt(e.css('paddingTop'), 10) + parseInt(e.css('paddingBottom'), 10) + border_top + border_bottom;
				        maxHeight = (height > maxHeight) ? height : maxHeight;
				    });
				}
				for (colnum=1; colnum<=settings.cols; colnum++) {
					var eh = jQuery('#li_container'+ y +' li');
			        var border_top = ( isNaN(parseInt(eh.css('borderTopWidth'),10)) ? 0 : parseInt(eh.css('borderTopWidth'),10) );
			        var border_bottom = ( isNaN(parseInt(eh.css('borderBottomWidth'),10)) ? 0 : parseInt(eh.css('borderBottomWidth'),10) );
					mh = maxHeight - (parseInt(eh.css('paddingTop'), 10) + parseInt(eh.css('paddingBottom'), 10) + border_top + border_bottom );
			        eh.height(mh);
				}
			} else 
			if (settings.equalHeight=='ul' || settings.equalHeight=='ol') {
				for (colnum=1; colnum<=settings.cols; colnum++) {
				    jQuery('#li_container'+ y +' .li_cont'+colnum).each(function() {
				        var e = jQuery(this);
				        var border_top = ( isNaN(parseInt(e.css('borderTopWidth'),10)) ? 0 : parseInt(e.css('borderTopWidth'),10) );
				        var border_bottom = ( isNaN(parseInt(e.css('borderBottomWidth'),10)) ? 0 : parseInt(e.css('borderBottomWidth'),10) );
				        height = e.height() + parseInt(e.css('paddingTop'), 10) + parseInt(e.css('paddingBottom'), 10) + border_top + border_bottom;
				        maxHeight = (height > maxHeight) ? height : maxHeight;
				    });
				}
				for (colnum=1; colnum<=settings.cols; colnum++) {
					var eh = jQuery('#li_container'+ y +' .li_cont'+colnum);
			        var border_top = ( isNaN(parseInt(eh.css('borderTopWidth'),10)) ? 0 : parseInt(eh.css('borderTopWidth'),10) );
			        var border_bottom = ( isNaN(parseInt(eh.css('borderBottomWidth'),10)) ? 0 : parseInt(eh.css('borderBottomWidth'),10) );
					mh = maxHeight - (parseInt(eh.css('paddingTop'), 10) + parseInt(eh.css('paddingBottom'), 10) + border_top + border_bottom );
			        eh.height(mh);
				}
			}
		    jQuery('#li_container' + y).append('<div style="clear:both; overflow:hidden; height:0px;"></div>');
		});
	}
}

jQuery.fn.uncolumnlists = function(){
	jQuery('.li_cont1').each(function(i) {
		var onecolSize = jQuery('#li_container' + (++i) + ' .li_cont1 > li').size();
		if(jQuery('#li_container' + i + ' .li_cont1').is('ul')) {
			jQuery('#li_container' + i + ' > ul > li').appendTo('#li_container' + i + ' ul:first');
			for (var j=1; j<=onecolSize; j++) {
				jQuery('#li_container' + i + ' ul:first li').removeAttr('class').removeAttr('style');
			}
			jQuery('#li_container' + i + ' ul:first').removeAttr('style').removeClass('li_cont1').insertBefore('#li_container' + i);
		} else {
			jQuery('#li_container' + i + ' > ol > li').appendTo('#li_container' + i + ' ol:first');
			for (var j=1; j<=onecolSize; j++) {
				jQuery('#li_container' + i + ' ol:first li').removeAttr('class').removeAttr('style');
			}
			jQuery('#li_container' + i + ' ol:first').removeAttr('style').removeClass('li_cont1').insertBefore('#li_container' + i);
		}
		jQuery('#li_container' + i).remove();
	});
}

