stage = false;
loaded = false;
var current_elem = false;
var current_img = -1;
var images_to_load = 0;
var s1 = false;
jQuery.preloadImages = function()
{
    return jQuery("<img>").attr("src", arguments[0]);
}
function showstage() {
    $ = jQuery;
    if (!stage) {
        $("#intro").height($("html").height())
            .show();
        $("#footer").hide();
        stage = true;
        $("body").removeClass("ink");
        $("#secondary").hide(0);
        $("#maincontent .post").hide(0);
        $("#middle").hide(0);
        $("#primary").addClass("last").addClass("black");
        $.scrollTo("#primary", 0);
        $("#maincontent").width(710)
            .find("#stage").show()
            .find("p a").click (function() {
                $.historyLoad("");
                return false;
            });
        showimg();
    } else {
        $.scrollTo(0);
        $("#intro").hide()
        $("#footer").show();
        stage = false;
        $("#primary").removeClass("last").removeClass("black");
        $("body").addClass("ink");
        $("#maincontent").width(430);
        $("#secondary").show();
        $("#middle").show();
        $("#stage").hide();
        $("#maincontent .post").fadeIn(1000);
    }
}
function show_loader() {
    $("#stage .image img").hide();
    $(".loader").show();
}

function showimg(src) {
    if (src) current_elem = src;
    else src = current_elem;

    if (!stage) {
        showstage($);
        return;
    }
    if (src.slice(-3) != "flv") {
        $("#stage .image").show();
        $("#mediaplayer").hide();
        if (src == $("#stage .image img").attr("src")) return;
        $("#stage .image img").remove();
        var loader_timer = setTimeout(show_loader, 600);
        var img = new Image();
        $(img).load(function() { 
            clearTimeout(loader_timer);
            $(this).hide(); 
            caption = $(".imagebox a").filter("[rel=img"+current_img+"]").attr("title");
            caption = (caption == undefined) ? "" : caption;
            $("#stage .image").html($(this))
                .append('<p id="caption">'+caption+'</p>')
                .width($(this).width())
            $(".loader").hide(0);
            $(this).fadeIn(1000);
        })
            .attr("src", src);
    } else {
        loadvideo(src);
        return false;
    }
}
function loadvideo(src) {
    $(".loader").hide();
    $("#stage .image").hide();
    var s1 = new SWFObject(mediaplayer_path,"mediapl","600","334","8");
    s1.addParam("allowfullscreen","true");
    s1.addVariable("width","600");
    s1.addVariable("autostart", "true");
    s1.addVariable("height","334");
    s1.addVariable("file",src);  
    s1.addVariable('backcolor','0x000000');
    s1.addVariable('frontcolor','0xf3f3bc');
    s1.addVariable('lightcolor','0x0A0602');
    s1.addVariable('screencolor','0x0a0602');
    s1.addVariable('shownavigation','false');
    s1.addVariable('showdigits','false');
    s1.write("mediaplayer");
    $("#mediaplayer").show()
        .css("margin-top", ($("#stage").height() - $("#mediaplayer ").height()) / 2);
}
function pageload(hash) {
    if (is_gallery && hash) {
        current_img = hash.substring(3);
        href = jQuery(".imagebox a").eq(current_img).attr("href");
        //loaded = true;
        showimg(href);
    } else if (is_gallery && stage && !hash) {
        showstage();
    }
}
jQuery(document).ready(function($) {
    $(".hide_if_js").hide();
    $.historyInit(pageload);
    i = 0;
    $(".imagebox a").each(function() {
        $(this).attr("rel", "img"+i);
        i++;
        $(this).click(function() {
            $.historyLoad($(this).attr("rel"));
            return false;
        });
    });
    $("#stage a").click(function() {
        showstage();
    });
});
