  /**
   * 
   * @author Pawe� Gruchociak, 2011
   * @link http://www.oneplusone.pl
   */    
            
jQuery(document).ready(function()
{

   var $smallGallery = $("#smallGallery");
   var $bigPhotoContainer = $("#bigPhotoContainer");
   var $left = $("#bigPhotoContainer #arrowLeft");
   var $right = $("#bigPhotoContainer #arrowRight");
   var $desc = $("#descriptions");
   var $navigation = $("#bigPhotoContainer").find(".navigation");
   //var $loading = $bigPhotoContainer.find("#loading");
   var $currentBig = null;
   var $currentSmall = null;
   
   $smallGallery.find("a").click(function(e)
   {
        var $img = $(this).find("img");
        var id = $img.attr("src").replace(/.+small([0-9]+)\.[a-z]{3,4}$/,'$1');
        var index = $(this).index();
        var url = $(this).attr("href");
        
        //var $big = $bigPhotoContainer.find("img[src*='"+ url +"']");
        var $big = $bigPhotoContainer.find("img").eq(index);
        
        if( $currentBig && $currentBig.index() == $big.index() ) return false;
        
       // console.log( $img );
       // console.log( $big );
       // console.log( index );
        
       // $current = $img;
        
        if( $currentBig ){
            $currentSmall.removeClass("active").fadeTo(0,.5);
            $currentBig.fadeOut();
           // $currentBig.animate({left:$currentBig.width()+"px"},"fast");
        } 
        


        
        $currentBig = $big.fadeIn();
//        $currentBig.load(function(){
//            var top = ((  $bigPhotoContainer.height() - $currentBig.height() )/2 );
//            var left = (($bigPhotoContainer.width() - $currentBig.width())/2 );
//            if(top<0) top=0;
//            if(left<0) left=0;
//            $(this).css({
//            top: top+ "px",
//            left: left+ "px"
//            });
//        });
//        
//        if($currentBig.height()>0)
//            {
//
//
//                var top = ((  $bigPhotoContainer.height() - $currentBig.height() )/2 );
//                var left = (($bigPhotoContainer.width() - $currentBig.width())/2 );
//                    if(top<0) top=0;
//                    if(left<0) left=0;
//                $currentBig.css({
//                    top: top+ "px",
//                    left: left+ "px"
//                });
//            }
        
        //$currentBig = $currentBig.css({left:-$bigPhotoContainer.width()+"px"}).animate({left:"0px"},"fast");
        $(this).addClass("active");
        $currentSmall = $(this).fadeTo(0,1);        
        
        if($desc)
            {
               $desc.find("li").fadeOut().eq( $currentSmall.index() ).
                   //dequeue().
                   delay(500).fadeIn();
            }
        
        e.preventDefault();
    });
    
    $bigPhotoContainer.find("img").hide();
    $smallGallery.find("a").fadeTo(0, .5);
    
    if($left && $right)
    {
        $left.click(function(e){
            
            $currentSmall.prev().click();
            e.preventDefault();
        })
        $right.click(function(e){
            $currentSmall.next().click();
            e.preventDefault();
        })
        
        if($navigation)
            {
                if($bigPhotoContainer.find("img").length>0)
                    setTimeout(enableHoverNavigation, 1000);// enableHoverNavigation();
                else
                    $navigation.hide();
            }
        
        
    }
    
    
    function enableHoverNavigation(){
        $bigPhotoContainer.hover(
        function(){
           $navigation.stop(true,true).fadeIn(); 
        }
        ,
        function(){
           $navigation.fadeOut("slow"); 
        });
        $bigPhotoContainer.trigger('mouseleave');
    }
    
    
    
    setTimeout(function(){$smallGallery.find("a:first").trigger('click')}, 1000);
    //$smallGallery.find("a:first").trigger('click');
   
  
});
