/*
 * $Date: 2010/01/12 05:58:55 $
 * $Revision: 1.1 $
 */


$('a.thumbnail-image').live('click', load_preview);

var preview_zoomed = false;

function browserVersion()
{
    var version = "";
    var name    = "";
    var ua      = navigator.userAgent;

    var offset  = ua.indexOf("Firefox");

    if (ua.indexOf("Firefox") > -1)
    {
        name    = "Firefox";
        version = ua.substring(offset+8, ua.indexOf("(", offset));
    }

    offset = ua.indexOf("MSIE ");

    if (offset > -1)
    {
        name    = "MSIE";
        version = parseFloat(ua.substring(offset+5, ua.indexOf(";", offset)));
    }

    offset = ua.indexOf("Opera");

    if (offset > -1)
    {
        name    = "Opera";
        version = ua.substring(offset+6, ua.indexOf("(", offset));
    }

    navname = navigator.appName;

    return {type:navname, version:version, name:name};
}

function update_page(id)
{
    id  = (/^(.+?)-(\d+)$/).exec(id);

    var cur_shot  = parseInt(id[2]);
    var file_name = id[1];

    if (cur_shot > 1)
    {
        $('#shots_prev').removeClass('shots_disabled').css('visibility','visible');

        document.getElementById('shots_prev').onclick =
            function() {
                ch_shot(file_name+"-"+(cur_shot - 1));
                return false;
            }
    }
    else
    {
        $('#shots_prev').addClass('shots_disabled').css('visibility','hidden');
    }
    if (cur_shot < 3)
    {
        $('#shots_next').removeClass('shots_disabled').css('visibility','visible');

        document.getElementById('shots_next').onclick =
            function() {
                ch_shot(file_name+"-"+(cur_shot + 1));
                return false;
            }
    }
    else
    {
        $('#shots_next').addClass('shots_disabled').css('visibility','hidden');
    }
}

function hide_shots()
{
//    $('#DG-video').show();
    if (browserVersion().name != "MSIE") hide_id('shots_div', 50);
    else $('#shots_div').hide();

    $('#s_shots_bg').hide();
}

function ch_shot(id)
{
    var data      = id.match(/^(.+?)-(\d+)$/);
    var game_id   = data[1];
    var screen_id = data[2];

    var src = domains_lang + '/images/localized/' + lang + '/' + game_id + '/big_' + screen_id + '.jpg';
    $('#shots_pic').html('<img src="'+src+'" width="640" height="480" border="0">');
    update_page(id);
}

function load_preview()
{
//    $('#DG-video').hide();

    var id = this.id;

    var data      = id.match(/^(.+?)-(\d+)$/);
    var game_id   = data[1];
    var screen_id = data[2];

    $('a.thumbnail-image').removeClass('thumbnail-image-active');
    $(this).addClass('thumbnail-image-active');

    var x = 0;
    var y = 0;
    var scroll_y  = 0;
    var scroll_x  = 0;
    var src = domains_lang + '/images/localized/' + lang + '/' + game_id + '/big_' + screen_id + '.jpg';

    $('#shots_pic').html('<img src="'+src+'" width="640" height="480" border="0">');

    var browser_name = browserVersion().name;

    if (browser_name == "MSIE")
    {
        x = document.documentElement.offsetWidth*0.5 - 340 + document.documentElement.scrollLeft + document.body.scrollLeft;
        y = document.documentElement.offsetHeight*0.5 - 302 + document.documentElement.scrollTop + document.body.scrollTop;
        if (y<0)y = document.documentElement.scrollTop + document.body.scrollTop;
        if (x<0)x = document.documentElement.scrollLeft + document.body.scrollLeft;
        scroll_y = document.documentElement.scrollTop + document.body.scrollTop;
        scroll_x = document.documentElement.scrollLeft + document.body.scrollLeft;
    }
    else if (browser_name == "Firefox")
    {
        x = window.innerWidth*0.5 - 340 + window.scrollX;
        y = window.innerHeight*0.5 - 302 + window.scrollY;
        scroll_y = window.scrollY;
        scroll_x = window.scrollX;
    }
    else if(browser_name == "Opera")
    {
        x = window.innerWidth*0.5 - 340 + document.documentElement.scrollLeft + document.body.scrollLeft;
        y = window.innerHeight*0.5 - 302 + document.documentElement.scrollTop + document.body.scrollTop;
        scroll_y = document.documentElement.scrollTop + document.body.scrollTop;
        scroll_x = document.documentElement.scrollLeft + document.body.scrollLeft;
    }

    if(x==0)
    {
        x = window.innerWidth*0.5 - 340 + window.scrollX;
        y = window.innerHeight*0.5 - 302 + window.scrollY;
        scroll_y = window.scrollY;
        scroll_x = window.scrollX;
    }

    win_height = document.body.clientHeight;
    win_width  = document.body.clientWidth;

    var bg = document.getElementById('s_shots_bg');

//    bg.style.width   = win_width +"px";
//    bg.style.height  = win_height+"px";
    bg.style.display = 'block';

    if (y<0) y=1;
    var load = document.getElementById('shots_div');
    load.style.left = x+"px";
    load.style.top  = y+"px";
    if ((x > 0)&&(y > 0)) load.style.display = 'block';
    update_page(id);

    if (browser_name != "MSIE") show_id('shots_div',30,98);

    return false;
}

function show_id(elem_id, begin, end)
{
    begin = begin + 3;

    document.getElementById(elem_id).style.filter="alpha(opacity="+begin+")";
    document.getElementById(elem_id).style.opacity = begin/100;

    t = setTimeout("show_id(\""+elem_id+"\","+begin+","+end+")",1);

    if (begin>=end) clearTimeout(t);
}

function hide_id(elem_id, begin)
{
    begin = begin - 5;

    document.getElementById(elem_id).style.filter  = "alpha(opacity="+begin+")";
    document.getElementById(elem_id).style.opacity = begin/100;

    t = setTimeout("hide_id(\""+elem_id+"\","+begin+")",1);
    if (begin<=0)
    {
        clearTimeout(t);

        $('#'+elem_id).hide();
    }
}
