var offset_arrow_container, interval_id;

/**
 * Carrousel fader extended function of jQuery
 */
jQuery.fn.extend ({
	/**
	 * @param interval 		: Number in milliseconds
	 * @param startIndex 	: int to start
	 * @param endIndex		: [optional] int to end
	 */
	carrousel: function(interval, startIndex, endIndex){
		var container = this;
		var startCounter = startIndex;
		
		clearInterval(interval_id);
		interval_id = setInterval(function(){
			animate(container, startCounter, function(){
				if (endIndex && startCounter == endIndex-1){
					clearInterval(interval_id);
					return;
				}
				
				if (startCounter == container.children('div.image').length-1){
					startCounter = 0;
				}else{
					startCounter++;
				}
			});
		}, interval);
	},
	offsetArrow: function(value){
		offset_arrow_container = value;
		this.parent().find('.slide_arrow').css('margin-left', value);
	}
});

/**
 * Fade animation
 * @param container : container which contains all the div.image elements
 */
function animate(container, index, callbackfunction, setDisplayNone){
	var images = container.children('div.image');
	var selected_image = container.children('div.image')[index];
	var arrow_container = container.parent().find('.slide_arrow');
	
	$.each(images, function(i, image){
		if (i == images.length-1){
			if (setDisplayNone){
				if (image == selected_image) {
					$(image).stop(true, true).fadeIn(1500, 'easeOutQuint', function(){
						if (callbackfunction) callbackfunction();	
					});
				}else{
					$(image).stop(true, true).fadeOut(1500, 'easeOutQuint', function(){
						if (callbackfunction) callbackfunction();	
					});
				}
			}else{
				if (image == selected_image) {
					$(image).stop(true, true).animate({'opacity':1}, 1500, 'easeOutQuint', function(){
						if (callbackfunction) callbackfunction();	
					});
				}else{
					$(image).stop(true, true).animate({'opacity':0}, 1500, 'easeOutQuint', function(){
						if (callbackfunction) callbackfunction();	
					});
				}
			}
		}else{
			if (setDisplayNone){
				if (image == selected_image){ 
					$(image).stop(true, true).fadeIn(1500, 'easeOutQuint');
				}else{
					$(image).stop(true, true).fadeOut(1500, 'easeOutQuint');
				}
			}else{
				if (image == selected_image){ 
					$(image).stop(true, true).animate({'opacity':1}, 1500, 'easeOutQuint');
				}else{
					$(image).stop(true, true).animate({'opacity':0}, 1500, 'easeOutQuint');
				}
			}
		}
	});
	
	var offset = (offset_arrow_container) ? offset_arrow_container : -900;
	arrow_container.stop().animate({
		'margin-left': offset + (index * 199)
	}, 1500, 'easeOutQuint');
}

/**
 * Stop current animation
 */
function stopAnimation(){
	clearInterval(interval_id);
}

/**
 * Submenu set active class
 */
function setSubmenuActive(element){
	// remove all active classes
	var current_element = $(element);
	while (!current_element.hasClass('baboon_menu'))
		current_element = current_element.parent();
	
	current_element.find('a').removeClass('active');
	$(element).addClass('active');
}

