var CLASS_NAME = "image_fotogallery";
var SCREEN_X   = screen.width;
var SCREEN_Y   = screen.height;

var current_id = 0; //current image
var photo_list = null;

window.onload = init;

function init()
{
    //alert("init");
    photo_list = make_photo_list( CLASS_NAME );
    SCREEN_Y = getDocHeight();
    SCREEN_X = getDocWidth();
    
}

function getDocHeight() {
    var D = document;
    return Math.max(
        Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
        Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
        Math.max(D.body.clientHeight, D.documentElement.clientHeight)
    );
}

function getDocWidth() {
    var D = document;
    return Math.max(
        Math.max(D.body.scrollWidth, D.documentElement.scrollWidth),
        Math.max(D.body.offsetWidth, D.documentElement.offsetWidth),
        Math.max(D.body.clientWidth, D.documentElement.clientWidth)
    );
}

function make_photo_list( name_attr )
{
    var items = document.getElementsByTagName("img");
    //alert(items.length);
    var output = new Array();
    var i = 0;
    var q = 0;

    while(items[i])
    {
        
        if(items[i].className == name_attr)
        {
            //alert("found")
            output[q] = items[i];
            q++;        
        }

        i++;
    }

    return output;
}

function get_id( addr_photo )
{
    var list = photo_list;
    var i = 0;
    var str = addr_photo;
    var tmp = str.split('/');
    var real_name = tmp[3];
    //alert(real_name);

    while(list[i])
    {
        str = list[i].getAttribute("src");
        /*
        var real_source = str.split('thumb-');
        if(real_source[real_source.length-1] == real_name)
        {
            //alert("matching");
            return i;
        }*/

        if( str.match( real_name ) )
            return i;
        i++;
    }
    return -1; //NO SUCH PHOTO!
}

function update_img()
{
    var img = document.getElementById("main_photo");

    var static_addr = (photo_list[current_id].getAttribute("src"));
    static_addr = static_addr.split("thumb-");
    var out = static_addr[0] + (!static_addr[1] ? "" : static_addr[1]);

    //alert(out);

    img.setAttribute("src" , out);
}

function decrease_id()
{
    if(!current_id)
        return false;

    current_id--;

    update_img();
    

    return true;
}

function increase_id()
{
    var len = photo_list.length;

    if(!(current_id < (len-1)))
        return false;


    //alert(len+":"+current_id);

    current_id++;

    update_img();

    return true;
}    

function activate_photo( addr_photo )
{
    var id_photo = get_id( addr_photo );
    var canvas   = document.getElementById("photo");

    if(id_photo < 0 || !canvas) // ERROR
    {
        //alert(id_photo);
        return false;
    }

    
    current_id = id_photo;

    canvas.style.width = SCREEN_X+'px';
    canvas.style.height= SCREEN_Y+'px';
    canvas.style.zIndex= 100;
    canvas.innerHTML = "<div class='wnd'><span class='decrease' id='decrease' onmousedown='decrease_id()'>&lt;&lt;</span><span class='decrease' onmousedown='deactivate_photogallery()'>Zavřít</span><span class='increase' id='increase' onmousedown='increase_id()'>&gt;&gt;</span><br/><img src='"+addr_photo+"' id='main_photo' style='z-index: 101;' alt='Kliknutím zavřete' class='front_photo'/></div>";
}

function deactivate_photogallery()
{
    var canvas   = document.getElementById("photo");
    canvas.style.width = 0+'px';
    canvas.style.height= 0+'px';
    canvas.style.zIndex= 0;
    canvas.innerHTML = "";
    return true;
}
    

        

