(function (window, undefined) {
  window.tabify = function tabify ($tabs, $contents, remember) {
    var current_index = -1
    , old_state
    , select = 0;
    $tabs.click(function () {
      var $this = $(this)
      , index = $tabs.index($this);
      if (index !== current_index) {
        $contents.hideByPosition().eq(index).showByPosition();
        $tabs.removeClass('active');
        $this.addClass('active');
        current_index = index;
        if (remember) {
          $.cookie('tabify_'+remember, index);
        }
      }
    });
    if (remember) {
      old_state = parseInt($.cookie('tabify_'+remember), 10);
      if (old_state !== null && ! isNaN(old_state) ) {
        select = old_state;
      }
    }
    $tabs.eq(select).click();
  }

  window.popup_show = window.popup_hide = function ($el) {
    alert('old javascript code used!');
  }
  
  var pop = {
    current: null,
    dimmer: $('#dimmer'),
    
    show: function ($el) {
      if ( ! $el || typeof($el.length) === 'undefined' || $el.length === 0) {
        return false;
      }
      pop.hideCurrent();
      pop.dimmer.show();
      
      $el.show();
      pop.current = $el;
      $(document).scrollTop(0);
      return $el;
    },
    
    hideCurrent: function () {
      var cur = pop.current;
      if (cur !== null) {
        cur.hide();
        if (cur.parent().widget()) {
          cur.siblings('a.widget_closer').hide();
        }
        pop.current = null;
      }
    },
    
    hide: function () {
      pop.hideCurrent();
      pop.dimmer.hide();
    }
  }
  
  window.popupManager = pop;
  
  // jquery plugin to get the offset of a hidden element
  $.fn.hiddenOffset = function() {
    var visible = this.is(':visible')
    , offsets;
    
    if ( ! visible)
      this.show();
      
    offsets = this.offset();
    
    if ( ! visible)
      this.hide();
      
    return offsets;
  };
  
})(window);
