 /*
  * All JQuery functions in here
  */
$(document).ready(function() {
	var comparablePropertyWidth = $('.comparableProperty').width() * 3;
	// PNG Fix
	$('body').pngFix();
	
	$('#mask-gallery, #gallery li').width($('#slider').width());      
	$('#gallery').width($('#slider').width() * $('#gallery li').length);  
	$('#mask-gallery, #gallery li, #mask-excerpt, #excerpt li').height($('#slider').height());
	
	$('#gallery').cycle({
		fx:'shuffle',
		timeout:5000,
		delay:-1500
	});
	$('#excerpt').cycle({
		fx:'fade',
		timeout:5000,
		delay:-1500
	});
	// Property Search Box - Top
	$('#search-top-sale-button').click(function(e) {
		e.preventDefault();
		var inp = $('#top-property-search #searchToLet');
		var frm = $('form#top-property-search');
		$(inp).val('');
		frm.submit();
	});
	$('#search-top-let-button').click(function(e) {
		e.preventDefault();
		var inp = $('#top-property-search #searchToLet');
		var frm = $('form#top-property-search');
		$(inp).val('l');
		frm.submit();
	});
	$('.frmQuickSearchRHColumn #forsale').click(function(e) {
		e.preventDefault();
		var inp = $('.frmQuickSearchRHColumn #searchToLet');
		var frm = $('.frmQuickSearchRHColumn');
		$(inp).val('');
		frm.submit();	
	});
	$('.frmQuickSearchRHColumn #tolet').click(function(e) {
		e.preventDefault();
		var inp = $('.frmQuickSearchRHColumn #searchToLet');
		var frm = $('.frmQuickSearchRHColumn');
		$(inp).val('l');
		frm.submit();	
	});
	$('ul.secondary').hide();
	$('ul.tertiary').hide();
	$('ul.primary').supersubs({
		minWidth:12,
		maxWidth:27,
		extraWidth:1	
	}).superfish().find('ul').bgIframe({opacity:false});
	
	/* Conveyancing Dynamic Validation */
	/* IE Fix Conveyancing Dynamic Validation */
	$('#quoteType').live('click',function(e) {
		var quoteType = $(this).val();
		switch(quoteType) {
			case "Sale":
				$('#lblSalePrice').html('Sale Price<span class="requiredField">*</span>');
				$('#lblSaleTenure').html('Sale Tenure<span class="requiredField">*</span>');
				$('#lblSalePostCode').html('Sale Postcode<span class="requiredField">*</span>');
				$('#lblPurchasePrice').html('Purchase Price');
				$('#lblPurchaseTenure').html('Purchase Tenure');
				$('#lblPurchasePostCode').html('Purchase Postcode');
				$('#salePrice').addClass('validate[required,custom[onlyNumber]]');
				$('#saleTenure').addClass('validate[required]');
				$('#salePostCode').addClass('validate[required,custom[ukPostCode]]');
				$('#purchasePrice').removeClass('validate[required,custom[onlyNumber]]');
				$('#purchaseTenure').removeClass('validate[required]');
				$('#purchasePostCode').removeClass('validate[required,custom[ukPostCode]]');
			break;
			case "Sale and Purchase":
				$('#lblSalePrice').html('Sale Price<span class="requiredField">*</span>');
				$('#lblSaleTenure').html('Sale Tenure<span class="requiredField">*</span>');
				$('#lblSalePostCode').html('Sale Postcode<span class="requiredField">*</span>');
				$('#lblPurchasePrice').html('Purchase Price<span class="requiredField">*</span>');
				$('#lblPurchaseTenure').html('Purchase Tenure<span class="requiredField">*</span>');
				$('#lblPurchasePostCode').html('Purchase Postcode<span class="requiredField">*</span>');
				$('#salePrice').addClass('validate[required,custom[onlyNumber]]');
				$('#saleTenure').addClass('validate[required]');
				$('#salePostCode').addClass('validate[required,custom[ukPostCode]]');
				$('#purchasePrice').addClass('validate[required,custom[onlyNumber]]');
				$('#purchaseTenure').addClass('validate[required]');
				$('#purchasePostCode').addClass('validate[required,custom[ukPostCode]]');				
			break;
			case "Purchase":
				$('#lblSalePrice').html('Sale Price');
				$('#lblSaleTenure').html('Sale Tenure');
				$('#lblSalePostCode').html('Sale Postcode');
				$('#lblPurchasePrice').html('Purchase Price<span class="requiredField">*</span>');
				$('#lblPurchaseTenure').html('Purchase Tenure<span class="requiredField">*</span>');
				$('#lblPurchasePostCode').html('Purchase Postcode<span class="requiredField">*</span>');
				$('#salePrice').removeClass('validate[required,custom[onlyNumber]]');
				$('#saleTenure').removeClass('validate[required]');
				$('#salePostCode').removeClass('validate[required,custom[ukPostCode]]');
				$('#purchasePrice').addClass('validate[required,custom[onlyNumber]]');
				$('#purchaseTenure').addClass('validate[required]');
				$('#purchasePostCode').addClass('validate[required,custom[ukPostCode]]');
			break;
		}
		/* Refire Validation */
		rebindValidation();
		$.validationEngine.closePrompt('.formError',true);

	});
	
	/* Collapsible Sections */
	$('#myAccountAddressDetails').hide();
	$('#myAccountCurrentSituation').hide();
	
	$('.expandCollapse').live('click',function(e) {
		e.preventDefault();
		var button = $(this);
		var theLink = $(this).attr('href');
		var el = $(theLink);
		$(el).slideToggle('fast',function() {
			if($(button).hasClass('sectionExpanded')) {
				$(button).removeClass('sectionExpanded');
				$(button).addClass('sectionClosed');
			} else {
				$(button).removeClass('sectionClosed');
				$(button).addClass('sectionExpanded');		
			}
		});
	});
	
	$('.modal-dialog').live('click',function(e) {
		e.preventDefault();
		var dataURL = $(this).attr('href') + '&ajax=1';
		if ($('#simplemodal-data').html()!=null) {
			$.ajax({
				type: 'GET',
				url: dataURL,
				maxWidth: 500,
				success: function(data, textStatus, XMLHttpRequest) {
					$('#simplemodal-data').html(data);
					$('#simplemodal-container').css('height', 'auto');
					//$('#simplemodal-container').css('width', 'auto');
					$('#simplemodal-container').css('position', 'absolute');
					rebindValidation();
					rebindAjax();
				},
				dataType: 'html',
				cache:false
			});
		}
		else {
			$.ajax({
				type: 'GET',
				url: dataURL,
				success: function(data, textStatus, XMLHttpRequest){
					$.modal(data, {
						position: ["30%", "30%"],
						escClose: true,
						autoResize: true,
						maxHeight: 500,
						maxWidth: 500,
						onOpen: function(dialog){
							dialog.overlay.fadeIn('slow', function(){
								dialog.container.slideDown('slow', function(){
									$('#simplemodal-container').css('height', 'auto');
									//$('#simplemodal-container').css('width', 'auto');
									$('#simplemodal-container').css('position', 'absolute');
									dialog.data.fadeIn('slow');
									rebindValidation();
									rebindAjax();
								});
							});
						},
						onClose: function(dialog){
							$.validationEngine.closePrompt('.frm', true);
							$.validationEngine.closePrompt('.formError', true);
							dialog.data.fadeOut('slow', function(){
								dialog.container.slideUp('slow', function(){
									dialog.overlay.fadeOut('slow', function(){
										$.validationEngine.closePrompt('.frm',true);
										$.validationEngine.closePrompt('.formError',true);
										$.modal.close();
									});
								});
							});
						}
					});
				},
				dataType: 'html',
				cache:false
			});
		}
	});
	rebindValidation();
	rebindAjax();
});
changeAccountLinks = function() {
	location.reload(true);
}

