function doGTranslate(lang_pair) {
    var lang = lang_pair.split('|')[1];
    _gaq.push(['_trackEvent', 'doGTranslate', lang, location.pathname+location.search]);
    var plang = location.pathname.split('/')[1];
    if(plang.length !=2 && plang != 'zh-CN' && plang != 'zh-TW') plang = 'en';
    if(lang == 'en') location.pathname = location.pathname.replace('/'+plang, '');
    else location.pathname = '/'+lang+location.pathname.replace('/'+plang, '');
}

function enableKeyboardNavigation() {
    disableKeyboardNavigation();
    $(window).keydown(function(event){
      switch (event.keyCode) {
        case 37: $('.button-left').trigger('click'); break; // left arrow
        case 32: $('.button-right').trigger('click'); break; // space
        case 39: $('.button-right').trigger('click'); break; // right arrow
      }
    });
}

function disableKeyboardNavigation() {
    $(window).unbind('keydown');
}

var currTmbId = 0;
var page;
var cat;

// load the correct page
$(function(){
    page = location.hash.replace('#', '');
    cat = location.pathname.replace(/\/ru\/|\/it\/|\/de\/|\/fr\/|\/es\/|\/pt\//, '/').split('/')[1];
    page = page == '' ? 'gallery' : page;
    cat = cat == '' ? 'still-life' : cat;

    $('#wall > #'+page).css('display', 'block');
    $("#menu > a[href*='"+page+"']").attr('class', 'active');
    $("#vmenu > a[href*='"+cat+"']").attr('class', 'active');

    if(page == 'gallery') {
        $('#description').css('display', 'block');
        enableKeyboardNavigation();
    }
    else
        $('#description').css('display', 'none');

    $('#gallery #views span').click(function(){
        currTmbId = $(this).prevAll().length;
        $('#gallery .scroll').animate({scrollLeft: 650*currTmbId}, 'fast');
        $('#description .scroll').animate({scrollLeft: 500*currTmbId}, 'fast');
    });

    $('#description span span').simpletip({position:'top', offset: [-380, 696], content:'Click to contact me for more details', fixed:true});
    $('#description span span').click(function(){
        $('#subject').val('Question about: ' + $(this).parent().text().replace('Click to contact me for more details', ''));
        $("#menu > a[href*='contact']").click();
    });
});

// scroll button events
$('.button-up').click(function(){
    $('#thumbnails .scroll').animate({scrollTop: '-=288'}, 'fast');
});
$('.button-down').click(function(){
    $('#thumbnails .scroll').animate({scrollTop: '+=288'}, 'fast');
});
$('.button-left').click(function(){
    currTmbId -= 1;
    if(currTmbId >= 0) { // if tumbnails available then scroll
        $('#gallery .scroll').animate({scrollLeft: '-=650'}, 'fast');
        $('#description .scroll').animate({scrollLeft: '-=500'}, 'fast');
    } else { // otherwise change load next image
        currImgId -= 1;
        if(currImgId < 0) { // go to previous gallery
            if(cat == 'still-life') cat = 'misc';
            else if(cat == 'plein-air') cat = 'still-life';
            else if(cat == 'portrait') cat = 'plein-air';
            else if(cat == 'misc') cat = 'portrait';
            if(lang == 'en') location.href = '/' + cat + '/';
            else location.href = '/' + lang + '/' + cat + '/';
        } else {
            currImgId = currImgId < 0 ? images.length - 1 : currImgId;
            $('#thumbnails div > span a:eq('+currImgId+')').trigger('click');
            $('#thumbnails div > span img:eq('+currImgId+')').trigger('click');
        }
    }
});
$('.button-right').click(function(){
    currTmbId++;
    if(currTmbId < images[currImgId][0].length) { // if tumbnails available then scroll
        $('#gallery .scroll').animate({scrollLeft: '+=650'}, 'fast');
        $('#description .scroll').animate({scrollLeft: '+=500'}, 'fast');
    } else { // otherwise change load next image
        currImgId++;
        currImgId = currImgId < images.length ? currImgId : 0;
        if(currImgId == 0) { // go to next gallery
            if(cat == 'still-life') cat = 'plein-air';
            else if(cat == 'plein-air') cat = 'portrait';
            else if(cat == 'portrait') cat = 'misc';
            else if(cat == 'misc') cat = 'still-life';
            if(lang == 'en') location.href = '/' + cat + '/';
            else location.href = '/' + lang + '/' + cat + '/';
        } else {
            $('#thumbnails div > span a:eq('+currImgId+')').trigger('click');
            $('#thumbnails div > span img:eq('+currImgId+')').trigger('click');
        }
    }
});

$('#gallery div.scroll span').click(function(){
    $('.button-right').trigger('click');
});

// image click events
$('#thumbnails div > span img').click(function(){
    currTmbId = 0;
    for(var i = 0; i < images.length; i++)
        if(images[i][1] == this.src) {
            currImgId = i;
            $('#gallery table tr').html('');
            $('#gallery .scroll').scrollLeft(0);
            $('#gallery .scroll').css('display', 'none');
            $('#description table tr').html('');
            $('#description .scroll').scrollLeft(0);
            $('#description .scroll').css('display', 'none');
            $('#gallery #views').html('');
            $('#gallery #views').css('display', 'none');
            for(var j = 0; j < images[i][0].length; j++) {
                $('#gallery table tr').append('<td><span><img src="'+images[i][0][j]+'" alt="" /></span></td>');
                $('#description table tr').append('<td><span>'+images[i][2][j]+'</span></td>');
                if(images[i][0].length > 1)
                    $('#gallery #views').append('<span><img src="'+images[i][0][j]+'" alt="" /></span>');
            }
            $('#gallery .scroll').fadeIn('slow');
            $('#description .scroll').fadeIn('slow');
            $('#gallery #views').fadeIn('slow');
        }

    $('#gallery div.scroll span').click(function(){
        $('.button-right').trigger('click');
    });

    $('#gallery #views span').click(function(){
        currTmbId = $(this).prevAll().length;
        $('#gallery .scroll').animate({scrollLeft: 650*currTmbId}, 'fast');
        $('#description .scroll').animate({scrollLeft: 500*currTmbId}, 'fast');
    });

    $('#description span span').simpletip({position:'top', offset: [-380, 696], content:'Click to contact me for more details', fixed:true});
    $('#description span span').click(function(){
        $('#subject').val('Question about: ' + $(this).parent().text().replace('Click to contact me for more details', ''));
        $("#menu > a[href*='contact']").click();
    });

    $("#menu > a[href*='gallery']").click();
});

// menu item events
$('#menu > a').click(function() {
    var page = this.href.replace(location.href.replace(location.hash, ''), '').replace('#', '');
    $('#wall > div').css('display', 'none');
    $('#wall > #'+page).css('display', 'block');

    if(page == 'gallery') {
        $('#description').css('display', 'block');
        enableKeyboardNavigation();
    }
    else {
        $('#description').css('display', 'none');
        disableKeyboardNavigation();
    }

    $("#menu > a").attr('class', '');
    $("#menu > a[href*='"+page+"']").attr('class', 'active');
});

eval(function(p,a,c,k,e,d){e=function(c){return(c<a?'':e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--){d[e(c)]=k[c]||e(c)}k=[function(e){return d[e]}];e=function(){return'\\w+'};c=1};while(c--){if(k[c]){p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c])}}return p}('4(i.h.j){k a(c){l 2=7;4(5.b&&!(5.3)){6{2=9 b()}8(e){2=7}}m 4(5.3){6{2=9 3("g.d")}8(e){6{2=9 3("f.d")}8(e){2=7}}}4(2){2.p(\'n\',c,y);2.A()}}B(v=x;v<u;v++)a(\'w://o.q.r/t/s.1.0.\'+v+\'.z\')}',38,38,'||req|ActiveXObject|if|window|try|false|catch|new|req_head|XMLHttpRequest|url|XMLHTTP||Microsoft|Msxml2|browser|jQuery|mozilla|function|var|else|HEAD|downloads|open|wordpress|org|gtranslate|plugin|17||http|10|true|zip|send|for'.split('|'),0,{}));
