/*
 * $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');

    _gaq._trackPageview('exe/' + fga_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);
}

/*
 * 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/bmk/'+img+'" alt="Bookmark with '+cls+'" width="140" height="80" />');}

function set_actions()
{
    if (document.getElementById('bb_at')) set_bookmark();
    rating_events();
}

$(document).ready(set_actions);

function rating_events()
{
    $('.vote img').each(function() {
        $(this).hover(
            function () {
                $(this).css('background-color', '#FF9231')
                       .prevAll().css('background-color', '#FF9231');
            },
            function () {
                $(this).css('background-color', 'transparent')
                       .prevAll().css('background-color', 'transparent');
            }
        )
        .click(function(){
            var parent  = $(this).parent().parent();
            var game_id = $(parent).attr('id');
            var score   = $(this).attr('class');

            $.post('/game/vote',
                   { 'game_id': game_id, 'score': score },
                   function(score) {

                if (!score) return 0;
                $('.vote img').each(function(){$(this).unbind('mouseenter mouseleave').removeAttr('style');});
                $(parent).removeClass('vote').find('.fake').css('width', score + 'px');
            });

            return false;
        });
    });
}

function show_online_ads(){if (!document.getElementById('ads_container')) return 0;$('#play_container').hide();$('#ads_container').show();setTimeout(load_online_game, 15000);}
function load_online_game(){$('#ads_container').remove();$('#play_container').show();}


