/**
 * Perspective - jQuery plugin
 * Version 0.9 - Last edit : 21.10.2010
 * @author VIUU - France
 * http://viuu.fr
 * Copyright (c) 2010.
 */

jQuery.fn.perspective = function(options){
	
	var defaults = {
		scrollingSpeed : 5000,
		slidingSpeed : 800,
		depth : 20,
		flightPoint : 'left',
		timerPers : 'show',
		playPers : 'show'
	};
	
	var opts = jQuery.extend(defaults, options);
	
	var loadCount = 0;
	var moving = false;
	
	var thatPers = $(this);
	var persDiap = thatPers.children('div');
	
	var nbrDiap = $('div', this).length;
	var nbrDiapPers = nbrDiap - 1;
	
	var posPause = $('.pause_rota').position();
	var posTimer = $('.timer_rota').position();
	
	var widthPers = thatPers.width();
	var heightPers = thatPers.height();
			
	var widthDiap = persDiap.width();
	var heightDiap = persDiap.height();
	
	var widthImg = $('img', persDiap).width();
	var heightImg = $('img', persDiap).height();
	
	var differencePersHoriz = widthPers - widthDiap;
	var decalDiapHoriz = differencePersHoriz / nbrDiapPers;
	
	var differencePersVertic = heightPers - heightDiap;
	var decalDiapVertic = differencePersVertic / nbrDiapPers;
	
	if ((opts.flightPoint == 'left')||(opts.flightPoint == 'right')) {
		var rapportDecal = heightDiap / opts.depth;
		var otherDepth = widthDiap / rapportDecal;
	}
	
	if ((opts.flightPoint == 'top')||(opts.flightPoint == 'bottom')) {
		var rapportDecal = widthDiap / opts.depth;
		var otherDepth = heightDiap / rapportDecal;
	}
	
	var depthMax = heightPers / nbrDiap;
	if (depthMax < opts.depth) {
		opts.depth = depthMax;
	}


	for (i = 0; i < nbrDiap; i++) {
		
		var backgroundImg = '<em></em>'
		
		if ((opts.flightPoint == 'left')||(opts.flightPoint == 'right')) {
			var heightDiapPers = heightDiap + (nbrDiapPers - i) * -opts.depth;
			var posTopDiap = (nbrDiapPers - i)*opts.depth/2;
			var widthDiapPers = widthDiap + (nbrDiapPers - i) * -otherDepth;
		}
		if ((opts.flightPoint == 'top') || (opts.flightPoint == 'bottom')) {
			var posLeftDiap = (nbrDiapPers - i)*opts.depth/2;
			var heightDiapPers = heightDiap + (nbrDiapPers - i) * -otherDepth;
			var widthDiapPers = widthDiap + (nbrDiapPers - i) * -opts.depth;
		}
		if (opts.flightPoint == 'left') {
			var posLeftDiap = i * decalDiapHoriz;
		}
		if (opts.flightPoint == 'top') {
			var posTopDiap = i * decalDiapVertic;
		}
		if (opts.flightPoint == 'right') {
			var posLeftDiap = (nbrDiapPers - i) * decalDiapHoriz + otherDepth * (nbrDiapPers - i);
		}
		if (opts.flightPoint == 'bottom') {
			var posTopDiap = (nbrDiapPers - i) * decalDiapVertic + otherDepth * (nbrDiapPers - i);
		}
		
		thatPers.find('div:eq('+ i +')').css({
			left : posLeftDiap,
			height : heightDiapPers,
			top : posTopDiap,
			width : widthDiapPers
		}).attr('tabindex', nbrDiapPers - i);
		
		thatPers.find('div:eq('+ i +')').append(backgroundImg);
		
		thatPers.find('div:eq('+ i +')').find('img, em').css({
			height : heightDiapPers,
			width : widthDiapPers
		});
		thatPers.find('div:eq('+ i +')').find('em').css({
			opacity : 0.5 - i  * (0.5 / nbrDiapPers)
		});
	}
	
	jQuery.fn.persRota = function(cibleRank){
		clearInterval(autoRota);
		$('.timer_rota').hide();
		if (opts.timerPers != 'no') {
			clearInterval(timerRota);
		}
		var currentCount = -1;
		
		persDiap.each(function(){
			currentCount++;
			
			var currentDiap = thatPers.find('div:eq(' + currentCount + ')');
			var diapSpeed = opts.slidingSpeed / cibleRank;
			var lastDiapSpeed = opts.slidingSpeed / 2 / cibleRank;
			
			if ((opts.flightPoint == 'left')||(opts.flightPoint == 'right')) {
				var posDiap = currentDiap.position().left;
			}
			if ((opts.flightPoint == 'top')||(opts.flightPoint == 'bottom')) {
				var posDiap = currentDiap.position().top;
			}
			
			if (currentCount < nbrDiapPers) {
				if ((opts.flightPoint == 'left')||(opts.flightPoint == 'right')) {
					var heightDiapPers = '+=' + opts.depth;
					var posTopDiap = '-=' + opts.depth/2;
					var widthDiapPers = '+=' + otherDepth;
				}
				if ((opts.flightPoint == 'top') || (opts.flightPoint == 'bottom')) {
					var posLeftDiap = '-=' + opts.depth/2;
					var heightDiapPers = '+=' + otherDepth;
					var widthDiapPers = '+=' + opts.depth;
				}
				if (opts.flightPoint == 'left') {
					var posLeftDiap = posDiap + decalDiapHoriz;
				}
				if (opts.flightPoint == 'top') {
					var posTopDiap = posDiap + decalDiapVertic;
				}
				if (opts.flightPoint == 'right') {
					var posLeftDiap = posDiap - decalDiapHoriz - otherDepth;
				}
				if (opts.flightPoint == 'bottom') {
					var posTopDiap = posDiap - decalDiapVertic - otherDepth;
				}
				
				currentDiap.animate({left : posLeftDiap, height : heightDiapPers, top : posTopDiap, width : widthDiapPers}, diapSpeed).attr('tabindex', nbrDiapPers - currentCount - 1);
				currentDiap.find('em').css({opacity : 0.5 - (currentCount + 1)  * (0.5 / nbrDiapPers)}, diapSpeed);
				currentDiap.find('img, em').animate({height : heightDiapPers, width : widthDiapPers}, diapSpeed);
				if (currentCount == nbrDiapPers - 1) {
					currentDiap.find('p').delay(diapSpeed).fadeIn(lastDiapSpeed);
				}
			}
			else {
				currentDiap.find('p').fadeOut(lastDiapSpeed);
				
				if ((opts.flightPoint == 'left') || (opts.flightPoint == 'right')) {
					var posLeftDiap = '+=' + decalDiapHoriz;
					var heightDiapPers = heightDiap + nbrDiapPers * -opts.depth;
					var posTopDiap = nbrDiapPers*(opts.depth/2);
					var widthDiapPers = widthDiap + nbrDiapPers * -otherDepth;
					var fadeTop = 0;
					var preTopDiap = posTopDiap;
					var preLeftDiap = decalDiapHoriz;
					var topBack = posTopDiap;
				}
				if ((opts.flightPoint == 'top') || (opts.flightPoint == 'bottom')) {
					var posLeftDiap = nbrDiapPers*(opts.depth/2);
					var heightDiapPers = heightDiap + nbrDiapPers * -otherDepth;
					var posTopDiap = '+=' + decalDiapVertic;
					var widthDiapPers = widthDiap + nbrDiapPers * -opts.depth;
					var fadeLeft = 0;
					var preTopDiap = decalDiapVertic;
					var preLeftDiap = posLeftDiap;
					var leftBack = posLeftDiap;
					
				}
				if (opts.flightPoint == 'left') {
					var fadeLeft = posLeftDiap;
					var leftBack = 0;
				}
				if (opts.flightPoint == 'top') {
					var fadeTop = posTopDiap;
					var topBack = 0;
				}
				if (opts.flightPoint == 'right') {
					var fadeLeft = -posLeftDiap;
					var leftBack = nbrDiapPers * decalDiapHoriz + nbrDiapPers * otherDepth;
				}
				if (opts.flightPoint == 'bottom') {
					var fadeTop = -posTopDiap;
					var topBack = nbrDiapPers * decalDiapVertic + nbrDiapPers * otherDepth;
				}
				
				currentDiap.animate({left : fadeLeft, top : fadeTop, opacity : 0}, lastDiapSpeed, function(){
					currentDiap.find('img, em').css({opacity : 0.5});
					currentDiap.find('img').css({opacity : 1});
					$('img, em', this).css({height : heightDiapPers, width : widthDiapPers});
					
					$(this).prependTo(thatPers).css({left : preLeftDiap, top : preTopDiap, height : heightDiapPers, width : widthDiapPers, opacity : 1})
					.attr('tabindex', nbrDiapPers)
					.animate({left: leftBack, top : topBack}, lastDiapSpeed, function(){
						cibleRank--;
						if (cibleRank > 0) {
							$(this).persRota(cibleRank);
						}
						else {
							moving = false;
							if (! $(thatPers).hasClass('enPause')) {
								$(thatPers).autoRotaPers();
								if (opts.timerPers != 'no') {
									$('.timer_rota').find('span').css({top: 0});
									$('.timer_rota').show().timerRotaPers();
								}
							}
						}
					});
				});
			}
		});
	};
	
	jQuery.fn.timerRotaPers = function(){
		timerRota=setInterval(function(){
			$('.timer_rota').find('span').animate({top : '-=24'},0);
		},opts.scrollingSpeed/24);
	};
	
	jQuery.fn.autoRotaPers = function(){
		autoRota=setInterval(function(){
			if (moving == false) {
				var cibleRank = 1;
				moving = true;
				$(this).persRota(cibleRank);
			}
		},opts.scrollingSpeed);
	};
	
	
	$(this).find('div').click(function(){
		var cibleRank = $(this).attr('tabindex');
		if ((moving == false)&&(cibleRank != 0)) {
			moving = true;
			clearInterval(autoRota);
			$(this).persRota(cibleRank);
		}
	});
	
	thatPers.find('div p').hide().filter('div:last p').show();
	
	if (opts.timerPers != 'no') {
		$('.timer_rota').timerRotaPers();
	}
	
	$(thatPers).autoRotaPers();
	
	if (opts.playPers == 'hide') {
		$('.pause_rota').hide();
		$(thatPers).hover(function(){
			$('.pause_rota').fadeIn(200);
		}, function(){
			$('.pause_rota').fadeOut(200);
		});
	}
	if (opts.timerPers == 'hide') {
		$('.timer_rota').hide().addClass('timerHidden');
		$(thatPers).hover(function(){
			$('.timer_rota').fadeIn(200);
		}, function(){
			$('.timer_rota').fadeOut(200);
		});
	}
	
	$('.pause_rota').live('click', function(){
		$('.timer_rota').find('span').css({top : 34});
		if (opts.timerPers != 'no') {
			clearInterval(timerRota);
		}
		clearInterval(autoRota);
		$(this).css({left : posPause.left, top : posPause.top}).animate({left : posPause.left + (posTimer.left - posPause.left), top : posPause.top + (posTimer.top - posPause.top)},200).attr('class', 'lecture_rota');
		$(thatPers).addClass('enPause');
	});
	
	$('.lecture_rota').live('click', function(){
		$('.timer_rota').find('span').css({top : 0});
		if (opts.timerPers != 'no') {
			$('.timer_rota').show().timerRotaPers();
		}
		$(thatPers).autoRotaPers();
		$(this).animate({left : posPause.left, top : posPause.top},200).attr('class', 'pause_rota');
		$(thatPers).removeClass('enPause');
	});
	
	
	$('div', thatPers).hover(function(){
		$('em', this).animate({opacity : 0},200);
		if ($(this).attr('tabindex') != 0) {
			var posTool = $(this).position();
			var toolTipContent = $('p', this).text();
			if (toolTipContent != '') {
				thatPers.append('<span class="toolPers">' + toolTipContent + '</span>');
				$('.toolPers').css({left : posTool.left, top : posTool.top});
			}
				
		}
			
		
		
	},function(){
		var emIndex = $(this).attr('tabindex');
		$('em', this).stop(true, true).animate({opacity : emIndex  * (0.5 / nbrDiapPers)},200);
		$('.toolPers').remove();
	});
};
