// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults


$(document).ready(function() {
  //form validation
  $("#enquiryform").validate();
  $("#loginform").validate();
  $("#registerform").validate();
  
    $("#orderform").submit(function() {
      if( $("#acceptTerms").attr("checked") == false ) {
        alert("You must access the terms and conditions and privacy policy to proceed.");
        return false;
      } else {
         //popup_creditcard();
         return true;
      }
    });

    // Updates the shipping costs for the checkout process.

    $('#wf_shipping').change(function(){
      $.ajax({
        type : 'get',
        dataType : 'html',
        url : '/orders/update_shipping',
		data : { shipping : $(this).find('option:selected').val(),comments : $('#comments').val()},	
        success : function(response){
          $('#wf_message').attr('value', response);
        }
      });
    });

    //rotating images
    $('#slideshow').cycle({fx: 'fade', timeout: 5000, speed: 2000});


    // open external link in new tab/window
    // use rel="external" instead of target="_blank"
    $('a[rel="external"]').click( function() {
        window.open( $(this).attr('href') );
        return false;
    });


   //IE6 Hack for buttons hover function in form
   $('.buttons button').hover(
     function() {
       $(this).css("color", "#5d19a4");
     },
     function() {
       $(this).css("color", "#FFFFFF");
     }
   );

   $('table#cart-table .buttons button').hover(
     function() {
       $(this).css("background-color", "#F0F6F8" );
     },
     function() {
       $(this).css( {"color": "#565656", "background-color" : "#FFFFFF"} );
     }
   );

   $('#go').hover(
     function() {
       $(this).css("color", "#5d19a4" );
     },
     function() {
       $(this).css( "color", "#fff" );
     }
   );
   
   $("span.mailto").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();
 	 });


});

