/*
Script Galerie Slide 3.0.0
2010 djm Web sprl

Need jQuery 1.3.2
*/
jQuery.noConflict();

class_cont_slide = function(vId,vWidthBox,vHeightBox,vPadding,vStepW,vStepH,vCol,vRow,vVitesse,vDuree,vAnime,vTypeAnime) {
	this.widthBox = vWidthBox;
	this.heightBox = vHeightBox;	
	this.padding = vPadding;	
	this.nCol = vCol;
	this.nRow = vRow;
	this.vitesse = vVitesse;
	this.stepW = vStepW;
	this.stepH = vStepH;
	
	this.count;
	this.countScreen;
	this.countVisi = this.nCol*this.nRow;
	this.pos = 0;	
	this.pos_temp=this.pos;
	this.where_screen = 0;
	
	this.flag_sens = 0;
	this.flag_click = 0;
	this.etatAuto = 0;

	this.contGal = '#'+vId; 
	
	this.Anime = vAnime;
	this.timeAuto;
	this.durAuto = vDuree*1000;
	
	this.img_mis = 0;
	this.nbre_box = 0;
	
	this.cont_cache = false;
	this.box_cont_step = false;
	
	//slide or fade
	this.vTypeAnime = 'slide';
	
	if (vTypeAnime!=null) {
		this.vTypeAnime=vTypeAnime;
		}
	
	this.timeFadeAuto;
	
	this.table_visi = new Array();
	
	return this.construct_cont_box();
	};
	
