// JavaScript Document

(function($) {
	$.castarprod = function(data, klass) {
		$.castarprod.loading()
	}
	
	$.fn.fullstop = function(settings) {
		$.castarprod.fullstop()
	}


   $.extend($.castarprod, { 
		settings: {
			topElement: 0,
			speed: 3,
			nbElement: 20,
			posMiniatures: 0,
			posInfos: 0,
			sens: 1,
			currentElement: 0,
			scrollup: '#up',
			scrolldown: '#down',
			scroller: '#scroller',
			master: '.artiste',
			mini: '#miniature',
			bouton: '.hiddenButton',
			timerAuto: null,
			playing: false,
			playingMorceau: false
		},
		
		loading: function(){
			this.settings.nbElement = $('#listeMiniatures li').size();
			this.initHiddenBouton();
			this.initScrollbars();
			this.prepareAutomate();
		},
		
		initScrollbars: function(){
			var thisObj = this;
			$('#scroller').css({'height': 260/(this.settings.nbElement/5)+'px'});
			this.settings.posInfos = parseFloat($('#listeArtistes').css('marginTop'));
			this.settings.posScroller = parseFloat($('#scroller').css('top'));
			$('#bottomScroll').css({'bottom' : 0});
			$('#up').mousedown(function(){
				thisObj.slideMiniaturesUp();					
			});
			$('#down').mousedown(function(){
				thisObj.slideMiniaturesDown();					
			});
			$('img#up, img#down').hover(function(){
				$(this).css({'opacity':0.5});
			}, function(){
				$(this).css({'opacity':1});
			});
			this.initScroller();
		},
		
		prepareAutomate: function(){
			var thisobj = this;
			$('#Artiste0').fadeIn(1);
			$('#miniature0').attr({'className':'hover'});
			$('div.description_artiste').hover(function(){ thisobj.stop(); }, function(){ if(!thisobj.playingMorceau) thisobj.automatise(); });
			this.automatise();
		},
		
		automatise: function(){
			obj = this;
			if(!this.settings.playing)
				this.settings.timerAuto = setTimeout(function() { obj.nextStep(obj); }, 4000);
			this.settings.playing = true;
			this.playingMorceau = false;
		},
		
		stop: function(){
			clearTimeout(this.settings.timerAuto);
			this.settings.playing = false;
		},
		
		fullstop: function(){
			clearTimeout(this.settings.timerAuto);
			this.settings.playing = false;
			this.playingMorceau = true;
		},
		
		nextStep: function(obj){
			if(obj.settings.sens==1) {
				if(obj.settings.currentElement==(obj.settings.nbElement-1))
					obj.settings.sens = -1;
				if(((parseInt(obj.settings.currentElement) + obj.settings.sens)%5)==0)

            		obj.slideMiniaturesDownFor(5);
			}
			else {
				if(obj.settings.currentElement==0)
					obj.settings.sens = 1;
				if((parseInt(obj.settings.currentElement) + obj.settings.sens)==(parseInt(obj.settings.topElement)-1))

            		obj.slideMiniaturesUpFor(5);
			}
			obj.settings.currentElement = parseInt(obj.settings.currentElement) + obj.settings.sens;
			obj.annimateChangeTo($('#miniature'+(obj.settings.currentElement)));
			obj = this;
			this.settings.timerAuto = setTimeout(function() { obj.nextStep(obj); }, 4000);
		},
		
		initScroller: function(){
			var startPos, startTop;
			var thisObj = this;
			$('#scroller').mousedown(function(event){  
				startPos = event.pageY;
				startTop = parseFloat($(this).css('top'));
				$(this).css({'opacity': 0.5});
				thisObj.stop();
			});
			$('#scroller').bind('drag', function(event){
				var newTop = startTop + (event.pageY-startPos);
				if(newTop<0)
					newTop = 0;
				else if(newTop>parseFloat($('#scrollerblock').css('height'))-parseFloat($(this).css('height')))
					newTop = parseFloat($('#scrollerblock').css('height'))-parseFloat($(this).css('height'));
				$(this).css({'top': newTop });
				var decalage = Math.round((newTop-startTop)/(260/thisObj.settings.nbElement)); 
				if(decalage>0)
					thisObj.slideMiniaturesDownTo(decalage);
				else
					thisObj.slideMiniaturesUpTo(decalage);
			}).bind('dragend', function(event){
				$(this).css({'opacity': 1});
				thisObj.settings.posScroller = parseFloat($(this).css('top'));
				var oneElement = (260/thisObj.settings.nbElement);
				var scrollingMove = thisObj.settings.posScroller-startTop;
				var decalage = Math.round(scrollingMove/oneElement);
				thisObj.settings.posMiniatures = parseFloat($('#listeMiniatures').css('marginTop'));
				if((decalage + thisObj.settings.topElement)>(thisObj.settings.nbElement-5))
					decalage = (thisObj.settings.nbElement-5) - thisObj.settings.topElement;
				else if((decalage + thisObj.settings.topElement)<0)
					decalage = 0 - thisObj.settings.topElement;
				thisObj.settings.topElement = parseInt(decalage) + parseInt(thisObj.settings.topElement);
				thisObj.settings.currentElement = parseInt(decalage) + parseInt(thisObj.settings.currentElement);
				thisObj.changeTo($('#miniature'+thisObj.settings.currentElement));
				thisobj.automatise();
			});
		},
		
		slideMiniaturesUpTo: function(decalage){
			if((decalage + this.settings.topElement)<0)
				decalage = 0 - this.settings.topElement;
			var toMove = 58; 
			var posMiniaturesTemp = this.settings.posMiniatures + decalage * toMove * -1;
			$('#listeMiniatures').css({'marginTop': posMiniaturesTemp});
		},
		
		slideMiniaturesDownTo: function(decalage){
			if((decalage + this.settings.topElement)>(this.settings.nbElement-5))
				decalage = (this.settings.nbElement-5) - this.settings.topElement;
			var toMove = 58; 
			var posMiniaturesTemp =  this.settings.posMiniatures + decalage * toMove * -1;
			$('#listeMiniatures').css({'marginTop': posMiniaturesTemp});
		},
		
		slideMiniaturesUp: function(){
			if(this.settings.topElement==0)
				return;
			this.stop();
			--this.settings.topElement;
			if(this.settings.topElement==0)
				this.settings.posScroller = 0;
			else
				this.settings.posScroller -= (260/this.settings.nbElement);
			$('#scroller').css({'top': this.settings.posScroller });
			var toMove = 58; 
			this.settings.posMiniatures = this.settings.topElement * toMove * -1;
			thisObj = this;
			$('#listeMiniatures').animate({'marginTop':this.settings.posMiniatures}, 1, function(){ thisObj.automatise(); });
			if(this.settings.currentElement==(this.settings.topElement+5))
				this.changeTo($('.hover').prev());
		},
		
		slideMiniaturesDown: function(){
			if(this.settings.topElement+5>=this.settings.nbElement)
				return;
			this.stop();
			++this.settings.topElement;
			this.settings.posScroller += (260/this.settings.nbElement);
			if(this.settings.posScroller>(260-parseFloat($('#scroller').css('height'))))
				this.settings.posScroller = 260-parseFloat($('#scroller').css('height'));
			$('#scroller').css({'top': this.settings.posScroller });
			var toMove = 58; 
			this.settings.posMiniatures = this.settings.topElement * toMove * -1; 
			thisObj = this;
			$('#listeMiniatures').animate({'marginTop':this.settings.posMiniatures}, 1, function(){ thisObj.automatise(); });
			if(this.settings.currentElement==(this.settings.topElement-1))
				this.changeTo($('.hover').next());
		},
		
		slideMiniaturesUpFor: function(nb){
			if(this.settings.topElement == 0)
				return;
			if(parseInt(this.settings.topElement) - nb<0)
				nb = (0 - parseInt(this.settings.topElement))*-1;
			this.settings.topElement = parseInt(this.settings.topElement) - nb;
			if(this.settings.topElement == 0)
				this.settings.posScroller = 0;
			else
				this.settings.posScroller -= (260/this.settings.nbElement) * nb;
			$('#scroller').css({'top': this.settings.posScroller });
			var toMove = 58; 
			this.settings.posMiniatures = this.settings.topElement * toMove * -1;
			$('#listeMiniatures').animate({'marginTop':this.settings.posMiniatures}, 800);
			//if(this.settings.currentElement == (this.settings.topElement+5))
				//this.changeTo($('.hover').parent().prev());
		},
		
		slideMiniaturesDownFor: function(nb){
			if(this.settings.topElement+5>=this.settings.nbElement)
				return;
			if(parseInt(this.settings.topElement)+5+nb>=this.settings.nbElement)
				nb =  this.settings.nbElement - parseInt(this.settings.topElement) - 5;
			this.settings.topElement = parseInt(this.settings.topElement) + nb;
			this.settings.posScroller += (260/this.settings.nbElement) * nb;
			if(this.settings.posScroller > (260-parseFloat($('#scroller').css('height'))))
				this.settings.posScroller = 260-parseFloat($('#scroller').css('height'));
			$('#scroller').css({'top': this.settings.posScroller });
			var toMove = 58; 
			this.settings.posMiniatures = this.settings.topElement * toMove * -1; 
			$('#listeMiniatures').animate({'marginTop': this.settings.posMiniatures}, 800);
			//if(this.settings.currentElement == (this.settings.topElement-1))
				//this.changeTo($('.hover').parent().next());
		},
		
		initHiddenBouton: function(){
			var thisObj = this;
			$('a.nofollow').click(function(){ return false; });
			$('a.hiddenButton').click(function(){
				thisObj.changeTo($(this).parent());
				return false;
			});
			$('a.hiddenButton').hover(function(){ 
				$(this).parent().attr('className', 'on') 
			}, function(){ 
				$(this).parent().attr('className', '')
			});
			
		},
		
		changeTo: function(obj){
			var from = $('.hover').attr('id').replace('miniature', '');
			var to = $(obj).attr('id').replace('miniature', '');
			$('#'+$(obj).attr('id')+' a.hiddenButton')
						.appendTo($('.hover'))
						.css('opacity', 0)
						.hover(function(){ $(this).parent().attr('className', 'on') }, function(){ $(this).parent().attr('className', '')});
			$('.hover').attr({'className':''});
			$(obj).attr({'className':'hover'});
			$('#'+$(obj).attr('id')+' a.hiddenButton').remove();
			this.moveInfos(from, to);
			this.stop();
			this.automatise();
		},
		
		annimateChangeTo: function(obj){
			var from = $('.hover').attr('id').replace('miniature', '');
			var to = $(obj).attr('id').replace('miniature', '');
			$('#'+$(obj).attr('id')+' a.hiddenButton').appendTo($('.hover')).css('opacity', 0).hover(function(){ $(this).parent().attr('className', 'on') }, function(){ $(this).parent().attr('className', '')});
			$('.hover').attr({'className':''});
			$(obj).attr({'className':'hover'});
			$('#'+$(obj).attr('id')+' a.hiddenButton').remove();
			this.moveInfos(from, to);
		},
		
		moveInfos: function(from, to){
			this.settings.currentElement = to;
			this.settings.posInfos -= (to-from)*391;
			var thisobj = this;
			$('#presentationArtistes div.artiste[id!=Artiste'+from+']').css({'display': 'none'});
			setFullPlayers(false, '#Artiste'+from+' .fullPlayer', false)
			$('#Artiste'+from).fadeOut(1000);
			$('#Artiste'+to).fadeIn(1000);
			//stopPlayers();
		}
	})

   $.fn.castarprod = function(settings) {
    	$.castarprod.loading();
   }

})(jQuery);
			
