(function ($) {
	$.event.special.load = {
		add: function (hollaback) {
			if ( this.nodeType === 1 && this.tagName.toLowerCase() === 'img' && this.src !== '' ) {
				// Image is already complete, fire the hollaback (fixes browser issues were cached
				// images isn't triggering the load event)
				if ( this.complete || this.readyState === 4 ) {
					hollaback.handler.apply(this);
				}

				// Check if data URI images is supported, fire 'error' event if not
				else if ( this.readyState === 'uninitialized' && this.src.indexOf('data:') === 0 ) {
					$(this).trigger('error');
				}
				
				else {
					$(this).bind('load', hollaback.handler);
				}
			}
		}
	};
}(jQuery));


$(document).ready(function() {
  var $oe_menu		= $('#oe_menu');
  var $oe_menu_items	= $oe_menu.children('li');
  var $oe_overlay		= $('#oe_overlay');

  $oe_menu_items.bind('mouseenter',function(){
    var $this = $(this);
    $oe_menu_items.children('div').hide();
    $this.addClass('slided selected');
    $this.children('div').stop(true,true).slideDown(200,function(){
      $this.removeClass('slided');
    });
  }).bind('mouseleave',function(){
    var $this = $(this);
    $this.removeClass('selected');
    $oe_menu_items.children('div').hide();
  });

  $oe_menu.bind('mouseenter',function(){
    var $this = $(this);
    $oe_overlay.stop(true,true).show().fadeTo(200, 0.6);
    $this.addClass('hovered');
  }).bind('mouseleave',function(){
    var $this = $(this);
    $this.removeClass('hovered');
    $oe_overlay.stop(true,true).fadeTo(200, 0, function() { $(this).hide(); });
    $oe_menu_items.children('div').hide();
  });

  $('#oe_menu ul, #oe_menu ul li').bind('mouseenter mouseleave', function(e) {
    var fn = e.type == 'mouseenter' ? 'addClass' : 'removeClass';
    $(this)[fn]('hovered');
  });
  
  $('.prev-picture, .next-picture, .middle a').click(function(e) {
    var el = $(this);
    var pages = $('.middle a').removeClass('active');
    var old = current;

    if (el.hasClass('next-picture') && current < pictures.length - 1) {
      current++;
    }

    if (el.hasClass('prev-picture') && current > 0) {
      current--;
    }

    var idx = pages.index(el);
    if (idx != -1) {
      current = idx;
    }

    if (current != 0) {
      $('a.prev-picture').show();
    } else {
      $('a.prev-picture').hide();
    }

    if (current != pictures.length - 1) {
      $('a.next-picture').show();
    } else {
      $('a.next-picture').hide();
    }

    $(pages[current]).addClass('active');

    if (old == current) {
      return false;
    }

    $oe_overlay.stop(true,true).show().fadeTo(200, 0.6);
    $('.progress').removeClass('offset');

    loadImage(path + pictures[current]);

    return false;
  });

  function loadImage(src) {
    $('#main-container img').remove();

    var img = new Image();
    $(img)
      .load(function(e){
        $oe_overlay.stop(true,true).fadeTo(200, 0, function() { $(this).hide(); });
        $('.progress').addClass('offset');

        var wh = $(window).height(), ww = $(window).width(), ratio = wh / this.height;

        if (this.height > wh) {
          $(this).css({
            width: this.width * ratio,
            height: wh,
            'margin-left': (ww - this.width * ratio) / 2
          });
        } else {
          $(this).css('margin-left', (ww - this.width) / 2);
        }
        
        $('#main-container').append(img);
      })
      .attr('src', src);
  }

  loadImage(path + pictures[0]);
});

function l(t) {
  console.log(t);
}
