$(function()
{
    var thContainerLength = parseInt($('#galThContainer img').length);
    $('#galThContainer').css({'width':thContainerLength*111+'px'});
    var hiddenPart = (thContainerLength-8)*111;
    var shImg = 0;

     $('#galThContainer div').hover(function()
     {
        $(this).find('.imgShad').hide();
        $(this).find('img').animate({'width':'162px','height':'102px','top':'-17px','left':'-27px'},200,'linear');
     },function()
     {
        $(this).find('img').animate({'width':'107px','height':'68px','top':'0','left':'0'},70,'linear',function()
        {
            $(this).next().show();
        });
     });

     $('#galThContainer img').click(function()
     {
          $('.'+$(this).parent().attr('class').slice(0,-1)).show();
          shImg = parseInt($(this).parent().attr('rel').slice(0,-1));
          $('.toMove').hide();
          $('.closeMove').show();
          $('.forGalL').show();
          $('.forGalR').show();
          $('#autoGalleryThumbs').hide();
          $('.autoGalPhoto').not('.autoGalPhoto[rel='+shImg+']').css("display", "none");
          $('.autoGalPhoto[rel='+(shImg)+']').css("display", "block");
     });


   $('.moveToRight').click(function()
   {
        if (hiddenPart >= 111)
        {
            hiddenPart = hiddenPart - 111;
            $('#galThContainer').animate({'left':'-=111px'});
            //console.log(hiddenPart)
        }
   });

   $('.moveToLeft').click(function()
   {
        if (hiddenPart <+ thContainerLength*111 - 920)
        {
            hiddenPart = hiddenPart + 111;
            $('#galThContainer').animate({'left':'+=111px'});
            //console.log(hiddenPart)
        }
   });

   $('.closeMove').click(function()
   {
        $(this).hide();
        $('.forGalL').hide();
        $('.forGalR').hide();
        $('.toMove').show();
        $('.autoGalPhoto').hide();
        $('#autoGalleryThumbs').show();
   });

   /*var px = 0;

   $('.autoGalPhoto').mousemove(function(e)
   {
    px = e.pageX;
 })

   $('.autoGalPhoto').click(function()
   {
    if ( px >= Math.round(parseInt($('body').width())/2) )
    {
        if ( shImg < thContainerLength )
        {
            $('.autoGalPhoto').not('.autoGalPhoto[rel='+shImg+']').hide();
            $('.autoGalPhoto[rel='+(shImg+1)+']').show();
            shImg++;
        }
    } else
    {
        if ( shImg > 0 )
        {
            $('.autoGalPhoto').not('.autoGalPhoto[rel='+shImg+']').hide();
            $('.autoGalPhoto[rel='+(shImg-1)+']').show();
            shImg--;
        }
    }
   });*/

   $('.forGalL.forGal').click(function()
   {
        if ( shImg > 0 )
        {
            shImg--;
            $('.autoGalPhoto').not('.autoGalPhoto[rel='+shImg+']').css("display", "none");
            $('.autoGalPhoto[rel='+(shImg)+']').css("display", "block");
        }
        if ( shImg == 0 )
        {
            shImg = thContainerLength;
            $('.autoGalPhoto').not('.autoGalPhoto[rel='+shImg+']').css("display", "none");
            $('.autoGalPhoto[rel='+(shImg)+']').css("display", "block");
        }
   });

   $('.forGalR.forGal').click(function()
   {
        if ( shImg <= thContainerLength )
        {
            shImg++
            $('.autoGalPhoto').not('.autoGalPhoto[rel='+shImg+']').css("display", "none");
            $('.autoGalPhoto[rel='+(shImg)+']').css("display", "block");
        }
        if ( shImg > thContainerLength )
        {
            shImg = 1;
            $('.autoGalPhoto').not('.autoGalPhoto[rel='+shImg+']').css("display", "none");
            $('.autoGalPhoto[rel='+(shImg)+']').css("display", "block");
        }
   });


})
