var cookie_options = { path: '/', expires: 1 };
var playItem = 0;
var cookie_timeslap = 0;
var autoplay = false;
var myPlayList = null;

function isHTML5() {
    return (document.createElement("video") != null);
}

shuffle = function(o) { //v1.0
    for (var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
    return o;
};

renderArray = function(o) {
    var arr = '[{0}]';
    var objStr = '{{0}},';
    var formatedStr = '';
    var obj = null;
    var re = /\{0\}/;
    for (var i = 0; i < o.length; i++) {
        obj = o[i];
        formatedStr += (objStr.replace(re, 'name: "' + obj.name + '",data:"' + obj.data + '",ogg:""'));
    }
    formatedStr = formatedStr.substr(0, formatedStr.length - 1);
    return arr.replace(re, formatedStr);
};

$(".album ul li i.iplay, .sketch-scroll-box .iplay").click(function() {
    var title = $(this).children(0).attr("title");
    var data = $(this).children(0).attr("name");
    playTrack(title, data);
});

$("#jPlayer").jPlayer({
    swfPath: "../js/",
    ready: function() {
        var idx = $.cookie("cookie_playitem");
        var volume = $.cookie("cookie_playervolume");
        if (volume != null) setVolume(volume);
        if (idx == null || idx == "-1") {
            myPlayList = eval(myPlayListStr);
            myPlayList = shuffle(myPlayList);
            myPlayListStr = renderArray(myPlayList);
            $.cookie("cookie_playlist", myPlayListStr, cookie_options);
        } else {
            myPlayList = eval($.cookie("cookie_playlist"));
        }
        playListConfig(0);
    },
    oggSupport: false,
    nativeSupport: false
})
.jPlayer("onSoundComplete", function() {
    playListNext();
});

function playListConfig(index) {

    playItem = $.cookie("cookie_playitem");

    if (playItem == null || playItem == "-1") {
        playItem = index;
    } else {
        cookie_timeslap = $.cookie("cookie_timeslap");
        $.cookie("cookie_playitem", "-1", cookie_options);
        autoplay = true;
    }
    preparePlay();
}

function getTrack(data) {
    return data + ".mpx";
}

function preparePlay() {
    playItem = parseInt(playItem);
    var item = myPlayList[playItem];
    $(".mp3-player .now").html(item.name);
    $("#jPlayer").jPlayer("setFile", getTrack(item.data), item.ogg);
    if (autoplay) doPlay();
}

function doPlay() {
    $("#jPlayer").jPlayer("playHeadTime", cookie_timeslap);
}

function playTrack(title, data) {
    $(".mp3-player .now").html(title);
    $("#jPlayer").jPlayer("setFile", getTrack(data), "").jPlayer("play");
    return false;
}

function playListChange(index) {
    cookie_timeslap = 0;
    playListConfig(index);
    $("#jPlayer").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);
}

$("#jplayer_previous").click(function() {
    playListPrev();
    return false;
});
$("#jplayer_next").click(function() {
    playListNext();
    return false;
});

$(document).ready(function() {
    $("#jplayer_volume_bar, #jplayer_volume_bar_value").click(function() {
        var w = $('#jplayer_volume_bar_value').css('width');
        var width = parseFloat(w.substr(0, w.length - 1));
        return setVolume(100 - width);
    });
});

function setVolume(value) {
    $('#jplayer_volume_bar_value').css('width', value + '%');
    $("#jPlayer").jPlayer("volume", value);
    $.cookie("cookie_playervolume", value, cookie_options);
    return false;
}

$(window).unload(function() {
    var cookie_timeslap_value = "";
    var cookie_playitem_value = playItem;

    if ($("#jPlayer").jPlayer("getData", "diag.isPlaying")) {
        cookie_timeslap_value = $("#jPlayer").jPlayer("getData", "diag.playedTime");
        $.cookie("cookie_timeslap", cookie_timeslap_value, cookie_options);
        $.cookie("cookie_playitem", cookie_playitem_value, cookie_options);
    }

});
