	
	
$(function(){
	// DONT REMOVE - fix z-index bug in IE6 and IE 7
	// var zIndexNumber = 2000;
	// $('div').each(function() {
		// $(this).css('zIndex', zIndexNumber);
		// zIndexNumber -= 10;
	// });

	//mega menu
	function megaHoverOver(){
		$('.sub', this).stop().show();
			
		//Calculate width of all ul's
		(function($) { 
			jQuery.fn.calcSubWidth = function() {
				rowWidth = 0;
				//Calculate row
				$('ul', this).each(function() {					
					rowWidth += $(this).width(); 
				});	
			};
		})(jQuery); 
		

		if ( $('.row', this).length > 0 ) { //If row exists...
			var biggestRow = 0;	
			//Calculate each row
			$('.row', this).each(function() {							   
				$(this).calcSubWidth();
				//Find biggest row
				if(rowWidth > biggestRow) {
					biggestRow = rowWidth;
				}
			});
			//Set width
			$('.sub', this).css({'width' :biggestRow,'left':'8px'});
			$('.row:last', this).css({'margin':'0'});

		} else { //If row does not exist...
			var nbul = $('ul', this).length; // we get number of ul element
			$(this).calcSubWidth();
			//Set Width
			$('.sub', this).css({'width' : rowWidth, 'margin-left':-(rowWidth/nbul)});
		}
	}
	
	function megaHoverOut(){ 
	  $('.sub', this).hide();	  
	}
 
 
	var config = {    
		 sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)    
		 interval: 0, // number = milliseconds for onMouseOver polling interval    
		 over: megaHoverOver, // function = onMouseOver callback (REQUIRED)    
		 timeout: 0, // number = milliseconds delay before onMouseOut    
		 out: megaHoverOut // function = onMouseOut callback (REQUIRED)    
	};
 
	$("ul#megamenu li").hoverIntent(config);
// end of mega menu



	// partners links
	$("a.external").live('click', function() {
		if( $(this).attr('href') != '#')
			$(this).attr('target', '_blank');
		
	});
	
	// if javascript active or not
	$('#noscript').remove();
	
	// animation of shopping cart
	// adding item to basket
	$("#basketItemsWrap li:first").hide();
	if( $("#basketItemsWrap li a").length == 0 ){
		$('#buttoncheckout').hide();
	}
	$(".comment-addbasket a img").click(function() {
		var img = $(this);
		var productIDVal 			= $(this).parent().attr('rel');
	
		var productX 		= $("#productImageWrapID_" + productIDVal).offset().left;
		var productY 		= $("#productImageWrapID_" + productIDVal).offset().top;
		
		if( $("#productID_" + productIDVal).length > 0){
			var basketX 		= $("#productID_" + productIDVal).offset().left;
			var basketY 		= $("#productID_" + productIDVal).offset().top;			
		} else {
			var basketX 		= $("#basketTitleWrap").offset().left;
			var basketY 		= $("#basketTitleWrap").offset().top;
		}
		
		var gotoX 			= basketX - productX;
		var gotoY 			= basketY - productY;
		
		var newImageWidth 	= $("#productImageWrapID_" + productIDVal).width() / 3;
		var newImageHeight	= $("#productImageWrapID_" + productIDVal).height() / 3;
		
		// car animation
		$('#camroule').animate({ marginLeft:'+160px'},1500, function(){
			$(this).animate({opacity: 0}, 1000, function(){
				$(this).animate({marginLeft:'0px'}, function(){
					$(this).animate({opacity:1}, 500);
				});
			});
		});
		
		$("#productImageWrapID_" + productIDVal + " img")
		.clone()
		.prependTo("#productImageWrapID_" + productIDVal)
		.css({'position' : 'absolute'})
		.animate({opacity: 0.6}, 100 )
		.animate({opacity: 0.1, marginLeft: gotoX, marginTop: gotoY, width: newImageWidth, height: newImageHeight}, 800, function() {
  			$(this).remove();
			$("#notificationsLoader").show();
			$.ajax({  
				type: "POST",  
				url: "?mod=basket", 
				dataType: "json",
				data: { productID: productIDVal, action: "addToBasket"},  
				success: function(theResponse) {
					if(theResponse.succes)
					{
						$('#countelementinsidebasket').text(theResponse.countBasketItem); // update top bar 
						$('#basketGlobalTotal').text( theResponse.totalBasket ) // update total basket price
						$('#imagebasketcaddy').attr('src', 'themes/2009/images/caddyfull.gif');

						
						if( $("#productID_" + productIDVal).length > 0){
							$("#productID_" + productIDVal).animate({ opacity: 0 }, 500);
							$("#productID_" + productIDVal).before(theResponse.addtobasket).remove();
							$("#productID_" + productIDVal).animate({ opacity: 0 }, 500);
							$("#productID_" + productIDVal).animate({ opacity: 1 }, 500);
							$("#notificationsLoader").hide();
							
						} else {
							$("#basketItemsWrap li:first").before(theResponse.addtobasket);
							$("#basketItemsWrap li:first").hide();
							$("#basketItemsWrap li:first").show(300);  
							$("#notificationsLoader").hide();			
							$('#buttoncheckout').show('slow');
						}
					}
					else
					{
						$("#notificationsLoader").hide();
						$('#basketError').html('<p style="margin:10px 0;color:red;">' + theResponse.msg + '</p>');
					}
				},
				error:function(XMLHttpRequest, textStatus, errorThrows){ 
					alert(XMLHttpRequest.reponseText);
				}				
			});  
		});
		return false;
	});
	
	// remove item from basket
	$("#basketItemsWrap li a").live("click", function(event) { 
		var productIDVal = $(this).attr('rel');	
		$("#notificationsLoader").show(); // displaying spinner
		$.ajax({  
			type: "POST",  
			url: "?mod=basket",  
			dataType:"json",
			data: { productID: productIDVal, action: "deleteFromBasket"},  
			success: function(theResponse) {
				if( theResponse.countBasketItem == 0){ // if 0 item in basket wrapper
					$('#buttoncheckout').hide(); // we hide the pay button
					$('#imagebasketcaddy').attr('src', 'themes/2009/images/caddy.gif'); // we change image to an empty cart
					$('#basketError').empty();
				}
				$('#countelementinsidebasket').text(theResponse.countBasketItem); // update top bar 
				$('#basketGlobalTotal').text( theResponse.totalBasket ) // update total basket price
				$("#productID_" + productIDVal).hide(300,  function() {$(this).remove();}); // remove li element from list
				$("#notificationsLoader").hide(); // hide the spinner
			},
			error:function(XMLHttpRequest, textStatus, errorThrows){ 
				alert(XMLHttpRequest.responseText);
			}			
		});
		return false;
	});	
	
	// delete basket
	$('#deleteAllBasketItem').live('click', function(){
		if( $("#basketItemsWrap li a").length > 0 )
		{
			$("#notificationsLoader").show(); // displaying spinner
			$.ajax({  
				type: "POST",  
				url: "?mod=basket",  
				dataType:"json",
				data: { action: "deleteAllBasket"},  
				success: function(theResponse) {
					$('#buttoncheckout').hide(); // we hide the pay button
					$('#countelementinsidebasket').text(theResponse.countBasketItem); // update top bar 
					$('#imagebasketcaddy').attr('src', 'themes/2009/images/caddy.gif'); // we change image to an empty cart
					$('#basketGlobalTotal').text( theResponse.totalBasket ) // update total basket price
					$("#basketItemsWrap li[id^=productID_]").hide(300,  function() {$(this).remove();}); // remove li element from list
					$("#notificationsLoader").hide(); // hide the spinner
					$("#basketError").empty(); // empty basket error 

				},
				error:function(XMLHttpRequest, textStatus, errorThrows){ 
					alert(XMLHttpRequest.responseText);
				}			
			}); 
		}

	});
});

    function pagerFactory(idx, slide) {
        var s = idx > 2 ? ' style="display:none"' : '';
        return '<li'+s+'><a href="#">'+(idx+1)+'</a></li>';
    };

