jauslin = window.jauslin || {
	currentBottle: null,
	loadNext: false,
	loadPrev: false
};

jQuery(document).ready(function(){
	
	// var winH = jQuery(window).height();
	//     var winW = jQuery(window).width();
	//     var centerDiv = jQuery('#body');
	//     centerDiv.css('top', winH/2-centerDiv.height()/2);
    
	jQuery("#contentContainer").mouseenter( function(){
		jQuery(".arrows").fadeIn();
	}).mouseleave( function(){jQuery(".arrows").fadeOut();});
	
	jQuery(".arrows").mouseover(function(){
		movePanorama(this.id.replace("Arrow", ""))
	}).mouseout(function(){jQuery("#panorama").stop()});
	
	jQuery(".bottle").mouseover(function() {
		this.style.color = "#686753"; 	
		jQuery(this.firstChild.nextSibling).css({'border-color':'#686753'}) 
	}).mouseout(function(){this.style.color = "#ffffff"; jQuery(this.firstChild.nextSibling).css({'border-color':'#ffffff'})})




	/**
	 * Fine tune the height of the lines (title-----image)
	 */ 

	jQuery('.bottle .vLine').each(function() {
		var $this = jQuery(this);
		var containerHeight = $this.parent().height();
		
		var top = $this.prev().height() + 5;
		
		var height = containerHeight - top - $this.next().height() - 10;
		
		$this.animate({top:top+'px',height:height+'px'},100);
		
	});



	/**
	 * Presort all bottles to simplify back/forward navigation in the details view
	 */
	
	var sortedBottles = jQuery('.bottle');
	sortedBottles.sort(function(bottleA,bottleB) { 
		return jQuery(bottleA).position().left-jQuery(bottleB).position().left;
	});
	// Re-append all bottles in the correct order	
	sortedBottles.each(function() {
		jQuery('#panorama').append(this);
	});
	
	
	/**
	 * Prev/next wine
	 */
	
	jQuery('#bottle .prev').live('click', function() {
		loadPrevBottle();
		return false;
	});
	
	jQuery('#bottle .next').live('click', function() {
		loadNextBottle();
		return false;
	});
	
	/**
	 * Overlay close button action
	 */
	
	jQuery("#bottleClose").live('click',function(){
		jQuery("#overlay .background").fadeOut(1000);
		jQuery("#overlay").css({height:'560px'}).animate({ left : "-350", width: "2000"}, 1000, null, function(){
			jQuery("#overlay").remove();
			jQuery('body').attr('class','')
		});
		return false;
	});
	
	
	jQuery('.bottle').click(function() {
		return loadBottleContent(jQuery(this).attr('href'));
	});
	

});

function movePanorama(dir){
	if (dir == "left"){
		jQuery("#panorama").animate({ 
		left : "0"}, { queue:true, duration: jQuery("#panorama").position().left*-10000/2226 });  
	}
	else
	{
		jQuery("#panorama").animate({ 
		left : "-2226px"}, { queue:true, duration: 10000-jQuery("#panorama").position().left*-10000/2226 });  
	}
}


function loadPrevBottle() {
	// Temporarly replace the load function.
	var _loadBottleContent = loadBottleContent;
	loadBottleContent = loadPrevBottleContent;
	jauslin.currentBottle = jauslin.currentBottle.prev('.bottle');
	jauslin.currentBottle.click();
	// Reset
	loadBottleContent = _loadBottleContent;
	
}
function loadNextBottle() {
	// Temporarly replace the load function.
	var _loadBottleContent = loadBottleContent;
	loadBottleContent = loadNextBottleContent;
	jauslin.currentBottle = jauslin.currentBottle.next('.bottle');
	jauslin.currentBottle.click();
	// Reset
	loadBottleContent = _loadBottleContent;
}


function loadNextBottleContent(path) {
	loadBottleContentInDirection(path,1);
}

function loadPrevBottleContent(path) {
	loadBottleContentInDirection(path,-1);
}

function loadBottleContentInDirection(path,dir) {
	validateButtons();
	jQuery.get(path, function(html) {
		var $html = jQuery(html);
		var $bottle = $html.find('#bottle');
		var $info = $html.find('#bottleDescriptionContainer');
		
				
		var type = $info.attr('class');
		
		jQuery('body').attr('class',type);
		
		var $oldImg = jQuery('#bottle').find('.img');
		var $newImg = $bottle.find('.img');

		$newImg.css({left: (dir*355)+'px'}).appendTo('#bottle');

		setTimeout(function() {
			$newImg.animate({left:0},750);
		},200);

		$oldImg.animate({left: (dir*-355)+'px'},500,function() {
			jQuery(this).remove();
		});

		
		jQuery('#bottleDescriptionContainer').html($info.html());
				
	});
}


function validateButtons() {
	
	jQuery('#bottle .prev').css({display: (jauslin.currentBottle.prev('.bottle').length?'block':'none') });
	jQuery('#bottle .next').css({display: (jauslin.currentBottle.next('.bottle').length?'block':'none') });
	
}


function loadBottleContent(path){
	jauslin.currentBottle = jQuery(".bottle[href$='"+path+"']");
	jQuery("<div />")
		.attr("id", "overlay")
		.appendTo("#body")
		.load(path+" #bottle, #bottleDescriptionContainer", null, function(responseText, textStatus, XMLHttpRequest) {
			loadBottleCallBack(responseText, textStatus, XMLHttpRequest, jauslin.loadPrev, jauslin.loadNext);
		});

		
	return false;
}



function loadBottleCallBack(responseText, textStatus, XMLHttpRequest, loadPrev, loadNext) {
	jQuery.ajax({
		complete: function(){
			
			
			/**
			 * Set the color theme for the current type of wine
			 */
					
			jQuery('body').attr('class',jQuery('#bottleDescriptionContainer').attr('class'));
		
			var $white = jQuery("<div/>").addClass('background').css({position:'absolute',top:0,left:0,width:'355px',height:'560px', display:'none'}).append(
				jQuery("<div/>").css({
					position:'absolute',
					background:'#fff',
					top: 0,
					width: '960px',
					height: '428px'
				}),
				jQuery("<div/>").css({
					position: 'absolute',
					bottom: 0,
					width: '368px',
					height: '132px',
					background: '#fff'
				})
			);
		
			$white.prependTo('#overlay').fadeIn(1200);
			jQuery("#overlay").animate({ left : "10px", width: "960"}, {queue:true, duration: 1000});
			
			setTimeout(function() {
				jQuery('#overlay').css({height:'428px'});
			},1000);
			
			validateButtons();
			

		}

	});
}

function removeOv(){
	jQuery("#overlay").remove();
}

function loadImage(path, alt){
	jQuery("<img />").attr("src", path).attr("alt", alt).appendTo("#largeImage");
	jQuery("#largeImage::first").fadeOut("slow", function(){alert("done")});
	jQuery("#largeImage::last").fadeIn("slow");
}
