var artistId = -1;
function showArtist(id, albumId, dapt) {
    if (artistId != id) {
        artistId = id;
        Element.hide('artist');
        new Ajax.Updater('artist', 'artist.php?id='+artistId+'&albumId='+albumId+'&dapt='+dapt, {asynchronous:false});
        new Effect.Appear('artist');
    }
}

function showDefaultArtist() {
    showArtist(gARTISTID, gALBUMID, gDAPT);
}

var curPage, maxPage;
var contentShown = false;
window.onload=function() {
    curPage = 0;
    maxPage = $('tracksTable').getElementsByTagName('tbody').length - 1;
    Element.update('currentPage', '' + (curPage + 1));
    Element.update('totalPages', '' + (maxPage + 1));
    Element.show('page0');
    if (maxPage > curPage) {
        $('right').style.visibility = 'visible';
    }
    splash();
}

function splash() {
    new Effect.Appear('splash', {duration:3.0, afterFinish:waitLanding});
}

function waitLanding() {
    if (!contentShown) {
        setTimeout('landing()', 1000);
    }
}

function landing() {
    if (!contentShown) {
        new Effect.Fade('splash', {duration:3.0, afterFinish:landingShow});
    }
}

function landingShow() {
    if (!contentShown) {
        new Effect.Appear('landing', {duration:3.0, afterFinish:waitContent});
    }
}

function waitContent() {
    if (!contentShown) {
        //setTimeout('enter()', 10000);
    }
}

function enter() {
    if (!contentShown) {
        contentShown = true;
        if (Element.visible('splash')) {
            new Effect.Shrink('splash', {afterFinish:contentShow});
        } else if (Element.visible('landing')) {
            new Effect.Shrink('landing', {afterFinish:contentShow});
        } else {
            contentShow();
        }
    }
}

function contentShow() {
    new Effect.Grow('content', {afterFinish:showDefaultArtist});
}

function left() {
    if (curPage != 0) {
        hideArrows();
        new Effect.toggle('page' + curPage--, 'slide', {duration:0,afterFinish:function(){new Effect.toggle('page' + curPage, 'slide', {duration:0,afterFinish:showArrows});}});
    }
}

function right() {
    if (curPage < maxPage) {
        hideArrows();
        new Effect.toggle('page' + curPage++, 'slide', {duration:0,afterFinish:function(){new Effect.toggle('page' + curPage, 'slide', {duration:0,afterFinish:showArrows});}});
    }
}

function hideArrows() {
    $('left').style.visibility = 'hidden';
    $('right').style.visibility = 'hidden';
}

function showArrows() {
    Element.update('currentPage', '' + (curPage + 1));
    if (maxPage > curPage) {
        $('right').style.visibility = 'visible';
    }
    if (curPage != 0) {
        $('left').style.visibility = 'visible';
    }
}

function play(id) {
    if (document.QuicktimePlayer) {
        try {
            document.QuicktimePlayer.Stop();
        } catch (err) {
        }
    }
    Element.hide('player');
    var handlerFunc = function(resp) {
        Element.update('player', resp.responseText);
    }
    new Ajax.Request('player.php?id='+id, {asynchronous:true, onSuccess:handlerFunc});
    Element.show('player');
}

function setPreferences(id) {
    if (document.preference.media_player[0].checked || document.preference.media_player[1].checked || document.preference.media_player[2].checked) {
        var val;
        if (document.preference.media_player[0].checked)  val = document.preference.media_player[0].value;
        if (document.preference.media_player[1].checked)  val = document.preference.media_player[1].value;
        createCookie("media_player", val, 3650);
        play(id);
        return true;
    }
    return false;
}

function unsetPreferences(id) {
    eraseCookie("media_player");
    play(id);
}

function appendChildren() {
    if(arguments.length > 1 && arguments[0].nodeType) {
        for(var i = 1; arguments[i].nodeType && i < arguments.length; i++) {
            arguments[0].appendChild(arguments[i]);
        }
    }
}
