/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Отображает заданную картинку.
 * @param string src путь к картинке для отображения
 */
function imagebox_show(src)
{
    // Создаем полупрозрачный фон
    var wrapper = document.createElement("div");
    wrapper.setAttribute("id", "imagebox-wrapper");
    document.body.appendChild(wrapper);

    var div = hydra.get("b-imagebox");
    // Еще не создан div - создаем его
    if (!div) {
        div = document.createElement("div");
        div.setAttribute("id", "b-imagebox");

        var img = document.createElement("img");
        img.setAttribute("id", "b-imagebox-img");
        hydra_add_event(img, "click", function(){ imagebox_hide(); });
        div.appendChild(img);
        document.body.appendChild(div);
    }
    else {
        img = hydra.get("b-imagebox-img");
    }

    img.setAttribute("src", src);


    // Позционирование блока с картинкой исходя из размеров картинки
    imagebox_center_elem(img, div);

    // Создаем кнопку close
    var a = document.createElement("a");
    hydra_add_event(a, "click", function(){ imagebox_hide(); });
    a.setAttribute("title", "Закрыть");
    div.appendChild(a);

    div.style.display = "block";
}

function imagebox_hide()
{
    var div = hydra.get("imagebox-wrapper");
    document.body.removeChild(div);

    var div = hydra.get("b-imagebox");
    document.body.removeChild(div);
}

function imagebox_center_elem(img, div)
{
    var img_width = img.clientWidth;
    var img_height = img.clientHeight;

    var screen_width = screen.width;
    var screen_height = screen.height;

    var div_top = ((screen_height - img_height)/2)-100;
    var div_left = (screen_width - img_width)/2;

    div.style.top = div_top+"px";
    div.style.left = div_left+"px";
}