$(document).ready( function() {
	$(".adjustHeight").equalHeights();
	
	var comparablePropertyWidth = $('.comparableProperty').width() * 3;
	var thumbnailsWidth = $('.property-thumbnail').width() * 2;
	//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 <= 203) {
			$('.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 > 1) {
			$('.property-thumbnails-wrapper').animate({
				top: '0px',
				left: newpos + 'px'
			}, '500');
		};
	});
	$('#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'; 
	    }
	});
	$('#gallery').cycle({
		fx:'shuffle',
		timeout:5000,
		delay:-1500
	});
	$('#excerpt').cycle({
		fx:'fade',
		timeout:5000,
		delay:-1500
	});
	
	rebindValidation();
	$.validationEngine.closePrompt('.formError',true);
});

/* Validation */
rebindValidation = function() {
	$('.validate-frm').validationEngine({
		validationEventTriggers: "blur focus",
		success: false,
		scroll:false
	});
}

