jQuery(document).ready(function(){

	//AJAXed CART
	jQuery(".ajax-cart").click(function() {
		var productIDValSplitter 	= (this.id).split("-");
		var productIDVal 			= productIDValSplitter[1];
		
		var productX 		= jQuery("#image-" + productIDVal).offset().left;
		var productY 		= jQuery("#image-" + productIDVal).offset().top;
		
		if( jQuery("#product_mini_cart_" + productIDVal).length > 0){
			var basketX 		= jQuery("#product_mini_cart_" + productIDVal).offset().left;
			var basketY 		= jQuery("#product_mini_cart_" + productIDVal).offset().top;
		} else {
			var basketX 		= jQuery("#cart").offset().left;
			var basketY 		= jQuery("#cart").offset().top;
		}
		
		var gotoX 			= basketX - productX;
		var gotoY 			= basketY - productY;
		
		var newImageWidth 	= (jQuery("#product_mini_cart_" + productIDVal).width() > 0 ? jQuery("#product_mini_cart_" + productIDVal).width() : 50) / 3;
		var newImageHeight	= (jQuery("#product_mini_cart_" + productIDVal).height() > 0 ? jQuery("#product_mini_cart_" + productIDVal).height() : 35) / 3;
		
		jQuery("#image-" + productIDVal + ' img')
		.clone()
		.prependTo("#image-" + productIDVal)
		.css({'position' : 'absolute'})
		.animate({opacity: 0.8}, 100 )
		.animate({opacity: 0.2, marginLeft: gotoX, marginTop: gotoY, width: newImageWidth, height: newImageHeight}, 1000, function() {
		jQuery(this).remove();
	
			var pQuantity = jQuery("#pq-" + productIDVal).val();
			var pQtyX = jQuery("#pq-" + productIDVal).offset().left;
			var pQtyY = jQuery("#pq-" + productIDVal).offset().top;
			
			jQuery("#pb-" + productIDVal).attr('src', 'images/loader.gif');

			var xhr = jQuery.ajax({
				type: "POST",  
				url: "controller.php",
				data: { productID: productIDVal, quantity: pQuantity, 'jump': 'cart'},  
				success: function(theResponse) {
					if( jQuery("#product_mini_cart_" + productIDVal).length > 0){
						jQuery("#product_mini_cart_" + productIDVal).animate({ opacity: 0 }, 500);
						jQuery("#product_mini_cart_" + productIDVal).before(theResponse).remove();
						jQuery("#product_mini_cart_" + productIDVal).animate({ opacity: 0 }, 500);
						jQuery("#product_mini_cart_" + productIDVal).animate({ opacity: 1 }, 500);
						jQuery("#pb-" + productIDVal).attr('src', 'includes/languages/ro_RO/images/buttons/button_update_cart.gif');
						jQuery("#mini-cart-noproducts").html(xhr.getResponseHeader('NOITEMS'));
						jQuery("#mini-cart-subtotal").html(xhr.getResponseHeader('TOTAL'));
					} else {
						jQuery("#mini-cart-summary").show();
						if (jQuery("#mini-cart-empty").length>0) jQuery("#mini-cart-empty").remove();
						if (jQuery("#mini-cart-noproducts").length>0) jQuery("#mini-cart-noproducts").html(xhr.getResponseHeader('NOITEMS'));
						if (jQuery("#mini-cart-subtotal").length>0) jQuery("#mini-cart-subtotal").html(xhr.getResponseHeader('TOTAL'));
						jQuery("#pb-" + productIDVal).attr('src', 'includes/languages/ro_RO/images/buttons/button_update_cart.gif');
						jQuery("#cart ul li:first").after(theResponse);
						jQuery("#cart ul").children().next().next().show("slow");
					}
					if (xhr.getResponseHeader('QUANTITY')) {
						message(pQtyX, pQtyY, 'Cantitatea produsului a fost actualizata conform stocului fizic: '+xhr.getResponseHeader('QUANTITY')+' buc!');
						jQuery("#pq-" + productIDVal).val(xhr.getResponseHeader('QUANTITY'));
					}
					minicartpopup();
					if (jQuery("#microcart-total-span").length>0) {
						jQuery("#microcart-total-span").remove();
						jQuery("#microcart-shipping").before('<div id="microcart-total-div" class="microcart-total-div">'+xhr.getResponseHeader('TOTALVALUE')+'</div>');
						jQuery("#microcart-shipping").show();
					} else {
						jQuery("#microcart-total-div").html(xhr.getResponseHeader('TOTALVALUE'));
					}
				}  
			});  
		
		});
		
	});
	
	//AJAXed FAVOURITES
	jQuery(".favourites").click(function(e) {
		var productIDValSplitter 	= (this.id).split("-");
		var productIDVal 			= productIDValSplitter[1];
		
		jQuery("#pf-" + productIDVal).attr('src', 'images/loader.gif');

		var xhr = jQuery.ajax({
			type: "POST",  
			url: "controller.php",
			data: { productID: productIDVal, 'jump': 'favourites'},  
			success: function(theResponse) {
				jQuery("#pf-" + productIDVal).attr('src', 'includes/languages/ro_RO/images/buttons/button_'+xhr.getResponseHeader('BUTTON')+'_favourites.gif');
				
				jQuery("body").append('<div align="center" id="popup-favourites" style="display:none">'+theResponse+'<\/div>');
				
				var tipX = e.pageX + 12;
				var tipY = e.pageY + 12;
				
				var tipWidth = jQuery("#popup-favourites").outerWidth(true);
				var tipHeight = jQuery("#popup-favourites").outerHeight(true);
				
				if(tipX + tipWidth > jQuery(window).scrollLeft() + jQuery(window).width()) tipX = e.pageX - tipWidth - 6;
				if(jQuery(window).height()+jQuery(window).scrollTop() < tipY + tipHeight) tipY = e.pageY - tipHeight - 6;
				
				jQuery("#popup-favourites").css("left", tipX).css("top", tipY).fadeIn("fast");
				
				jQuery("#favourites-count").html(' ('+xhr.getResponseHeader('NOP')+')');
			}
		});
		
		jQuery(document).click(function() {
			jQuery("#popup-favourites").remove();
		});
	});
	
	//IMAGE POPUP
	jQuery('a.tooltip').hover(function(e) {
		jQuery(this).children('img').attr('title', '');
		
		jQuery("body").append('<div id="popup" style="display:none"><img alt="Loader" id="ajax-loader" src="images/ajax-loader.gif" \/><\/div>');
		var tipX = e.pageX + 12;
		var tipY = e.pageY + 12;

		var tipWidth = jQuery("#popup").outerWidth(true);
		var tipHeight = jQuery("#popup").outerHeight(true);
		
		if(tipX + tipWidth > jQuery(window).scrollLeft() + jQuery(window).width()) tipX = e.pageX - tipWidth - 6;
		if(jQuery(window).height()+jQuery(window).scrollTop() < tipY + tipHeight) tipY = e.pageY - tipHeight - 6;
		
		jQuery("#popup").css("left", tipX).css("top", tipY).fadeIn("medium");

		jQuery.ajax({
			type : 'POST',
			url : 'controller.php',
			data: { productID : jQuery(this).attr('id'), 'jump': 'popup' },
			success : function(data){
				jQuery('#ajax-loader').hide(100);
				jQuery('#popup').html(data);
			},
			error : function(XMLHttpRequest, textStatus, errorThrown) {
				jQuery('#ajax-loader').hide(100);
				jQuery('#popup').html('Eroare la incarcare!');
			}
		});
	}, function() {
		jQuery('#popup').remove();
	}).mousemove(function(e){
		var tipX = e.pageX + 12;
		var tipY = e.pageY + 12;
		var tipWidth = jQuery("#popup").outerWidth(true);
		var tipHeight = jQuery("#popup").outerHeight(true);
		if(tipX + tipWidth > jQuery(window).scrollLeft() + jQuery(window).width()) tipX = e.pageX - tipWidth - 6;
		if(jQuery(window).height()+jQuery(window).scrollTop() < tipY + tipHeight) tipY = e.pageY - tipHeight - 6;
		jQuery("#popup").css({"left": tipX, "top": tipY}).fadeIn("medium");
	});
	
	
	//MESSAGE TO X - Y
	function message(x, y, msg) {
		if (jQuery("#ajax-message")) jQuery("#ajax-message").remove();
		jQuery("body").append('<div id="ajax-message" style="display:none"><ul><li style="list-style-image: url(\'/images/icons/warning.gif\')">'+msg+'<\/li><\/ul><\/div>');
		jQuery("#ajax-message").append('<div align="center"><input type="button" value="OK" id="ajax-message-close" \/><\/div>');
		
		var mWidth = jQuery("#ajax-message").outerWidth(true);
		var mHeight = jQuery("#ajax-message").outerHeight(true);
		var mX = jQuery("#ajax-message").offset().left;
		var mY = jQuery("#ajax-message").offset().top;
		
		if(x + mWidth > jQuery(window).scrollLeft() + jQuery(window).width()) x = x - mWidth - 6;
		if(jQuery(window).height()+jQuery(window).scrollTop() < x + mHeight) y = y - mHeight - 6;
		
 		jQuery("#ajax-message").css("left", x).css("top", y).fadeIn("fast");
 		
 		jQuery('#ajax-message-close').click(function() {
 			jQuery("#ajax-message").remove();
 		});
	}
	
	//CART POPUP
	function minicartpopup() {
		jQuery('a.mini-cart-tooltip').hover(function(e) {
	
			jQuery(this).children('img').attr('title', '');
			
			var image = jQuery(this).children('img').attr('src').split('/');
			var imagesrc = image[image.length-1];
			
			jQuery("body").append('<div id="mini-cart-popup" style="display:none"><img alt="Loader" id="ajax-loader" src="images/loader.gif" \/><\/div>');
			var tipX = e.pageX + 12;
			var tipY = e.pageY + 12;
			
			var tipWidth = jQuery("#mini-cart-popup").outerWidth(true);
			var tipHeight = jQuery("#mini-cart-popup").outerHeight(true);
			
			if(tipX + tipWidth > jQuery(window).scrollLeft() + jQuery(window).width()) tipX = e.pageX - tipWidth - 6;
			if(jQuery(window).height()+jQuery(window).scrollTop() < tipY + tipHeight) tipY = e.pageY - tipHeight - 6;
			
			jQuery("#mini-cart-popup").css("left", tipX).css("top", tipY).fadeIn("medium");
			jQuery('#ajax-loader').hide(100);
			
			jQuery('#mini-cart-popup').html('<img src="images/products/thumbnails/'+imagesrc+'" />');
		}, function() {
			jQuery('#mini-cart-popup').remove();
		}).mousemove(function(e){
			var tipX = e.pageX + 12;
			var tipY = e.pageY + 12;
			var tipWidth = jQuery("#mini-cart-popup").outerWidth(true);
			var tipHeight = jQuery("#mini-cart-popup").outerHeight(true);
			if(tipX + tipWidth > jQuery(window).scrollLeft() + jQuery(window).width()) tipX = e.pageX - tipWidth - 6;
			if(jQuery(window).height()+jQuery(window).scrollTop() < tipY + tipHeight) tipY = e.pageY - tipHeight - 6;
			jQuery("#mini-cart-popup").css({"left": tipX, "top": tipY}).fadeIn("medium");
		});
	} minicartpopup();
	
	//Quick SEARCH
	var timestamp = 0;
	jQuery('#quicksearch').bind('keyup', function() {
		clearTimeout(timestamp);
		
		if (jQuery(this).val().length > 1) {
			timestamp = setTimeout(function() {
				jQuery.ajax({
					type : 'POST',
					url : 'controller.php',
					data: { 'search': jQuery('#quicksearch').val(), 'jump': 'quick' },
					success : function(data){
						jQuery('#quickresults').html(data);
						jQuery('#quickresults').show(100);
					},
					error : function(XMLHttpRequest, textStatus, errorThrown) {
						jQuery('#quickresults').html(textStatus);
						jQuery('#quickresults').show(100);
					}
				});
			}, 500);
		} else {
			jQuery("#quickresults").html("");
			jQuery("#quickresults").hide();
		}	
	}).blur(function() {
		setTimeout(function() {
			jQuery("#quickresults").hide(100);
			jQuery("#quickresults").html("");
			jQuery('#quicksearch').val('');
		}, 500);
	});
	
	jQuery('#quicksearch').attr('autocomplete', 'off');
	
	jQuery('.switchopacity').hover(function() {
		jQuery(this).fadeTo('fast', 1);
	}, function() {
		jQuery(this).fadeTo('fast', 0.4);
	});
	
	jQuery('.category-root').hover(function() {
		jQuery('#x'+this.id).show();
		jQuery(this).css('background-image', 'url(/images/arrow.gif)');
		jQuery(this).css('background-repeat', 'no-repeat');
		jQuery(this).css('background-position', '150px 4px');
		//alert(this.id);
	}, function() {
		jQuery('#x'+this.id).hide(100);
		jQuery(this).css('background-image', 'none');
	});
	
	jQuery('.category').hover(function() {
		jQuery('#x'+this.id).show();
		jQuery(this).css('background-image', 'url(/images/arrow.gif)');
		jQuery(this).css('background-repeat', 'no-repeat');
		jQuery(this).css('background-position', '192px 4px');
		//alert(this.id);
	}, function() {
		jQuery('#x'+this.id).hide();
		jQuery(this).css('background-image', 'none');
	});
	
	if (typeof(notifications) != 'undefined') jAlert(notifications);
});