/***** EASING *****/

jQuery.easing.jswing=jQuery.easing.swing;jQuery.extend(jQuery.easing,{def:"easeInOutQuad",swing:function(e,f,a,h,g){return jQuery.easing[jQuery.easing.def](e,f,a,h,g)},easeInQuad:function(e,f,a,h,g){return h*(f/=g)*f+a},easeOutQuad:function(e,f,a,h,g){return -h*(f/=g)*(f-2)+a},easeInOutQuad:function(e,f,a,h,g){if((f/=g/2)<1){return h/2*f*f+a}return -h/2*((--f)*(f-2)-1)+a},easeInQuint:function(e,f,a,h,g){return h*(f/=g)*f*f*f*f+a},easeOutQuint:function(e,f,a,h,g){return h*((f=f/g-1)*f*f*f*f+1)+a},easeInOutQuint:function(e,f,a,h,g){if((f/=g/2)<1){return h/2*f*f*f*f*f+a}return h/2*((f-=2)*f*f*f*f+2)+a},easeInExpo:function(e,f,a,h,g){return(f===0)?a:h*Math.pow(2,10*(f/g-1))+a},easeOutExpo:function(e,f,a,h,g){return(f==g)?a+h:h*(-Math.pow(2,-10*f/g)+1)+a},easeInOutExpo:function(e,f,a,h,g){if(f===0){return a}if(f==g){return a+h}if((f/=g/2)<1){return h/2*Math.pow(2,10*(f-1))+a}return h/2*(-Math.pow(2,-10*--f)+2)+a},easeInRegular:function(e,f,a,h,g){return h*(f/=g)*f+a},easeOutRegular:function(e,f,a,h,g){return -h*(f/=g)*(f-2)+a},easeInOutRegular:function(e,f,a,h,g){if((f/=g/2)<1){return h/2*f*f+a}return -h/2*((--f)*(f-2)-1)+a}});

/*
 * Pixastic - JavaScript Image Processing Library
 * Copyright (c) 2008 Jacob Seidelin, jseidelin@nihilogic.dk, http://blog.nihilogic.dk/
 * MIT License [http://www.pixastic.com/lib/license.txt]
 */
