/*
 * $Date: 2010/02/01 10:35:02 $
 * $Revision: 1.2 $
 */


$('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()
{
    if (browserVersion().name != "MSIE") hide_id('shots_div', 50);
    else $('#shots_div').hide();

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

function ch_shot(id)
{
    var file_name = '/images/screenshots/' + id + '_big.jpg';
    $('#shots_pic').html('<img src="'+file_name+'" width="640" height="480" border="0">');
    update_page(id);
}

function  getPageSize()
{
    var xScroll, yScroll;
    var windowWidth, windowHeight;

    if (window.innerHeight && window.scrollMaxY)
    {
	xScroll = document.body.scrollWidth;
        yScroll = window.innerHeight + window.scrollMaxY;
    }
    else if (document.body.scrollHeight > document.body.offsetHeight)
    {
	// all but Explorer Mac
        xScroll = document.body.scrollWidth;
        yScroll = document.body.scrollHeight;
    }
    else if (document.documentElement && document.documentElement.scrollHeight > document.documentElement.offsetHeight)
    {
	// Explorer 6 strict mode
        xScroll = document.documentElement.scrollWidth;
        yScroll = document.documentElement.scrollHeight;
    }
    else
    {
	// Explorer Mac...would also work in Mozilla and Safari
        xScroll = document.body.offsetWidth;
        yScroll = document.body.offsetHeight;
    }

    if (self.innerHeight)
    {
	// all except Explorer
        windowWidth = self.innerWidth;
        windowHeight = self.innerHeight;
    }
    else if (document.documentElement && document.documentElement.clientHeight)
    {
	// Explorer 6 Strict Mode
        windowWidth = document.documentElement.clientWidth;
        windowHeight = document.documentElement.clientHeight;
    }
    else if (document.body)
    {
	// other Explorers
        windowWidth = document.body.clientWidth;
        windowHeight = document.body.clientHeight;
    }

    // for small pages with total height|width less then height|width of the viewport
    pageHeight = yScroll < windowHeight ? windowHeight : yScroll;
    pageWidth  = xScroll < windowWidth  ? windowWidth  : xScroll;

    pageWidth -= 20;

    return [pageWidth, pageHeight, windowWidth, windowHeight];
}


function load_preview()
{
    var id = this.id;

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

    var x  = 0;
    var y  = 0;
    var bg = document.getElementById('s_shots_bg');

    var page_size    = getPageSize();
    var file_name    = '/images/screenshots/' + id + '_big.jpg';
    var browser_name = browserVersion().name;

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

    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;
    }
    else if (browser_name == "Firefox")
    {
        x = window.innerWidth*0.5 - 340 + window.scrollX;
        y = window.innerHeight*0.5 - 302 + window.scrollY;
    }
    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;
    }

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

    bg.style.width   = page_size[0]+"px";
    bg.style.height  = page_size[1]+"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();
    }
}

