$(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
  if ( activePage == "cart" || activePage == "checkout" || activePage == "checkout_confirmation" ) { $("#nav-store").addClass('active') }
  
	/*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 = 'protwin.com.au';
    $(this).append('<input type="hidden" name="Email_To" value="' + s1 + s2 + s3 + '" />');
  });

	$('.baseform').validate();
  $('.cart_add_form').validate();

	// Banner
	$('#banner-feature').cycle({
    fx:     'scrollLeft',
    speed:  'fast',
    timeout: 8000,
    next:   '#next', 
    prev:   '#prev',
    pager:	'#banner-nav'
  });

  $("#banner-nav-aboutus").click(function(){
    $('#banner-feature').cycle(0);
  });
  
  $("#banner-nav-services").click(function(){
    $('#banner-feature').cycle(1);
  });
  
  $("#banner-nav-racing").click(function(){
    $('#banner-feature').cycle(2);
  });
  
  $("#banner-nav-store").click(function(){
    $('#banner-feature').cycle(3);
  });

	// Showroom
	$('#gallery').cycle({
      fx:     'scrollLeft',
      speed:  'fast',
      timeout: 4000,
      pager:	'#gallery-nav'
  });

  $("#gallery-nav a").hover(function(){
    var index_to_select = $("#gallery-nav a").index($(this))

    $('#gallery').cycle(index_to_select);
  });

	//cufon
	Cufon.replace('h1');
	Cufon.replace('#banner h2');
	Cufon.replace('#secondary h2');
	Cufon.replace('#content-dynamic h2');
  Cufon.replace('#content h2');
  Cufon.replace('div.calendar div.month_title_buttons div.current_date');
  Cufon.replace('body#p-events div.calendar ul li.has_content div.more-info a');
  Cufon.replace('div.calendar ul li span.day_number');

	//colorbox
  $(".colorbox_image").each(function() {
    $('.colorbox_image').colorbox({ maxWidth:"80%", maxHeight:"80%", transition: "elastic", scalePhotos: true });
  });
  $(".colorbox_page").each(function() {
    var current = ""
    var rel     = $(this).attr("rel")

    if ( !current.length ) {
      current = '{current} of {total} ' + rel
    }

    $('.colorbox_page').colorbox({ width:"80%", height:"80%", iframe:true, current: current });
  });
  $("body#p-events div.calendar ul li.has_content a").colorbox({ width:"760", height:"80%", iframe:true });

	// cat listing hovers & grab href and apply to li
	$("#categories li").click(function() {
		window.location = $(this).find("a:first").attr("href");
	});

	$("#categories li").hover(function(){
		$(this).addClass('active');																	 
	},function(){
		$(this).removeClass('active');	
	});

  //copy over delivery
  $("li.copy_over a").click(function() {
    var fieldset_selector = "fieldset." +$(this).attr("rel")

    $(fieldset_selector).find("input[type=text]").each(function() {
      var input_val     = $(this).val()
      var copy_to_name  = "Delivery" +$(this).attr("name")

      $("input[name=" + copy_to_name + "]").val(input_val)
    });

    $(fieldset_selector).find("select").each(function() {
      var input_val     = $(this).val()
      var copy_to_name  = "Delivery" +$(this).attr("name")

      $("select[name=" + copy_to_name + "]").val(input_val)
    });

    return false;
  });

  //back button
  $("a.back_button").click(function() {
    history.back()

    return false;
  });
});
