$(document).ready(function() {

	jQuery.easing.def = "easeInOutExpo";
	
	$("#designHistoria ul li.beer img").hover(function() {
		$(this).animate({
			width: "43",
			height: "104",
			top: "0",
			left: "0"
		}, 200);
		}, function() {
		$(this).animate({
			width: "28",
			height: "75",
			top: "15",
			left: "5"
		}, 200);
		$(this).dequeue();
	});
	
	$("#designHistoria ul li#stamp img").hover(function() {
		$(this).animate({
			width: "77",
			height: "104",
			top: "0",
			left: "-5"
		}, 200);
		}, function() {
		$(this).animate({
			width: "56",
			height: "75",
			top: "15",
			left: "5"
		}, 200);
		$(this).dequeue();
	});
	if(!playing) {
		$("#timeline ul li a#id1").addClass("active");
		$("#timeline ul li a#id1").find(".bubble").css("display", "block");
	} else {
		$("#timeline ul li a").find(".bubble").css("display", "none !important");
	}

	$("#designHistoria a").toggle(function() {
		$("#timeline ul").find(".active div.bubble").fadeOut(500);
		$("#designHistoria").animate({
			top: "0"
		}, 450);
	}, function() {
		$("#designHistoria").animate({
			top: "-136"
		}, 450);
		$("#timeline ul").find(".active div.bubble").fadeIn(500);
	});
	
	$("#designHistoria ul li").hover(function() {
		$("p", this).css("display", "block");
	}, function() {
		$("p", this).css("display", "none");
	});
	
	$(".buttonPlay").click(function() {
		$('.buttonPlay').toggleClass("buttonStop");
		$("#historySlide").stopTime(); times = 0 ;
		
		if( !playing )
		{
			playing = true;
			slideShow();
		}
		else
			playing = false;
			$("#timeline ul").find(".active div.bubble").fadeIn(350);
			//$("#id"+slideTo+ " div.bubble").fadeIn(350);
	});
	
	$("#timeline ul li a").click(function() {
		var newSlide = (parseInt($(this).attr("id").substr(2)));
		stopSlideshow();
		$("#timeline ul li a").find(".bubble").css("display", "none");
		gotoSlide(newSlide);
		$("#id"+newSlide+" div.bubble").delay(900).fadeIn(350);
	});
	
});

var playing = false;
var times = 0 ;

gotoSlide = function(slideTo) {
	
	var checkDesign = parseInt($("#designHistoria").css("top").replace("px", ""));
	
	if(checkDesign > -500) {
		$("#designHistoria").animate({
			top: "-136"
		}, 650);
	}
	
	$("#timeline ul li a.active .bubble").fadeOut(350);
	$("#timeline ul").find(".active").removeClass('active');
	
	margin = (slideTo-1)*-980;
	
	$("#historySlide").animate({
	marginLeft: margin
	}, 700, 'swing', function() {
		if(playing) {
			$("#id"+slideTo+ " div.bubble").css("display", "none");
			$("#id"+slideTo).addClass('active');
		}
		else {
		$.each($("#timeline ul").find(".active"), function() {
			$("#timeline ul").find(".active").removeClass("active");
		});
		$("#id"+slideTo).addClass("active");
		}
	});
	
}

slideShow = function() {
	var slideshowSlide = (parseInt($("#timeline ul").find(".active").attr("href").substr(1)));
	times = 8 - slideshowSlide;
	if (slideshowSlide == 8) {
		slideshowSlide = 1;
	} else{ slideshowSlide = slideshowSlide + 1; }
	
	playSlideshow(slideshowSlide);
}

playSlideshow = function(slideshowSlide){
	$("#historySlide").oneTime(3500, function() {
		gotoSlide(slideshowSlide);
		slideshowSlide = slideshowSlide + 1;
		times--;
		if( times > 0 )
			playSlideshow(slideshowSlide);
		else
			stopSlideshow();
	});
}

stopSlideshow = function(){
	if( playing )
		$('.buttonPlay').toggleClass("buttonStop");
		playing = false;
		times = 0;
		$("#historySlide").stopTime();
		$("#timeline ul").find(".active div.bubble").fadeIn(350);
}
