var photos; //nesetridene fotky
var photosBig; //nesetridene velke fotky
var listAlbum = Array(); //setridene rel alb
var listPhoto = Array(); //setridene fotky podle alb
var currentPhoto = Array();
currentPhoto['index'] = 0;
currentPhoto['album'];
var oldHeight = 160;
var newHeight;
var heightImg;
var loadImg = Array();
var gi;
function removeActive() {
	$('ul.menugalerie li a').removeClass('active');
}
function getIndex(x) {
	for(var i = 0;i < listPhoto[x.rel]['url'].length;i++) {
	     if(listPhoto[x.rel]['url'][i] == x.href) {
	         currentPhoto['index'] = i;
	         currentPhoto['album'] = x.rel;
	         loadImg[i] = new Image();
	         loadImg[i].src = listPhoto[x.rel]['url'][i];
	         gi = i;
	         if(loadImg[i].complete) {
	        	 newHeight = loadImg[i].height;
	        	 showMainPhoto(); 
	         } 
	         else {
	        	 loadImg[i].onload = function() {
	        		 newHeight = loadImg[gi].height;
		        	 showMainPhoto(); 
	        	 };
	         }
	     }
	}
}
function imageForSlideShow(album,imgIndex,pole) {
	var xpole = Array();
	var text = '[';
	for(var i = 0;i < listPhoto[album]['url'].length;i++) {
		text += '[\"'+ listPhoto[album]['urlBig'][i] +'\", \"'+ listPhoto[album]['title'][i] +'\"]';
		var helpXpole = Array();
		helpXpole.push(listPhoto[album]['urlBig'][i]);
		helpXpole.push(listPhoto[album]['title'][i]);
		xpole.push(helpXpole);
		if(i != (listPhoto[album]['url'].length - 1)) {
			text += ',';
		}
	}
	text += '], '+ imgIndex;
	if(pole) {
	    return xpole;
	}
	else {
	    return text;	
	}
}
function showMainPhoto() {
	var nextArrow = '';
	var prevArrow = '';
	var separator = 0;
	
	if(!(currentPhoto['index'] == (listPhoto[currentPhoto['album']]['url'].length - 1))) {
	    nextArrow = "<a href='"+listPhoto[currentPhoto['album']]['url'][currentPhoto['index'] + 1]+"' rel='"+currentPhoto['album']+"' onclick='nextPhoto(this);return false;'><img src='"+ mediaUrl +"img/icon/sipkaDoprava.gif' alt='Šipka doprava'/></a>";
	    separator = 1;
	}
	if(!(currentPhoto['index'] == 0)) {
	    prevArrow = "<a href='"+listPhoto[currentPhoto['album']]['url'][currentPhoto['index'] - 1]+"' rel='"+currentPhoto['album']+"' onclick='nextPhoto(this);return false;'><img src='"+ mediaUrl +"img/icon/sipkaDoleva.gif' alt='Šipka doleva'/></a>";
	    separator = 1;
	}
	if(separator == 1) {
		separator = prevArrow +' | '+ nextArrow;
	}
	else {
		separator = '';
	}
	var imagesForSlideShowText = imageForSlideShow(currentPhoto['album'], currentPhoto['index'], false);
	var imagesForSlideShowPole = imageForSlideShow(currentPhoto['album'], currentPhoto['index'], true);
    $('#mainPhoto').attr('href', listPhoto[currentPhoto['album']]['url'][currentPhoto['index']]);

    $('#mainPhoto').bind('click', function() { jQuery.slimbox(imagesForSlideShowPole, currentPhoto['index']); return false;});
    $('#mainPhoto img').attr({
    	  src: listPhoto[currentPhoto['album']]['url'][currentPhoto['index']],
    	  alt: listPhoto[currentPhoto['album']]['title'][currentPhoto['index']]
    });
   
	$('#fotogalerieBottom').html("<div><a href='"+ listPhoto[currentPhoto['album']]['url'][currentPhoto['index']] +"' class='floatLeft' onclick='jQuery.slimbox("+ imagesForSlideShowText +");return false;'><img src='"+ mediaUrl +"img/icon/lupa.gif' alt='Lupa'/> zvětšit obrázek</a>"+ separator);
	
	animateHeight();
}
function animateHeight() {
	if(oldHeight > newHeight) {
		if(oldHeight > (newHeight + 10)) {
		    oldHeight -= 10;	
		}
		else {
		    oldHeight -= 1;
		}
		$('#mainPhoto img').attr({
			   height: oldHeight,
			   width: 228
		});
		if(oldHeight > newHeight) {
		    window.setTimeout('animateHeight()', 5);
		}
	}
	else if (oldHeight == newHeight) {
		oldHeight = newHeight;
	}
	else {
		if((oldHeight + 10) < newHeight) {
		    oldHeight += 10;	
		}
		else {
		    oldHeight += 1;
		}
		$('#mainPhoto img').attr({
			   height: oldHeight,
			   width: 228
		});
		if(oldHeight < newHeight) {
		     window.setTimeout('animateHeight()', 5);
		}
	}
}
function nextPhoto(x) {
	getIndex(x);
	$('ul.menugalerie li a:eq('+ currentPhoto +')').addClass('active');
	showMainPhoto();
	return false;
}
$(document).ready(function () {
	$('ul.menugalerie li a').bind("click", function(){
    	removeActive();
    	$(this).addClass('active');
    	getIndex(this);
        return false;
});
	
    photos = $('a.gallery').get();
    
    //Zjisti vsechny alba fotek
    for(var i = 0;i < photos.length;i++) {
    	var z = photos[i].rel;
        if(jQuery.inArray(z, listAlbum) == -1) {
        	listAlbum.push(z);
        	listPhoto[z] = Array();
        	listPhoto[z]['url'] = Array();
        	listPhoto[z]['title'] = Array();
        	listPhoto[z]['urlBig'] = Array();
        }
    }
    //Priradi fotky k jednotlivym album
    for(var i = 0;i < photos.length;i++) {
    	var z = photos[i].rel;
    	if(jQuery.inArray(photos[i].href ,listPhoto[z]['url']) == -1) {
            listPhoto[z]['url'].push(photos[i].href);
            listPhoto[z]['title'].push(photos[i].title);
            listPhoto[z]['urlBig'].push($(photos[i]).html());
    	}
    }
    
  //Testovaci
   /*  for(var i = 0;i < listAlbum.length;i++) {
    	var a = listAlbum[i];
    	for (var ii = 0;ii < listPhoto[a]['url'].length;ii++) {
    	    alert(listPhoto[a]['height'][ii]);	
    	}
    } */
    //Konec testovaci
    
    getIndex(photos[0]);
     
    $('ul.menugalerie li a:eq('+ currentPhoto['index'] +')').addClass('active');
});
/* function scrolling() {
	   var actualY = $(window).scrollTop();
	   if(actualY >= positionPhoto && actualY <= (maxBottom - oldHeight)) {
	       $('.fotogalerie').css('top', actualY +'px');	   
	   }
};
*/
/* SKROLOVANI FOTKY */
var maxBottom;
var positionPhoto;
var actualY;
$(document).ready(function () {
   maxBottom = $('ul.menugalerie li a:last').offset().top;
   positionPhoto = $('.fotogalerie').offset().top;  
   $(window).scroll(function() {
	   actualY = $(window).scrollTop();
	   actualY += 100;
	   if(actualY >= positionPhoto && actualY <= (maxBottom - oldHeight)) {
	       $('.fotogalerie').css('top', actualY +'px');	   
	   }
	   else if(actualY < positionPhoto) {
		   $('.fotogalerie').css('top', positionPhoto +'px');	
	   }
   });
});
/* KONEC SKROLOVANI FOTKY */