if( !TD )var TD = {};

TD.imagegallery = {
	init: function() {
		if ($('#image-gallery').length == 0)
			return;
			
		this.prepareThumbs();
		this.prepareStage();
		this.prepareNavi();
	},
	prepareThumbs: function() {
		$('#gallery-thumbs a').click(function() {
			$('#ctl00_imgTopImage').attr('src', $(this).attr('href'));
			return false;
		});
	},
	prepareStage: function() {
		/*$('#ctl00_imgTopImage').click(function() {
			alert('Open in lightbox: ' + $(this).attr('src'));
		});*/
	},
	prepareNavi: function() {
		var thumb = $('#gallery-thumbs a');
		var slide = $('#gallery-thumbs ul');

		var number = thumb.length;
		var width = thumb.width() + 1;
		var offset = Math.round(slide.width() / width);

		slide.css('width', number * width);
		slide.data('clicks', 0)

		$('#gallery-prev a').click(function() {
			var clicks = slide.data('clicks') - 1;

			if (clicks < 0) {
				$(this).parent().addClass('disabled');
				return false;
			} else
				if (clicks <= 0) {
				$(this).parent().addClass('disabled');
			} else {
				$(this).parent().removeClass('disabled');
			}
			$('#gallery-next').removeClass('disabled');

			slide.animate({ marginLeft: width * clicks * -1 + 'px' }, 200, 'swing');
			slide.data('clicks', clicks)
			return false;
		});
		$('#gallery-next a').click(function() {
			var clicks = slide.data('clicks') + 1;

			if (clicks > number - offset) {
				$(this).parent().addClass('disabled');
				return false;
			} else
				if (clicks >= number - offset) {
				$(this).parent().addClass('disabled');
			} else {
				$(this).parent().removeClass('disabled');
			}
			$('#gallery-prev').removeClass('disabled');

			slide.animate({ marginLeft: width * clicks * -1 + 'px' }, 200, 'swing');
			slide.data('clicks', clicks);
			return false;
		});
	}

}
$( function(){
	TD.imagegallery.init();		
} );