$(document).ready(function(){
  jQuery.fn.extend({
    wait: function(time, type) {
      time = time || 1000;
      type = type || "fx";
      return this.queue(type, function() {
        var self = this;
        setTimeout(function() {
          $(self).dequeue();
        }, time);
      });
    },
    scrollTo: function() {
//      if($('.viewport img:lt(' + $('.viewport img').index(this) + '):last').length == 1) {
        offset = ($(this).position().left)*(-1)+($('#mmcontent').width()-$(this).width())/2;
        $('.viewport').animate({left: offset});
        $('#mmnav_left').animate({width: ($('#mmcontent').width()-$(this).width())/2 });
        $('#mmnav_right').animate({width: ($('#mmcontent').width()-$(this).width())/2,
                                   marginLeft: $('#mmcontent').width() - ($('#mmcontent').width()-$(this).width())/2 });
        $('.viewport img').removeClass('actualImage');
        $(this).addClass('actualImage');
        $('#mmthumbnails img.hover').mouseleave().removeClass('hover');
        $('#mmthumbnails img:eq(' + $('.viewport img').index($('.actualImage'))  + ')').mouseenter().addClass('hover');
        
        
//      }
    },
    
    mminit: function() {
      var first_clone = $('.viewport').find('img:first').clone();
      $(first_clone).addClass('lastImage');
      $('.viewport img:last').after(first_clone);
          
      var last_clone = $('.viewport').find('img:last').prev().clone();
      $(last_clone).addClass('firstImage');
      $('.viewport img:first').before(last_clone);
    
      var act;
      var firstImage = new Image();
      $('.viewport').css('left', $('#mmcontent').position().left);
      $('.viewport img:eq(' + (($('.viewport img').length - $('.viewport img').length % 2)/2) + ')').addClass('actualImage'); // eq(' + ($('.viewport img').length/2 - $('.viewport img').length%2 - 1) + ')
      var fake_src = $('.actualImage').attr('src') + '?x=' + Math.random()*1000;
      $(firstImage).load(function() { 
          $('#mmthumbnails img').fadeTo(0, 0.3);
          $('#mmthumbnails').fadeIn(0);
          $('#mmthumbnails img').mouseenter(function() {
              $(this).stop(true,false);
              $(this).next('img').stop(true,false);
              $(this).prev('img').stop(true,false);
              
              $(this).fadeTo("fast", 1);
              $(this).next('img').fadeTo("fast", 0.6);
              $(this).prev('img').fadeTo("fast", 0.6);
          });
          $('#mmthumbnails img').mouseleave(function() {
              $(this).fadeTo("fast", 0.3);
              $(this).next('img').fadeTo("fast", 0.3);
              $(this).prev('img').fadeTo("fast", 0.3);    
          });    
          $('#loading').fadeOut("slow"); 
          $('.actualImage').scrollTo();          
          
          
      }).attr('src', fake_src); 
      
    }
  });

  $('body').mminit();

  $('#mmnav_left').click(function() {
    if($('.actualImage').prev('img').hasClass('firstImage')) {
      $('.viewport img:last').prev().scrollTo();
    } else {
      $('.actualImage').prev('img').scrollTo();
    }

  });
  
  $('#mmnav_right').click(function() {
    if($('.actualImage').next('img').hasClass('lastImage')) {
      $('.viewport img:first').next().scrollTo();
    } else {
      $('.actualImage').next('img').scrollTo(); 
    }
  });  

  $('.mmmenu_right a').click(function() {
    $('.mmmenu_right a').removeClass('active');
    $(this).addClass('active');
    $('#loading').fadeIn("fast");
    $('#mmthumbnails').fadeOut("slow");
    var loaded = 0;
    $('.viewport').load("ajaxLoader.php div.big img", 
                        {gallery: $(this).attr('title'), size: 'big' }, 
                        function() { if((loaded=loaded+1)==2) { $('body').mminit(); } });
                        
    $('#mmthumbnails').load("ajaxLoader.php div.thumb img", 
                        {gallery: $(this).attr('title'), size: 'thumb' },
                        function() { if((loaded=loaded+1)==2) { $('body').mminit(); } });
    return false;
  });

  //$('#mmthumbnails').mouseenter(function() { $('#mmthumbnails img').mouseleave(); });
  $('#mmthumbnails img').live('click', function() {
    $('.viewport img:eq(' + ($('#mmthumbnails img').index(this)+1) + ')').scrollTo();
  });
  
  
  $('.showContact a').mouseenter(function() {
    $('.showContact').next().stop(true,false);
    $(this).parent().next().animate({opacity:1},100);
    $('.showContact').next().removeClass('hidden');
  });
  $('.showContact a').mouseleave(function() {
    $(this).parent().next().animate({opacity:0},1000, function () {
      $('.showContact').next().addClass('hidden');
    });
  });
  
  $('.showContact').next().mouseenter(function() {
    $(this).stop(true,false);
  });
  $('.showContact').next().mouseleave(function() {
    $(this).animate({opacity:0},1000);
  });
  
  
  $('#mmnav_left').hover(
    function() {
      $(this).fadeTo("medium",0.9);
    },
    function () {
      $(this).fadeTo("medium",1);
    }
  );

  $('#mmnav_right').hover(
    function() {
      $(this).fadeTo("medium",0.9);
    },
    function () {
      $(this).fadeTo("medium",1);
    }
  );


  $('.mmmenu_right li:first a').click();
  
  $()
});

