// true if IE
var IE = /*@cc_on!@*/false;

Cufon.replace('h1, h2, .leftnav a, .infiniteCarousel .caption, .projectshome li a.clickthrough, .gallerycats a.title, .formarea li label, .homeboxes li span, .mainnewslisting span.title, .projectslisting .title');
Cufon.replace('#navigation a.firstlevel', {
				textShadow: '#000 0px 1px'
			});



		
var citrusCMS = {
		
	load : function() {
		citrusCMS.coreFunctions();
		citrusCMS.carousel();
		citrusCMS.textResizer();
		//citrusCMS.pngfix();
	},
	
		coreFunctions : function(){
		
		$('.openWindow').click(function() {pageTracker._trackPageview('/outbound/'+this.href.replace('http://','')); window.open(this.href); return false});
		$('.printWindow').click(function() {window.print()});
	
		$('.textform').focus(function() {
			if(this.nodeName != 'SELECT'){
				$('#' + this.id).attr('class', 'textformfocus');
				if (this.value.substr(this.value.length-3,3) == '...') {this.value=''; }
						}
			});
		 $('.textform').blur(function(){$('#' + this.id).attr('class', 'textform');});
		 
		 $('#navigation li').mouseover(function () {
		 	$(this).addClass('over');
		 });
		 $('#navigation li').mouseleave(function () {
		 	$(this).removeClass('over');
		 });
		 
		 
		 
	},
	
	pngfix : function(){
		if (IE) {DD_belatedPNG.fix('img, div, h1, h2, h3, h4, h5, h6, a, li, span, input, #container, button, .overlay, .slider_handle, .slider_bar');}
	},
	
	carousel : function() {
		
	   var autoscrolling = true;
		
		$('.infiniteCarousel').infiniteCarousel().mouseover(function () {
			autoscrolling = false;
		}).mouseout(function () {
			autoscrolling = true;
		});
		
		setInterval(function () {
			if (autoscrolling) {
				$('.infiniteCarousel').trigger('next');
			}
		}, 9000);
		
		$('a.back span, a.forward span').hide();
		
		$('a.back').mouseover(function () {
			$('a.back span').show();
		});
		$('a.back').mouseleave(function () {
			$('a.back span').hide();
		});
		
			
		$('a.forward').mouseover(function () {
			$('a.forward span').show();
		});
		$('a.forward').mouseleave(function () {
			$('a.forward span').hide();
		});

	},
	
	projectLocations : function() {
		$('#category').change(function(){
				window.location = '/project-locations/' + $('#category').attr('value');
				});	
	},
	
	projectsHome : function() {
		$('.projectshome li').mouseenter(function () { 
			$(this).addClass('over');
		});
		$('.projectshome li').mouseleave(function () { 
			$(this).removeClass('over');
		});
	},
	
	
	fancyBox : function() {
		$('a.viewlarger').fancybox({
			'overlayOpacity'	:	0.7,
			'overlayColor'		:	'#000000'
		});

	},
	
	cookie : function(name,value,options) {
		if(typeof value!='undefined'){options=options||{};if(value===null){value='';options.expires=-1;}var expires='';if(options.expires&&(typeof options.expires=='number'||options.expires.toUTCString)){var date;if(typeof options.expires=='number'){date=new Date();date.setTime(date.getTime()+(options.expires*24*60*60*1000));}else{date=options.expires;}expires='; expires='+date.toUTCString();}var path=options.path?'; path='+(options.path):'';var domain=options.domain?'; domain='+(options.domain):'';var secure=options.secure?'; secure':'';document.cookie=[name,'=',encodeURIComponent(value),expires,path,domain,secure].join('');}else{var cookieValue=null;if(document.cookie&&document.cookie!=''){var cookies=document.cookie.split(';');for(var i=0;i<cookies.length;i++){var cookie=jQuery.trim(cookies[i]);if(cookie.substring(0,name.length+1)==(name+'=')){cookieValue=decodeURIComponent(cookie.substring(name.length+1));break;}}}return cookieValue;}
	},
	
	
	opacityrollover : function() {
		var defaults = {
		mouseOutOpacity:   0.67,
		mouseOverOpacity:  1.0,
		fadeSpeed:         'fast',
		exemptionSelector: '.selected'
	};

		$.fn.opacityrollover = function(settings) {
			// Initialize the effect
			$.extend(this, defaults, settings);
	
			var config = this;
	
			function fadeTo(element, opacity) {
				var $target = $(element);
				
				if (config.exemptionSelector)
					$target = $target.not(config.exemptionSelector);	
				
				$target.fadeTo(config.fadeSpeed, opacity);
			}
	
			this.css('opacity', this.mouseOutOpacity)
				.hover(
					function () {
						fadeTo(this, config.mouseOverOpacity);
					},
					function () {
						fadeTo(this, config.mouseOutOpacity);
					});
	
			return this;
	};
		
		
	},
	
	textResizer : function() {
			var startSize = citrusCMS.cookie('fontSize');
				if (startSize !='' && startSize!= null){startSize = parseFloat(startSize, 10);} else {startSize = 12; citrusCMS.cookie('fontSize', 12);}
				$('.resizeable').css('font-size', startSize);
				
				$('.slider_bar').slider({
						handle: '.slider_handle',
						min: 8,
						max: 23,
						value: startSize
					}).bind('slidestop', function(event, ui) {
						var sliderValue = $('.slider_bar').slider('value');
						$('.resizeable').css('font-size', sliderValue).fadeIn('slow');
						citrusCMS.cookie('fontSize', parseFloat(sliderValue, 10));
						Cufon.refresh();
					});
					$('.slider_bar').slider('option', 'value', 20 );		
	}
	
}

var citrusWeb = {
	
	
}


var citrusAjax = {
	
	makeRequest: function(requestURL,getID){
		$.ajax({
		    url: requestURL,
		    success: function(data) {
			$(getID).html(data);
		  }
		});
	},
	
	submitForm : function(pageUrl) {
		
		$('#waiting').html('<img src="/_img/layup/loading.gif" />');
		$('#submitButton').type == 'button'; //if js then remove the submit function of the button
		$('#submitButton').disabled = 'disabled'; //when submited disable button
		
		waitPadd = (($('#cmsForm')).height() - 45) /2
		$('#waiting').css('paddingTop', waitPadd + 'px');
		$('#waiting').css('paddingBottom', waitPadd + 'px');
		$('#waiting').fadeIn(); //show waiting div
		
		//stops whole site appearing when ajax is called
		if(pageUrl) {pageLocation = pageUrl} else {pageLocation = window.location}
		
		$.post(pageUrl, 'ajax=1&' +  $('#cmsForm').serialize(), function(data){citrusAjax.showResponse(data)});
		
	},

	showResponse : function(originalRequest) {
			
			if(originalRequest.substring(0,4) == 'http') {
				window.location = originalRequest; //redirect
			  } else {
				$('#waiting').fadeOut() ; //hide waiting div
				$('#cmsForm').html(originalRequest); //show response
				//$('#submitButton').effect('shake', { times:3 }, 100);
				citrusCMS.load();
  			}

	}
	
}


$(document).ready(citrusCMS.load);
//$(document).ready(DD_belatedPNG.fix('img, div, h1, h2, h3, h4, h5, h6, a, li, span, input, #container, button, .overlay, .slider_handle, .slider_bar'));