var Pixastic=(function(){function d(m,n,l){if(m.addEventListener){m.addEventListener(n,l,false)}else{if(m.attachEvent){m.attachEvent("on"+n,l)}}}function g(n){var l=false;var o=function(){if(!l){l=true;n()}};document.write('<script defer src="//:" id="__onload_ie_pixastic__"><\/script>');var m=document.getElementById("__onload_ie_pixastic__");m.onreadystatechange=function(){if(m.readyState=="complete"){m.parentNode.removeChild(m);o()}};if(document.addEventListener){document.addEventListener("DOMContentLoaded",o,false)}d(window,"load",o)}function k(){var l=c("pixastic",null,"img");var o=c("pixastic",null,"canvas");var n=l.concat(o);for(var m=0;m<n.length;m++){(function(){var q=n[m];var s=[];var r=q.className.split(" ");for(var v=0;v<r.length;v++){var x=r[v];if(x.substring(0,9)=="pixastic-"){var u=x.substring(9);if(u!=""){s.push(u)}}}if(s.length){if(q.tagName.toLowerCase()=="img"){var p=new Image();p.src=q.src;if(p.complete){for(var w=0;w<s.length;w++){var t=Pixastic.applyAction(q,q,s[w],null);if(t){q=t}}}else{p.onload=function(){for(var y=0;y<s.length;y++){var z=Pixastic.applyAction(q,q,s[y],null);if(z){q=z}}}}}else{setTimeout(function(){for(var y=0;y<s.length;y++){var z=Pixastic.applyAction(q,q,s[y],null);if(z){q=z}}},1)}}})()}}if(typeof pixastic_parseonload!="undefined"&&pixastic_parseonload){g(k)}function c(r,p,l){var o=new Array();if(p==null){p=document}if(l==null){l="*"}var n=p.getElementsByTagName(l);var m=n.length;var q=new RegExp("(^|\\s)"+r+"(\\s|$)");for(i=0,j=0;i<m;i++){if(q.test(n[i].className)){o[j]=n[i];j++}}return o}var e;function a(m,n){if(!Pixastic.debug){return}try{switch(n){case"warn":console.warn("Pixastic:",m);break;case"error":console.error("Pixastic:",m);break;default:console.log("Pixastic:",m)}}catch(l){}if(!e){}}var b=(function(){var n=document.createElement("canvas");var m=false;try{m=!!((typeof n.getContext=="function")&&n.getContext("2d"))}catch(l){}return function(){return m}})();var f=(function(){var o=document.createElement("canvas");var n=false;var l;try{if(typeof o.getContext=="function"&&(l=o.getContext("2d"))){n=(typeof l.getImageData=="function")}}catch(m){}return function(){return n}})();var h=(function(){var n=false;var p=document.createElement("canvas");if(b()&&f()){p.width=p.height=1;var o=p.getContext("2d");o.fillStyle="rgb(255,0,0)";o.fillRect(0,0,1,1);var l=document.createElement("canvas");l.width=l.height=1;var q=l.getContext("2d");q.fillStyle="rgb(0,0,255)";q.fillRect(0,0,1,1);o.globalAlpha=0.5;o.drawImage(l,0,0);var m=o.getImageData(0,0,1,1).data;n=(m[2]!=255)}return function(){return n}})();return{parseOnLoad:false,debug:false,applyAction:function(E,p,o,n){n=n||{};var r=(E.tagName.toLowerCase()=="canvas");if(r&&Pixastic.Client.isIE()){if(Pixastic.debug){a("Tried to process a canvas element but browser is IE.")}return false}var m,v;var u=false;if(Pixastic.Client.hasCanvas()){u=!!n.resultCanvas;m=n.resultCanvas||document.createElement("canvas");v=m.getContext("2d")}var t=E.offsetWidth;var x=E.offsetHeight;if(r){t=E.width;x=E.height}if(t==0||x==0){if(E.parentNode==null){var s=E.style.position;var C=E.style.left;E.style.position="absolute";E.style.left="-9999px";document.body.appendChild(E);t=E.offsetWidth;x=E.offsetHeight;document.body.removeChild(E);E.style.position=s;E.style.left=C}else{if(Pixastic.debug){a("Image has 0 width and/or height.")}return}}if(o.indexOf("(")>-1){var A=o;o=A.substr(0,A.indexOf("("));var q=A.match(/\((.*?)\)/);if(q[1]){q=q[1].split(";");for(var B=0;B<q.length;B++){thisArg=q[B].split("=");if(thisArg.length==2){if(thisArg[0]=="rect"){var l=thisArg[1].split(",");n[thisArg[0]]={left:parseInt(l[0],10)||0,top:parseInt(l[1],10)||0,width:parseInt(l[2],10)||0,height:parseInt(l[3],10)||0}}else{n[thisArg[0]]=thisArg[1]}}}}}if(!n.rect){n.rect={left:0,top:0,width:t,height:x}}else{n.rect.left=Math.round(n.rect.left);n.rect.top=Math.round(n.rect.top);n.rect.width=Math.round(n.rect.width);n.rect.height=Math.round(n.rect.height)}var y=false;if(Pixastic.Actions[o]&&typeof Pixastic.Actions[o].process=="function"){y=true}if(!y){if(Pixastic.debug){a('Invalid action "'+o+'". Maybe file not included?')}return false}if(!Pixastic.Actions[o].checkSupport()){if(Pixastic.debug){a('Action "'+o+'" not supported by this browser.')}return false}if(Pixastic.Client.hasCanvas()){if(m!==E){m.width=t;m.height=x}if(!u){m.style.width=t+"px";m.style.height=x+"px"}v.drawImage(p,0,0,t,x);if(!E.__pixastic_org_image){m.__pixastic_org_image=E;m.__pixastic_org_width=t;m.__pixastic_org_height=x}else{m.__pixastic_org_image=E.__pixastic_org_image;m.__pixastic_org_width=E.__pixastic_org_width;m.__pixastic_org_height=E.__pixastic_org_height}}else{if(Pixastic.Client.isIE()&&typeof E.__pixastic_org_style=="undefined"){E.__pixastic_org_style=E.style.cssText}}var z={image:E,canvas:m,width:t,height:x,useData:true,options:n};var D=Pixastic.Actions[o].process(z);if(!D){return false}if(Pixastic.Client.hasCanvas()){if(z.useData){if(Pixastic.Client.hasCanvasImageData()){m.getContext("2d").putImageData(z.canvasData,n.rect.left,n.rect.top);m.getContext("2d").fillRect(0,0,0,0)}}if(!n.leaveDOM){m.title=E.title;m.imgsrc=E.imgsrc;if(!r){m.alt=E.alt}if(!r){m.imgsrc=E.src}m.className=E.className;m.style.cssText=E.style.cssText;m.name=E.name;m.tabIndex=E.tabIndex;m.id=E.id;if(E.parentNode&&E.parentNode.replaceChild){E.parentNode.replaceChild(m,E)}}n.resultCanvas=m;return m}return E},prepareData:function(q,m){var l=q.canvas.getContext("2d");var o=q.options.rect;var n=l.getImageData(o.left,o.top,o.width,o.height);var p=n.data;if(!m){q.canvasData=n}return p},process:function(m,l,n,q){if(m.tagName.toLowerCase()=="img"){var p=new Image();p.src=m.src;if(p.complete){var o=Pixastic.applyAction(m,p,l,n);if(q){q(o)}return o}else{p.onload=function(){var r=Pixastic.applyAction(m,p,l,n);if(q){q(r)}}}}if(m.tagName.toLowerCase()=="canvas"){var o=Pixastic.applyAction(m,m,l,n);if(q){q(o)}return o}},revert:function(l){if(Pixastic.Client.hasCanvas()){if(l.tagName.toLowerCase()=="canvas"&&l.__pixastic_org_image){l.width=l.__pixastic_org_width;l.height=l.__pixastic_org_height;l.getContext("2d").drawImage(l.__pixastic_org_image,0,0);if(l.parentNode&&l.parentNode.replaceChild){l.parentNode.replaceChild(l.__pixastic_org_image,l)}return l}}else{if(Pixastic.Client.isIE()){if(typeof l.__pixastic_org_style!="undefined"){l.style.cssText=l.__pixastic_org_style}}}},Client:{hasCanvas:b,hasCanvasImageData:f,hasGlobalAlpha:h,isIE:function(){return !!document.all&&!!window.attachEvent&&!window.opera}},Actions:{}}})();Pixastic.Actions.desaturate={process:function(b){var c=!!(b.options.average&&b.options.average!="false");if(Pixastic.Client.hasCanvasImageData()){var d=Pixastic.prepareData(b);var l=b.options.rect;var m=l.width;var e=l.height;var a=m*e;var f=a*4,k,g;if(c){while(a--){d[f-=4]=d[k=f+1]=d[g=f+2]=(d[f]+d[k]+d[g])/3}}else{while(a--){d[f-=4]=d[k=f+1]=d[g=f+2]=(d[f]*0.3+d[k]*0.59+d[g]*0.11)}}return true}else{if(Pixastic.Client.isIE()){b.image.style.filter+=" gray";return true}}},checkSupport:function(){return(Pixastic.Client.hasCanvasImageData()||Pixastic.Client.isIE())}};


