/*
 * 	easyAccordion 0.1 - jQuery plugin
 *	written by Andrea Cima Serniotti	
 *	http://www.madeincima.eu
 *
 *	Copyright (c) 2010 Andrea Cima Serniotti (http://www.madeincima.eu)
 *	Dual licensed under the MIT (MIT-LICENSE.txt) and GPL (GPL-LICENSE.txt) licenses.
 *	Built for jQuery library http://jquery.com
 */
 
 	var etActive = false;
	var expandido = false;
 
(function(jQuery) {
	jQuery.fn.easyAccordion = function(options) {
			
	this.each(function() {
		
		
		jQuery(this).find('dl').addClass('easy-accordion');
		
		
		// -------- Set the variables ------------------------------------------------------------------------------
		
		jQuery.fn.setVariables = function() {
			dlWidth = jQuery(this).width();
			dlHeight = jQuery(this).height();
			dtWidth = jQuery(this).find('dt').outerHeight();
			if (jQuery.browser.msie){ dtWidth = $(this).find('dt').outerWidth();}
			dtHeight = dlHeight - (jQuery(this).find('dt').outerWidth()-jQuery(this).find('dt').width());
			slideTotal = jQuery(this).find('dt').size();
			ddWidth = dlWidth - (dtWidth*slideTotal) - (jQuery(this).find('dd').outerWidth(true)-jQuery(this).find('dd').width());
			ddHeight = dlHeight - (jQuery(this).find('dd').outerHeight(true)-jQuery(this).find('dd').height());
		};
		jQuery(this).setVariables();
	
		
		// -------- Fix some weird cross-browser issues due to the CSS rotation -------------------------------------

		if (jQuery.browser.safari){ var dtTop = (dlHeight-dtWidth)/2; var dtOffset = -dtTop;  /* Safari and Chrome */ }
		if (jQuery.browser.mozilla){ var dtTop = dlHeight - 20; var dtOffset = - 20; /* FF */ }
		if (jQuery.browser.msie){ var dtTop = 0; var dtOffset = 0; /* IE */ }
		
		
		// -------- Getting things ready ------------------------------------------------------------------------------
		
		var f = 1;
		jQuery(this).find('dt').each(function(){
			jQuery(this).css({'width':dtHeight,'top':dtTop,'margin-left':dtOffset});	
			f = f + 1;
		});
		
		if(jQuery(this).find('.active').size()) { 
		//alert("activeeee");
			jQuery(this).find('.active').next('dd').addClass('active');
		} else {
			//alert("no activeeee");
			jQuery(this).find('dt:first').addClass('active').next('dd').addClass('active');
		}
		
		jQuery(this).find('dt:first').css({'left':'0'}).next().css({'left':dtWidth});
		jQuery(this).find('dd').css({'width':ddWidth,'height':ddHeight});	

		
		// -------- Functions ------------------------------------------------------------------------------
		
		jQuery.fn.findActiveSlide = function() {
//			alert("findActiveSlide");
			var i = 1;
			this.find('dt').each(function(){
				if(jQuery(this).hasClass('active')){
					activeID = i; // Active slide
				//} else if (jQuery(this).hasClass('no-more-active')){
//					noMoreActiveID = i; // No more active slide
				}
				i = i + 1;
			});
		};
			
		jQuery.fn.calculateSlidePos = function() {
			var u = 2;
			//jQuery(this).find('dt').not(':first').each(function(){
			jQuery(this).find('dt').each(function(){
													//alert("bb");
				var activeDtPos = dtWidth*activeID;
				//if(u <= activeID){
				if(etActive){
					//var leftDtPos = dtWidth*(u-1);
					var leftDtPos = dtWidth*(u-2);
					jQuery(this).animate({'left': leftDtPos});
					//if(u < activeID){ // If the item sits to the left of the active element
					//alert("et1");
					//	jQuery(this).next().css({'left':leftDtPos+dtWidth});	
					//} else { // If the item is the active one
	//->				alert("et2");
						jQuery(this).next().animate({'left':activeDtPos});
					//}
					//expandido = true;
				} else {
					//alert("et3");
					//var rightDtPos = dlWidth-(dtWidth*(slideTotal-u+1));
					var rightDtPos = dlWidth-(dtWidth*(slideTotal-u+2));
					//rightDtPos = 600;
					//alert(rightDtPos);
					jQuery(this).animate({'left': rightDtPos});
					var rightDdPos = rightDtPos+dtWidth;
					//alert(rightDtPos);
					jQuery(this).next().animate({'left':rightDdPos});
					//expandido = false;
				}
				u = u+ 1;
			});
			setTimeout( function() {
				jQuery('.easy-accordion').find('dd').not('.active').each(function(){ 
					jQuery(this).css({'display':'none'});
				});
			}, 400);
		};
	
		jQuery.fn.activateSlide = function() {
			//alert("aa");
//			if(this.parent('dl').find('.active')) alert("active");
//			if(this.parent('dl').find('.no-more-active')) alert("no-more-active");
			this.parent('dl').setVariables();	
			this.parent('dl').find('dd').css({'display':'block'});
			//this.parent('dl').find('dd.plus').removeClass('plus');
//			this.parent('dl').find('.no-more-active').removeClass('no-more-active');
//			this.parent('dl').find('.active').removeClass('active').addClass('no-more-active');
			//this.addClass('active').next().addClass('active');	
//			this.parent('dl').findActiveSlide();
			//if(activeID < noMoreActiveID){
			//	this.parent('dl').find('dd.no-more-active').addClass('plus');
			//}
			this.parent('dl').calculateSlidePos();
		};


		// -------- Let's do it! ------------------------------------------------------------------------------
		
		jQuery(this).findActiveSlide();
		jQuery(this).calculateSlidePos();
		
		etActive = false;
		
		jQuery(this).find('dt').not('active').click(function() {
			etActive = !etActive;
			if(etActive)
				$("#accordion").css({'z-index':'3'});
			//else
				//$("#accordion").css({'z-index':'-3'});
			jQuery(this).activateSlide();
			expandido = etActive;
		});	
				
		if (!(jQuery.browser.msie && jQuery.browser.version == 6.0)){ 
			jQuery('dt').hover(function(){
				jQuery(this).addClass('hover');
			}, function(){
				jQuery(this).removeClass('hover');
			});
		}
	});
	};
	
})(jQuery);
		
