/* Custom jQuery easing */
jQuery.extend(jQuery.easing, {
  easeOutCubic: function (x, t, b, c, d) {
    return c*((t=t/d-1)*t*t + 1) + b;
  }
});

/* The lightbox */
var LightboxOne = {};

LightboxOne = {
  settings: {
    defaults: {
      height: 600,
      width: 500
    },
    html: "\
<div id=\"lightbox\" style=\"display:none; position: absolute; left: 0; top: 0;\"> \
  <div id=\"lightbox-container\"> \
    <div id=\"lightbox-content\"></div> \
  </div>\
</div> \
    ",
    initialized: false,
    movement_amount: 100
  },
  current: {},
  return_to: undefined,
  make: function(text, options) {
    LightboxOne.init();
    $('#lightbox-content').html(text);
    LightboxOne.open(options);
  },
  // TODO: add caching so that reloading a url doesn't reload (maybe optional)
  pull: function(url, options) {
    if (typeof LightboxOne.settings.loading != 'undefined') {
      LightboxOne.settings.loading(url, options);
    }
    
    $.get(url, function(data) {
      LightboxOne.make(data, options);
    });
  },
  find_all: function() {
    return $('a[rel=lightbox]');
  },
  init: function() {
    $('#lightbox').stop();
    
    if (!LightboxOne.settings.initialized) {
      LightboxOne.initialize();
    }
  },
  initialize: function() {
    $('body').append(LightboxOne.settings.html);
    
    $('#lightbox').css({ 
      width: LightboxOne.settings.defaults.width, 
      height: LightboxOne.settings.defaults.height
    });
    
    LightboxOne.center();
    
    LightboxOne.finished();
    $(window).resize(LightboxOne.center);
    $(window).scroll(LightboxOne.center);
    LightboxOne.settings.initialized = true;
  },
  visible: function() {
    return !LightboxOne.hidden();
  },
  hidden: function() {
    return $('#lightbox').css('display') == 'none' || $('#lightbox').css('opacity') == 0;
  },
  close: function() {
    LightboxOne.init();
       $('#lightbox').css('display', 'none');
  },
  open: function(options) {
    LightboxOne.init();
    options = LightboxOne.parse_options(options);
    
    
    
    LightboxOne.show(options);
  },
  show: function(options) {
    options = LightboxOne.parse_options(options);
    options.opacity = 1;
    LightboxOne.layout(options);
  },
  hide: function(options) {
    options = LightboxOne.parse_options(options);
    options.opacity = 0;
//    LightboxOne.layout(options);
  },
  finished: function() {
    if (typeof LightboxOne.return_to != 'undefined') {
      $('#lightbox').css({ 
        width: LightboxOne.return_to.width+'px', 
        height: LightboxOne.return_to.height+'px' 
      });
      LightboxOne.center();
      LightboxOne.return_to = undefined;
    }
    
    LightboxOne.current = { 
      width: $('#lightbox').width(), 
      height: $('#lightbox').height() 
    };
    
    // if the lightbox is hidden, but still display block, then display none it so it's not on top of things we need
    if ($('#lightbox').css('display') == 'block' && $('#lightbox').css('opacity') == 0) {
      $('#lightbox').css('display', 'none');
    }
    
    // jog IE's memory
   // if ($.browser.msie) {
      $('#lightbox .filler').css('opacity', 'hide');
      $('#lightbox .filler').css('opacity', 'show');
  //  }
  },
  layout: function(options, force) {
    $('#lightbox').stop();
    
    var center = LightboxOne.center_options(options);
    
    var new_options = { 
      'top': center.top+'px', 
      'left': center.left+'px', 
      width: options.width+'px', 
      height: options.height+'px' 
    }
    
    if (options.opacity >= 0) {
      new_options.opacity = options.opacity;
    }
    
    if (force) {
      $('#lightbox').css(new_options);
      LightboxOne.finished()
    } else {
      if (LightboxOne.visible()) {
        
        if (new_options.opacity === 0) {
          $('#lightbox .filler').animate({ opacity: 0 }, 150, 'easeOutCubic', function() {
            $('#lightbox').animate(new_options, 200, 'easeOutCubic', LightboxOne.finished);
          });
        } else {
          $('#lightbox').animate(new_options, 200, 'easeOutCubic', LightboxOne.finished);
        }
        
      } else {
        $('#lightbox').animate(new_options, 200, 'easeOutCubic', function() {
          $('#lightbox .filler').animate({ opacity: 1 }, 150, 'easeOutCubic', LightboxOne.finished);
        });
      }
      
    } // end of if
    
  },
  center: function(options) {
    var center = LightboxOne.center_options(options);
    
    $('#lightbox').css({ 'top':center.top+'px', 'left':center.left+'px' });
  },
  center_options: function(options) {
    var opts = {};
    var width = $('#lightbox').width();
    var height = $('#lightbox').height();
    
    if (options && options.width && options.height) { 
      width = options.width;
      height = options.height; 
    }
    
    opts.top = Math.floor($(window).height()/2 - height/2 + $(window).scrollTop());
    opts.left = Math.floor($(window).width()/2 - width/2);
    
    return opts;
  },
  parse_options: function(options) {
    if (options == {} || options == undefined) {
      options = {};
      options.width = LightboxOne.current.width;
      options.height = LightboxOne.current.height;
    } else {
      if (options.width == 'same' || options.width == undefined) { 
        options.width = LightboxOne.current.width; 
      }
      if (options.height == 'same' || options.height == undefined) { 
        options.height = LightboxOne.current.height; 
      }
      if (options.width < 0) { options.width = 0; }
      if (options.height < 0) { options.height = 0; }
    }
    
    return options;
  }
};
