(function( $ ){

	$.extend($.fn.disableTextSelect = function() {
		return this.each(function(){
			if($.browser.mozilla){//Firefox
				$(this).css('MozUserSelect','none');
			}else if($.browser.msie){//IE
				$(this).bind('selectstart',function(){return false;});
			}else{//Opera, etc.
				$(this).mousedown(function(){return false;});
			}
		});
	});


	function GalleryViewer(_options) {
		this.options = {
			"mainScale" : false,
			"smallScale" : false,
			"startIndex" : false,
			"slide" : 3
		};

		if (_options) {
			$.extend(this.options, _options);
		}

		this._create();
	}

	GalleryViewer.prototype = {
		_create : function() {
			this.e_slider = $('#mask');
			this.e_thumb = this.e_slider.find('img');
			this.e_thumbbox = this.e_slider.children();

			this.e_main = $('#mainPicture');
			this.e_mainAuthor = $('#pictureAuthor');

			this.e_index = $('#index');

			this.left = 0;
			this.maxLeft = this.e_slider.width() - this.e_slider.parent().width();

			if (this.options.startIndex) {
				this.index = this.options.startIndex;
				this._showActivePicture();
			} else {
				this.index = 0;
			}

			var self = this;

			$('#prevPicture').bind('click', function(e) { return self._shiftSlider(-1); }).disableTextSelect();
			$('#nextPicture').bind('click', function(e) { return self._shiftSlider(1); }).disableTextSelect();

			$('#shift_left').bind('click', function(e) { return self._stepPic(-1); }).disableTextSelect();
			$('#shift_right').bind('click', function(e) { return self._stepPic(1); }).disableTextSelect();

			$('#shift_left').css('cursor', (this.index>0)?'pointer':'default');
			$('#shift_right').css('cursor', (this.index<this.e_thumb.length)?'pointer':'default');

			this.e_thumb.bind('click', function(e) { return self._setPic(this); }).disableTextSelect();
		},

		_shiftSlider : function(change) {
			change *= this.options.slide;

			var act = this.e_thumbbox.eq(this.index);

			change *= act.width();

			var left = this.left + change;

			if (left > this.maxLeft) left = this.maxLeft;
			if (left < 0) left = 0;

			this.e_slider.stop().animate({"left": (-left) + "px"}, "normal");

			this.left = left;

			this._checkSliderButtons(left);
		},
		
		_setPic : function(element) {
			var i = this.e_thumb.index(element);

			if (i < 0) return false;
			if (i >= this.e_thumb.length) return false;

			this.index = i;

			$('#shift_left').css('cursor', (this.index>0)?'pointer':'default');
			$('#shift_right').css('cursor', (this.index<this.e_thumb.length)?'pointer':'default');

			this._showActivePicture();
			this._switchMainPicture();
		},
		
		_stepPic : function(change) {
			if (this.index + change < 0) return false;
			if (this.index + change >= this.e_thumb.length) return false;

			this.index += change;

			$('#shift_left').css('cursor', (this.index>0)?'pointer':'default');
			$('#shift_right').css('cursor', (this.index<this.e_thumb.length)?'pointer':'default');

			this._showActivePicture();
			this._switchMainPicture();
		},
		
		_switchMainPicture : function() {
			var act = this.e_thumb.eq(this.index);

			var tSRC = act.attr('src');

			var bSRC = tSRC.replace(this.options.smallScale, this.options.mainScale);

			this.e_main.attr('src', bSRC);

			if (act.attr('author')) {
				this.e_mainAuthor.text(act.attr('author'));
			}

			this.e_thumbbox.removeClass('selectedImage');
			act.parent().addClass('selectedImage');

			this.e_index.html(this.index + 1);
		},
		
		_showActivePicture : function(skipAnimate) {
			var act = this.e_thumbbox.eq(this.index);

			var a = act.position().left;

			var b = this.e_slider.parent().width();

			var c = act.width();

			var left = Math.round(a - b / 2 + c / 2);

			if (left > this.maxLeft) left = this.maxLeft;
			if (left < 0) left = 0;

			if (skipAnimate) {
				this.e_slider.css({"left": (-left) + "px"});
			} else {
				this.e_slider.stop().animate({"left": (-left) + "px"}, "normal");
			}

			this.left = left;

			this._checkSliderButtons(left);
		},

		_checkSliderButtons : function(left) {
			$('#prevPicture').css('cursor', (left>0)?'pointer':'default');
			$('#nextPicture').css('cursor', (left<this.maxLeft)?'pointer':'default');
		}
	};

	$.galleryViewer = function(_options) {
		new GalleryViewer(_options);
	};

})( jQuery );
		

