var $j = jQuery.noConflict();

	function spidoScrollTo(scrollTarget){
		$j(function() {
			$j('html, body').animate({
				scrollTop:$j(scrollTarget).offset().top
			}, 500);		
		});	
	};
	
	
	$j(document).ready(function(){
							   
		// ********delete link dotted line on IE**********	
		$j("#wine_animation a").focus(function(){
			$j(this).blur();	
		});
		
		
		//***************** tooltip***********************
		$j("#wine_animation li a").hover(function(e){
			//mouse on
			var title = $j(this).attr("title");
			savetitle = title;
			var html = "<div id='info'>";
				html += title;
				html += "</div>";
			$j("body").append(html).children("#info").hide().show(500);
			$j("#info").css("top", e.pageY + 30)
					  .css("left", e.pageX - 50);
			$j("#info").addClass("rounded");
			$j(this).removeAttr("title");
				
		},function(){
			//mouse out
			$j("#info").fadeOut(300).remove();
			var title = $j(this).attr("title", savetitle);
			
		});	
		
		$j("#wine_animation li a").mousemove(function(e){
			//mouse move
			$j("#info").css("top", e.pageY + 30)
					  .css("left", e.pageX - 50);	
		
		});
		
		
		// *************show wine sheet******************
		$j("#wine_animation li a").each(function(){
												
			$j(this).click(function(e){
								   
				var position = $j(this).parent().position();
				var animation_container;
				var fiche = $j(this).attr("href")+" #the_content";
				var img_bottle =$j(this).children().attr("src");
				
				//Set the container height depending the number of row
				if($j("#wine_animation li").length <= 12){
					animation_container = "300px";
				}else{
					animation_container = "600px";
				}				
				$j("#wine_animation").height(animation_container);
				
				//duplicate element without efore transform to absolute element				
				$j(this).parent().after("<li class='selected'><img src='"+img_bottle+"' alt='' /></li>");
				//Transform the duplicate bottle to an absolute element
				$j(".selected").css({
					position : "absolute",
					"z-index"  : "10",
					left     : position.left,
					top      : position.top
				});				
				//hide the clicked bottle
				$j(this).parent().css({"visibility":"hidden"});
				
				//Start animation
				$j("#info").fadeOut(300).remove();
				$j(".selected").siblings().fadeOut(function(){
					$j(".selected").stop().animate({"left" : "0px", "top" : "15px" },1000, function(){	
						$j("#wine_animation").animate({"height" : "600px"},500);
						spidoScrollTo ('#content_container');
						$j("#fiche_animation").load( fiche, function(){
							$j("#the_content").append("<a id='fiche_close' href='#'>X</a>");
							$j("#fiche_content_text ul").after("<p id='back_to_list'><a href='#'>< retour a la liste des vins</a></p>"); 
							$j("#fiche_animation").fadeIn();
							
							// Close wine sheet
							$j("#fiche_close, #back_to_list").click(function(e_close){								
								$j("#fiche_animation").fadeOut(function(){
									$j(this).remove();
									$j("#wine_animation").animate({"height" : animation_container},500);
									spidoScrollTo ('#header');
									$j(".selected").stop().animate({"left" : position.left ,"top" : position.top },1000, "easeOutQuint", function(){
										$j(this).siblings().fadeIn(function(){
											$j(".selected").remove();
											$j("#wine_animation li").removeClass().attr("style","");
										});
										$j("#wine_animation").after("<div id='fiche_animation'></div>");
									});
																		
								});	
								e_close.preventDefault();
							});	
						});
					});											 
				});
				e.preventDefault();
			});
		});
					  
	});

