(function ($) {

	var $lb = null;
    var $body = null;
    var $lbContent = null;
    var $lbCache = null;
    var $lbContentContainer = null;
    var $lbOverlay = null;
    
    var lightboxCreate = function() {
    	
    	if (!$lb) {

    		$body = $("body");

            $body.append("<div id='lightbox'><div class='lightbox-content'><a href='javascript:;' class='close' title='Close'></a><div class='lightbox-content-container'/></div><div class='lightbox-overlay' /><div class='lightbox-cache' /></div>");
            $lb = $("#lightbox");

            $lb.hide();

            $lbContent = $(">.lightbox-content", $lb);
            $lbCache = $(">.lightbox-cache", $lb);
            $lbContentContainer = $(">.lightbox-content-container", $lbContent);
            $lbOverlay = $(">.lightbox-overlay", $lb);

            $(window).scroll(function () {

                if ($lb.is(":visible"))
                    lightboxCenterContent();

            }).resize(function () {

                if ($lb.is(":visible")) {

                    lightboxSize();
                    lightboxCenterContent();

                }

            }).keyup(function (evt) {

                if ($lb.is(":visible")) {

                    if (evt.which == 27)
                        lightboxClose();

                }

            });
            
            $lbOverlay.click(function() { lightboxClose(); });

            $(">.close", $lbContent).click(function () {

                lightboxClose();

                return false;

            });

            $lbContent.css("opacity", 0);
            $lbOverlay.css("opacity", 0)

        }
    	
    };
    
    var lightboxCenterContent = function ($elm) {

        $lbContent.css({
            top: Math.round(($(window).height() - $lbContent.height()) / 2) + $(window).scrollTop(),
            left: Math.round(($(window).width() - $lbContent.width()) / 2) + $(window).scrollLeft()
        });

    };
    
    var lightboxClose = function () {
    	
    	$lbContent.stop().animate({

            opacity: 0

        }, {

            duration: 300,
            complete: function () {

                $lbOverlay.stop().animate({

                    opacity: 0

                }, {

                    duration: 300,
                    complete: function () {

                        $lb.hide();
                        $lbCache.append($lbContentContainer.children());

                    }

                });

            }

        });

    };
    
    var lightboxSize = function () {

        $lb.css({

            width: $body.outerWidth(true),
            height: $body.outerHeight(true)

        });

    };

    var lightboxShow = function (callback) {

        lightboxSize();

        $lb.show();

        $lbOverlay.stop().animate({

            opacity: .75

        }, {

            duration: 300,
            complete: function () {
        	
        		lightboxCenterContent();

                $lbContent.stop().animate({

                    opacity: 1

                }, {

                    duration: 300,
                    complete: function () {

                        if (parseFloat($lbContent.css("opacity")) == 1)
                            $lbContent.css("opacity", "");
                        
                        if (callback) callback();

                    }

                });

            }

        });

    };
    
    var flashVideoCount = 0;
	
    $.fn.lightbox = function (options) {

        var defaults = {};
        var options = $.extend(defaults, options);

        return this.filter("a").each(function () {

        	var $link = $(this);
        	
        	var href = $link.attr("href");
        	var extension = href.substring(href.lastIndexOf(".") + 1).toLowerCase();
        	var width = $link.data("lightbox-width") ? parseInt($link.data("lightbox-width")) : -1;
        	var height = $link.data("lightbox-height") ? parseInt($link.data("lightbox-height")) : -1;
        	
        	var type = 'iframe';
        	
        	if (extension == 'flv')
        		type = 'video';
        	else if (extension == 'png' || extension == 'gif' || extension == 'jpg' || extension == 'jpeg')
        		type = 'image';
        	else if (href[0] == '#')
        		type = 'content';
        	
        	$link.click(function() {
        		
        		lightboxCreate();
        	
        		var $content = null;
        		
        		if (!$link[0].cache) {
        			
        			$content = $("<div class='" + type + "'/>");
        			
        			$lbCache.append($content);
        			$link[0].cache = $content[0];
        			
        			if (type == 'image') {
        				
        				$content.append("<img src='" + href + "' />");
        				
        			} else if (type == 'video') {
        				
        				$content.append('<div id="flash-video-' + flashVideoCount +'"/>');
        				
        				$link[0].video = $("#flash-video-" + flashVideoCount)[0];
        				
        				jwplayer($link[0].video).setup({
        					
        					  flashplayer: '/wp-content/themes/rc/swf/player.swf',
        					  file: href,
        					  height: height,
        					  width: width
        					  
        				});
        				
        				flashVideoCount++;
        				
        			}
        			
        		} else {
        			
        			$content = $($link[0].cache);
        			
        		}
        		
        		if (width > 0)
    				$lbContentContainer.width(width);
    			if (height > 0)
    				$lbContentContainer.height(height);
        		
        		$lbContentContainer.append($content);
        		
        		lightboxShow(function() {
        			
        			if (type == 'video') {
                		
            			jwplayer($link[0].video).play();
            			
            		}
        			
        		});
        		
        		return false;
        		
        	});

        });

    };

})(jQuery);