/***** WICKEDWEB LIGHTBOX v1.1 | COPYRIGHT WICKEDWEB 2011 *****/

var lbStatus = 'closed';

function setCSS(css) {
	try{
		document.getElementsByTagName("head")[0].appendChild(css);
	}catch(e){
		setTimeout(function(){setCSS(css)}, 100);
	}
}
var css = document.createElement("link");
css.setAttribute("href","/static/css/wwightbox.css");
css.setAttribute("rel","stylesheet");
css.setAttribute("type","text/css");
setCSS(css);
css = null;

String.prototype.endsWith = function (b) {
    return (this.match(b + "$") == b)
};

function querySt(value, string){
	valueArray = string.split('?').join('&').split('&');
	for(i=0; i<valueArray.length; i++) {
		result = valueArray[i].split('=');
		if(result[0] == value) {
			return result[1];
		}
	}
}

function repositionLightBox(){
	if(($('#lightBoxInner').outerHeight() + 40) > $(window).height()){
		$('#lightBoxContent').css('top', ($(window).scrollTop() + 20) + 'px').css('position', 'absolute').css('margin-top', 0);
	}
	else{
		$('#lightBoxContent').css('top', '50%').css('position', 'fixed').css('margin-top', -($('#lightBoxInner').outerHeight() / 2));
	}
}

function animateLightBox(){
	$('#lightBoxInner').prepend('<a href="/Close" id="closeLightBoxBtn" class="closeLightBox"></a>')
	var mTop = -($('#lightBoxInner').outerHeight() / 2);
	var mLft = -($('#lightBoxInner').outerWidth() / 2);
	if(($('#lightBoxInner').outerHeight() + 40) > $(window).height()){
		$('#lightBoxContent').css('top', ($(window).scrollTop() + 20) + 'px').css('position', 'absolute').css('margin-top', 0);
		mTop = 0;
	}
	else{
		$('#lightBoxContent').css('top', '50%').css('position', 'fixed');
	}
	$('#lightBoxContent').stop().animate({ 
		width: $('#lightBoxInner').outerWidth(),
		height: $('#lightBoxInner').outerHeight(),
		marginTop: mTop,
		marginLeft: mLft,
		opacity: 1
	}, 600, "easeInOutRegular", function(){
		repositionLightBox();
		$('#lightBoxContent').css('filter', '');
		$('#lightBoxInner').css('visibility', 'visible');
		$('#lightBoxInner').stop().fadeTo(500, 1, function(){
			$('#lightBoxContent, #lightBoxInner').css('filter', '')
		});
	});
}

