/* Gallery functions
# +====+====+====+====+====+====+====+====+ */ 
function makeBoolean(value){
    if (value == "true") {
        return true;
    }
    if (value == "false") {
        return false;
    }
    return "";
}
function getImage(id) {
    
    
    var thumbW = $("#thumbnails").width();
    var thumbH = $("#thumbnails").height();
    
    
    // Build the spinner (12 blades, WebKit only)
    var spinner = "<div id='spinner'><div class='white spinner'>";
    for (var i=1;i<13;i++) { spinner += "<div class='bar"+i+"'></div>"; }
    spinner += "</div></div>";
    // Set the spinner in the blackout
    $("#blackout").html(spinner);
    
    
    // Blackput setup...
    $("#blackout").css("width", thumbW)
    $("#blackout").css("height", thumbH)
    $("#blackout").fadeIn("fast");
    
    
    // Position the spinner in the blackout
    $("#spinner").css("marginLeft", ((thumbW / 2) - $("#spinner").width() / 2));
    if (thumbH < 601) {
        $("#spinner").css("marginTop", ((thumbH / 2) - ($("#spinner").height() / 2)));
    } else {
        $("#spinner").css("marginTop", 246);
    }
    
    
    // Append the close button
    $("#blackout").append("<img id='close' src='/i/close.60px-white.png' />");
    $("#close").click(function(){
        $("#blackout").fadeOut("fast");
    });
    
    
    // Add the target to the blackout
    $("#blackout").append("<div id='target'></div>");
    $("#target").css("width", thumbW)
    $("#target").css("height", thumbH)
    var image = $(("#" + id)).attr("data-image");
    var title = $(("#" + id)).attr("data-title");
    var href = $(("#" + id)).attr("href");
    var imageW = $(("#" + id)).attr("data-width");
    var imageH = $(("#" + id)).attr("data-height");
    var source = $(("#" + id)).attr("data-source");
    var previous = makeBoolean($(("#" + id)).attr("data-previous"));
    var next = makeBoolean($(("#" + id)).attr("data-next"));
    var id = parseInt(parseFloat(id.split("image")[1]));
    
    
    // Setup the image
    $("#target").hide();
    $("#target").html("<img id='image' src='" + image + "' />");
    // Set Offset
    $("#image").css("marginLeft", ( (thumbW / 2) - (imageW / 2) ) );
    if (thumbH < 601) {
        $("#image").css("marginTop", ( (thumbH / 2) - (imageH / 2) ) );
    } else {
        $("#image").css("marginTop", (300 - (imageH / 2) ) );
    }
    // Show the image
    $("#target").fadeIn("fast");
    
    
    // Append the title
    if (source != "") {
        var domain = source.split("//")[1].split("/")[0];
        title = "<a href='" + source + "'>" + title + "</a> [<a href='" + source + "'>via: " + domain + "</a>]";
    }
    $("#blackout").append("<div id='title'>" + title + " (<a href='" + href + "'>#</a>)</div>");
    $("#title").css("marginTop", (thumbH - 50))
    
    
    // Set next image click function...
    if (next) {
        // Append the next button
        $("#blackout").append("<img id='next' src='/i/arrow.right.73x62px.white.png' />");
        $("#next").css("marginLeft", (thumbW - 82));
        $("#next").css("marginTop", (thumbH - 110));
        $("#next").click(function(){
            getImage("image" + (id + 1).toString());
        });
        $("#image").click(function(){
            getImage("image" + (id + 1).toString());
        });
    } else {
        $("#image").click(function(){
            $("#blackout").fadeOut("fast");
        });
    }
    
    
    // Set previous image click function...
    if (previous) {
        // Append the previous button
        $("#blackout").append("<img id='previous' src='/i/arrow.left.73x62px.white.png' />");
        $("#previous").css("marginLeft", 10);
        $("#previous").css("marginTop", (thumbH - 110));
        $("#previous").click(function(){
            getImage("image" + (id - 1).toString());
        });
    }
}
function gallery_setup() {
    if (window.location.href.search("images") != -1) {
        $('.image.link').click(function(){
            //getImage(id, image, title, source, previous, next);
            getImage(this.id)
            return false;
        });
    }
}
/* +====+====+====+====+====+====+====+====+ */ 




/* Command-Line Interface Search (CLI)
# +====+====+====+====+====+====+====+====+ */ 
function cli() {
    if ($("#cli_search").hasClass("autocomplete")) {
        $("#cli_search").show();
        $(function() {
            var cache = {},
                lastXhr;
            $( "#command" ).autocomplete({
                source: function( request, response ) {
                    var term = request.term;
                    if ( term in cache ) {
                        response( cache[ term ] );
                        return;
                    }
                    lastXhr = $.getJSON( "/search/cli/autocomplete.django", request, function( data, status, xhr ) {
                        cache[ term ] = data;
                        if ( xhr === lastXhr ) {
                            // Remap the JSON to expected jQuery UI 
                            response( $.map( data, function( item ) {
                                return {
                                    label: item.fields.title,
                                    value: item.fields.urltitle,
                                    id:    item.pk
                                }
                            }));
                        }
                    });
                },
                minLength: 2,
                delay: 0,
                select: function( event, ui ) {
                    ui.item ?
                        //Redirect user to article
                        window.location = window.location.pathname + ui.item.value + ".html":
                        "Nothing selected, input was " + this.value;
                },
            });
        });
    }
}
/* +====+====+====+====+====+====+====+====+ */ 




/* ONLOAD document functions
# +====+====+====+====+====+====+====+====+ */ 
$(document).ready(function(){
    gallery_setup();
    //about();
    cli();
});
/* +====+====+====+====+====+====+====+====+ */ 

