window.addEvent = function(type, fn) {
  jQuery.bind(type, fn);
}

var gEval = eval;
(function($){
  var lightbox, content, closeAction = null, loaded = {scripts:[],stylesheets:[]},
    methods = {
      create: function(){
        $('script').each(function(){ loaded.scripts.push(this.src); });
        $('link[rel=stylesheet]').each(function(){ loaded.stylesheets.push(this.href); });
        
        lightbox = $('#lightbox');
        if (lightbox.length == 0) {
          lightbox = $('<div id="lightbox"><a class="lb-close" title="Close">X</a><div class="content"></div></div>');
          $('body').append(lightbox);
        }
        content = $('#lightbox > .content');
        lightbox.find('a.lb-close').click(methods.close);
        return content;
      },
      attach: function(live) {
        if (live !== false)
          return this.live('click',methods.open);
        else
          return this.bind('click',methods.open);
      },
      detach: function(live) {
        if (live == undefined || live == true)
          this.die('click',methods.open);
        
        if (live == undefined || live == false)
          this.unbind('click',methods.open);
      },
      open: function(e) {
        if (!lightbox)
          methods.create();
        
        var opts = {
          beforeSend: methods.beforeSend,
          complete: methods.complete,
          error: methods.error,
          success: methods.success
        };
        if (e.preventDefault != undefined) {
          e.preventDefault();
        } else if (e == undefined) {
          if (typeof e == 'object')
            $.extend(opts, e);
          else
            opts.url = e;
        }
        
        if (this.nodeName == 'A') {
          opts.url = this.href;
        } else if (this.nodeName == 'FORM') {
          opts.url = this.action;
          opts.type = 'POST';
          opts.data = $(this).serialize();
        }
        
        $.ajax(opts);
      },
      beforeSend: function(jqXHR, settings) {
        if (lightbox.addClass('loading').css('display') == 'block')
          return true;
        
        lightbox.removeClass(function(){
          var classes = this.className.split(' '), remove = '';
          for (var i=0, l=classes.length; i<l; i++) {
            if (classes[i].indexOf('lbw-') === 0)
              remove = remove + ' ' + classes[i];
          }
          return remove;
        });
        $('body').addClass('overlay');
        lightbox.fadeTo(0,0.01);
        methods.center();
        lightbox.fadeTo(500,1);
        return true;
      },
      complete: function(jqXHR, textStatus) {
        lightbox.removeClass('loading');
      },
      success: function(data, textStatus, jqXHR) {
        var html, scripts, opts;
        
        if (typeof data == 'object') {
          if (data.redirect)
            window.location = data.redirect;
          
          if (data.html)
            html = data.html;
        
        } else {
          html = data;
        }
        
        html = $(html);
        if (html.find('body').length > 0)
          html = html.find('body').html();
        
        if (typeof data == 'object') {
          if (data.width)
            lightbox.removeClass('lbw-narrow, lbw-medium, lbw-wide').addClass('lbw-'+data.width);
          
          if (data.scripts) {
            $.each(data.scripts, function(script, type) {
              if ($.inArray(script, loaded.scripts) == -1) {
                loaded.scripts.push(script);
                $.ajax({
                  url: script,
                  dataType: "script",
                  async: false,
                  cache: true
                });
              }
            });
          }
          if (data.stylesheets) {
            $.each(data.stylesheets, function(stylesheet, attrs) {
              if ($.inArray(stylesheet, loaded.stylesheets) == -1) {
                loaded.stylesheets.push(stylesheet);
                if (!attrs)
                  attrs = {};
                $('<link href="'+stylesheet+'" rel="stylesheet" />').attr(attrs).appendTo($('head'));
              }
            });
          }
        }
        
        try {
          scripts = html.find('script[type="text/javascript"]');
          $.merge(scripts, html.filter('script[type="text/javascript"]'));
          html.find('script[type="text/javascript"]').remove();
          content.html(html.filter(':not(script[type="text/javascript"])'));
          
          scripts.each(function(){
            //try{
              gEval(this.text);
            //} catch(err) {}
          });
        } catch(err) {}
        
        opts = {
          beforeSend: methods.beforeSend,
          complete: methods.complete,
          error: methods.error,
          success: methods.success
        };
        
        content.find('form').ajaxForm(opts);
        
        methods.center();
        $(document).trigger('ready');
        try { window.fireEvent('domready'); } catch(err) { }
      },
      error: function(jqXHR, textStatus, errorThrown) {
        content.html('<p class="error">'+textStatus+'</p>');
      },
      close: function(e, preventDefault) {
        if (preventDefault !== false && e.preventDefault != undefined)
          e.preventDefault();
        
        lightbox.fadeOut(500, function(){        
          content.html('');
          $('body').removeClass('overlay');
        });
      },
      center: function() {
        if (lightbox.hasClass('lbw-fit'))
          lightbox.css({
            'margin-left': -(lightbox.outerWidth() / 2),
            'left': '50%'
          });
        else
          lightbox.css({'margin-left': false, 'left': false});
      }
    };
  $.fn.lightbox = function(method) {
    if (methods[method]) {
      return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
    } else if ( typeof method === 'object' || ! method ) {
      return methods.attach.apply( this, arguments );
    } else {
      $.error('Method '+method+' does not exist on jQuery.lightbox');
    }
  };
})(jQuery);

(function($){
  $('a[rel=lightbox], #lightbox > .content a:not([rel])').lightbox();
})(jQuery);
