//<a href="http://localhost/zaklad/fotka/721/"><img style="float: left;" src="http://localhost/zaklad/fotka_nahled/721/" /></a>

var gallery_width = 900;

function show_gallery(hrefsrc)
{
  $('body').append('<div id="fade"></div>'); //Add the fade layer to bottom of the body tag.
  $('#fade').css({'filter' : 'alpha(opacity=80)'}).fadeIn(300); //Fade in the fade layer - .css({'filter' : 'alpha(opacity=80)'}) is used to fix the IE Bug on fading transparencies 
  $('body').append('<div id=\"gallery\"><div id=\"gallerycontainer\">Loading......</div></div>');
  //div = $("<div id=\"gallery\"><div id=\"gallerycontainer\">").html("Loading......");
  //$("body").prepend(div);
  
  
  
  ajax_show_content(hrefsrc + "?ajaxgallery=1");
  
  
  
  $('#gallery').fadeIn(300).css({ 'width': Number(gallery_width) }).prepend('<a href="#" class="close">&nbsp;</a>');
  
  var popMargTop = ($('#gallery').height() + 80) / 2;
  var popMargLeft = ($('#gallery').width() + 80) / 2;

    //Apply Margin to Popup
    $('#gallery').css({
        /*'margin-top' : -popMargTop,*/
        'margin-left' : -popMargLeft
    });
    
   
  
  //Close Popups and Fade Layer
  $('a.close, #fade').live('click', function() { //When clicking on the close or fade layer...
    $('#fade , #gallery').fadeOut(300,function() {
        $('#fade, a.close').remove();  //fade them both out
    });
    return false;
  });
  
  
  
  
  //$(this).addClass("selected").parent().append('<div class="messagepop pop"><form method="post" id="new_message" action="/messages"><p><label for="email">Your email or name</label><input type="text" size="30" name="email" id="email" /></p><p><label for="body">Message</label><textarea rows="6" name="body" id="body" cols="35"></textarea></p><p><input type="submit" value="Send Message" name="commit" id="message_submit"/> or <a class="close" href="/">Cancel</a></p></form></div>');
  //alert(imgsrc);
  
}

function change_image(imgindex)
     {
      if($('#thumbs').children().length < imgindex){imgindex = $('#thumbs').children().length;}
      if(imgindex < 1){imgindex = 1;}
      if(($('#thumbs a.active').index()+1) != imgindex)
      {
      
      
      $('#thumbs').children().removeClass('active');
      $('#thumbs a:nth-child('+ imgindex +')').addClass('active');
      /*var image_width = $('#thumbs a:nth-child('+ imgindex +')').outerWidth()+2;
      $('#thumbs').scrollLeft(((imgindex-4)*image_width));*/
      var image_height = $('#thumbs a:nth-child('+ imgindex +')').outerHeight()+2;
      $('#thumbs').scrollTop(((imgindex-2)*image_height));
      
      $('#loading').show();
      $('#largeImage').attr('src',$('#thumbs a:nth-child('+ imgindex +')').attr('href'));
      $('#description').html($('#thumbs a:nth-child('+ imgindex +')').attr('title'));
      $('#counter').html( imgindex + ' / ' + $('#thumbs').children().length);
      if(imgindex-1 > 0){$('#go-previous').html('<a href=\'javascript:change_image(' + (imgindex-1) +')\'><span>&nbsp;</span></a> ');}
      else{$('#go-previous').empty();}
      if(imgindex+1 <= $('#thumbs').children().length){$('#go-next').html(' <a href=\'javascript:change_image(' + (imgindex+1) +')\'><span>&nbsp;<span></a>');}
      else{$('#go-next').empty();}
      $('#largeImage').one('load', function() {$('#loading').hide();});
      //$('#largeImage').ready(function(){$('#loading').hide();});
      }
     }


$(document).ready(function() {

    $(document).keydown(function(e){
    
     if($('#gallery').css('display') != 'none')
     { 
     if (e.keyCode == 37){change_image($('#thumbs a.active').index());}
     if (e.keyCode == 39){change_image($('#thumbs a.active').index()+2);}
     //alert(e.keyCode);
  }
  
   });

  $('a[href*="/fotka/"] img[src*="/fotka_nahled/"]').click(function(){
  show_gallery($(this).parent('a').attr('href'));
  return false;
});
  
  $('a[href*="/fotka/"] img[src*="/files/fotky/nahledy/"]').click(function(){
  show_gallery($(this).parent('a').attr('href'));
  return false;
});
  
  
 });

