$(document).ready(function(){
    var playItem = 0;
    
    var myPlayList = [{
        mp3: 'leb_deine_traeume.mp3',
		title: 'Leb Deine Träume',
		id: 'song00',
		item: 0
    }, {
        mp3: 'sag_es_wie_es_ist.mp3',
		title: 'Sag es wie es ist!',
		id: 'song01',
		item: 1
    }, {
        mp3: '1000_km_bis_zum_meer.mp3',
		title: '1000 km bis zum Meer',
		id: 'song02',
		item: 2
    }, {
        mp3: 'unsterblich.mp3',
		title: 'Unsterblich',
		id: 'song03',
		item: 3
    }];
    
    
    $("#jquery_jplayer").jPlayer({
        ready: function(){
            playListInit(true); // Parameter is a boolean for autoplay.
        }
    }).jPlayerId("play", "player_play").jPlayerId("pause", "player_pause").jPlayerId("stop", "player_stop").jPlayerId("volumeMin", "player_volume_min").jPlayerId("volumeMax", "player_volume_max").jPlayerId("volumeBar", "player_volume_bar").jPlayerId("volumeBarValue", "player_volume_bar_value").onSoundComplete(function(){
        playListNext();
    });
    
    $("#ctrl_prev").click(function(){
        playListPrev();
        return false;
    });
    
    $("#ctrl_next").click(function(){
        playListNext();
        return false;
    });
    
    function playListInit(autoplay){
        if (autoplay) {
            playListChange(playItem);
        }
        else {
            playListConfig(playItem);
        }
    }
    
    function playListConfig(index){
        playItem = index;
        $("#jquery_jplayer").setFile('mp3/' + myPlayList[playItem].mp3);
        $("#song").attr('src', ('images/' + myPlayList[playItem].id + '.png'));
        $("#song_file").val(myPlayList[playItem].mp3);
		$("#song_title").val(myPlayList[playItem].title);
		$("#song_id").val(myPlayList[playItem].id);
		$("#song_item").val(playItem);
    }
    
    function playListChange(index){
        playListConfig(index);
        $("#jquery_jplayer").play();
    }
    
    function playListNext(){
        var index = (playItem + 1 < myPlayList.length) ? playItem + 1 : 0;
        playListChange(index);
    }
    
    function playListPrev(){
        var index = (playItem - 1 >= 0) ? playItem - 1 : myPlayList.length - 1;
        playListChange(index);
    }
    
    $(function(){
        $("#submit").formValidator({
            scope: '#ecard_form',
            errorDiv: '#error_msg',
            errorMsg: {
                reqString: 'Bitte fülle alle Felder aus!',
                reqMailNotValid: 'Ungültige E-Mail-Adresse!',
                reqMailEmpty: 'Bitte fülle alle Felder aus!'
            }
        });
    });
});

