/* Greybox Redux
 * Required: http://jquery.com/
 * Written by: John Resig
 * Based on code by: 4mir Salihefendic (http://amix.dk)
 * License: LGPL (read more in LGPL.txt)
 */
 
var GB_HEIGHT = 400;
var GB_WIDTH = 400;
var left = null;
var right = null;

function GB_show(caption, url, height, width) {
    GB_HEIGHT = height || GB_HEIGHT;
    GB_WIDTH = width || GB_WIDTH;
    
    GB_load_content(url);
    
    $(document.body)
        .append(
        "<div id='GB_overlay'></div><div id='GB_window'><div id='GB_caption'></div>"+
        "<img class='button' src='/site_media/img/close.png' alt='Close window'/></div>");
    $("#GB_window img").click(GB_destroy);
    $("#GB_overlay").click(GB_destroy);
    $(window).resize(GB_position);

    $("#GB_window").before(
        "<img id='GB_left_button' src='/site_media/img/arrow-left.png' />");
    $("#GB_window").after(
        "<img id='GB_right_button' src='/site_media/img/arrow-right.png' />");
    
    $("#GB_left_button").hover(function() {
        $("#GB_left_button").attr("src", "/site_media/img/arrow-left-hover.png")},
        function() {
        $("#GB_left_button").attr("src", "/site_media/img/arrow-left.png")});
    $("#GB_left_button").click(GB_left_click);
    $("#GB_right_button").hover(function() {
        $("#GB_right_button").attr("src", "/site_media/img/arrow-right-hover.png")},
        function() {
        $("#GB_right_button").attr("src", "/site_media/img/arrow-right.png")});
    $("#GB_right_button").click(GB_right_click);

    $("#GB_caption").html(caption);
    $("#GB_overlay").show();
    GB_position();
}

function GB_load_content(url) {
    $.getJSON("/gallery/get/"+url, function(data) {
        left = data.left;
        right = data.right;
        
        if (left == null) $("#GB_left_button").hide();
        else $("#GB_left_button").show();
        if (right == null) $("#GB_right_button").hide();
        else $("#GB_right_button").show();
        
        if ($("#GB_content").length) {
            $("#GB_content").fadeOut(function() {
                $("#GB_content").remove();
                $("#GB_window").append(
                    "<div id='GB_content'><img src='"+data.url+"' /></div>");
                $("#GB_content").hide();
                $("#GB_content").fadeIn();
                $("#GB_content").click(GB_destroy);
                })
        } else {
            $("#GB_window").append(
                "<div id='GB_content'><img src='"+data.url+"' /></div>");
            $("#GB_content").hide();
            $("#GB_content").fadeIn();
            $("#GB_content").click(GB_destroy);
        }
    });
}

function GB_left_click() {
    if (left == null) return;
    GB_load_content(left);
}

function GB_right_click() {
    if (right == null) return;
    GB_load_content(right);
}

function GB_destroy() {
    $("#GB_window,#GB_overlay,#GB_left_button,#GB_right_button").remove();
}
$("#GB_window").append(
        "<p id='GB_content'>"+content+"</p>");
function GB_position() {
    var de = document.documentElement;
    var w = self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
    $("#GB_window").css({width:GB_WIDTH+"px",height:GB_HEIGHT+"px",
    left: ((w - GB_WIDTH)/2)+"px" });
}
