/**
 * @author Stéphane Roucheray 
 * @extends jquery
 * Modified by Olivier Berrewaerts 11/09/2009
 */

jQuery.fn.carousel = function(previous, next, options){
	// added: defaults and options
	var defaults = { 
		beforeAnimate: null,
		afterAnimate: null,
		startId: ''
	}
	var options = jQuery.extend(defaults, options); 
	
	var sliderList = jQuery(this).children()[0];
	
	if (sliderList) {
		var increment = jQuery(sliderList).children().outerHeight("true"),
		elmnts = jQuery(sliderList).children(),
		numElmts = elmnts.length,
		sizeFirstElmnt = increment,
		shownInViewport = Math.round(jQuery(this).width() / sizeFirstElmnt),
		firstElementOnViewPort = 1,
		isAnimating = false;
		
		// add slide position bullets
		var tt = ''; // tooltip to show
		var href = '';
		for (i = 1; i <= numElmts; i++) {
			// build tootip
			tt = jQuery('#viewport ul li:eq('+(i-1)+') h4').text();
			tt += " - "+jQuery('#viewport ul li:eq('+(i-1)+') h3').text();
			if (tt == ' - ') tt = '';
			href = jQuery('#viewport ul li:eq('+(i-1)+')').attr('id');
			if (href == 'bureau347') {
				href = '';
			} else {
				href = '/work/'+href;
			}
			// set new bullet
			jQuery('#slidePosition ul').append('<li><div id="tooltip_'+i+'" class="tooltip"></div><a href="#'+href+'" id="work_'+i+'"></a></li>');
		}
		jQuery('#slidePosition').hide();
		// select first bullet
		jQuery('#slidePosition ul li a:first').addClass('active');
		// add click on bullet
		jQuery('#slidePosition ul li a').click(function(event) {
			var idx = parseInt(jQuery(this).attr('id').substr(5),10) - 1;
			var id = jQuery('#viewport ul li:eq('+idx+')').attr('id');
			if (jQuery(this).attr('href').substr(0,7) != '#/work/') {
				jQuery('#tooltip_'+(idx+1)).html('<p>See you soon</p>');
				tooltipWidth = jQuery('#tooltip_'+(idx+1)).outerWidth();
				tooltipMargin = (tooltipWidth/2) - 7;
				jQuery('#tooltip_'+(idx+1)).css("margin-left", "-"+tooltipMargin+"px");				
			}
			slideTo(id);
			linePositioning();
		});
		jQuery('#slidePosition ul li a').mouseover(function(event) {
			var idx = parseInt(jQuery(this).attr('id').substr(5),10);
			var id = jQuery('#viewport ul li:eq('+(idx-1)+')').attr('id');
			
			if (jQuery(this).hasClass("active")) {
				jQuery('#tooltip_'+idx).html('<p>Launch project</p>');
				tooltipWidth = jQuery('#tooltip_'+idx).innerWidth();
				tooltipMargin = (tooltipWidth/2) - 7;
				jQuery('#tooltip_'+idx).css("margin-left", "-"+tooltipMargin+"px")
			} else {
				if (allworks[id].thumbnails != undefined && allworks[id].thumbnails != '') {
					jQuery('#tooltip_'+idx).html('<img src="'+allworks[id].thumbnails+'"/>').children(':first').load(function(){
						tooltipWidth = jQuery('#tooltip_'+idx).outerWidth();
						tooltipMargin = (tooltipWidth/2) - 7;
						jQuery('#tooltip_'+idx).css("margin-left", "-"+tooltipMargin+"px");
					});
				}
			}
		});
		jQuery('#slidePosition ul li a').mouseout(function(event) {
			var idx = parseInt(jQuery(this).attr('id').substr(5),10);
			jQuery('#tooltip_'+idx).html('');
		});
		
		jQuery('#logo').click(function(event) {
			var id = jQuery('#viewport ul li:eq(0)').attr('id');
			slideTo(id);
			linePositioning();
		});
		jQuery('#slideClose').click(function(event) {
			var id = jQuery('#viewport ul li:eq(0)').attr('id');
			slideTo(id);
			linePositioning();
		});
		
		for (i = 0; i < shownInViewport; i++) {
			jQuery(sliderList).css('height',(numElmts+shownInViewport)*increment + increment + "px");
			jQuery(sliderList).append(jQuery(elmnts[i]).clone());
		}
		
		jQuery(previous).click(function(event){
			linePositioning();
			if (!isAnimating) {
				// added: function to run before animation
				if (jQuery.isFunction(options.beforeAnimate)) {
					options.beforeAnimate(jQuery(sliderList).children()[firstElementOnViewPort-1]);
				}
				
				isAnimating = true;
				if (firstElementOnViewPort == 1) {
					jQuery(sliderList).css('top', "-" + numElmts * sizeFirstElmnt + "px");
					firstElementOnViewPort = numElmts;
				}
				else {
					firstElementOnViewPort--;
				}
				
				// slide position
				var workNumber =  firstElementOnViewPort > numElmts ? 1 : firstElementOnViewPort;
				if (workNumber <= 1 || workNumber > numElmts) {
					jQuery('#slidePosition').hide('fast');
					jQuery('#slideClose').hide('fast');
				} else {
					jQuery('#slidePosition').show('fast');
					jQuery('#slideClose').show('fast');
					jQuery('#slidePosition #work_'+workNumber).addClass('active');
				}
				
				jQuery(sliderList).animate({
					top: "+=" + increment,
					y: 0,
					queue: false // set to false otherwise double call
				}, "swing", function(){
					isAnimating = false;
					// added: function to run after animation
					if (jQuery.isFunction(options.afterAnimate)) {
						options.afterAnimate(jQuery(sliderList).children()[firstElementOnViewPort-1]);
					}
				});
			}
		});
		
		jQuery(next).click(function(event){
			linePositioning();
			if (!isAnimating) {
				// function to run before animation
				if (jQuery.isFunction(options.beforeAnimate)) {
					options.beforeAnimate(jQuery(sliderList).children()[firstElementOnViewPort-1]);
				}
				isAnimating = true;
				
				if (firstElementOnViewPort > numElmts) {
					firstElementOnViewPort = 2;
					jQuery(sliderList).css('top', "0px");
				}
				else {
					firstElementOnViewPort++;
				}
				
				// slide position
				var workNumber =  firstElementOnViewPort > numElmts ? 1 : firstElementOnViewPort;
				if (workNumber <= 1 || workNumber > numElmts) {
					jQuery('#slidePosition').hide('fast');
				} else {
					jQuery('#slidePosition').show('fast');
					jQuery('#slidePosition #work_'+workNumber).addClass('active');
				}
				
				jQuery(sliderList).animate({
					top: "-=" + increment,
					y: 0,
					queue: false // set to false otherwise double call
				}, "swing", function(){
					isAnimating = false;
					// added: function to run after animation
					if (jQuery.isFunction(options.afterAnimate)) {
						options.afterAnimate(jQuery(sliderList).children()[firstElementOnViewPort-1]);
					}
				});
			}
		});
		
		var slideTo = function(id) {
			// get work index to show
			var work_idx = 0;
			jQuery('#viewport ul li').each(function(idx) {
				if (work_idx == 0 && this.id == id) work_idx = idx + 1;
			});
			
			if (work_idx > 0 && work_idx != firstElementOnViewPort) {
				
				if (jQuery.isFunction(options.beforeAnimate)) {
					options.beforeAnimate(jQuery(sliderList).children()[firstElementOnViewPort-1]);
				}
				isAnimating = true;
				
				if (firstElementOnViewPort > numElmts) {
					firstElementOnViewPort = 1;
					jQuery(sliderList).css('top', "0px");
				}
				
				var work_diff = work_idx - firstElementOnViewPort;
				
				firstElementOnViewPort = work_idx;
				
				// slide position
				if (work_idx <= 1 || work_idx > numElmts) {
					jQuery('#slidePosition').hide('fast');
				} else {
					jQuery('#slidePosition').show('fast');
					jQuery('#slidePosition #work_'+work_idx).addClass('active').mouseout();
				}
				
				// TODO if all loop must be done, start to scroll to the end
				
				// animate to the element
				jQuery(sliderList).animate({
					top: "-=" + (work_diff * increment),
					y: 0,
					queue: false // set to false otherwise double call
				}, "swing", function(){
					isAnimating = false;
					// added: function to run after animation
					if (jQuery.isFunction(options.afterAnimate)) {
						options.afterAnimate(jQuery(sliderList).children()[firstElementOnViewPort-1]);
					}
				});
				
			}
		}
		
		if (options.startId != '') {
			slideTo(options.startId)
		}
		
	}
	return jQuery(this);
};