$(document).ready(function() {

    $("a").each(function() {
        var ohr = "" + $(this).attr("href");

        //alert(ohr.indexOf(".jpg"));

        if (ohr.indexOf(".jpg") >= 0) {
            //var ohr=$(this).attr("href");
            $(this).attr("href", "javascript:;");
            $(this).attr("target", "");
            $(this).click(function() {
                showImage(ohr);
            });
        }

    });

    if ($("#div_current_image").length != 0) {
        //JOS LÖYTYY KUVAROTAATTORI :)
        $(".hf_rotator").attr("showing", "false");
        $(".hf_curr").attr("showing", "true");
        setTimeout(function() { showNextRotation(); }, 5000);

    }

});

var currentRotation = 0;
function showNextRotation() {
    //alert(currentRotation);
    var curr = 0;
    var found = false;
    $(".hf_rotator[showing=false]").each(function() {

        if (!found) {
            found = true;
            //alert($(this).val().replace("_thumb", ""));
            $("#div_current_image").css("background-image", "url(" + $(this).val().replace("_thmb", "") + ")");
            $(this).attr("showing", "true");
            currentRotation = currentRotation + 1;
            setTimeout(function() { showNextRotation(); }, 5000);

        }

    });

    if (!found) {
        $(".hf_rotator").attr("showing", "false");
        showNextRotation();
    }

}

function showImage(id, text) {
        //alert(id);
        if ($("#div_image").length == 0) {
        
            $("body").append("<div id='div_image' style='padding: 20px; z-index: 9999; position: absolute; background-color: black; border: 1px solid gray; display: none;' onclick='hideImage();'></div>");
            $("body").append("<div id='div_image_back' style='z-index: 9998; position: absolute; background-color: black; top: -20px; left: -20px;' onclick='hideImage();'>&nbsp;</div>");

        }

        
        $("#div_image").html('<img id="img_image" src="' + id + '" align="absmiddle" onclick="hideImage();" onload="resizeDiv();" /><div style="font-size: 12px; color: black; padding-top: 10px; padding-bottom: 10px;" id="div_text">Sulje kuva klikkaamalla sitä.</div>');
        $("#div_image").css("top", "-5000px");
        $("#div_image").css("display", "");
        
    }

    function resizeDiv() {

        $("#div_image_back").fadeTo("fast", 0.01, function() {
            $("#div_image_back").css("left", "0px");
            $("#div_image_back").css("width", $(window).width() + "px");
            $("#div_image_back").css("top", "0px");

            var maxHeight = 0;
            maxHeight = $(window).height();

            if ($(document).height() > maxHeight) maxHeight = $(document).height();

            $("#div_image_back").css("height", maxHeight + "px");
            $("#div_image_back").fadeTo("fast", 0.80, function() {

                var pleft = ($(window).width() - $("#img_image").width() - 20) / 2;
                $("#div_text").css("width", $("#img_image").width() + "px");
                $("#div_image").css("left", pleft + "px");
                $("#div_image").hide();
                $("#div_image").css("top", $(window).scrollTop() + 20 + "px");
                $("#div_image").fadeIn();

            });
        });

        
    }

    function hideImage() {

        $("#div_image").fadeOut();
        $("#div_image_back").fadeTo("fast", 0.01, function() {

        $("#div_image_back").css("left", "-10px");
        $("#div_image_back").css("width", 1 + "px");
        $("#div_image_back").css("height", 1 + "px");
        $("#div_image_back").css("top", "-10px");


        });

    }

function openImage(id) {
showImage(id,'');
}

