/* prepare */
var indexGal = {
	init : function(){
		maxSize=0;
		img=1;
		maxImg = 0;
		jQuery(".container img").each(function(i){
			i++;
			jQuery(this).attr("id","img"+(i));
			maxSize +=  jQuery(this).width() + 4;
			maxImg++;
		});
		maxSize += jQuery("#img1").width() + jQuery("#img"+maxImg).width() + 8;
		jQuery("#slider .container").css("width",maxSize);
		jQuery("#slider .container").prepend('<img class="first" src="'+jQuery("#img"+maxImg).attr("src")+'" />');
		imgW = jQuery("img.first").width() + 4 + ((jQuery("#img"+img).width() + 4)/2);
		chiffre = (836/2) - imgW;
		this.slideTo(chiffre);
		jQuery("#slider a.prev").click(this.previousClick);
		jQuery("#slider a.next").click(this.nextClick);
		this.checkImg();
	},
	reinit : function(){
		img=1;
		imgW = jQuery("img.first").width() + 4 + ((jQuery("#img"+img).width() + 4)/2);
		chiffre = (836/2) - imgW;
		this.slideTo(chiffre);
		jQuery("#slider a.prev").click(this.previousClick);
		jQuery("#slider a.next").click(this.nextClick);
		this.checkImg();
	},
	setImage : function(imgToSet,sens) {
		e = jQuery("#slider .container");
		if (!this.checkQueue(e)) return false;
		img = imgToSet;
		this.checkImg();
		if (e.css("left"))
			leftNum = e.css("left");
		else
			leftNum = 0;
		leftNum = leftNum.substring(0,leftNum.indexOf("px"));
		imgW = jQuery("#img"+img).width();
		if (sens=="prev") {
			imgW2 = jQuery("#img"+(img+1)).width();
			chiffre = parseFloat(leftNum) + 2 + parseFloat(imgW2/2 + 2) + parseFloat(imgW/2 + 2);
		}
		else {
			imgW2 = jQuery("#img"+(img-1)).width();
			chiffre = Math.ceil(leftNum) + 2 - Math.ceil(imgW2/2 + 2) - Math.ceil(imgW/2 + 2);
		}
		this.slideTo(chiffre);
	},
	slideTo : function(pixel) {
		jQuery("#slider .container").animate({left: pixel}, 1500 );
	},
	previousClick : function() {
		indexGal.setImage(img-1,"prev");return false;
	},
	nextClick : function() {
		indexGal.setImage(img+1,"next");return false;
	},
	checkImg : function() {
		if (img == 1) {
			jQuery("#slider a.prev").hide();
		}
		if (img > 1 && img == maxImg) {
			lol=jQuery("#slider .base").html();
			newImg = jQuery("#slider .base img").length + 1;
			width=jQuery("#slider .container").width();
			maxImg=0;
			jQuery("#slider .container").width(width+maxSize).append('<div class="base">'+lol+'</div>').find("img").each(function(i){
				i++;
				jQuery(this).attr("id","img"+(i));
				//maxSize +=  jQuery(this).width() + 4;
				if (jQuery(this).attr("class")!="first")
					maxImg++;
			});	
		}
		if (img > 1) {
			jQuery("#slider a.prev").show();
		}
	},
	checkQueue : function(e) {
		n=e.queue("fx").length;
		return (n==0);
	}
}
/* Onload Functions //---------------------------*/
jQuery(window).load(function(){
	if (jQuery("#middleContent.home").length>0) {
		indexGal.init();
	}
});