function detectContent(boxID){
	$('#lightBoxInner').empty();
	$('#lightBoxInner').width('auto').height('auto');
	$('#lightBoxContent').css('overflow', 'hidden').width($('#lightBoxContent').width()).height($('#lightBoxContent').height()).css('margin', (-($('#lightBoxContent').outerHeight() / 2)) + 'px 0 0 ' + (-($('#lightBoxContent').outerWidth() / 2)) + 'px');
	var Title = querySt('title', boxID);
	var Desc = querySt('desc', boxID);
	var Width = querySt('width', boxID);
	var Height = querySt('height', boxID);
	var LatLang = querySt('sll', boxID);
	if(Width !== undefined){
		$('#lightBoxInner').width(parseInt(Width));
	}
	if(Height !== undefined){
		$('#lightBoxInner').height(parseInt(Height));
	}
	if(Title !== undefined){
		$('#lightBoxInner').append('<h1>' + Title + '</h1>');
	}
	if(Desc !== undefined){
		$('#lightBoxInner').append(Desc);
	}
	if(boxID.indexOf('?') > -1){
		boxID = boxID.slice(0, boxID.indexOf('?'));
	}
	
	r = document.location.href.toString().replace(document.location.hash.toString(), '');
    if(boxID.indexOf('?') > -1){
        q = boxID.slice(boxID.indexOf('?'));
        boxID = boxID.slice(0, boxID.indexOf('?'))
    }
    if(boxID.indexOf(r) === 0){
        boxID = boxID.replace(r, '');
    }
	if(LatLang !== undefined){
		LatLang = LatLang.split(',');
		$('#lightBoxInner').append('<div id="map"></div>');
		var latlng = new google.maps.LatLng(LatLang[0], LatLang[1]);
		var myOptions = {
			zoom: 16,
			center: latlng,
			mapTypeId: google.maps.MapTypeId.ROADMAP
		};
		var map = new google.maps.Map(document.getElementById("map"), myOptions);
		//var image = 'imagesbeachflag.png';
		var beachMarker = new google.maps.Marker({
			position: latlng,
			map: map//,
			//icon: image
		});
		animateLightBox();
		lbStatus = 'content';
	}
    else if(boxID.indexOf('#') === 0) {
		$(boxID).clone(true).appendTo('#lightBoxInner').show();
		animateLightBox();
		lbStatus = 'content';
    }
	else if(boxID.endsWith('.html') || boxID.endsWith('.htm') || boxID.endsWith('.php') || boxID.endsWith('.aspx')){
		$('#lightBoxMid').addClass('loading');
		if(lbStatus == 'closed'){
			$('#lightBoxInner').width(100).height(100);
			animateLightBox();
		}
		$('#lightBoxInner').load(boxID, function(){
			$('#lightBoxMid').removeClass('loading');
			animateLightBox();
			lbStatus = 'html';
		});	
	}
	else if(boxID.endsWith('.jpg') || boxID.endsWith('.gif') || boxID.endsWith('.png') || boxID.endsWith('.bmp') || boxID.endsWith('.tiff')){
		$('#lightBoxMid').addClass('loading');
		if(lbStatus == 'closed'){
			$('#lightBoxInner').width(100).height(100);
			animateLightBox();
		}		
		var img = "img" + boxID.slice(boxID.lastIndexOf("/") + 1, boxID.lastIndexOf("."));
		eval[img] = new Image();
		eval[img].onload = function (evt){
			$('#lightBoxInner').fadeTo(0, 0, function(){
				if(Width !== undefined){
					eval[img].width = parseInt(Width);
				}
				if(Height !== undefined){
					eval[img].height = parseInt(Height);
				}
				$('#lightBoxInner').width(eval[img].width).height(eval[img].height).append(eval[img]);
				if(galleryArray.length > 1){
					$('#lightBoxInner').append('<a href="/Previous" class="prevImg"></a><a href="/Next" class="nextImg"></a>')
				}
				$('#lightBoxMid').removeClass('loading');
				animateLightBox();
				lbStatus = 'image';
			});
		};
		eval[img].src = boxID;
		imgNo = jQuery.inArray(boxID, galleryArray);
	}
}

function showLightBox(boxID){
	$('#lightBoxInner').css('visibility', 'hidden');
	if($('#lightBoxContent:visible').size() > 0){
		detectContent(boxID);
	}
	else{
		$('#lightBox').stop().show().fadeTo(0, 0).fadeTo(500, 0.75, function(){
			$('#lightBoxContent').stop().show().fadeTo(0, 0, function(){
				detectContent(boxID);
			});
		});
	}
}

