/*
 * $Date: 2010/01/12 05:52:11 $
 * $Revision: 1.8 $
 */

var Base64={_keyStr:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",decode:function(input){var output="";var chr1,chr2,chr3;var enc1,enc2,enc3,enc4;var i=0;input=input.replace(/[^A-Za-z0-9\+\/\=]/g,"");while(i<input.length){enc1=this._keyStr.indexOf(input.charAt(i++));enc2=this._keyStr.indexOf(input.charAt(i++));enc3=this._keyStr.indexOf(input.charAt(i++));enc4=this._keyStr.indexOf(input.charAt(i++));chr1=(enc1 << 2)|(enc2 >> 4);chr2=((enc2&15)<<4)|(enc3>>2);chr3=((enc3&3)<<6)|enc4;output=output+String.fromCharCode(chr1);if (enc3 != 64){output=output+String.fromCharCode(chr2);}if (enc4!=64){output=output+String.fromCharCode(chr3);}}output=Base64._utf8_decode(output);return output;},_utf8_decode:function (utftext){var string = "";var i=0;var c=c1=c2=0;while ( i < utftext.length ){c=utftext.charCodeAt(i);if (c < 128) {string += String.fromCharCode(c);i++;}else if((c > 191) && (c < 224)){c2=utftext.charCodeAt(i+1);string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));i+=2;}else{c2=utftext.charCodeAt(i+1);c3=utftext.charCodeAt(i+2);string+=String.fromCharCode(((c&15)<<12)|((c2&63)<<6)|(c3&63));i+=3;}}return string;}}
$('.link_to_tracker').live('click',function()
{
    name = this.id;
    name = name.replace(/(^\s+)|(\s+$)/g, "");
    rel  = $(this).attr('rel');
    type = $(this).hasClass('pt_play') ? 'play' : 'exe';

    pageTracker._trackPageview(type + '/' + fg4r_lang + '/' + name);

    if (rel != '' && rel != 'nofollow')
    {
        link = decode_link(rel);
        $(this).attr('target','_blank').attr('href', link);
    }
});


function decode_link(link)
{
    link = link.replace(/-_-/g, "=");
    link = link.replace(/-/g, "+");
    link = link.replace(/_/g, "/");
    return Base64.decode(link);
}


function show_image(elem, game_id)
{
    $(elem).find('.images_for_show')
           .find('img').attr('src', domains_lang+'/images/localized/'+lang+'/'+game_id+'/100x100.gif');
    $(elem).find('.images_for_show').show();
}

function hide_image(elem){$(elem).find('.images_for_show').hide();}

$(document).ready(function()
{
    $('#ul_preview a').imgPreview({
	containerID: 'imgpreview',
        srcAttr: 'rel',
	imgCSS: {
            // Limit preview size:
	    height: 100,
    	width: 100
	}
    });
});

/*
 * addthis buttons
 */

function set_bookmark(){var bm_index=0;var bm_array=new Array();var bm_value=$.cookie('dg_bookmark');if (bm_value){bm_array=bm_value.split(';');if ((bm_array.length)==16){bm_array=new Array();bm_value='';}else{bm_value=';'+bm_value+';';}}else{bm_value='';}do{bm_index=Math.floor(Math.random()*16);}while(bm_value.search(';'+bm_index+';')!= -1)bm_array.push(bm_index);var date=new Date(0);var expires=date.getUTCSeconds()+15552000;$.cookie('dg_bookmark', bm_array.join(';'), { expires: expires, path: '/' });get_bookmark(bm_index);}
function get_bookmark(index){switch(parseInt(index)){case 0:img='aim.png';cls='aim';break;case 1:img='blogger.png';cls='blogger'; break;case 2:img='delicious.png';cls='delicious'; break;case 3:img='digg.png';cls='digg';break;case 4:img='facebook.png';cls='facebook';break;case 5:img='google.png';cls='google';break;case 6:img='mixx.png';cls='mixx';break;case 7:img='myspace.png';cls='myspace';break;case 8:img='reddit.png';cls='reddit';break;case 9:img='stumbleupon.png';cls='stumbleupon';break;case 10:img='technorati.png';cls='technorati';break;case 11:img='twitter.png';cls='twitter';break;case 12:img='yahoo.png';cls='yahoobkm';break;case 13:img='friendfeed.png';cls='friendfeed';break;case 14:img='bebo.png';cls='bebo';break;case 15:img='linkedin.png';cls='linkedin';break;}$('#bb_at').attr('class', 'addthis_button_' + cls).append('<img src="/images/view2/bmk/'+img+'" alt="Bookmark with '+cls+'" width="140" height="80" />');}

/*
 * screenshots
 */
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();
    }
}


