jQuery(window).bind("load", function() {
	//gallery = new Gallery('#slide');
});

var timer = null;

function Gallery(object, options){
	
	options = jQuery.extend({
		panel: 'div.panel',
		galleryContainer: 'div.panelcontainer',
		thumbs: '.position ',
		preview: '.preview ',
		panelwidth: 800,
		toolTip: false
	}, options);



	this.gallery          = jQuery(object);
	this.galleryContainer = this.gallery.find(options.galleryContainer);
	this.panelCount       = this.gallery.find(options.panel).size();
	this.panelWidth       = options.panelwidth;
	this.galleryWidth     = this.panelWidth * this.panelCount;
	this.panelPos         = 0;
	this.preferences      = options;
	
	this.prevPos = this.panelCount - 1;
	this.nextPos = this.panelCount - 1;

	this.galleryContainer.css("width" , this.galleryWidth);


	if (location.hash && parseInt(location.hash.slice(1)) <= this.panelCount) {
		this.panelPos = parseInt(location.hash.slice(1)) - 1;
		var cnt = - (this.panelWidth * this.panelPos);
		this.gallery.find(options.galleryContainer).css({ left: cnt });
	} else { 
		this.panelPos = 0;
	};

	thisGallery = this;

	this.navigation();

	jQuery('a', options.thumbs).each(function(index){

		var thumb = jQuery(this);

		jQuery(this).click(function(e){
			e.preventDefault();
			thisGallery.next(index);
		});
	});
	
	timer = setTimeout('AutoStart()', 2500);
}


function AutoStart(){
	timer = setTimeout('AutoStart()', 2500);
	var pos = (gallery.panelPos + 1 == gallery.panelCount) ? 0 : gallery.panelPos + 1;
	gallery.next(pos);
}

Gallery.prototype.next = function(index){
	if(index == -1){
		//this.navigation(index);
	}

	var galleryObj = this;

	var left = index * this.panelWidth;
	var thisGallery = this.galleryContainer;
	
	thisGallery.fadeOut('fast', function(){
		thisGallery.css({left:-left+'px'});
		thisGallery.fadeIn('fast');
	});
	
	/*
	thisGallery.hide();
	thisGallery.css({left:-left+'px'});
	thisGallery.show();
	*/
	this.panelPos = index;
	this.navigation();
	//location.hash = this.panelPos + 1;
}




Gallery.prototype.navigation = function(){

	jQuery('a', this.preferences.thumbs).removeClass('active').addClass('desactive');
	jQuery('a:eq('+this.panelPos+')', this.preferences.thumbs).addClass("active").removeClass('desactive');

	jQuery('li', this.preferences.preview).removeClass('active');
	jQuery('li:eq('+this.panelPos+')', this.preferences.preview).addClass("active");
	
	var galleryObj = this;
	var next = (this.panelPos + 1 == this.panelCount) ? 0 : this.panelPos + 1;
	var prev = (this.panelPos == 0) ? this.panelCount - 1 : this.panelPos - 1;

	//console.log(this.panelPos);

	jQuery('a.next', galleryObj.gallery).unbind('click');
	jQuery('a.next', galleryObj.gallery).click(function(e){
		e.preventDefault();
		galleryObj.next(next);
		if(timer!=null){clearTimeout(timer);}
	});

	jQuery('a.prev', galleryObj.gallery).unbind('click');
	jQuery('a.prev', galleryObj.gallery).click(function(e){
		e.preventDefault();
		galleryObj.next(prev);
		if(timer!=null){clearTimeout(timer);}
	});
	
	/*
	jQuery(document.documentElement).keyup(function(event){
		if(event.keyCode == 39 || event.keyCode == 40){
			thisGallery.next(next);
		}
		if(event.keyCode == 37 || event.keyCode == 38){
			thisGallery.next(prev);
		}
	});
	*/
}