function openLightBox(boxID){
	if($('#lightBoxContent:visible').size() > 0){
		$('#lightBoxInner').stop().fadeTo(500, 0, function(){
			showLightBox(boxID);
		});
	}
	else{
		$('#lightBoxInner').fadeTo(0, 0, function(){
			showLightBox(boxID);
		});
	}
}

var galleryArray=[];
var imgNo = 0;

function gallery(){
	galleryArray=[];
	$('.lightBox[href$=".jpg"], .lightBox[href$=".gif"], .lightBox[href$=".png"], .lightBox[href$=".bmp"], .lightBox[href$=".tiff"]').each(function(){
		var inArray = false
		var url = $(this).attr('href');
		if(jQuery.inArray(url, galleryArray) == -1){
			galleryArray.push(url);
		}
	});
	$('a[rel$=".jpg"], a[rel$=".gif"], a[rel$=".png"], a[rel$=".bmp"], a[rel$=".tiff"]').each(function(){
		var inArray = false
		var url = $(this).attr('rel');
		if(jQuery.inArray(url, galleryArray) == -1){
			galleryArray.push(url);
		}
	});
	$('.thumbs a').not('.thumbs a.lightBox').click(function(){
		$('.thumbs a:hidden').insertBefore($(this)).show();
		$(this).hide();
		$('.gallery a').attr('href', $(this).attr('rel'));
		var img = $(this).attr('href');
		eval[img] = new Image();
		eval[img].src = img;
		$('.gallery a img').stop().fadeTo(500,0,function(){
			eval[img] = new Image();
			eval[img].onload = function (evt){
				$('.gallery a img').attr('src', img);
				$('.gallery a img').fadeTo(500,1);
			};
			eval[img].src = img;
		});
		return false;
	});
}

function closeLightBox(){
	$('#lightBoxMid').removeClass('loading');
	$('#lightBoxContent').stop().fadeTo(500, 0, function(){
		$('#lightBox').stop().fadeTo(500, 0, function(){
			$('#lightBoxContent').hide().width('100px').height('100px');
			$('#lightBox').hide();
			$('#lightBoxInner').empty();
			lbStatus = 'closed';
		});
	});
}

function initWWightBox(){
	$('body').append('<div id="lightBox" class="closeLightBox"></div><div id="lightBoxContent"><div id="lightBoxMid"><div id="lightBoxInner"></div></div></div>');
	$(document).bind('mouseup', function(e){
		$('.zoomBox img').unbind('mousemove');
		$('.zoomBox img').css('cursor', 'pointer');
	});
	$(window).resize(function(){
		repositionLightBox();
	});
}

function prevImage(){
	imgNo--;
	if(imgNo < 0){
		imgNo = galleryArray.length-1;
	}
	openLightBox(galleryArray[imgNo]);
}

function nextImage(){
	imgNo++;
	if(imgNo > (galleryArray.length-1)){
		imgNo = 0;
	}
	openLightBox(galleryArray[imgNo]);
}

$('.lightBox').live('click', function(e){
	openLightBox($(this).attr('href'));
	return false;
});

$('.closeLightBox').live('click', function(){
	closeLightBox()
	return false;
});

$('.prevImg').live('click', function(){
	prevImage()
	return false;
});

$('.nextImg').live('click', function(){
	nextImage()
	return false;
});

$(document).keyup(function(event){
	if(lbStatus != 'closed' && event.keyCode == '27'){
		event.preventDefault();
		closeLightBox();
	}
	else if(lbStatus == 'image' && (event.keyCode == '37' || event.keyCode == '80')){
		event.preventDefault();
		prevImage()
	}
	else if(lbStatus == 'image' && (event.keyCode == '39' || event.keyCode == '78')){
		event.preventDefault();
		nextImage()
	}
	else if(lbStatus == 'image' && ((parseInt(event.keyCode)) > 48 && (parseInt(event.keyCode)) < 58)){
		if((9 - (58 - (parseInt(event.keyCode)))) != imgNo){
			imgNo = (9 - (58 - (parseInt(event.keyCode))));
			if(imgNo > (galleryArray.length-1)){
				imgNo = (galleryArray.length-1)
			}
			openLightBox(galleryArray[imgNo]);
		}
	}
	else if(lbStatus == 'image' && ((parseInt(event.keyCode)) > 96 && (parseInt(event.keyCode)) < 106)){
		if((9 - (106 - (parseInt(event.keyCode)))) != imgNo){
			imgNo = (9 - (106 - (parseInt(event.keyCode))));
			if(imgNo > (galleryArray.length-1)){
				imgNo = (galleryArray.length-1)
			}
			openLightBox(galleryArray[imgNo]);
		}
	}
});


$(function(){
	initWWightBox();
});


/***** WEBSITE FUNCTIONS *****/

