var Gallery = {
	positions: null,
	curpos : 0,
	moving: false,
	thumb_x : false,

	init: function() {

		Gallery.thumb_x = parseInt($('#params').attr('thumb_x'));

		var total = parseInt($('#params').attr('positions')) * (Gallery.thumb_x +2);
		var mWidth = document.getElementById('scroller_width').clientWidth;
		var pos = Math.round( (total-mWidth) / (Gallery.thumb_x +2) )

		Gallery.positions = pos;

		$("#move_left").bind("mouseover click", Gallery.moveLeft);
		$("#move_right").bind("mouseover click", Gallery.moveRight);

		// Add handler for gallery images
		$('.thumb').bind('click', Gallery.imageClick);

		Gallery.moving = false;
	},

	moveLeft : function() {

		var total = parseInt($('#params').attr('positions')) * (Gallery.thumb_x +2);
		var mWidth = document.getElementById('scroller_width').clientWidth;
		var pos = Math.round( (total-mWidth) / (Gallery.thumb_x +2) )

		Gallery.positions = pos;

		if ( (Gallery.curpos < Gallery.positions) && (Gallery.moving == false)) {
			Gallery.moving = true;
			Gallery.curpos++;
			$("#image_scroll_hor").animate({left: "-="+(Gallery.thumb_x+2)}, 1500, 'linear', function (){Gallery.moving = false;});
		}
	},
	moveRight : function() {

		var total = parseInt($('#params').attr('positions')) * (Gallery.thumb_x +2);
		var mWidth = document.getElementById('scroller_width').clientWidth;
		var pos = Math.round( (total-mWidth) / (Gallery.thumb_x +2) )


		Gallery.positions = pos;

		if ( (Gallery.curpos > 0) && (Gallery.moving == false)) {
			Gallery.moving = true;
			Gallery.curpos--;
			$("#image_scroll_hor").animate({left: "+="+(Gallery.thumb_x+2)}, 1500, 'linear', function (){Gallery.moving = false;});
		}

	},

	imageClick: function() {
		document.location="gallery.php?id="+this.id.substr(2);
	},

	imageClickCallback: function(data) {
		$('#description').text(data.text);
	}


}

$(document).ready(Gallery.init);