function addCustomProduct(uniqueId) {
	$.ajax({
		url: base_url + 'ajax/add_to_cart/',
		type: 'post',
		async: true,
		data: 'type=custom&unique_id=' + uniqueId,
 		dataType: 'json',
		success: function(data) {
			$.colorbox({
				href: base_url + 'ui/custom_product_add',
				title: '"' + data.result.item.name + '" Added to Cart',
				initialWidth: 350,
				speed: 10,
				initialHeight: 0,
				width: '350px'
			});

			if (data.result.totals !== undefined)	{
				updateHeaderCart(data.result.totals.cartCount);
			}
		},
		error: function() {return false}
	});
}

function updateHeaderCart(total) {
	if(total > 0) {
		if($('#account-links a span').hasClass('red')) {
			$('#account-links a span').removeClass('red');
			$('#account-links a span').addClass('green');
		}
	} else {
		if($('#account-links a span').hasClass('green')) {
			$('#account-links a span').removeClass('green');
			$('#account-links a span').addClass('red');
			$('#empty-cart').show();
		}
	}
	$('#account-links a span').html(total);

}

function getCustomProduct() {
	var idArray, products = Array();
	$('.custom-product').each(function() {
		idArray = $(this).attr('id').split('-');
		$(this).addClass('product-waiting');

		if($(this).hasClass('sample-product')) {
			idArray[2] = idArray[2] + '-sample';
		}

		products.push(idArray[2]);
	});

	$.ajax({
		url: base_url + 'ajax/get_custom_products/',
		type: 'post',
		async: false,
		data: 'products=' + products.join('|'),
 		dataType: 'json',
		success: function(data) {
			if (data.status == 'OK') {
				$(data.result).each(function(row) {
					$('#custom-product-' + data.result[row].id).html(data.result[row].view);
					$('#custom-product-' + data.result[row].id + '-replace').unwrap();
				});
			}
		}
	})
}

function showConfirmColorbox(message) {
	$.colorbox({
		href: '/ajax/confirm/view/confirmation_view/message/' + encodeURI(message),
		initialWidth: 0,
		speed: 10,
		initialHeight: 0
	});
}

$(document).ready(function() {

  $('#create-order-form').submit(function()	{
  	return true;
  });
  
	if($('.custom-product').length) {
	  getCustomProduct();
	}  

	$('.colorbox').live('click', function() {
		var id = $(this).attr('id');
		var position = $(this).position();

		$.colorbox({
			href: base_url + 'ajax/tooltip/' + id,
			initialWidth: 0,
			speed: 10,
			initialHeight: 0,
			maxWidth: '700px'
    });
    
    return false;
  });
});

