﻿/// <reference path="jquery-1.4.2.js" />
/// <reference path="max.js" />

$(function () {

    // --- Playlist Move Up/Down
    var $container = $('.my-custom-playlist');

    if ($container.length == 0) return;

    var $songs = $container.children('.song');

    // make the container big enough to show all the songs
    var $firstSong = $songs.first();

    if ($firstSong.length > 0) {
        var containerHeight =
					(($firstSong.height() +
						($firstSong.css('margin-top').toString().fromPxToInt()) +
						($firstSong.css('margin-bottom').toString().fromPxToInt())
						) * $container.children('.song').length) + 35;
				$container.css('height', containerHeight);

        // absolutely position everything
        $songs
		.each(function (i) {
		    var pos = $(this).position();
		    $(this)
				.css('top', pos.top + 'px')
				.css('left', '0px')
				.attr('data-order-index', i);
		})
		.css('position', 'absolute');

        // move up/down handlers
        $container
		.find('button.move-up, button.move-down')
		.click(function () {
		    var $me = $(this);
		    var down = $me.hasClass('move-down');
		    var $song = $(this).parents('.song');

		    function getSongByIndex(index) {
		        return $('.my-custom-playlist>.song[data-order-index=' + index + ']');
		    }

		    var index = parseInt($song.attr('data-order-index'));

		    if (index == $('.my-custom-playlist>song').length - 1 && down ||
					index == 0 && !down) {
		        return;
		    }

		    var $swapSong = getSongByIndex((down ? index + 1 : index - 1));

		    moveSong($song, $swapSong.position().top, 2);
		    moveSong($swapSong, $song.position().top, 1);

		    function moveSong($song, newTop, zIndex) {
		        $song
					.css('z-index', zIndex)
					.animate(
						{ top: newTop },
						'fast',
						'swing',
						null);
		    }

		    $song.attr('data-order-index', $swapSong.attr('data-order-index'))
		    $swapSong.attr('data-order-index', index);

		    populateNumbering();
		});

        // delete handler
        $container
		.find('.delete')
		.click(function () {
		    var $song = $(this).parents('.song');

		    var $songsAfterThis = [];
		    $songs.each(function () {
		        if ($(this).position().top > $song.position().top)
		            $songsAfterThis.push($(this));
		    });

		    var $prevSong = $song;
		    $song.css('z-index', 1);
		    for (var index in $songsAfterThis) {
		        $songsAfterThis[index]
					.css('z-index', 2)
					.animate(
						{ top: $prevSong.position().top },
						'fast',
						'swing',
						null);
		        $prevSong = $songsAfterThis[index];
		    }

		    $song.remove();

		    $songs = $container.children('.song');
		    $songs.each(function (i) {
		        $(this).attr('data-order-index', i);
		    });

		    populateNumbering();

		    return false;
		});

        function populateNumbering() {
            $songs.each(function (i) {
                var order = parseInt($(this).attr('data-order-index')) + 1;
                $(this).find('input[id*=ordering]').val(order);
                $(this).find('table td:first').text((order) + '.');
            });
        }
    }
});