function footer(){
	$('#footer4 img').css('opacity','0.5');
	$('#footer4 a').hover(function(){
		$(this).find('img').stop().fadeTo(500,1);
	},function(){
		$(this).find('img').stop().fadeTo(500,0.5);
	});
}

function mainNav(){
	//$('#mainNav > ul > li:last').css('border-right','none');
	$('#mainNav > ul > li').hover(function(){
		$(this).addClass('over');
		$('#subNavInner').html('<ul>' + $(this).find('ul:first').html() + '</ul>');
		$('#subNavInner > ul > li:nth-child(3n)').each(function(){
			var height = $(this).prev().prev().height();
			if($(this).prev().height() > height){
				height = $(this).prev().height();
			}
			if($(this).height() > height){
				height = $(this).height();
			}
			
			if($(this).prev().prev().nextAll().find('ul').size() > 0){
				$(this).prev().prev().height(height);
			}
			if($(this).prev().nextAll().find('ul').size() > 0){
				$(this).prev().height(height);
			}
			if($(this).nextAll().find('ul').size() > 0){
				$(this).height(height);
			}
		});
		var navHeight = $('#subNavInner').height();
		$('#mainNavSub').stop().animate({
			height: navHeight
		}, 500, "easeOutQuint");
	},function(){
		$(this).removeClass('over');
		$('#mainNavSub').stop().animate({
			height:0
		}, 500, "easeOutQuint");
	});
	$('#mainNavSub').hover(function(){
		var navHeight = $('#subNavInner').height();
		$('#mainNavSub').stop().animate({
			height: navHeight
		}, 500, "easeOutQuint");
	},function(){
		$('#mainNavSub').stop().animate({
			height:0
		}, 500, "easeOutQuint");
	});
	if($('#leftNav > *').size() == 0){
		$('#leftNav').remove();
	}
}

function searchField(){
	$('.searchField').focus(function(){
		if($(this).val() == $(this).attr('title')){
			$(this).val('');
		}
	});
	$('.searchField').blur(function(){
		if($(this).val().length <= 0 || $(this).val() == $(this).attr('title')){
			$(this).val($(this).attr('title'));
		}
	});
}

function changeFade(n){
	$('.pages li a.on').removeClass('on');
	$('.pages li:eq(' + n + ') a').addClass('on');
	$('.fader li:visible').first().not($('.fader li:eq(' + n + ')')).stop().fadeOut(500,function(){
		$('.fader li').removeAttr('style').hide();
		$('.fader li:eq(' + n + ')').stop().fadeIn(500);
	});
}

function fader(){
	var index = 0;
	$('<ol class="pages"></ol>').insertAfter('.fader');
	$('.fader li').each(function(e){
		$(this).parent().next('.pages').append('<li><a>' + (e+1) + '</a></li>');
	});
	$('.homeCallout').height($('#homeCallouts').height() - 55);
	$('.fader li:gt(0)').hide();
	$('.pages li a:first').addClass('on');
	$('.pages li a').click(function(){
		clearInterval(fadeInt);
		$this = $(this);
		index = $this.parent().parent().find('a').index($this);
		changeFade(index);
		return false;
	});
	var fadeInt = setInterval(function(){
		index++;
		if(index >= $('.pages li').size()){
			index = 0;
		}
		changeFade(index);
	},5000);
}

function greyScale(){
	$('.greyScale').each(function(){
		var $this = $(this);
		var img = new Image();
		img.onload = function() {
			Pixastic.process(img, "desaturate", {average : false});
			$this.find('.colour').not($('.slider .colour:eq(1), .quoteList .colour:eq(0)')).stop().fadeTo(500,0);
		}
		$(img).insertAfter($this.find('img:first'));
		$this.find('> *:first').addClass('colour');
		img.src = $this.find('img:first').attr('src');
	});
	$('.greyScale').not('.offset .greyScale, .quoteTop .greyScale').hover(function(){
		$(this).find('> *:first').stop().fadeTo(500,1);
	},function(){
		$(this).find('> *:first').stop().fadeTo(500,0);
	});
}

