$(document).ready(function() {                
	
	// match active page with a nav link
  pattern = /^p-(.*)$/; // anything in a string after 'p-'
	activePage = $('body').attr('id').match(pattern)[1]; // grab the body's id attribute, strip 'p-' located at beginning of line (e.g p-products becomes products)
  activeLink = $('ul#nav').find('a#nav-' + activePage); // find a matching link item (p-products, find nav-products)
  $(activeLink).addClass('active'); // add '.active' class to nav link
	
	/*email protect*/
	$("span.safemail").each(function(){
		exp = $(this).text().search(/\((.*?)\)/) != -1 ? new RegExp(/(.*?) \((.*?)\)/) : new RegExp(/.*/);
		match = exp.exec($(this).text());
		addr = match[1] ? match[1].replace(/ at /,"@").replace(/ dot /g,".") : match[0].replace(/ at /,"@").replace(/ dot /g,".");
		link = match[2] ? match[2] : addr;
		subject = $(this).attr('title') ? "?subject="+$(this).attr('title').replace(/ /g,"%20") : "";
		$(this).after('<a href="mailto:'+addr+subject+'">'+ link + '</a>');
		$(this).remove();
	});    

  // on focus for input boxes*/
    $.fn.resetValue = function() {
     return this.focus(function() {if( this.value == this.defaultValue ) {this.value = "";}})
     .blur(function() {if( !this.value.length ) {this.value = this.defaultValue;}});
    };
  // $('.search-form input').resetValue();


	// Open links in new window when rel="external" is applyed to a tag
	$('a[rel="external"]').click(function(){
		this.target = "_blank";
	});
	
	
  //hide email and add the hidden email_To on submit
  $("#contact-form").submit(function() {
    var s1 = 'info';
    var s2 = '@';
    var s3 = 'crawfordrealty.com.au';
    $(this).append('<input type="hidden" name="Email_To" value="' + s1 + s2 + s3 + '" />');
  });

	$('.baseform').validate();
	
	
	// tabs
	$('#tabs').tabs();

	

	//cufon
	Cufon.replace('#p-home h1');
	Cufon.replace('p.highlight');
	Cufon.replace('#featured h2');
	


	// Gallery on property detail page
	$("ul#nav").superfish(); 
	
	
	$('#gallery-large').cycle({
			fx:     'scrollLeft',
			speed:  'fast',
			timeout: 6000
	});
	
	$("#gallery-thumbs a").click(function(){
		$('#gallery-large').cycle(parseInt($(this).attr('rel')));
		return false;
	});
	
	/*
	$("#gallery-thumbs .one").click(function(){
		$('#gallery-large').cycle(0);
	});
	
	$("#gallery-thumbs .two").click(function(){
		$('#gallery-large').cycle(1);
	});
	
	$("#gallery-thumbs .three").click(function(){
		$('#gallery-large').cycle(2);
	});
	
	$("#gallery-thumbs .four").click(function(){
		$('#gallery-large').cycle(3);
	});
	
	$("#gallery-thumbs .five").click(function(){
		$('#gallery-large').cycle(4);
	});
	
	$("#gallery-thumbs .six").click(function(){
		$('#gallery-large').cycle(5);
	});
	*/ 
	 
	 
	 //colorbox
	$("a[rel='enlarge']").colorbox();
	$(".colorbox_image").colorbox();
	 
	 //banner cycle
	 $('#slideshow').cycle();
	 

	$('input[value=Calculate]').click(function(){
		var price = parseFloat($('input[name=Price]').val());
		var rent = parseFloat($('input[name=Rent]').val());
		var interest = parseFloat($('input[name=InterestRate]').val()) / 100;
		
		var total = (rent * 52) - (price * interest);
		
		if(isNaN(total)){
			$('input[name=Total]').val('');	
		}
		else{
			$('input[name=Total]').val('$' + Math.round(total).toString());	
		}
		
		return false;
	});
	
  if($.browser.msie){
    $('input:radio').click(function(){
      this.blur();
      this.focus();
    });
  }
	
	$('input[value=ressale]').click(function(){
		$('select[name=res_property_type]').show();
		$('select[name=comm_property_type]').hide();
		$('select[name=land_property_type]').hide();
	});

	$('input[value=commsale]').click(function(){
		$('select[name=res_property_type]').hide();
		$('select[name=comm_property_type]').show();
		$('select[name=land_property_type]').hide();
	});

	$('input[value=land]').click(function(){
		$('select[name=res_property_type]').hide();
		$('select[name=comm_property_type]').hide();
		$('select[name=land_property_type]').show();
	});

});