/**
*	Carta Accordion 1.2
*	Author: Stefano Giliberti (kompulsive@gmail.com)
*	(winterbits.com)
*/
(function(jQuery) {
	jQuery.fn.wb_accordion = function(options) {
	
		/* Presets */
		var defaults = {
			slide: ".slide", // slide selector
			content: ".slide-content", // slide content selector
			first: 1, // slide to open first
			fullwidth: 730, // open slide width
			dropshadow: true,
			hoveropacity: 0.5,
			hovercolor: "#FFF",
			speed: 700,
			easing: "easeOutExpo" // optional
		};
		var options = jQuery.extend(defaults, options);
		
		//
		return this.each(function() {
			
			var slider = jQuery(this),
				slides = slider.find(options.slide)

			slides.css({
				"width": options.fullwidth,
			})
						
			var count = slides.length,
				minwidth = (slider.width() - options.fullwidth) / (count - 1),
				tabindex = options.first - 1,
				last = slider.find(options.slide + ":eq(" + tabindex + ")"),
				easing = (jQuery.easing.def) ? options.easing : "linear",
				slideimages = slides.children("img").hide(),
				height = slides.parent().height()

			//
			open(slider.find(options.slide + ":eq(" + tabindex + ")"))
			
			slider.find(options.slide + ":not(:eq(" + tabindex + "))").css("width", minwidth)
						
			slideimages.each(function(i) {	
				jQuery(this).delay(i*200).fadeIn()
			})
			
			jQuery('<div class="overlay"></span>')
				.prependTo(slides)
				.css("position", "absolute")
				.css("width", jQuery(this).width())
				.css("height", height)
				.css("z-index", "50")
				.css("background", options.hovercolor)
				.hide()
			
			slides.find(options.content).hide()
			
			if (options.dropshadow) {	
				slider.find(options.slide + ":not(:last-child)").prepend('<div class="drop-shadow"></span>')
				slider.find(".drop-shadow").css("height", height)
			}
					
			slides.hoverIntent(function(){
				if (!jQuery(this).hasClass("open")) {
					close(last)
					open(this)
					last = this;
				}
			}, function(){});
			
			slides.hover(function(){
				jQuery(this).siblings(options.slide).find(".overlay").show().css("opacity", options.hoveropacity)
			}, function(){
				jQuery(this).siblings(options.slide).find(".overlay").hide()
			})
			
			function open(tab) {
				jQuery(tab)
					.addClass("open")
					.animate({ width: options.fullwidth }, { queue: false, duration: options.speed, "easing": easing })
				if (jQuery(tab).find(options.content).length)	
					jQuery(tab)
						.find(options.content)
						.delay(options.speed / 2)
						.slideDown(100)
			}
			
			function close(tab) {
				if (jQuery(tab).find(options.content).length) {			
					jQuery(tab)
						.find(options.content)
						.stop(true, true)
						.hide()
				}
				jQuery(tab)
					.removeClass("open")
					.animate({ width: minwidth }, { queue: false, duration: options.speed, "easing": easing })
			}
						
		})
		//
		
	}
})(jQuery)

