 /*
  * All JQuery functions in here
  */
$(document).ready(function() {
	var comparablePropertyWidth = $('.comparableProperty').width() * 3;
	var thumbnailsWidth = $('.property-thumbnail').width() * 3;
	// PNG Fix
	$('body').pngFix();
	//You have to specify width and height in #slider CSS properties  
	//After that, the following script will set the width and height accordingly  
	$('#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()); 
	
	$('.comparable-property-wrapper').width(
		$('.comparableProperty').width() * $('.comparableProperty').length
	);
	$('.property-thumbnails-wrapper').width(
		$('.property-thumbnail').width() * $('.property-thumbnail').length
	);
	$('#comparable-property-previous a').live('click',function(e) {
		e.preventDefault();
		var p = $('.comparable-property-wrapper').position();
		var newpos = parseInt(p.left + comparablePropertyWidth);
		if (newpos <= 0) {
			$('.comparable-property-wrapper').animate({
				top: '0px',
				left: newpos + 'px'
			}, '500');
		}
	});
	$('#property-thumbnails-previous a').live('click',function(e) {
		e.preventDefault();
		var p = $('.property-thumbnails-wrapper').position();
		var newpos = parseInt(p.left + thumbnailsWidth);
		if (newpos <= 0) {
			$('.property-thumbnails-wrapper').animate({
				top: '0px',
				left: newpos + 'px'
			}, '500');
		}
	});
	$('#comparable-property-next a').live('click',function(e) {
		e.preventDefault();
		var p = $('.comparable-property-wrapper').position();
		var newpos = parseInt(p.left - comparablePropertyWidth);
		var totalRemaining = $('.comparable-property-wrapper').width() + newpos;
		if (newpos <= 0 && totalRemaining >= 0) {
			$('.comparable-property-wrapper').animate({
				top: '0px',
				left: newpos + 'px'
			}, '500');
		};
	});
	$('#property-thumbnails-next a').live('click',function(e) {
		e.preventDefault();
		var p = $('.property-thumbnails-wrapper').position();
		var newpos = parseInt(p.left - thumbnailsWidth);
		var totalRemaining = $('.property-thumbnails-wrapper').width() + newpos;
		if (newpos <= 0 && totalRemaining >= 0) {
			$('.property-thumbnails-wrapper').animate({
				top: '0px',
				left: newpos + 'px'
			}, '500');
		};
	});
	$('#gallery').cycle({
		fx:'shuffle',
		timeout:5000,
		delay:-1500
	});
	$('#excerpt').cycle({
		fx:'fade',
		timeout:5000,
		delay:-1500
	});
	$('#excerpt_youtube').cycle({
		fx:'fade',
		timeout:5000
	});
	$('#gallery_youtube').cycle({
		fx:'fade',
		timeout:5000
	});
	$('#excerpt_probate').cycle({
		fx:'fade',
		timeout:8000
	});
	$('#gallery_probate').cycle({
		fx:'fade',
		timeout:8000,
		pager: '#pager'
	});
	
	$('#property-main-images ul').cycle({
		fx:'fade',
		timeout:0,
		pager: '#property-thumbs-list',
	    pagerAnchorBuilder: function(idx, slide) { 

// return selector string for existing anchor 
	        return '#property-thumbs-list li:eq(' + idx + ') a'; 
	    }
	});
	// 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});
	
	$('#drpHouseStatus').live('change',function(e) {
		var vow = $(this).val();
		if (vow == 'L')
		{
			var href = $('#lettingSearchListViewURL').val();
		}
		else 
		{
			var href = $('#buyingSearchListViewURL').val();
		}
		$('#mapViewImageLink').attr('href',href);
	});

	/* Conveyancing Dynamic Validation */
	/* IE Fix Conveyancing Dynamic Validation */
	
	/*$('#quoteType radio[name="quoteType"]').live('change', function(e) {
        alert('hello');
      });*/

	$('#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('required');
				$('#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);
}

/* 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'
			});
		}
	});
}

// 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);
		});	
	});
}

// Blog js. R.Bowen 
function ryans_blog() {	 
	$(function() {
		$('.blog_link').click(function() {
			new_front_blog =  $(this).find('iframe.hidden').attr('src');
			new_back_blog = $('#top_frame').attr('src');
			new_title =  $(this).find('h3').html();
			new_blog_content = $(this).find('.content_right').html();
			
			$("html, body").animate({ scrollTop: 0 }, "slow"); 
			$('#bottom_frame').attr('src', new_back_blog).delay(1000).slideUp(1000);
			$('#top_frame').delay(1000).slideUp(1000).attr('src', new_front_blog).slideDown(1000);
			$('.main_blog_title').slideUp(500).html(new_title).delay(2000).slideDown(500);
			$('.main_blog_text').slideUp(500).html(new_blog_content).find('p.hidden').removeClass('hidden');
			$('.main_blog_text').delay(2000).fadeIn(2500);
	
			return false
		});
	});
}

// Probate functions. R.Bowen 
(function($) {
	$.fn.customFadeIn = function(speed, callback) {
		$(this).fadeIn(speed, function() {
			if(jQuery.browser.msie)
				$(this).get(0).style.removeAttribute('filter');
			if(callback != undefined)
				callback();
		});
	};
	$.fn.customFadeOut = function(speed, callback) {
		$(this).fadeOut(speed, function() {
			if(jQuery.browser.msie)
				$(this).get(0).style.removeAttribute('filter');
			if(callback != undefined)
				callback();
		});
	};
})(jQuery);

function probate_thanks() { 
	$(function() {
		$("html, body").delay(1000).animate({ scrollTop: $('#thanks_target').offset().top }, 1000 ,'easeInSine');
		$('.home_thanks_header').hide().delay(1500).customFadeIn(1500, function(){     
		});
		$('.home_thanks_text_outer').delay(3000).animate({
			'width': '400px'
		}, 4000, 'swing' )
	});
}
function full_form_probate_thanks() { 
	$(function() {
		$('#success_options_outer').hide();
		$('.thanks_header').hide().delay(100).customFadeIn(1500, function(){     
		});
		$('.thanks_text_outer').delay(1500).animate({
			'width': '600px'
		}, 3000, 'swing' )
		$('#success_options_outer').delay(3500).slideDown(1000);
	});
}

// Legally Ready functions. R.Bowen 

function legallyReadyThanks() {	 
	$(function() { 
		$('#thanks_message').animate({
			'margin-left': '0px'
		} ,2500, 'swing');
		$("html, body").animate({ scrollTop: 300 }, 1200);
		$('#thanks_wrapper').delay(2100).animate({ 
			width:'370px'
		} ,1500, 'swing');
	});
}
function legallyReadyError() { 
	$(function() { 
	$("html, body").animate({ scrollTop: 300 }, 1200);
	});
}	 
