function imageSwap(image) {
	this.src = image;
}

function openLightbox ( title, message ){
	
	var message = '<h2>' + title + '</h2><div class="product-descrip">' + message + '</div>';
		
	jQuery.fancybox(
		message,
		{
			'autoDimensions'	: false,
			'width'         	: 450,
			'height'        	: 'auto',
			'transitionIn'		: 'none',
			'transitionOut'		: 'none'
		}
	);

}

function ajaxAddToCart ( pId, cartUrl, pName ) {
	
	var product_id = pId;
	var qty = jQuery('#qty' + pId).val();
	
	var product_name = pName;
	var current_cart = Number(jQuery('#_summary .num').text());
	var new_cart = current_cart + Number(qty);
	
	var params = "&qty=" + qty;
	
	//get each option
	jQuery('#po-'+pId+' .options').each(function(){
		var el = jQuery(this);
		if ( el.val() != '' ) {
			params += "&" + el.attr('name') + "=" + el.val();
		}		
	});
	
	var message = "<h2>" + product_name + " Added</h2><p>You have successfully added " + qty + " item/s to your cart</p><button onclick='jQuery.fancybox.close();'>Close Window</button>";
	
	jQuery.fancybox.showActivity();
	jQuery('#loading' + product_id).fadeIn('fast');
	jQuery('.success').fadeOut('fast');
	jQuery('.button').attr('disabled', true);
	jQuery('.button').addClass('disabled'); //so we can style new buttons 
	jQuery('.msg').removeClass('msg-info').text('');
		
	var msg1 = ' Note we noticed that you had updated the quantity for ';
	var msg = '';
		
	jQuery('.check_item').each(function(index){
		if ( jQuery(this).val() != product_name ) {
			msg = 'Note we noticed you had updated the quantity for another product. Products need to be added to the cart individually so to add this other product please click the add to cart button next to the product.';
		} else {
			jQuery(this).remove(); //remove the check for the product added to the cart
		}
	});
	
	jQuery.ajax({
				type: "POST",
				url: cartUrl + params,
				success: function(){
					jQuery('#loading' + product_id).fadeOut('fast');
					jQuery('#success' + product_id).fadeIn('fast');
					jQuery('#qty' + product_id).val('1'); //reset the quantity box to one after successful addtocart
					jQuery('.options').val(''); //reset seleect boxes
					jQuery('.button').removeAttr('disabled');
					jQuery('.button').removeClass('disabled');
					jQuery('#_summary .num').text(new_cart);
					jQuery('#_summary').fadeIn('slow');
					jQuery('.block-cart .actions').fadeIn('slow');
					jQuery('.block-cart p.empty').fadeOut('slow');
					jQuery('#recent').load('../../scripts/recent.php');
					if ( msg != '' ) {
						jQuery('#msg' + product_id).addClass('msg-info');
						jQuery('#msg' + product_id).text(msg);
					}
				}
			});

}

$.noConflict();
jQuery(document).ready(function($){
		
	var cart_count = $('#_summary .num').text();
	
	if ( cart_count > 0 ) {
		$('#_summary').fadeIn('slow');
		jQuery('.block-cart .actions').fadeIn('slow');
	}
	
	$('.lightbox').fancybox();
	
	$('#wholesale').jPaginate()
	
	//$('#product-list').jPaginate();
	
	$('#featured').jcarousel({ wrap: 'circular' });
	
	$('.qty').focusin(function(){
		
		var current_item = $(this).parent().find('input[name="i"]').val();
		
		var el = $(this).parent().find('.check_item').length;
				
		var product_name = $('#n' + current_item).val();

		if (!el) {
			
			$(this).parent().prepend('<input type="hidden" name="checked'+current_item+'" class="check_item" value="'+product_name+'" />');	
			
		}
		
	});
	
	$('#RetailerID').focusin(function(){
		$(this).css('color', '#333');
	});
	
	$('#RetailerID').focusout(function(){
		$(this).css('color', '#666');
	});
	
	var $pc = $('#Postcode').val();
	$('#Postcode').focusin(function(){
		$(this).css('color', '#333');
		if ($(this).val() == $pc) { 
			$(this).val('');
		}
	});
	$('#Postcode').focusout(function(){
		$(this).css('color', '#666');
		if ($(this).val() == '') {
			$(this).val($pc);
		}
	});
	var $search = $('#search').val();
	$('#search').focusin(function(){
		if ($(this).val() == $search) {
			$(this).val('');
		}
	});
	$('#search').focusout(function(){
		if ($(this).val() == '') {
			$(this).val($search);
		}
	});							  
	
});
