$(document).ready(function() {                
	
	// match active page with a nav link
  pattern = /^page-(.*)$/; // 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 = $('#nav ul').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();
	});    

	// 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
  $("#enquiryform").submit(function() {
    var s1 = 'info';
    var s2 = '@';
    var s3 = 'ucsolutions.com.au';
    
    $(this).append('<input type="hidden" name="recipient" value="' + s1 + s2 + s3 + '" />');
  });
  
  $('#Same').click(function(){
    if( $('#Same').is(':checked') ) {
      disableRequired('#Address1_Delivery');
      disableRequired('#Address2_Delivery');      
      disableRequired('#Suburb_Delivery');
      disableRequired('#State_Delivery');
      disableRequired('#Postcode_Delivery');
      disableRequired('#Country_Delivery');
    } else {
      enableRequired('#Address1_Delivery');
      $('#Address2_Delivery').removeAttr('disabled');    
      enableRequired('#Suburb_Delivery');
      enableRequired('#State_Delivery');
      enableRequired('#Postcode_Delivery');
      enableRequired('#Country_Delivery');
    }
	});

	$('.baseform').validate();
	
	// banner cycle
	$('#slideshow').cycle();
	
	// products
	$('#secondary.featured-products #slideshow').cycle({ 
		fx:		'scrollLeft', 
		timeout:		5000, 
		speedOut:	200, 
		speedIn:	200
	});
	
	
	$('#logos').cycle({ 
		fx:    'fade', 
		speed:  1500,
		pause: 4
	});
	
	
	// random banner image (inner pages)
	bgImageTotal=4;
	randomNumber = Math.round(Math.random()*(bgImageTotal-1))+1;
	imgPath=('images/banner/innerbanner0'+randomNumber+'.jpg');
	$('#banner.inner').css('background-image', ('url("'+imgPath+'")'));
	
	
	// forms, hover submit buttons
	$('.buttons input').hover(function(){
		$(this).addClass('hover');																	 
	},function(){
		$(this).removeClass('hover');
	});
	

	// listing page
	$("#listing li:first").css('border-top', 'none');


	//colorbox
	$("a[rel='enlarge']").colorbox();

});

function disableRequired(id) {
  $(id).val('');
  $(id).attr('disabled','disabled');
  $(id).removeAttr('class');
}

function enableRequired(id) {
  $(id).removeAttr('disabled');
  $(id).attr('class','required');
}

function addToCart(qty, price, id) {
  qty = parseInt(qty);
  price = parseFloat(price);

  items += qty;
  cost += price * qty;
  
  cost = Math.round(cost * 100) / 100;
  
  text = ' items ';
  if (items == 1)
    text = ' item ';

  $('#cart-panel').html(items + text + '<strong>$' + cost + '</strong> <a href="http://www.ucsolutions.com.au/cart.php">View your cart</a>');


  $.ajax({
      type: "GET",
      url: "http://www.ucsolutions.com.au/update-cart.php?id=" + id + "&price=" + price + "&qty=" + qty + "&action=add"});

  jAlert('<strong>This item has been added to the cart.</strong> <a href="http://www.ucsolutions.com.au/cart.php">View your cart now.</a>', 'Item added successfully');
}

function updateCart(qty, price, id) {
  $.ajax({
      type: "GET",
      url: "http://www.ucsolutions.com.au/update-cart.php?id=" + id + "&price=" + price + "&qty=" + qty + "&action=update"});
 
  location.reload(true); 
}

function deleteFromCart(price, id) {
  if (confirm('Are you sure you want to delete this item from your cart?')) {
    $.ajax({
        type: "GET",
        url: "http://www.ucsolutions.com.au/update-cart.php?id=" + id + "&price=" + price + "&action=delete"});

    location.reload(true);
  }
}

function wipeCart() {
  if (confirm('Are you sure you want to delete your cart?')) {
    $.ajax({
        type: "GET",
        url: "http://www.ucsolutions.com.au/update-cart.php?action=wipe"});

    location.reload(true);
  }
}