function slider(){
	if($('.slider li').size() > 0){
		function rightClick(){
			$('.rightArrow').click(function(){
				var $ul = $(this).parent().find('ul');
				$(this).unbind('click');
				if($ul.find('li:first').position().left < startX){
					$ul.css('margin-left',startX + 'px');
					$ul.find('li:first').appendTo($ul);
				}
				$ul.find('.colour').not($ul.find('.colour:eq(2)')).fadeTo(500,0);
				$ul.find('.colour:eq(2)').fadeTo(500,1)
				$ul.animate({
					marginLeft:('-=' + sliderWidth)
				},500,function(){
					rightClick();
				});
			});
		}
		function leftClick(){
			$('.leftArrow').click(function(){
				var $ul = $(this).parent().find('ul');
				$(this).unbind('click');
				if($ul.find('li:first').position().left > -sliderWidth){
					$ul.css('margin-left','-' + (sliderWidth + (-startX)) + 'px');
					$ul.find('li:last').prependTo($ul);
				}
				$ul.find('.colour').not($ul.find('.colour:eq(1)')).fadeTo(500,0);
				$ul.find('.colour:eq(1)').fadeTo(500,1)
				$ul.animate({
					marginLeft:('+=' + sliderWidth)
				},500,function(){
					leftClick();
				});
			});
		}
		$('.slider').each(function(){
			$this = $(this);
			$ul = $this.find('ul');
			sWidth = $this.find('li').size() * $this.find('li:first').width() - parseInt($this.find('li:first').css('padding-left'));
			if(sWidth > $this.parent().width()){
				$this.append('<div class="leftArrow">&nbsp;</div><div class="rightArrow">&nbsp;</div>');
				if($this.parent('#middleColumn').size() > 0){
					$this.find('ul').addClass('offset');
					if($this.find('li').size() == 3){
						$this.find('li').clone().appendTo($this.find('ul'));
					}
				}
			}
		});
		var sliderWidth = $('.slider li:first').outerWidth();
		var startX = $('.slider ul li:first').position().left;
		rightClick();
		leftClick();
	}
	else{
		$('.slider, h2.caseStudies').remove();
	}
}

function borderImages(){
	$('img.borderImage').each(function(){
		$(this).wrap('<div class="borderImage"></div>');
		$(this).parent().append('<div></div>')
		$(this).removeClass('borderImage');
	});
}

function months(){
    $('.monthList ul').not(':first').hide();
    $('.monthList > li:first').addClass('on');
    $('.monthList > li').click(function(){
        $(this).parent().find('> li').removeClass('on').find('ul:visible').not($(this).find('ul')).slideUp(500, function(){
		});
        $(this).addClass('on').find('ul').slideDown(500, function(){
		});
		return false;
    });
}

function newsList(){
	$('.newsList li, .projectList li, .documentList li').hover(function(){
		$(this).addClass('over');
	},function(){
		$(this).removeClass('over');
	});
	$('.newsList li, .projectList li, .documentList li').click(function(){
		var url = $(this).find('a:first').attr('href');
		if($(this).find('a:first').attr('target') == '_blank'){
			window.open(url, '_blank');
		}
		else{
			document.location.href = url;
		}
	})
}

function overlayText(){
	$('#mainBanner .overlay').wrapInner('<div></div>');
	var textHeight = $('#mainBanner .overlay div:first').height();
	var margin = Math.round(160 - textHeight)/2;
	$('#mainBanner .overlay div:first').css('margin-top', margin + 'px');
}

function quotes(){
	$('.quoteList li').not(':first').addClass('closed').height(115)
	$('.quoteList li').click(function(){
		var $this = $(this)
		$('.quoteList li').not($(this)).not('.closed').addClass('closed').animate({
			height:115
		}, 500).find('.colour').stop().fadeTo(500,0);
		var itemHeight = $(this).find('.quoteTop').outerHeight() + $(this).find('blockquote').outerHeight();
		$(this).removeClass('closed').animate({
			height:itemHeight
		}, 500).find('.colour').stop().fadeTo(500,1);
	});
}

