 /*
  * 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');
		}
	});

	$('div.site-intro p').expander();

	$('#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:'fade',
		timeout:5000,
		delay:-1500
	});
	$('#excerpt').cycle({
		fx:'fade',
		timeout:5000,
		delay:-1500
	});
	$('#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 */
	$('#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,
				success: function(data, textStatus, XMLHttpRequest) {
					$('#simplemodal-data').html(data);
					$('#simplemodal-container').css('height', 'auto');
					$('#simplemodal-container').css('width', '500px');
					$('#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: ["75px", "30%"],
						escClose: true,
						autoResize: true,
						maxHeight: 500,
						onOpen: function(dialog){
							dialog.overlay.fadeIn('slow', function(){
								dialog.container.slideDown('slow', function(){
									$('#simplemodal-container').css('height', 'auto');
									$('#simplemodal-container').css('width', '500px');
									$('#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'
			});
		}
	});
}

// Ryans Student Contact Form Animation 
function student_contact_form() { 
	$(function() { 
		if ($("input#email").val() != "") { 
			if ($.browser.msie  && parseInt($.browser.version) == 7) { 
			}else {$('#student_contact_form').css('margin-left', '680px'); }
		}
		$('#open_me').toggle(function() {
		$('#student_contact_form').animate({
			'margin-left': '680px'
		} , 1200, 'easeOutBounce')
		}, function() { 
			$('#student_contact_form').animate({
			'margin-left': '0px'
		} , 1000, 'easeInOutElastic')
		});
		if (!$.browser.msie) { 
		$('#student_contact_form').submit(function() { 
			$('#student_contact_form_wrapper').animate({
			'margin-left': '1000px'
		} , 2500, 'easeOutBounce')
		});
		}
	}); 
}

// Ryans Student Contact Submit Series 
function student_contact_form_submit() {
	$(function() {
		$('#student_contact_form').css('margin-left', '1320px' );
		$('#thankyouMessage').delay(1000).animate({
			'margin-left': '0px'
		} , 2500, 'swing')
		$("html, body").animate({ scrollTop: 300 }, 1200);
		$('.thanks').delay(10000).hide('puff', 2000);
	});
}

