
jQuery.fn.exists = function(){return jQuery(this).length>0;}

$(document).ready(function(){
	
	// Marquee functionality
	$('marquee').marquee('pointer').mouseover(function() {
		$(this).trigger('stop');
	}).mouseout(function() {
		$(this).trigger('start');
	});

	// If present, set up search box submit functionality
	if($('img#searchBoxSubmit').exists()) {
		$('img#searchBoxSubmit').css('cursor', 'pointer');
		$('img#searchBoxSubmit').click(function() {
			$('form#searchBoxForm').submit();
		});
		$('form#searchBoxForm').submit(function() {
			if($('select#sPriceMax').val() != '0') {
				if(parseInt($('select#sPriceMax').val(), 10) <= parseInt($('select#sPriceMin').val(), 10)) {
					alert('Error - Min price cannot be greater than (or equal to) max price!  Please correct and try again.');
					return false;
				}
			}
		});
	}
	
	// image popups
	$("a[rel=photo_group]").fancybox({
		'transitionIn'		: 'elastic',
		'transitionOut'		: 'elastic',
		'overlayColor'		: '#000',
		'overlayOpacity'	: '0.5',
		'titlePosition' 	: 'over',
		'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
			return '<span id="fancybox-title-over">Image ' + (currentIndex + 1) + ' / ' + currentArray.length + '</span>';
		}
	});
	
	// helpline form
	$('img#hfsend').css('cursor', 'pointer');
	$('img#hfsend').click(function() {
		$('form#helplineForm').submit();
	});
	
	// contact form
	$('form#enquiryForm input, form#enquiryForm textarea').focus(function() {
		$(this).parent().css('background-color', '#cbeaf6');
		$(this).siblings('span').html('');
	});
	$('input#efname').blur(function() {
		if($(this).val().length > 3) $(this).parent().css('background-color', '#ddd');
		else {
			$(this).parent().css('background-color', '#ecc');
			$(this).siblings('span').html('!');
		}
	});
	$('input#efemail').blur(function() {
		var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		if(reg.test($(this).val()) == true) $(this).parent().css('background-color', '#ddd');
		else {
			$(this).parent().css('background-color', '#ecc');
			$(this).siblings('span').html('!');
		}
	});
	$('input#efphone').blur(function() {
		var reg = /^([0-9 \-\.\+\(\))]{8,22})$/;
		if(reg.test($(this).val()) == true) $(this).parent().css('background-color', '#ddd');
		else {
			$(this).parent().css('background-color', '#ecc');
			$(this).siblings('span').html('!');
		}
	});
	$('textarea#efmessage').blur(function() {
		if($(this).val().length > 3) $(this).parent().css('background-color', '#ddd');
		else {
			$(this).parent().css('background-color', '#ecc');
		}
	});
	$('img#efsend').css('cursor', 'pointer');
	$('img#efsend').click(function() {
		$('form#enquiryForm').submit();
	});
	$('form#enquiryForm').submit(function() {
		var ok = 'yes';
		var emailreg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		var phonereg = /^([0-9 \-\.\+\(\))]{8,22})$/;
		if($('input#efname').val().length < 4) {
			$('input#efname').parent().css('background-color', '#ecc');
			$('input#efname').siblings('span').html('!');
			ok = 'no';
		}
		if(emailreg.test($('input#efemail').val()) != true) {
			$('input#efemail').parent().css('background-color', '#ecc');
			$('input#efemail').siblings('span').html('!');
			ok = 'no';
		}
		if(phonereg.test($('input#efphone').val()) != true) {
			$('input#efphone').parent().css('background-color', '#ecc');
			$('input#efphone').siblings('span').html('!');
			ok = 'no';
		}
		if($('textarea#efmessage').val().length < 4) {
			$('textarea#efmessage').parent().css('background-color', '#ecc');
			ok = 'no';
		}
		if(ok == 'no') {
			alert('Please ensure that you have entered valid data in all fields and click send again.');
			return false;
		}
		else return true;
	});

	// go to ID form
	$('img#gotoButton').css('cursor', 'pointer');
	$('input#gotoId').val('ref #');
	$('input#gotoId').focus(function() {
		if($('input#gotoId').val() == 'ref #') $('input#gotoId').val('');
	});
	$('input#gotoId').blur(function() {
		if($('input#gotoId').val() == '') $('input#gotoId').val('ref #');
	});
	$('img#gotoButton').click(function() {
		if(($('input#gotoId').val() != '') && ($('input#gotoId').val() != 'ref #'))
			$('form#gotoIdForm').submit();
	});

});
