(function($) {
$.fn.clearInputs = function() {
	return this.each(function() {		
		var default_value = $(this).val();
		
		$(this).focus(function() {			
			if ($(this).val() == default_value && $(this).attr('type') != 'submit') {
				$(this).val("");
			}
		});

		$(this).blur(function() {
			if ($(this).val() == "") {
				$(this).val(default_value);
			}
		});
	});
};
})(jQuery);

$(document).ready(function() {
//lets even out two main column heights
//computed once on page load
//may have to modify and recompute height
//in future when elements added/removed from the DOM dynamically

resizeColumns = function() {
	var cHeight = $('.maincontent').innerHeight();
	var lrHeight = $('.left_rail').innerHeight();

	//make left rail stretch to height of maincontent
	if (lrHeight < cHeight) {
		$('.left_rail').height(cHeight).css('padding-bottom', '0px');
	}
}

resizeColumns();

//cupcake hover main navigation
	$('.navigation ol li a').each(function() {
		
		$(this).hover(
			//add image to anchor, display / position
			function() {
				$(this).append($('.cuppington'));
				$('.cuppington').css('display', 'block');
				
				var leftOffset = $(this).position().left;
				var left = leftOffset + $(this).width() / 2;
				
				$('.cuppington').css('position', 'absolute');
				$('.cuppington').css('left', left - 25);
			},
			
			//hide
			function() {
				$('.cuppington').css('display', 'none');
			}
		);
	});
	
//cupcake hover 'resellers' box
	$('.shops li a').each(function() {
		
		$(this).hover(
			//add image to anchor, display / position
			function() {
				$(this).append($('.sm_cuppington'));
				$('.sm_cuppington').css('display', 'block');
				
				var top = $(this).position().top;
				
				$('.sm_cuppington').css('position', 'absolute');
				$('.sm_cuppington').css('top', top);	
			},
			
			//hide
			function() {
				$('.sm_cuppington').css('display', 'none');
			}
		);
	});

//'additional views' thumbnail viewer
	$('.list_add-views li a').hover(
		function() {
			var thumbhref = $(this).attr('href');
			$('.img_product img').attr('src', thumbhref);
		},
		
		function() {
			if ($(this).attr('class') != 'active') {
				var activeSrc = $('.list_add-views li a.active').attr('href');
				$('.img_product img').attr('src', activeSrc);
			}
		}
	);
	
	$('.list_add-views li a').click(function() {
		var thumbhref = $(this).attr('href');
		
		$('.img_product').attr('href', thumbhref);
		$('.icon_zoom').attr('href', thumbhref);
		
		$('.list_add-views li a').removeClass('active');
		$(this).attr('class', 'active');

		return false;
	});
	
//'chain/metal type' dropdown selectors
	$('#add-to-cart select').find(':first').attr('selected', 'selected');
	
	$('#add-to-cart select').change(function() {
		var ddId = $(this).attr('id');
		var ddText = $(this).find(':selected').text();

		$('.'+ddId).text(ddText);
		$(this).find(':first').attr('disabled', 'disabled');
		
		// clear validated msg.
		$(this).blur();
		
		//resize left/right column heights
		resizeColumns();
	});
	
	$('#material').change(function() {
		var materialIndex = $(this).attr("selectedIndex") - 1;
		var priceIndex = $('#prices span').get(materialIndex);
		var price = $(priceIndex).text();
		
		$('#price').html(price);
		
		// clear validated msg.
		$(this).blur();
		
		//resize left/right column heights
		resizeColumns();
	});

//update cart	
	$('.quantity').change(function() {
    var sel = $(this);
    var _item_id = sel.attr('id');
    var _quantity = sel.val();
    
    $.getJSON(
      '/cart/update',
      {
        item_id: _item_id,
        quantity: _quantity
      },
      function(data) {
        if (data.status == 'success') {
          $("#item_total_"+_item_id).fadeTo('fast', .5).html("$"+data.item_total).fadeTo('fast', 1);
          $("#cart_total, .quick-cart-subtotal").fadeTo('fast', .5).html("$"+data.cart_total).fadeTo('fast', 1);
					$(".items-in-cart").html(data.items_in_cart);
					$(".encrypted").val(data.encrypted_paypal_data);
        } else {
          $(sel).val(data.quantity); // revert to the original quantity
          alert("The quantity could not be updated");
        }
      }
    );
  });
});