var lb_groups = [];
jQuery.fn.tankbox = function(options) {
    var settings = jQuery.extend({
        
    }, options);
    
    var group = null;
    var identifier = null;
    var visible = false;
    
    function get_image_index() {
        var index = null;
        for (var i=0; i<lb_groups[group].length; i++) {
            if (lb_groups[group][i]['identifier'] == identifier) {
                index = i;
                break;
            }
        }
        return index;
    }
    
    function resize_overlay() {
        // set the overlay size
        $d = $(document);        
        var dw = $d.width();
        var dh = $d.height();
        if (!$.support.opacity) {
            // IE 6-8 hack
            dw -= 20;
            dh -= 40;
        }
        $o = $('.overlay');
        $o.css('width', dw+'px');
        $o.css('height', dh+'px');
    }
    
    function display_overlay() {
        visible = true;
        $('.overlay').css('display', 'block');
    }
    
    function hide_overlay() {
        visible = false;
        $('.overlay').css('display', 'none');
    }
    
    function center_lb() {
        // center it
        $w = $(window);
        $d = $(document);        
        $lbw = $('.lbwrap');        
        var ww = $w.width()
        var wh = $w.height();
        var st = $d.scrollTop();
        var sl = $d.scrollLeft();                
        var ow = $lbw.outerWidth()
        var oh = $lbw.outerHeight();
        //var vw = (ww-sl);
        //var vh = (wh-st);
        //console.log($(window).width());
        //console.log($(window).height());
        //console.log(dw, dh, st, sl, ow, oh);
        var bl = ww/2-ow/2+sl;
        if (bl < 0) {
            bl = 0;
        }
        var bt = wh/2-oh/2+st;
        if (bt < 0) {
            bt = 0;
        }
        $lbw.css('left', bl + 'px');        
        $lbw.css('top', bt + 'px'); 
    }
    
    function next_image() {
        index = get_image_index();
        output_image(index+1);
        resize_overlay();        
        center_lb();
        return false;
    }
    
    function previous_image() {
        index = get_image_index();
        output_image(index-1);
        resize_overlay();        
        center_lb();
        return false;
    }
    
    function output_image(index) {        
        var image = lb_groups[group][index];
        
        //update the current identifier in case we're coming from next/prev
        identifier = image['identifier'];
            
        
        var imgw = image['width'];
        var imgh = image['height'];
        
        // set the lightbox dimensions to help absolute positioning
        $lb = $('.lightbox');
        $lb.css('width', imgw+'px');
        //$lb.css('height', imgh+'px');
        
        // add the image, nav, title and description
        var html = '<img src="'+image['src']+'" width="'+imgw+'" height="'+imgh+'" />';
        html += '<div class="nav">';
        html += '<a href="#" class="next"><span>Next</span> &rarr;</a>';
        html += '<a href="#" class="prev">&larr; <span>Previous</span></a>';        
        if (typeof(image['comments']) != "undefined") {            
            html += '<a href="'+image['permalink']+'#comments" class="permalink">';
                        
            html += image['comments'] + ' comment';
            if (image['comments'] != 1) {
                html += 's'
            }
            
            html += '</a>';
        } else if (image['permalink']) {
            html += '<a href="'+image['permalink']+'" class="permalink">Permalink</a>';            
        }  else {
            html += '<span class="permalink">&nbsp;</span>';
        }
        html += '</div>';
        if (image['title'] || image['description']) {
            html += '<div class="text">';
            if (image['title']) {
                html += '<h4 class="title">'+image['title']+'</h4>';
            }
            if (image['description']) {
                html += '<div class="desc">'+image['description']+'</div>';
            }
            html += '</div>';
        }
        $lb = $('.lightbox');
        $lb.html(html);
        
        // show/hide next, handle click
        $next = $('.lightbox .next');
        if (index < lb_groups[group].length-1) {
            $next.css('display', 'block');
            $next.click(next_image);
        } else {
            $next.css('display', 'none');
        }
        
        // show/hide prev, handle click
        $prev = $('.lightbox .prev');
        if (index > 0) {
            $prev.css('display', 'block');
            $prev.click(previous_image);
        } else {
            $prev.css('display', 'none');
        }        
    }
    
    function show_image() {        
        // figure out the group and identifier
        group = null;
        identifier = this.href;
        
        var classnames = this.className.split(' ');
        for (var i=0; i<classnames.length; i++) {
            classname = classnames[i];
            if (classname.substring(0, 4) == 'grp_') {
                group = classname.substring(4);
                break;
            }
        }
        if (!group || !lb_groups[group]) {
            return false;
        }        
        var index = get_image_index(); 
        if (index == null) {
            return false;
        }
                
        output_image(index);
        
        resize_overlay();
        display_overlay(); // display before center so dimensions work
        center_lb();
        
        return false;
    }
    
    // install the divs at the end
    if (!$('.lightbox').length) {
        $('body').append('<div class="lightbox"></div>');
    }
    $('.lightbox').wrap('<div class="overlay"><div class="lbwrap"></div></div>'); 
    
    // ie doesn't like :hover on just anything
    $('.lightbox').mouseenter(function() {
        $('.lightbox').addClass('mouseover');
    });
    $('.lightbox').mouseleave(function() {
        $('.lightbox').removeClass('mouseover');       
    });
    
    // don't close if you click on the lightbox
    $('.lbwrap').click(function(e) {
        e.stopPropagation();        
    });
    
    // close if you click outside of the lightbox
    $(document).click(function() {         
        hide_overlay();
    });    
    
    // re-center the lightbox if we scroll
    //$(document).scroll(center_lb);
    
    // when clicking on a launcher, show the associated image
    this.click(show_image);
    
    return this;
}