// Property of the week. R.Bowen 
function property_of_the_week() {
	$(function(){ 
		$('img#thumb').click(function() {
			var thumbsrc = $(this).attr('src');						   
			$('#main_image').attr('src', thumbsrc);	
			return false
		});
		$('#comparablePropertySlider').cycle({speed:2000,fx:'fade',timeout: 12000});
	});
}

/* optimised valuation forms
   R.Bowen 
*/
function optimisedforms(){ 
	$(function() {
		$('#optimised_valuation_form').hide();
		$('#optimised_form_header').toggle(function() { 
			$('#optimised_valuation_form').slideDown(800);
		}, function() {
			$('#optimised_valuation_form').slideUp(800);
		});
	});
}
function optimisedformsError() { 
	$(function() { 
		$('#optimised_valuation_form').show();
		$("html, body").animate({ scrollTop: 300 }, 1200);
	});	
}
function optimisedformsThanks() { 
	$(function() { 
		$('#optimised_valuation_form').slideDown(800);
		$('#thankyouMessage').animate({
			'margin-left': '0px'
		} , 2500, 'swing')
		$("html, body").animate({ scrollTop: 300 }, 1200);
		$('#optimised_valuation_form').delay(4500).slideUp(800)
	});
}
// New myaccount form function. R.Bowen
function nice_myaccount() { 
	$(function() { 
		$('#tidy_forms').hide();
		$('#submit_trigger').click(function() { 
			$('#CapthchaBtnT2').hide();
			$('#submit_trigger').hide();
			$('#tidy_forms').delay(100).slideDown(1000);
			$('#CapthchaBtnT2').delay(1200).fadeIn(800);
		});
	});
	$(function () {  
		$(window).load(function () {  
			$('input#forename, input#friendsName, input#salePrice').focus(); 
		});  
	}); 
}
function myaccount_hotline() { 
	$(function() { 
		$('p#hotline_list').hide();
		$('#selectOfficeid').change(function() { 
			$('p#hotline').css({color : 'red'});
			var e = $(this).val();
			if ($('p').hasClass(e)){ 
				var new_num = $('p.' + e).html();
				$('#hotline').html(new_num);
			}
			$('p#hotline').delay(800).animate({
				color: '#000000'
			}, 1000);
		});	
	});
}

