/**
 * Gestion d'un caroussel avec JQuery
 *
 * Permet de gérer un systeme de caroussel en 3D
 *
 * @author bbroussolle
 * @since 2009-10-22
 * @requires jquery
 * @copyrights SQLI 2009
 */
(function($) {

	$.fn.caroussel = function (params){
		var master_box = $(this);
		
		var returns = new Array();
		arrowClick = false;
		master_box.each(function(){
			master_obj = $(this);
			var obj	= {
				constructor	: function(){
					this.saveParams(this.defaults);
					this.saveParams(params);
					this.convertHTML();
					master_obj = master_obj.blockImage({osT:this.params.osT, osL:this.params.osL, W:this.params.W,H:this.params.H,iH:this.params.iH, iW:this.params.iW, cp:this.params.cp,p:this.params.p, s:this.params.s})
					this.clickL();
					this.clickR();
					return obj;
				},
				defaults	: {btnL: $('#gauche'), btnR:$('#droite'), osT:0, osL:0, W:200, H:100, iH:110, iW:110, cp:300, p:2, s:1500},
				params		: {},
				saveParams	: function(newParams){this.params = $.extend(this.params, newParams);},
				clickL		: function(){
					var e = this;
					this.params.btnL.click(function(){
						if($(':animated').length == 0 ){
							$(this).unbind('click');
							arrowClick = true;
							master_obj.carouselMove('gauche',e.clickL());
						}
						return false
						
					});
				},
				clickR		: function(){
					var e = this;
					this.params.btnR.click(function(){
						if($(':animated').length == 0 ){	
							$(this).unbind('click');
							arrowClick = true;
							master_obj.carouselMove('droite',e.clickR());
						}
						return false
					});
				},
				convertHTML	: function(){
					var listElement = master_obj.children('ul').children('li').clone();
					master_obj.html('');

					listElement.each(function(){
						if($(':animated').length == 0 )return false;
						var child = $(this).children()
						var html = null;
						switch(child[0].tagName) {
							case 'A' :
								html = child.html();
								html=$(html);
								html.data('imgSrc', {
									name:html.attr('alt'), 
									normal: html.attr('src'), 
									hover:$(child.get(1)).val(), 
									lien:child.attr('href'), 
									textVoiture:$(child.get(2)).val(), 
									big:$(child.get(3)).val()
								});
								html.click(function(event){
									// chargement du grand visuel
									var big = $(this).data('imgSrc').big;
									var name = $(this).data('imgSrc').name;
									var textVoiture = $(this).data('imgSrc').textVoiture;
									var imgs = $(this).parent().children('img');
									var bigOne = false; 
									var bigfirst = false; 
									var clicked =false;
									var move = 0;
									for(var i=0; i<imgs.length; i++){
										if (parseInt($(imgs.get(i)).css('height').split('px')[0] ) == $(this).parent().data('carroussel').params.iH){
											bigOne = true;
											if(!clicked)bigfirst=true;
										}
										if(imgs.get(i) == this) clicked=true;
										if((bigOne && !clicked) || (!bigOne && clicked)) move+=1;
										if(bigOne && clicked) break;
									}
									if (move !=0){
										jQuery('#imgBlock #voiture').css('visibility', 'hidden');
										jQuery('#text_voiture').css('visibility', 'hidden');
										if (move > imgs.length/2)move=(move-imgs.length)*(bigfirst?-1:1);
										else move=(move)*(bigfirst?-1:1);
										$(this).parent().data('blockImage').carouselMove('gauche',function(){},move);
									}
									jQuery('#imgBlock #voiture img').attr('src', big);
									jQuery('#imgBlock #voiture img').attr('alt', name);
									jQuery('#imgBlock #voiture').stop().css({visibility:'visible', opacity:0}).delay(100).animate({opacity:1}, 2000);
									jQuery('#text_voiture').css('background', 'url('+textVoiture+') no-repeat center 0');
									jQuery('#text_voiture').hover(
											function(event){jQuery(this).css('background-position', 'center -18px');},
											function(event){jQuery(this).css('background-position', 'center 0');}
									);
									jQuery('#text_voiture').stop().css({visibility:'visible', opacity:0}).animate({opacity:1}, 2000);
									
									jQuery('#imgBlock #voiture').data('url',$(this).data('imgSrc').lien)
									jQuery('#imgBlock #voiture').unbind("click");
									jQuery('#imgBlock #voiture').bind("click", function(event){
										document.location = jQuery(this).data('url') ;
									});
									
									jQuery('#infoBloc #text_voiture').data('url',$(this).data('imgSrc').lien)
									jQuery('#infoBloc #text_voiture').unbind("click");
									jQuery('#infoBloc #text_voiture').bind("click", function(event){
										document.location = jQuery(this).data('url') ;
									});
									
									return false;
								});
								
								html.mouseenter(function(event){
									$(this).attr('src', $(this).data('imgSrc').hover);
								})
								html.mouseleave(function(event){
									$(this).attr('src', $(this).data('imgSrc').normal);
								})
								break;
						}
						var image = html;
						image.css({position:'absolute'});
						master_obj.append(image);
					});
				}
			};
			master_obj.data('carroussel', obj.constructor());
			returns.push(master_obj);
		});

		return returns;
	};
})(jQuery);
