var images = [];
var is_ready = false;
var is_loaded = false;
var is_wrapped = false;

function Image(link, preview, title, description, comments) {
    this.link = link;
    this.preview = preview;
    this.title = title;
    this.description = description;   
    this.comments = comments;
}

function get_image_data(link) {
    for (var i=0; i<images.length; i++) {
        image = images[i];        
        if (image.link == link) {            
            html = '<div class="image">';
            html += '<img src="'+image.preview+'" />';
            
            if (i > 0) {
                // prev
                h = images[i-1].link;
                html += '<a href="#" class="prev" title="previous" onclick="jQuery.facebox(get_image_data(\''+h+'\')); return false;"><div class="icon">&nbsp;</div></a>';
            }
            if (i < images.length-1) {
                // next
                h = images[i+1].link; 
                html += '<a href="#" class="next" title="next" onclick="jQuery.facebox(get_image_data(\''+h+'\')); return false;"><div class="icon">&nbsp;</div></a>';
            }
            
            html += '</div>';
            
                html += '<div class="info">';
                if (image.title) {
                    html += '<div class="title"><h3>'+image.title+'</h3></div>';
                }
                if (image.description) {
                    html += '<div class="description">'+image.description+'</div>';
                }
                if (comments_enabled) {
                    plural = (image.comments == 1) ? '' : 's';                    
                    html += '<div class="permalink"><a href="'+link+'">'+image.comments+' comment'+plural+'</a></div>';
                } else {
                    html += '<div class="permalink"><a href="'+link+'">fullscreen</a></div>';
                }
                html += '</div>';
            
            
            return html;
        }
    }
}

function wrap_elements(elements) {
    if (elements) {
        // add the widths of all the elements in the row together so we can do
        // something like margin: 0 auto; on the row to center it
        width = 0;
        for (var i=0; i<elements.length; i++) {
            w = jQuery(elements[i]).outerWidth();                
            width += w;                
        }
        jQuery(elements).wrapAll('<div class="row" style="width: '+width+'px"></div>');            
    }
}
function add_rows() {    
    if (is_wrapped) {
        return
    }
    
    is_wrapped = true;
    
    rows = []
    ctop = 0;
    celements = [];
    
    // group the thumbs into row arrays based on their top positions
    $(".photos .photo").each(function(i, el) {
        t = jQuery(el).position().top;        
        if (t != ctop) {
            if (celements.length) {                
                rows.push(celements);
            }
            celements = [];
            ctop = t;
        }
        celements.push(el)
    });
    if (celements.length) {        
        rows.push(celements);
    }
    
    // if the last row has only one thumb, add one from the previous row
    if (rows.length > 1) {
        if (rows[rows.length-1].length == 1) {
            rows[rows.length-1].unshift(rows[rows.length-2].pop());
        }
    }
    
    // wrap each row with a div so we can center them 
    for (var i=0; i<rows.length; i++) {        
        wrap_elements(rows[i]);
    }
    
}
