/*!
 * Lambert Js 
 *
 * Copyright 2010, Luca Bertaiola
 * http://www.luglio7.com/
 * Date: 26 Aug 2010
 */
 
// main lambert object
 
var lambert = {
	
	init: function()
	{
		if(document.getElementById("slideshow"))
		{
			slideshow.init();
		}
		
		if(document.getElementById("grid_page"))
		{
			gridPage.init();
		}
	}
	
};

// grid page category

var gridPage = {
	vars : {},
	elements : {},
	
	init : function()
	{
		gridPage.elements.images = $(".grid_box a img");
		gridPage.imagesSetup();
	},
	
	imagesSetup: function()
	{
		for(var i = 0; i < gridPage.elements.images.length; i++)
		{
			var element = $(gridPage.elements.images[i]);
			element.css({"opacity" : .35});
		
			element.mouseover(function()
			{
				$(this).animate({"opacity" : 1}, {"duration": 750, "easing" : "easeOutQuart", "queue" : false });
			});
			
			element.mouseout(function()
			{
				$(this).animate({"opacity" : .35}, {"duration": 750, "easing" : "easeOutQuart", "queue" : false });
			});
		}
	}
	
};

// slideshow object

var slideshow = {

	vars: {
		margin : 10,
		direction : 1,
		duration : 1000,
		opacityOutside: .1,
		interval: true,
		intervalValue: 7500,
		transition: 'easeOutExpo'
	},
	
	init: function()
	{
		// setup vars
		slideshow.vars.currentIndex = 0;
		slideshow.vars.animation = true;
	
		slideshow.barSetup();
		slideshow.buttonsSetup();
		slideshow.imagesSetup();
		slideshow.textSetup();
		
		// interval
		slideshow.setInterval();
	},
	
	barSetup: function()
	{
		// elements
		var imagesElements = $("#slideshow_bar img");
		var barElement = $("#slideshow_bar");
	
		// get values 		
		var imageWidth = imagesElements.width();		
		var totalWidth = (imageWidth + slideshow.vars.margin) * imagesElements.length;		
		
		// set values
		slideshow.vars.imageWidth = imageWidth;
		slideshow.vars.totalImages = imagesElements.length;
		barElement.css("width", totalWidth + "px");
		imagesElements.css("marginRight", slideshow.vars.margin + "px")
	},
	
	imagesSetup: function()
	{
		var imagesElements = $("#slideshow_bar img");
		
		if(imagesElements.length > 1)
		{
			for(var i = 1; i < imagesElements.length; i++)
			{
				$("#slideshow_bar img:eq("+ i +")").css("opacity", slideshow.vars.opacityOutside);
			}
		}
		else
		{
			$("#slideshow_next").hide();
			$("#slideshow_prev").hide();
		}		
	},
	
	textSetup: function()
	{
		var spanElements = $("#slideshow_info span");
		for(var i = 1; i < spanElements.length; i++)
		{
			$("#slideshow_info span:eq("+ i +")").hide();
		}
	},
	
	buttonsSetup: function()
	{
		// elements
		var buttonsElements = $(".slideshow_button");
		
		buttonsElements.click(function()
		{
			slideshow.vars.direction = ($(this).attr("id") == "slideshow_next")? 1 : 0;
			slideshow.clearInterval();
			slideshow.animateTextBar();
			return;
		});
	},
	
	animateTextBar: function(opacity)
	{
		if((!opacity) || (opacity == 0))
		{
			var nextIndex = slideshow.nextIndex();
			if(slideshow.vars.currentIndex != nextIndex)
			{
				$("#slideshow_info").animate(
					{
						"opacity": 0
					},
					{
						duration: 500,
						easing: 'easeOutExpo',
						queue: true,
						complete: function()
						{
							$("#slideshow_info span:eq("+ slideshow.vars.currentIndex +")").hide();
							$("#slideshow_info span:eq("+ nextIndex +")").show();
							slideshow.animateBar();
						}
					}
				);
			}
		}
		else
		{
			$("#slideshow_info").animate(
				{
					"opacity": 1
				},
				{
					duration: 750,
					easing: 'easeOutExpo',
					queue: true,
					complete: function()
					{
						slideshow.vars.animation = true;
						slideshow.setInterval();
					}
				}
			);
		};
	},
	
	animateBar: function()
	{
		var barElement = $("#slideshow_bar");
		var operator = (slideshow.vars.direction == 1)? "-=" : "+=";
		
		if(slideshow.vars.animation)
		{
			slideshow.vars.animation = false;
			slideshow.vars.currentIndex = slideshow.nextIndex();
			
			var left = ((slideshow.vars.imageWidth + slideshow.vars.margin) * slideshow.vars.currentIndex) * -1;
			
			// bar animation
			barElement.animate(
			{
				"left" : left + "px"
			},
			{
				duration: slideshow.vars.duration,
				easing: slideshow.vars.transition,
				queue: true,
				complete: function()
				{
					slideshow.animateTextBar(1);
				}
			}
			);
			
			slideshow.fadeImages();
			
		}		
		return false;
	},
	
	fadeImages: function()
	{
		for(var i=0; i < slideshow.vars.totalImages; i++)
		{
			if(i == slideshow.vars.currentIndex)
			{
				$("#slideshow_bar img:eq("+ i +")").fadeTo(slideshow.vars.duration, 1);
			}
			else
			{
				$("#slideshow_bar img:eq("+ i +")").fadeTo(slideshow.vars.duration, slideshow.vars.opacityOutside);
			}
		}
	},
	
	nextIndex: function()
	{
	
		if(slideshow.vars.direction == 0)
		{
			if(slideshow.vars.currentIndex - 1 >= 0)
			{
				return slideshow.vars.currentIndex - 1;
			}
			else
			{
				return slideshow.vars.totalImages - 1;
			}
		}
		
		if(slideshow.vars.direction == 1)
		{
			if(slideshow.vars.currentIndex + 1 < slideshow.vars.totalImages)
			{
				return slideshow.vars.currentIndex + 1;
			}
			else
			{
				return 0;
			}
		}
	},
	
	clearInterval: function()
	{
		if(slideshow.vars.interval)
		{
			clearTimeout(slideshow.vars.timeOutInstance);
		}
	},
	
	setInterval: function()
	{
		if(slideshow.vars.interval)
		{
			slideshow.vars.timeOutInstance = setTimeout("slideshow.animateTextBar()", slideshow.vars.intervalValue);
		}
	}
}

// jquery init

$(document).ready(function(){
	lambert.init();
});