function gallery_controls() { 
	$(function() { 
		$('div.nav-controls a.prev').delay(6000).animate({
			'margin-left': '-200px'
		},1000).hide(100);
		$('div.nav-controls a.next').delay(6000).animate({
			'margin-right': '-200px'
		},1000).hide(100);
		$('.slideshow-container').hover(
			function(){
				$('div.nav-controls a.prev').show().animate({
					'margin-left': '0px'
				},800);
				$('div.nav-controls a.next').show().animate({
					'margin-right': '0px'
				},800);
			},
			function(){
				$('div.nav-controls a.prev').delay(800).animate({
					'margin-left': '-200px'
				},800).hide(100);
				$('div.nav-controls a.next').delay(800).animate({
					'margin-right': '-200px'
				},800).hide(100);
			}
		);
	});
}


/* nice fade in
   R.Bowen 
*/
(function($) {
	$.fn.myfadeIn = function() { 
		this.animate({	
			'opacity': 1
		  }, 2000, "linear") 
		  
		  return this
	} 
})(jQuery);

/* meet the team animation function - 
   note: works for up to 8 members 
   insert a clear div after member 4 for IE fix
   R.Bowen   
*/ 
function meettheteam() { 
	$(function() {
		$('.member_1').animate({
			'margin-left': '2px'
		} , 1000, 'swing', function() { 
			$('.member_2').animate ({
			'margin-left': '55px'
		} , 1000, 'swing', function() {
			$('.member_3').animate ({
			'margin-left': '55px'
		} , 1000, 'swing', function() {
			$('.member_4').animate ({
			'margin-left': '55px'
		} , 1000, 'swing', function() {
			$('.member_5').animate ({
			'margin-left': '2px'
		} , 1000, 'swing', function() { 
			$('.member_6').animate ({
			'margin-left': '55px'
		} , 1000, 'swing', function() { 
			$('.member_7').animate ({
			'margin-left': '55px'
		} , 1000, 'swing', function() { 
			$('.member_8').animate ({
			'margin-left': '55px'
		} , 1000, 'swing');
		});});});});});});});
	});
}

/* Validation */
rebindValidation = function() {
	$('.validate-frm').validationEngine({
		validationEventTriggers: "blur focus",
		success: false,
		scroll:false
	});
}
/* Rebind Ajax forms */
rebindAjax = function() {
	$('.ajax-frm').submit(function(e){
		e.preventDefault();
		var frm = $(this);
		var frmReady = $(this).validationEngine({
			returnIsValid: true
		});
		// If form is ready then post the data
		if (frmReady) {
			var data = frm.serialize();
			var url = frm.attr('action');
			var dialog = $('#simplemodal-data');
			$.ajax({
				url: url,
				type: 'POST',
				data: data,
				success: function(data, textStatus, XMLHttpRequest){
					dialog.html(data);
					$('#simplemodal-container').css('height', 'auto');
					$('#simplemodal-container').css('width', 'auto');
					rebindValidation();
					rebindAjax();
				},
				dataType: 'html'
			});
		}
	});
}

