/**
 * jQuery gallery  by Sokolio
**/

$(function() {
					 
	jQuery.fn.kolgallery = function(settings) {
		settings = jQuery.extend({
			maxHeight: 500,
			maxWidth: 640,
			showNav: true,
			imageLoading:	'/js/kolgallery_img/loading.gif'			
		}, settings);

		var gallery = this;		
		var imageArray = new Array();
		var activeImage = 0;
		var images = $('.thumbNav a', gallery);
		var imageCount = images.size();
		var imageWin = $('.imageWindow', gallery);
		var imageObj = $('.imageWindow img', gallery);
		var arrNext, arrPrev, preloader;
	
		function _initialize() {
			if (imageCount>0)
			{ 
				images.each(function(){																						 
					imageArray.push(new Array($(this).attr('href'), $(this).attr('title')));
					$(this).click(function(event){
						event.preventDefault();
						reg = /^img(\d+)$/
						arr = reg.exec($(this).attr('id'))
						activeImage = Number(arr[1]);
						_change_image();
					});
				});
				
				imageWin.height(200);
				imageWin.prepend('<span class="loader"><img src="'+settings.imageLoading+'" alt></span>');
				preloader = $(".loader", gallery);
				
				if (settings.showNav)
				{
					imageWin.prepend('<span class="arrNext"></span><span class="arrPrev"></span>');
			
					arrNext = $(".arrNext", gallery);
					arrPrev = $(".arrPrev", gallery);
					$(".arrNext, .arrPrev", gallery).height(imageWin.height());
				
					arrNext.click(function(event){
						if(activeImage < imageCount-1){
							activeImage++;
							_change_image();
						}
					});
					
					arrPrev.click(function(event){
						if(activeImage > 0){
							activeImage--;
							_change_image();
						}
					});
				}
				
				_load_image();
			}
		}
		
		
		function _change_image(){
			imageObj.fadeOut('fast', function(){
						preloader.show();
						_load_image();
			});		
		}
		
		function _hide_arrows(){
			if (settings.showNav)
			{
				if(activeImage==0)
					arrPrev.hide();
				else
					arrPrev.show();
				if(activeImage==imageCount-1)
					arrNext.hide();
				else
					arrNext.show();
			}
		}
		
		function _load_image(){
			img = new Image();
			img.onload = function() {
				imageObj.attr('src',imageArray[activeImage][0]);	
				imageObj.attr('title',imageArray[activeImage][1]);
				
				imWidth = img.width;
				imHeight = img.height;				
				var koe1 = settings.maxWidth/imWidth;			
				
				if (koe1 < 1) {
					imWidth = imWidth*koe1;
					imHeight = imHeight*koe1;
				}
				
				var koe2 = settings.maxHeight/imHeight;			
				
				if (koe2 < 1) {
					imWidth = imWidth*koe2;
					imHeight = imHeight*koe2;
				}
				
				imageObj.attr('width', imWidth);
				imageObj.attr('height', imHeight);
				preloader.hide();

				
				imageWin.add(".arrNext, .arrPrev", gallery).animate({height: imHeight},'fast',function() {
					_hide_arrows();																																											
					imageObj.fadeIn('slow');																																										 
				});
				
				_preload_neighbor_images();
				var offset = gallery.offset();
				var scroll_top = document.body.scrollTop;
				if ($.browser.opera	|| $.browser.msie ||$.browser.mozilla)
					scroll_top = document.documentElement.scrollTop;
				//alert('top: '+scroll_top);
				scroll_top = (offset.top>scroll_top)? scroll_top : offset.top;
				window.scrollTo(0, scroll_top);
			}
			img.src = imageArray[activeImage][0];
		}
		
		function _preload_neighbor_images() {
			if ( (imageCount -1) > activeImage) {
				objNext = new Image();
				objNext.src = imageArray[activeImage + 1][0];
			}
			if (activeImage > 0 ) {
				objPrev = new Image();
				objPrev.src = imageArray[activeImage -1][0];
			}
		}
		
		_initialize()
	}
})

