$('html').addClass('json');

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

var citrusCMS = {
		
	load : function() {
		citrusCMS.coreFunctions();
		citrusCMS.textResizer();
		citrusCMS.currencyConverter();	
		if ($('#rotator .carousel')[0]){ citrusCMS.carousel();};
		if ($('.tabs')[0]){ citrusCMS.tabs();};
		if ($('.enquirebutton')[0]){ citrusCMS.enquirePopup();};
		if ($('#gallery')[0]){ citrusCMS.gallery();};
		if ($('a.viewlarger')[0]){ citrusCMS.fancybox();};	
	},
	
	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=''; }
			}
		}); 
		
		// ANIMATE DROP DOWN NAVIGATION
		$('#nav li ul').hide();
		$('#nav li').hover(function(){	
				$('ul', this).stop(true, true).slideDown(200);
			},
			function(){	
				$('ul', this).stop(true, true).hide();
		});
		
		Cufon.replace('h1, h2, h3, h4, h5, h6, .homecontent .contact a, #topbar .email, #topbar .phone, .listing .title, #propinfo .pricelabel, #propinfo .price, #calltoaction .phonelabel, #calltoaction .phone', { textShadow: '0px 1px white' } );
		Cufon.replace('#propsearch h2, .currency h2, .recentlyviewed h2', { textShadow: '0px 1px #4174a3' } );
		Cufon.replace('#rotator .caption', { textShadow: '0px 1px white' } );	
	},
	
	carousel : function(){
		var autoscrolling = true;
		$('#rotator .carousel').infiniteCarousel().mouseover(function () {
			autoscrolling = false;
		}).mouseout(function () {
			autoscrolling = true;
		});
		setInterval(function () {
			if (autoscrolling) {
				$('#rotator .carousel').trigger('next');
			}
		}, 4000);
	},
	
	fancybox : function(){
		 $("a.viewlarger").fancybox({
				'speedIn':		300,
				'speedOut':	300,
				'overlayColor':	'#000',
				'overlayOpacity':	0.7
		 });
	 },
	 
	 tabs : function(){
			
		$('.tab_content').hide(); 
		$('ul.tabs li:first').addClass('active').show(); 
		$('.tab_content:first').show();
		
		$('ul.tabs li').click(function() {
			$('ul.tabs li').removeClass('active'); 
			$(this).addClass('active'); 
			$('.tab_content').hide(); 
			var activeTab = $(this).find('a').attr('href'); 
			$(activeTab).show(); 
			return false;
		});
	 },
	 
	 gallery : function(){
		$('#gallery').galleryView({
			panel_width: 630,
			panel_height: 390,
			frame_gap: 5,
			transition_interval: 0,
			transition_speed: 500,
			overlay_opacity: 1,
			filmstrip_position: 'bottom',
			filmstrip_style: 'scroll',
			filmstrip_size: 4,
			frame_width: 133,
			frame_height: 82,
			frame_opacity: 0.6,
			pointer_size: 0,
			show_panel_nav: false
		});
		// APPEND OVERLAY ELEMENTS FOR DETAIL PAGE GALLERY
		$('div#gallery').append('<span class="galleryoverlay">&nbsp;</span>');
		$('.gv-img_wrap').append('<span class="thumboverlay">&nbsp;</span>');
	 },
	 
	 enquirePopup : function(){
		$('.enquirebutton').click(function(){
			$('#enquireoverlay').css({ height: $(document).height()});
			$('#enquireoverlay').show();
			$('#enquirecontent').slideDown(400);
			$('#enquirecontent').css({ top: $(window).scrollTop()+ 'px' });
			$(document).unbind('keydown.fb').bind('keydown.fb', function(e) {
				if (e.keyCode == 27) {
					$('#enquireoverlay').hide();
					$('#enquirecontent').slideUp();
				};
		});
		return false;
		});
		$('#enquire .close').click(function(){
			$('#enquireoverlay').hide();
			$('#enquirecontent').slideUp();
		return false;
		});
		
		 $('#enquirysubmit').click(function(){
			$.post('/content/ajax/processenquiry.asp', $('#enquiryform').serialize(),function(data){if (data=='true'){
					$('#showresponse').html('Thank you, your enquiry has been sent to a member of our team'); $('#enquiryform').hide(); return false;
				}else{
					$('#showresponse').attr('class','formerror');
					$('#showresponse').html('Please fill in all required fields.');
					if (data.indexOf('enquire_email') > 0){$('#showresponse').html($('#showresponse').html() + ' Please enter a valid email address');}
					results = data.split(',')
					$('#enquiryform li, #enquiryform li div').removeClass('formerror');
					$.each(results, function(){
						$('#'+this).parent().parent().parent().addClass('formerror');	
						$('#'+this).parent().parent().addClass('formerror'); 
					});
			}
			});
			return false;
		 });
	 },
	 
	 textResizer : function (){
		 // COOKIE JS INITIATION
		 jQuery.cookie = function(name, value, options) {
				if (typeof value != 'undefined') { // name and value given, set cookie
					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(); // use expires attribute, max-age is not supported by IE
					}
					// CAUTION: Needed to parenthesize options.path and options.domain
					// in the following expressions, otherwise they evaluate to undefined
					// in the packed version for some reason...
					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 { // only name given, get cookie
					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]);
							// Does this cookie string begin with the name we want?
							if (cookie.substring(0, name.length + 1) == (name + '=')) {
								cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
								break;
							}
						}
					}
					return cookieValue;
				}
			};
	
		 // TEXT RESIZER WITH COOKIE SUPPORT
		 
		 if($.cookie('TEXT_SIZE')) {
				  $('#container').addClass($.cookie('TEXT_SIZE'));
				  $('#largetext, #mediumtext, #smalltext').removeClass('on');
				  $('#' + $.cookie('TEXT_SIZE')).addClass('on');
			  }
			  $('#largetext, #mediumtext, #smalltext').click(function() {
				  $('#largetext, #mediumtext, #smalltext').removeClass('on');
				  $(this).addClass('on');
				  var textSize = $(this).attr('id');
				  $('#container').removeClass('smalltext mediumtext largetext').addClass(textSize);
				  $.cookie('TEXT_SIZE',textSize, { path: '/', expires: 10000 });
				  return false;
		 });
	 },
	 
	 
	 currencyConverter : function() {
			 $('#convert').click(function(){
 					 var amount = $('#amount').val();
					 var from = $('#fromCurrency').val();
					 var to = $('#toCurrency').val();
					  var dataString = "amount=" + amount + "&from=" + from + "&to=" + to;
					 
					 $.ajax({
					 type: "POST",
					 url: "/content/currencyconverter.asp",
					 data: dataString,
					 
					 success: function(data){
					 //Show results div
					 $('#results').show();
					 
					 //Put received response into result div
					 $('#results').html(data);
					 }
					 
					 });
					 return false;
			}); 
		 
	 }
	 
}


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);