function fadeImages(){
	$('.main-team-thumb-display .anotherone').each(function(){
		var $li = $(this);
		var src = $li.find('img:last').attr('src');
		$li.find('img').clone().insertAfter($li.find('img')).css('position', 'absolute').addClass('grey');;
		$li.find('img:first').load(function(){
			$li.find('span').fadeTo(600,0.7).width($li.width() - 28);
			Pixastic.process($li.find('img:first')[0], "desaturate", {average : false});
			$li.find('img:last').stop().fadeTo(0,0).css({visibility:'visible', zIndex:'10'}).fadeTo(600,1,function(){
				$li.find('img:first, canvas').css('visibility', 'visible');
				$li.find('img:last').fadeTo(600,0);
			});
		}).attr('src',src);
	
		$('.anotherone').hover(function(){
			$(this).find('img:first, canvas').css('visibility', 'visible');
			$(this).find('img:last').stop().fadeTo(500,1);
			$(this).find('span').stop().fadeTo(500,1);
		},function(){
			$(this).find('img:last').stop().fadeTo(500,0);
			$(this).find('span').stop().fadeTo(500,0.7);
		});
	});
}
function fadeImages2(){
	$('.rd-ways-wrapper .anotherone').each(function(){
		var $li = $(this);
		var src = $li.find('img:last').attr('src');
		$li.find('img').clone().insertAfter($li.find('img')).css('position', 'absolute').addClass('grey');;
		$li.find('img:first').load(function(){
			$li.find('span').fadeTo(600,0.7).width($li.width() - 28);
			Pixastic.process($li.find('img:first')[0], "desaturate", {average : false});
			$li.find('img:last').stop().fadeTo(0,0).css({visibility:'visible', zIndex:'10'}).fadeTo(600,1,function(){
				$li.find('img:first, canvas').css('visibility', 'visible');
				$li.find('img:last').fadeTo(600,0);
			});
		}).attr('src',src);
	
		$('.anotherone').hover(function(){
			$(this).find('img:first, canvas').css('visibility', 'visible');
			$(this).find('img:last').stop().fadeTo(500,1);
			$(this).find('span').stop().fadeTo(500,1);
		},function(){
			$(this).find('img:last').stop().fadeTo(500,0);
			$(this).find('span').stop().fadeTo(500,0.7);
		});
	});
}
function fadeImages3(){
	$('.rd-quart-left .anotherone').each(function(){
		var $li = $(this);
		var src = $li.find('img:last').attr('src');
		$li.find('img').clone().insertAfter($li.find('img')).css('position', 'absolute').addClass('grey');;
		$li.find('img:first').load(function(){
			$li.find('span').fadeTo(600,0.7).width($li.width() - 28);
			Pixastic.process($li.find('img:first')[0], "desaturate", {average : false});
			$li.find('img:last').stop().fadeTo(0,0).css({visibility:'visible', zIndex:'10'}).fadeTo(600,1,function(){
				$li.find('img:first, canvas').css('visibility', 'visible');
				$li.find('img:last').fadeTo(600,0);
			});
		}).attr('src',src);
	
		$('.anotherone').hover(function(){
			$(this).find('img:first, canvas').css('visibility', 'visible');
			$(this).find('img:last').stop().fadeTo(500,1);
			$(this).find('span').stop().fadeTo(500,1);
		},function(){
			$(this).find('img:last').stop().fadeTo(500,0);
			$(this).find('span').stop().fadeTo(500,0.7);
		});
	});
}
moredocs = function() {
	setTimeout('findout()', 1000);
}
findout = function() {
	 	var n = $("li.rd-quart-item").length;

			if (n > 3) {
				$("#more").show();
				
				$("#x li.rd-quart-item:gt(2)").fadeOut();
			}
			else {
				$("#x li.rd-quart-item:gt(2)").fadeIn();
			}
}
showtherest = function() {
		  $('#more').fadeOut();
		  $("#x li:gt(2)").fadeIn();
		 
};
testimonials = function() {
	$('.testimonial-plusminuschange').replaceWith( "<a class=\"hellow\"><img src=\"/static/images/testi-plus.png\" alt=\"more\"/></a>" );
	$('.testimonial-bottom').slideToggle('slow');
};
$('.hellow').live('click', function(e){
       $('.goodbyew').trigger('click');
       $(this).parent().next('.testimonial-bottom').slideToggle('slow');
	   $(this).replaceWith( "<a class=\"goodbyew\"><img src=\"/static/images/testi-minus.png\" alt=\"more\"/></a>" );
});
$('.goodbyew').live('click', function(e){
       $(this).parent().next('.testimonial-bottom').slideToggle('slow');
	   $(this).replaceWith( "<a class=\"hellow\"><img src=\"/static/images/testi-plus.png\" alt=\"more\"/></a>" );
});



$(function(){
	moredocs();
	mainNav();
	searchField();
	fader();
	slider();
	quotes();
	greyScale();
	fadeImages();
	fadeImages2();
	fadeImages3();
	months();
	newsList();
	overlayText();
	footer();
	//borderImages();
	
	if ( $.fn.cycle ) {
	
		//  Banner cycle
		
		var numBanners = $('#banner-images img' ).length;
	
		if ( numBanners > 0 ) {
		
			var transitionSpeed = 1500;
			
			//  Remove unrequired overlays
			$('#banner #overlay .banner-overlay:gt(' + ( numBanners - 1 ) + ')').remove();
		
			var $overlays 	= $('#banner #overlay .banner-overlay');
		
			//  Start cycle
			$( '#banner-images' ).cycle({
				before: function( currSlideElement, nextSlideElement, options, forwardFlag ) {
				
					if ( currSlideElement == nextSlideElement ) {
						return;
					}

					//  Hide current overlay
					$( $overlays.get( $( currSlideElement ).index() ) ).fadeOut( transitionSpeed );
					
					//  Show next overlay
					$( $overlays.get( $( nextSlideElement ).index() ) ).fadeIn( transitionSpeed );
					
				},
				speed: transitionSpeed,
				timeout: 6000
			});
		
		}
	
	}
	
});
