(function($) {
  $.olympic = {};
  
  $.olympic.contentBoxes = new $.mw.binder({
    bind: function(elem) {
      $("#olympic_content li.content:odd", elem).addClass('even');
    }
  });
  
  $.olympic.emailbox = new $.mw.binder({
    bind: function(elem) {
      $("#ccoptin input[name=ea]", elem).mwbind('emailbox', function() {
        var $field = $(this);
        $field.val('email').css('color', '#aaa');
        $field.bind('focus', function() {
          if ($field.val()=='email')
          {
            $field.val('').css('color', '#000');            
          }
        }).bind('blur', function() {
          if ($field.val()=='')
          {
            $field.val('email').css('color', '#aaa');
          }
        });
        return true;
      });
    }
  });
  
  $.olympic.logo_link = new $.mw.binder({
    bind: function(elem) {
      $("#olympic_logo", elem).mwbind('logo_link', function() {
        $(this).css('cursor','pointer').click(function() { window.location = $("a", this).attr('href'); });
      });
    }
  });
  $.olympic.address_link = new $.mw.binder({
    bind: function(elem) {
      $("#address_box", elem).mwbind('address_link', function() {
        $(this).css('cursor','pointer').click(function() { window.location = "/contact"; });
      });
    }
  });
  
  $.olympic.sidebar_gallery = new $.mw.binder({
    bind: function(elem) {
      $("#sidebar_gallery", elem).mwbind('sidebar_gallery', function() {
        var json = eval('('+$("#sidebar_gallery_json", this).text()+')');
        $("#sidebar_gallery_json", this).remove();
        var $gallery = $(this);
        $imgpool = $("<div/>").hide();
        $imgpool.appendTo($gallery);
        var $cur = $gallery.find('img');
        $.each(json.photos, function(k, photo) {
          $img = $("<img/>").attr('src', photo.src).attr('width', photo.width).attr('height', photo.height);
          $imgpool.append($img);
          $img.hide();
          if ($cur.attr('src') == $img.attr('src')) $img.remove(); 
        });
        $.olympic.sidebar_gallery.rotate = function() {
          var $next = $imgpool.find("img:first");
          $next.show().prependTo($gallery);
          if ($cur) {
            $cur.fadeOut(1500,function() {
              $(this).appendTo($imgpool);
            });
          }
          $cur = $next;
        };
        setInterval($.olympic.sidebar_gallery.rotate, 7500);
        
        var $links = $('.mw_admin', $gallery).hide();
        $gallery.hover(function() {$links.fadeIn(500);},function() {$links.fadeOut(500);});
      });
    }
  });
  
})(jQuery);