/**
 * Overlay plugin
 *
 * @author      Lex van der Woude <lex@xq.co.nz>
 * @copyright   Copyright (c) 2009, X-StatiQ Online Design Ltd.
 * @link        http://www.xq.co.nz
 */
(function($){
    
    /**
     * Create overlay trigger
     */
    $.fn.overlay = function(options){
        // Options
        var opts = $.extend({}, $.fn.overlay.defaults, options);
        
        // Variables
        var isOpen = false;
        
        // Required triggers
        $(window).bind('resize', showOverlayBox);
        $(window).bind('scroll', showOverlayBox);
        $('.overlay-cover, .closeOverlay').live('click', doOverlayClose);
        
        /**
         * Main loop
         */
        return this.each(function(){

            // Add trigger
            $(this).click(doOverlayOpen);
            
        });
        
        /**
         * Show overlay box
         */
        function showOverlayBox() {
            // Dont show non open overlays
            if( opts.isOpen == false ) return;
            
            // Change css on the overlay box
            $('.overlay-box').css({
                display:'block',
                left:( $(window).width() - $('.overlay-box').width() )/2,
                top:( $(window).height() - $('.overlay-box').height() )/2 -20 + $(window).scrollTop(),
                position:'absolute'
            });
            
            // Change css on the overlay cover
            $('.overlay-cover').css({
                display:'block',
                width: $(window).width(),
                height:$(window).height() + $(window).scrollTop()
            });
        }
        
        /**
         * Open overlay
         */
        function doOverlayOpen() {
            // First make sure that all overlay's are closed
            doOverlayClose();
            
            // Change is open status to true
            opts.isOpen = true;
            
            // Create markup
            $('body')
                .append('<div class="overlay-cover"></div>')
                .append('<div class="overlay-box"><div class="overlay-content"><div class="loading">Loading</div></div></div>');

            // Fade in overlay cover
            $('.overlay-cover').css({ opacity:0 }).animate({ opacity: opts.opacity });
            
            // Show the overlay box
            showOverlayBox();
          //  alert($(this).attr('href'));
            // Load the url into the overlay content box
            $('.overlay-content').load($(this).attr('href'), opts.data, function(){
                // Change the width from options
                $('.overlay-box').css({
                    width: opts.width
                });
                // Refresh overlay box
                showOverlayBox();
            });
            
            // Dont continue to follow the link
            return false;
        }
        
        /**
         * Close overlay
         */
        function doOverlayClose() {
            // Change is open status to false
            opts.isOpen = false;
            
            // Remove the html
            $('.overlay-cover, .overlay-box').remove();
            
            // Dont continue to follow the link
            return false;
        }
    }

    /**
     * Overlay defaults
     */
    $.fn.overlay.defaults = {
        isOpen: false,
        width: 500,
        opacity: 0.15,
        data: null
    }
    
})(jQuery);

