jQuery.fn.extend({
	galleryArticle: function(options) {
		return this.each(function(ile) {
			new jQuery.fnGalleryArticle(this, options, ile);
		});
	}
});


jQuery.fnGalleryArticle = function(object, options, ile) {

	ile++;
	var opt = options || {};
	opt.maxItem = 4;
	opt.slideSpeed = opt.slideSpeed || 0; // "slow", "normal", "fast" or the number of milliseconds -> 0 = no animation

	var thisObj = $(object); // $(object) = $(this)
	var currPosition = 0;
	var slideWidth = 88; // width+border+margin
	var slidesItem = thisObj.find(".group");
	var slidesNumber = slidesItem.length;

	//
	thisObj.attr('id', 'gal_'+ile);
	thisObj.find(".thumbnails .imgItem").first().addClass('Active');


	//
	loadBigImage = function(_obj, _rel) {
		var img = new Image();

		if ( !_obj && !_rel ) {
			var tmpObj = thisObj;
			var bigImg = tmpObj.find(".thumbnails .imgItem a").first().attr('rel');
		} else {
			var tmpObj = _obj;
			var bigImg = _rel;
		}

		$(img).load(function () {
			$(this).hide();
			tmpObj.find('.imgLeftBox').append(this);
			$(this).fadeIn(650);

			if ( tmpObj.find('.imgLeftBox img').length > 1 ) {
				tmpObj.find('.imgLeftBox img').first().remove();
			}
		})
		.error(function () {
	      tmpObj.find('.imgLeftBox').html('<span class="blad">Wystąpił błąd</span>');
		})
		.attr('src', bigImg);
	}


	//
	thisObj.mouseenter(function() {
		thisObj.find(".imgNavBar").animate({opacity: 'show'}, 250);
	}).mouseleave(function() {
		thisObj.find(".imgNavBar").animate({opacity: 'hide'}, 250);
	});


	//
	thisObj.find(".thumbnails .imgItem").bind("click", function() {
		var zoomFile = $(this).find('a').attr('rel');
		thisObj.find(".thumbnails .imgItem").removeClass('Active');
		$(this).addClass('Active');
		loadBigImage(thisObj, zoomFile);
	});


	//
	if ( slidesItem.length > opt.maxItem ) {
		thisObj.find(".thumbnails").width( slidesNumber * slideWidth );

		function mControl(_pos) {
			if (_pos == 0) {
				thisObj.find(".navBack").attr('id', 'Disabled');
				thisObj.find(".navBack").css({ backgroundPosition: '0% 0%', cursor: 'auto' });
			} else {
				thisObj.find(".navBack").attr('id', '');
				thisObj.find(".navBack").css({ backgroundPosition: '0% 100%', cursor: 'pointer' });
			}

			if (_pos == (slidesNumber-opt.maxItem)) {
				thisObj.find(".navNext").attr('id', 'Disabled');
				thisObj.find(".navNext").css({ backgroundPosition: '0% 0%', cursor: 'auto' });
			} else {
				thisObj.find(".navNext").attr('id', '');
				thisObj.find(".navNext").css({ backgroundPosition: '0% 100%', cursor: 'pointer' });
			}
		}

		mControl(currPosition);

		thisObj.find(".navBack, .navNext").bind("click", function() {
			if ( $(this).attr('id') != 'Disabled' ) {
				currPosition = ( $(this).attr("class") == "navNext" ) ? currPosition+1 : currPosition-1;
				mControl(currPosition);
				thisObj.find(".thumbnails").animate( {"marginLeft": slideWidth*(-currPosition)}, {queue: true, duration: opt.slideSpeed} );
			}
		});
	}
	else {
		thisObj.find(".imgNavBar").remove();
	}


	//
	loadBigImage();

}
