/**
 * Smashing Lightbox
 *
 * @package      Smashing Boxes UI
 * @author       Nate Hunzaker <nate.hunzaker@gmail.com>
 * @copyright    Copyright (c) 2011, Nate Hunzaker
 * @license      http://opensource.org/licenses/gpl-2.0.php GNU Public License
 *
 * FUTURE RELEASE
 * @todo 
 */

$(document).ready(function() {
    
    "use strict";
    
    function Lightbox() {
        
        var lightbox = {
            
            // Manages attributes            
            adjust: function(properties) {
                
                var p = properties || {};
                
                this.width = p.width || ( $(document).width() / 2 );
                this.height = p.height || "auto";
            },
            
            make: function(properties) {
                
                // If there are properties, adjust the lb, else continue
                (properties !== undefined) ? this.adjust(properties) : false;
                    
                // We create the object ahead of time and hideit
                var container = $("<div id='lb-blackout'></div><div id='lb-popup'></div>").hide(),
                    totalWidth = this.width;
                                    
                $(document.body).prepend(container);
                
                $("#lb-blackout").hide();
                
                $("#lb-popup").css("width", 0).hide();                
                                
                // Handle closing the lightbox
                $("#lb-blackout").click( function() {
                    lightbox.close();
                });
                
                return $.lightbox;
            },
            
            open: function(file, properties) {
                                
                $("#lb-popup").css("background-image", "images/loader.gif");
                              
                // If there are properties, adjust the lb, else continue
                (properties !== undefined) ? this.adjust(properties) : false;
                                           
                // If the lb doesn't exist, make it!
                ( $("#lb-popup").length === 0) ? this.make(properties) : false;
                                
                // Load content
                $.get(file, function(data) {
                
                    $("#lb-blackout").fadeIn();                    
                    
                    $("#lb-popup").show().animate({ opacity: 1, width: lightbox.width, height: lightbox.height, marginLeft: -lightbox.width / 2 }, 500).delay(500)
                                  .css("background-image", "none").css("height", lightbox.height)
                                  .html(data)
                                  .children("*").css("opacity", 0).delay(600).animate({ opacity: 1}, 700);
                });
                
                return $.lightbox;
            },
            
            close: function() {
                $("#lb-popup").children("*").fadeOut().end()
                    .animate({ opacity: 0, width: 0, height: 0, marginLeft: 0 }, 500, 
                    function() {
                        $("#lb-popup").hide();
                        $("#lb-blackout").fadeOut();
                    }
                );
                                
                return $.lightbox;
            }
        };
        
        return lightbox;
    }
    
    jQuery.lightbox = Lightbox();
        
});
