this.imagePreview = function(){	
		
	var xCord;
	var yCord;
	
	var loading = false;
	
	$("a.preview").hover(function(e){

		this.t = this.title;
		this.title = "";	
		
		var c = (this.t != "") ? "<br/>" + this.t : "";
		$("body").append("<div id='loader'></div><div id='preview'><img id='large' alt='Image preview' width='300' /></div>");
		
		$("#loader")
				.css("top",(yCord - 20) + "px")
				.css("left",(xCord + 20) + "px");
		
		xCord = e.pageX;
		yCord = e.pageY;
		preview_title = $(this).attr('preview_title');
		loading = true;
				
		$("img#large").load (function () {
			
			var poptop = $("#preview").offset().top;
			var popheight = $("#preview").height();
			
			var popleft = $("#preview").offset().left;
			var popwidth = $("#preview").width();
			
			var wintop = $(window).scrollTop();
			var winheight =  $(window).height();
			
			var winleft = $(window).scrollLeft();
			var winwidth =  $(window).width();
	
			var left = Math.ceil(xCord + 25);
			var top = Math.ceil(yCord + 25);
			
			// ----------------- Manage top ------------------------- //
			if ((top + popheight) >= (winheight + wintop)) {
				top = yCord - popheight - 25;
			}
			if (top <= wintop){
				top = wintop + 5;
			}
			if (isNaN(top)) {
				top = 0;
			}
			
			// ----------------- Manage left ------------------------- //
			if ((left + popwidth) >= (winwidth + winleft)) {
				left = xCord - popwidth - 25;
			}
			if (left <= winleft) {
				left = winleft;
			}
			if (isNaN(left)) {
				left = 0;
			}
		
			$("#preview")
				.css("top",(top) + "px")
				.css("left",(left) + "px")
				.css("display", "block");
				
			loading = false;
			$(this).width(300);
			$("#preview").append('<span class="preview_title">' + preview_title + '</span>');
			$("#loader").remove();
			
		}).attr('src',$(this).attr ('rel'));
	},
	function(){
		this.title = this.t;	
		$("#loader").remove();
		$("#preview").remove();
	});

	$("a.preview").mousemove(function(e){


		xCord = e.pageX;
		yCord = e.pageY;
		
		if (loading == true) {
			$("#loader")
				.css("top",(yCord - 20) + "px")
				.css("left",(xCord + 20) + "px");
		}
		
		try {	
			var poptop = $("#preview").offset().top;
		} catch (err) {
		}
		var popheight = $("#preview").height();
		
		try {	
			var popleft = $("#preview").offset().left;
		} catch (err) {
		}
		var popwidth = $("#preview").width();
			
		var wintop = $(window).scrollTop();
		var winheight =  $(window).height();
			
		var winleft = $(window).scrollLeft();
		var winwidth =  $(window).width();
	
		var left = Math.ceil(xCord + 25);
		var top = Math.ceil(yCord + 25);
			
		// ----------------- Manage top ------------------------- //
		if ((top + popheight) >= (winheight + wintop)) {
			top = yCord - popheight - 25;
		}
		if (top <= wintop){
			top = wintop + 5;
		}
		if (isNaN(top)) {
			top = 0;
		}
			
		// ----------------- Manage left ------------------------- //
		if ((left + popwidth) >= (winwidth + winleft)) {
			left = xCord - popwidth - 25;
		}
		if (left <= winleft) {
			left = winleft;
		}
		if (isNaN(left)) {
			left = 0;
		}
		
		$("#preview")
				.css("top",(top) + "px")
				.css("left",(left) + "px");

	});
};



function formatTitle () {
	return $('h3.caption').text();
}
$(document).ready(function(){
	imagePreview();
	
	$("a[rel=lightbox]").fancybox({
		'titlePosition' : 'inside',
		'titleFormat' : formatTitle,
		'transitionIn'	:	'elastic',
		'transitionOut'	:	'elastic',
		'speedIn'		:	400, 
		'speedOut'		:	200,
		'centerOnScroll' : true
	});
	
//	jQuery(".search").text('123');
	
	var termTemplate = "<strong>%s</strong>";
	$( "#autocomplete_shortsearch" ).autocomplete({
		source: "/autocomplete.php",
		minLength: 2,
		select: function( event, ui ) {
//			log( ui.item ?
//				"Selected: " + ui.item.value + " aka " + ui.item.id :
//				"Nothing selected, input was " + this.value );
		},
		open: function(e,ui) {
        var
            acData = $(this).data('autocomplete'),
            styledTerm = termTemplate.replace('%s', acData.term);

        acData
            .menu
            .element
            .find('li a')
            .each(function() {
                var me = $(this);
				var reg = eval ('/(' + acData.term + ')/i');
                me.html( me.text().replace(reg, "<strong>$1</strong>") );
            });
    	}
	});

	$('.cart_quantity, .amount').bind('keypress', function(e) {
		if ( e.which!=8 && e.which!=0 && (e.which<48 || e.which>57)) {
			return false;
		} else {
			return true;
		}				
	});
	
	$('.amount').keyup(function () {
		if (+$(this).val() <= 0) {$(this).val(1);}
		var total_sum = 0;
		$('.amount').each(function (e) {
			var product = $(this).attr('product');
			var new_sum = +$(this).val() * +$('.price_' + product).text();
			$('.sum_' + product).text(new_sum.toFixed(2));
			
			total_sum += new_sum;
			//alert(product);
		});
		
		
		$("#total_sum").text(total_sum.toFixed(2));
	});
	
	$('.amount').blur(function () {
		var amount = +$(this).val();
		var product = +$(this).attr('product');
		$.ajax({
			type: 'POST',
			async : true,
			url: 'ajax/update_product.ajax.php',
			data: 'product=' + product + '&amount=' + amount,
			success: function(xml_doc){
				//if (+xml_doc == 0) {alert('Problem updating the field. Please press Next to save the changes.');}
			}
		});
	});
	
	$('.arrow_down').click (function () {
		var current_value = +$(this).parent().parent().find('.cart_quantity').val();
		if (current_value <= 1) {
			$(this).parent().parent().find('.cart_quantity').val(1);
		} else {
			$(this).parent().parent().find('.cart_quantity').val(current_value - 1);
		}
		return false;
	});
	
	$('.arrow_up').click (function () {
		var current_value = +$(this).parent().parent().find('.cart_quantity').val();
		if (current_value <= 0) {
			$(this).parent().parent().find('.cart_quantity').val(1);
		} else {
			$(this).parent().parent().find('.cart_quantity').val(current_value + 1);
		}
		return false;
	});
	
	$('.cart_quantity').blur (function () {
		if (+$(this).val() <= 0) {
			$(this).val(1);
		}
	});

	
});