jQuery.extend(class_cont_slide.prototype, {
	InitContSlide: function() {
		this.get_cont();
		},
	dropOldCont: function () {
		jQuery(this.cont_box_slide).html('');
		this.pos = 0;
		jQuery(this.cont_box_slide).css('left',this.pos+'px');
		jQuery(this.cont_left).css('display','none');
		jQuery(this.cont_right).css('display','none');
		this.where_screen = 0;
		if (this.box_cont_step) {
			jQuery(this.box_cont_step).html('');
			}		
		},
	get_cont: function () {
		if (this.cont_box_slide) {
			this.dropOldCont();
			}		
		this.construct_cont();
		},
	construct_cont: function () {
		var obj = this;
		this.count = jQuery(this.contGal + ' div.content_slide').length;
		this.countScreen = Math.ceil(this.count/this.countVisi);
		
		jQuery(this.contGal + ' .cont_box_slide').css('width',(jQuery(this.contGal + ' .cont_cache').width()*this.countScreen)+'px');
		//savoir si plus que visible
		if (this.count>this.countVisi) {
			jQuery(this.contGal + ' .cont_right').css('display','block');
			}

		var vLink;
		var vContenu;
		var vNbre = 0;
		
		jQuery(this.contGal + ' div.content_slide').each(function() {
			if (jQuery(this).children('a').length >0) {
				vLink = jQuery(this).children('a').attr('href');
				}
				else {
					vLink = false;
					}
			vContenu = jQuery(this).html();			
			jQuery(this).css('display','none');
			obj.put_cont (vContenu, vLink, vNbre);
			vNbre++;
			});
		
		jQuery(document.createElement("div")).addClass('box_clear').appendTo(this.cont_box_slide);
		
		this.img_mis = 0;
		this.nbre_box = 0;
		
		this.contruct_step(this.countScreen);
		
		
		switch (this.vTypeAnime) {
			case 'slide':
				this.animation();
				break;
			case 'fade':
				this.modif_to_fade();
				this.animation_fade();
				break;
			}		
		},
	construct_cont_box: function() {
		var obj = this;
		if (!this.cont_cache) {
			//cont_box_left
			this.cont_box_left = document.createElement("div");
			jQuery(this.cont_box_left).addClass('cont_box_left').appendTo(this.contGal);
			jQuery(this.cont_box_left).css({
				'float' : 'left'
				});	
			
			//cont_left
			this.cont_left = document.createElement("div");
			jQuery(this.cont_left).addClass('cont_left').appendTo(this.cont_box_left);
			jQuery(this.cont_left).css({
				'display' : 'none',
				'cursor' : 'pointer'
				});
			jQuery(this.contGal + ' .cont_left').click(function () {
				if (obj.flag_click==0) {
					obj.etatAuto=1;					
					obj.move('prev');					
					}
				});
			
			//cont_cache
			this.cont_cache = document.createElement("div");
			jQuery(this.cont_cache).addClass('cont_cache').appendTo(this.contGal);
			jQuery(this.cont_cache).css({
				'float' : 'left',
				'white-space' : 'nowrap',
				'overflow' : 'hidden',
				'position' : 'relative',
				'width' : ((this.widthBox*this.nCol)+(this.padding*(this.nCol-1))) + 'px',
				'height' : ((this.heightBox*this.nRow)+(this.padding*(this.nRow-1))) + 'px'
				});	
			
			//cont_box_slide
			this.cont_box_slide = document.createElement("div");
			jQuery(this.cont_box_slide).addClass('cont_box_slide').appendTo(this.cont_cache);
			jQuery(this.cont_box_slide).css({
				'position' : 'relative',
				'left' : '0px',
				'height' : (jQuery(this.cont_cache).height()) + 'px'
				});
			
			//cont_box_right
			this.cont_box_right = document.createElement("div");
			jQuery(this.cont_box_right).addClass('cont_box_right').appendTo(this.contGal);
			jQuery(this.cont_box_right).css({
				'float' : 'left'
				});	
			
			//cont_right
			this.cont_right = document.createElement("div");
			jQuery(this.cont_right).addClass('cont_right').appendTo(this.cont_box_right);
			jQuery(this.cont_right).css({
				'display' : 'none',
				'cursor' : 'pointer'
				});
			jQuery(this.cont_right).click(function () {
				if (obj.flag_click==0) {
					obj.etatAuto=1;
					obj.move('next');
					}
				});
			
			//box_clear
			jQuery(document.createElement("div")).addClass('box_clear').appendTo(this.contGal);
			}
		},
	put_cont: function(vTxt, vLink, nbre) {
		if ((this.countVisi==this.img_mis) || (this.img_mis==0)) {
			this.nbre_box++;
			
			//box_visi
			this.box_visi = document.createElement("div");
			jQuery(this.box_visi).addClass('box_visi').appendTo(this.cont_box_slide);
			jQuery(this.box_visi).css({
				'float' : 'left',
				'overflow' : 'hidden',
				'width' : jQuery(this.cont_cache).width()+'px',
				'height' : jQuery(this.cont_cache).height()+'px'
				});
			
			this.table_visi[this.nbre_box-1] = this.box_visi;		
			this.img_mis = 0;
			}
		
		//cont_box
		this.cont_box = document.createElement("div");
		jQuery(this.cont_box).addClass('cont_box').appendTo(this.box_visi);
		jQuery(this.cont_box).css({
			'float' : 'left',
			'overflow' : 'hidden',
			'width' : this.widthBox+'px',
			'height' : this.heightBox+'px'
			});
		
		if (!(this.verif_numeric(this.img_mis/this.nCol))) {
			jQuery(this.cont_box).css('margin-left',this.padding+'px');
			}
		if (this.img_mis>=this.nCol) {
			jQuery(this.cont_box).css('margin-top',this.padding+'px');
			}
		
		if (vLink) {
			jQuery(this.cont_box).css('cursor','pointer');
			jQuery(this.cont_box).click(function() {
				document.location.href = vLink;
				});
			jQuery(this.cont_box).hover(
				function() {
					jQuery(this).fadeTo(0,0.80);
					},
				function() {
					jQuery(this).fadeTo(0,1);
					}
				);
			}
		
		jQuery(this.cont_box).html(vTxt);
		
		if ((this.img_mis==(this.countVisi-1)) || (nbre==(this.count-1))) {			
			jQuery(document.createElement("div")).addClass('box_clear').appendTo(this.box_visi);
			}
		
		this.img_mis++;
		},
	verif_numeric: function (variable) {
		var exp = new RegExp("^[0-9]+$","g");
		return exp.test(variable); 
		},
	contruct_step: function (nbre) {
		if (!this.box_cont_step) {
			this.box_cont_step = document.createElement('div');
			jQuery(this.box_cont_step).addClass('box_cont_step').appendTo(this.contGal);
			this.box_cont_step_center = document.createElement('div');
			jQuery(this.box_cont_step_center).addClass('box_cont_step_center').appendTo(this.box_cont_step);
			}

		jQuery(this.box_cont_step).css({
			'width' : ((this.widthBox*this.nCol)+(this.padding*(this.nCol-1)))+'px',
			'height' : this.stepH+'px'
			});
		
		jQuery(this.box_cont_step_center).css({
			'width' : (this.stepW*this.countScreen)+'px',
			'height' : this.stepH+'px',
			'margin-left' : 'auto',
			'margin-right' : 'auto'
			});
		
		this.box_step = new Array(nbre);
		
		var obj = this;
				
		for (var i=0;i<nbre;i++) {
			this.box_step[i] = document.createElement('div');
			jQuery(this.box_step[i]).addClass('box_step').appendTo(this.box_cont_step_center);
			jQuery(this.box_step[i]).css({
				'float' : 'left',
				'display' : 'block',
				'width' : this.stepW+'px',
				'height' : this.stepH+'px',
				'cursor' : 'pointer'
				});
			}
		
		jQuery(this.box_step).click(function () {
			obj.choice_step(jQuery(obj.box_step).index(this));
			});
			
		jQuery(document.createElement("div")).addClass('box_clear').appendTo(this.box_cont_step_center);
		
		this.step_sel(this.where_screen);
		},
	step_sel: function (lequel) {
		var box = this.box_step[lequel];
		
		for (i=0;i<this.box_step.length;i++) {
			if (i==lequel) {
				jQuery(this.box_step[i]).addClass('box_step_selected');
				}
				else {
					jQuery(this.box_step[i]).removeClass('box_step_selected');
					}
			}	
		},
	choice_step: function(lequel) {
		switch (this.vTypeAnime) {
			case 'slide':
				this.move_step(lequel);
				break;
			case 'fade':
				this.fade_step(lequel);
				break;
			}
		},
	move_step: function (lequel) {
		jQuery(this.cont_box_slide).stop();
		var newPos = (jQuery(this.cont_cache).width())*lequel;
		clearTimeout(this.timeAuto);
		this.pos = -newPos;
		this.pos_temp=0;
		this.flag_click=0;
		this.show_arrow();
		jQuery(this.cont_box_slide).css({
			'left' : this.pos+'px'
			});
		this.step_sel(lequel);
		this.animation();
		},
	move: function(direction) {
		switch (this.vTypeAnime) {
		  case 'slide':
		  	switch (direction) {
				  case 'prev':
				  	this.move_prev()
				  	break;
				  case 'next':
				  	this.move_next()
				  	break;
				  }
			  break;
		  case 'fade':
		 	switch (direction) {
				  case 'prev':
				  	this.fade_prev();
				  	break;
				  case 'next':
				  	this.fade_next();
				  	break;
				  }
			  break;
		  }	
		},
	move_next: function() {
		var obj = this;
		jQuery(this.cont_box_slide).stop();
		this.flag_click=1;
		if ((Math.abs(this.pos_temp)<jQuery(this.cont_cache).width()) && (Math.abs(this.pos)<(jQuery(this.cont_box_slide).width()-jQuery(this.cont_cache).width()))) {
			this.pos_temp = -(jQuery(this.cont_cache).width());
			this.pos -= jQuery(this.cont_cache).width();			
			jQuery(this.cont_box_slide).animate({
				'left' : this.pos+'px'
				},
				this.vitesse,
				'linear',
				function() {
					obj.pos_temp=0;
					obj.flag_click=0;
					obj.show_arrow();
					obj.where_screen=(Math.abs(obj.pos/jQuery(obj.cont_cache).width()));	
					obj.step_sel(obj.where_screen);
					});
			}
		},
	move_prev: function() {
		var obj = this;
		jQuery(this.cont_box_slide).stop();
		this.flag_click=1;
		if ((Math.abs(this.pos_temp)<jQuery(this.cont_cache).width()) && (Math.abs(this.pos)>0)) {
			this.pos_temp = (jQuery(this.cont_cache).width());
			this.pos += jQuery(this.cont_cache).width();			
			jQuery(this.cont_box_slide).animate({
				'left' : this.pos+'px'
				},
				this.vitesse,
				'linear',
				function() {
					obj.pos_temp=0;
					obj.flag_click=0;
					obj.show_arrow();
					obj.where_screen=(Math.abs(obj.pos/jQuery(obj.cont_cache).width()));	
					obj.step_sel(obj.where_screen);
					});
			}
		},
	move_auto: function() {
		var obj = this;
		if (this.etatAuto==0) {				
			if (this.flag_sens==0) {
				this.move_next();
				this.timeAuto = setTimeout(function() {obj.move_auto();},obj.durAuto);
				}
				else {
					this.move_prev();
					this.timeAuto = setTimeout(function() {obj.move_auto();},obj.durAuto);
					}
			}
			else {
				clearTimeout(this.timeAuto);
				}
		},
	show_arrow: function() {
		jQuery(this.cont_left).css('display','block');
		jQuery(this.cont_right).css('display','block');
		if (Math.abs(this.pos)==(jQuery(this.cont_box_slide).width()-jQuery(this.cont_cache).width())) {
			jQuery(this.cont_right).css('display','none');
			this.flag_sens=1;
			}
		if (Math.abs(this.pos)==0) {
			jQuery(this.cont_left).css('display','none');
			this.flag_sens=0;
			}
		},
	animation: function() {
		var obj = this;
		if ((this.countScreen > 1)&&(this.Anime == 1)) {
			this.timeAuto = setTimeout(function() {obj.move_auto();},obj.durAuto);
			}
		},
	animation_fade: function() {
		var obj = this;
		if ((this.countScreen > 1)&&(this.Anime == 1)) {
			this.timeFadeAuto = setTimeout(function() {obj.fade_auto();},obj.durAuto);
			}		
		},
	fade_step: function (lequel) {
		clearTimeout(this.timeFadeAuto);
		this.fade_stop();
		for (i=0;i<this.table_visi.length;i++) {
			jQuery(this.table_visi[i]).css({
				'display':'none',
				'opacity':0
				});
			}
		var newPos = (jQuery(this.cont_cache).width())*lequel;
		this.pos = -newPos;
		this.pos_temp=0;
		this.flag_click=0;
		this.show_arrow();		
		jQuery(this.table_visi[lequel]).css({
			'display':'block',
			'opacity':1
			});
		this.where_screen=lequel;
		this.step_sel(lequel);
		this.animation_fade();
		},
	modif_to_fade: function() {
		for (i=0;i<this.table_visi.length;i++) {
			jQuery(this.table_visi[i]).css({
				'z-index':this.table_visi.length-i,
				'position':'absolute',
				'display':'none',
				'opacity':0
				});
			}
		jQuery(this.table_visi[0]).css({
			'display':'block',
			'opacity':1
			});
		},
	fade_next: function() {
		var obj = this;
		this.fade_stop();
		this.flag_click=1;
		if ((Math.abs(this.pos_temp)<jQuery(this.cont_cache).width()) && (Math.abs(this.pos)<(jQuery(this.cont_box_slide).width()-jQuery(this.cont_cache).width()))) {
			this.pos_temp = -(jQuery(this.cont_cache).width());
			this.pos -= jQuery(this.cont_cache).width();
			jQuery(obj.table_visi[obj.where_screen+1]).css('display','block');
			jQuery(obj.table_visi[obj.where_screen+1]).animate({
				'opacity' : 1
				},
				this.vitesse,
				'linear',
				function() {});
			jQuery(obj.table_visi[obj.where_screen]).animate({
				'opacity' : 0
				},
				this.vitesse,
				'linear',
				function() {
					jQuery(obj.table_visi[obj.where_screen]).css('display','none');
					obj.pos_temp=0;
					obj.flag_click=0;
					obj.show_arrow();
					obj.where_screen=(Math.abs(obj.pos/jQuery(obj.cont_cache).width()));
					obj.step_sel(obj.where_screen);
					});
			}
		},
	fade_prev: function() {
		var obj = this;
		this.fade_stop();
		this.flag_click=1;
		if ((Math.abs(this.pos_temp)<jQuery(this.cont_cache).width()) && (Math.abs(this.pos)>0)) {
			this.pos_temp = (jQuery(this.cont_cache).width());
			this.pos += jQuery(this.cont_cache).width();
			jQuery(obj.table_visi[obj.where_screen]).animate({
				'opacity' : 0
				},
				this.vitesse,
				'linear',
				function() {
					jQuery(obj.table_visi[obj.where_screen]).css('display','none');
					});
			jQuery(obj.table_visi[obj.where_screen-1]).css('display','block');
			jQuery(obj.table_visi[obj.where_screen-1]).animate({
				'opacity' : 1
				},
				this.vitesse,
				'linear',
				function() {
					obj.pos_temp=0;
					obj.flag_click=0;
					obj.show_arrow();
					obj.where_screen=(Math.abs(obj.pos/jQuery(obj.cont_cache).width()));	
					obj.step_sel(obj.where_screen);
					});
			}
		},
	fade_auto: function() {
		var obj = this;
		if (this.etatAuto==0) {				
			if (this.flag_sens==0) {
				this.fade_next();
				this.timeFadeAuto = setTimeout(function() {obj.fade_auto();},obj.durAuto);
				}
				else {
					this.fade_prev();
					this.timeFadeAuto = setTimeout(function() {obj.fade_auto();},obj.durAuto);
					}
			}
			else {
				clearTimeout(this.timeFadeAuto);
				}
		},
	fade_stop: function() {
		for (i=0;i<this.table_visi.length;i++) {
			jQuery(this.table_visi[i]).stop();
			}
		}
	});