var dragObject = null;
var dragItem = null;
var dragPref = null;
var dragData = null;
var dragCount = null;
var dragStyle = null;

var needRebuildPlayList = false;
var reordered = true;

var playListData = null;
var playingClip = null;
var audioPlayerConfig = null;
var audioPlayer = null;
var audioListSelection = 0;
var finishHandling = false;

var isStopping=false;

var selectedTracksCount = 0;

function selectAllAudio() {
    for (a = 0; a < playListData.length; a++) {
        if (el = document.getElementById('acb_' + a))el.checked = true;
        if (el = document.getElementById('acbv_' + a))el.checked = true;
    }
    audioListSelection = 1;
    selectedTracksCount = playListData.length;
}

function selectNoneAudio() {
    for (a = 0; a < playListData.length; a++) {
        if (el = document.getElementById('acb_' + a))el.checked = false;
        if (el = document.getElementById('acbv_' + a))el.checked = false;
    }
    audioListSelection = 0;
    selectedTracksCount = 0;
}

function selectSwitch() {
    if (!audioListSelection) {
        selectAllAudio();
    } else selectNoneAudio();
}

function selectTrack(id, el) {
    document.getElementById('acb_' + id).checked = el.checked;

    if (el)
        if (el.checked) {
            if (selectedTracksCount < playListData.length)selectedTracksCount++;
        } else
        {
            if (selectedTracksCount > 0)selectedTracksCount--;
        }

}

//Start dragging
function beginDrag(obj, item, pref, data, count) {
	doStartDrag(obj, item, pref, data, count);
}

function doStartDrag(obj, item, pref, data, count) {
if(!audioPlayer.isPlaying()){

    if (dragObject)doStopDrag(obj);

    if (dragObject = obj) {
        dragItem = item;
        dragPref = pref;
        dragData = data;
        dragCount = data ? data.length : count;

        dragClass = dragObject.className;

        $j(dragObject).find(".tid, .nav, .trating, .tool").css("opacity", "0");

        dragObject.className = 'dragging';
        dragObject.style.cursor = 'move';
    }
}
}

//Drag into new position
function doDragOver(obj, item) {
    if (dragObject) {
        doStopDrag(obj, item);
        doStartDrag(obj, item, dragPref, dragData, dragCount);
    }
}

//Leaving position
function doDragOut(obj) {
    if (dragObject) {
        obj.style.cursor = '';
    }
}

//Stop dragging
function endDrag(obj){
	var clip=playingClip;

	reordered=needRebuildPlayList;

	doStopDrag(obj);
	rebuildPlayList();
}

function doStopDrag(obj, item) {
    if (dragObject) {
        if (obj != dragObject) {
            doDragItem(obj, dragItem, item, dragPref, dragData, dragCount);
        }

        $j(dragObject).find(".tid, .nav, .trating, .tool").css("opacity", "1");

        dragObject.className = dragClass;
	dragObject.style.cursor = '';

        dragObject = null;

        return true;
    }
    return false;
}


//Drag array data
function doDragItem(obj, from, to, pref, arr, count) {

    if (from && to && pref && arr && (from <= count) && (to <= count) && (to != from)) {

        if (el = document.getElementById('trackSequence' + arr[from - 1].id))el.value = to;
        if (el = document.getElementById('trackSequence' + arr[to - 1].id))el.value = from;

        tmp = arr[from - 1];
        arr[from - 1] = arr[to - 1];
        arr[to - 1] = tmp;

        tmp = dragObject.innerHTML;
        dragObject.innerHTML = obj.innerHTML;
        obj.innerHTML = tmp;

        needRebuildPlayList = true;
    }
}

//Find track in playListData by url
function findTrack(url) {
    var a

    for (a = 0; a < playListData.length; a++)
        if (playListData[a].clip.url == url) {
            return(playListData[a]);
        }
    return(null);
}


//Rebuilding playlist
function rebuildPlayList() {
    if (needRebuildPlayList) {
        var pl = [];
        for (a = 0; a < playListData.length; a++) {
            pl[a] = playListData[a].clip;
        }

        audioPlayer.setPlaylist(pl);
        needRebuildPlayList = false;
    }
}

/*Player functions*/

function playTrack(id) {
    var pc
    if ((!playingClip) || ((pc = findTrack(playingClip.url)) && (pc.id != id))) {
        var index;

        for (index = 0; index < playListData.length; index++)if (playListData[index].id == id)break;

        //rebuildPlayList();

        audioPlayer.play(index);
    } else {
        if (audioPlayer.isPlaying()) {
            audioPlayer.pause();
        } else audioPlayer.play();
    }

}

function onClipChanged(clip) {
    if (playingClip)onClipDone(playingClip);
    onPlay(clip);
}

function onPlay(clip) {
    if ((!isStopping) && (data = findTrack(clip.url)) && (el = document.getElementById('track' + data.id))) {
            playingClip = clip;
            el.className = 'playing';
    }
}

function onResume(clip) {
    onPlay(clip);
}

function onPause(clip) {
    onClipDone(clip);
}

function onStop(clip) {
    onClipDone(clip);
    playingClip = null;
}

function onFinish(clip) {
    onClipDone(clip);
    playingClip = null;

    if (needRebuildPlayList && (!finishHandling)) {
        finishHandling = true;

        rebuildPlayList();

        var data = findTrack(clip.url);
        var index;
        for (index = 0; index < playListData.length; index++)if (data.id == playListData[index].id)break;
        finishHandling = false;

        return(false);
    }
}

function onClipDone(clip) {
    var data = findTrack(clip.url);

    if (data && (el = document.getElementById('track' + data.id))) {
        el.className = 'stopped';
    }
}

function hiliteTrack() {
    var data;
    var el;
    if (playingClip && (data = findTrack(playingClip.url)) && (el = document.getElementById('track' + data.id))) {
        el.className = 'playing';
    }
}