/*******************************************************************************
 * Playlist Editor
 * -----------------------------------------------------------------------------
 * Copyright (C) 2007,2008 FOR INTERNET a.s.
 * $Id$
 *
 *
 */



/**
 *   Controller to manage playlist model
 */
function PlaylistUserController()
{
}
PlaylistUserController.prototype = new PlaylistController();

/**
 *   Initializator method of PlaylistController aka constructor
 *
 * @param DOMDocument model
 * @param DOMElement  viewElement
 * @return PlaylistController
 */
PlaylistUserController.prototype.PlaylistUserControllerInitializer = function PlaylistUserControllerInitializer(playlistModel, viewElement)
{
        this.PlaylistControllerInitializer(playlistModel, viewElement);
        this.modelNode = this.model.documentElement;
        return this;
}


/**
 *   Atomic model for a song in a playlist. It is a DOMElement:
 *
 *     <song id="" name="" band="" year="" ... />
 *
 * Only id attribut is necessary for data transfer and storing, but others are
 * important for view generator
 *
        $song->setAttribute('n',  $item['name']);
        $song->setAttribute('ar', $item['band_name']);
        $song->setAttribute('al', $item['round_count'] > 1 ? $item['round_count'] : 'Novinka');
        $song->setAttribute('y',  $item['year']);
        $song->setAttribute('url',$item['band_alias'].'/'.$item['path']);
        $song->setAttribute('id', $item['id']);
 *
 * @param Song song
 * @return DOMElement
 */
PlaylistController.prototype.songModel = function songModel(song)
{
        var songModel = this.model.createElement('s');
        songModel.setAttribute('n',  song.getAttribute('n'));
        songModel.setAttribute('ar', song.getAttribute('ar'));
        songModel.setAttribute('al', song.getAttribute('al'));
        songModel.setAttribute('y',  song.getAttribute('y'));
        songModel.setAttribute('url',song.getAttribute('url'));
        songModel.setAttribute('id', song.getAttribute('id'));
        return songModel;
}


/**
 *   Atomic view for a single song.
 *
 * @param DOMElement songModel
 * @return DOMElement

   <tr class="odd">
      <td class="btn-move"><a href=""><img src="/hudebni-kanal/on-line-radio/_clip/btn_down.gif" alt="dolů" title="Posunout skladbu dolů" /></a><a href=""><img src="/hudebni-kanal/on-line-radio/_clip/btn_up.gif" alt="nahoru" title="Posunout skladbu nahou" /></a></td>
      <td><strong>THE FIALKY</strong> - Punk Sex Pivo</td>
      <td class="btn"><a href=""><img src="/hudebni-kanal/on-line-radio/_clip/btn_delete.gif" alt="x" title="Vymazat z playlistu" /></a></td>
   </tr>

 */
PlaylistController.prototype.songView = function songView(songModel)
{
        var view;
        return view = tr([
                td([
                        a([
                                img({
                                        'src': '/hudebni-kanal/on-line-radio/_clip/btn_down.gif',
                                        'alt': 'dolů',
                                        'title': 'Posunout skladbu dolů'
                                })
                        ],{
                                href: 'javascript: noop()'
                        },{
                                onclick: function(e) {playlist.moveDown(view);}
                        }),
                        a([
                                img({
                                        'src': '/hudebni-kanal/on-line-radio/_clip/btn_up.gif',
                                        'alt': 'nahou',
                                        'title': 'Posunout skladbu nahou'
                                })
                        ],{
                                href: 'javascript: noop()'
                        },{
                                onclick: function(e) {playlist.moveUp(view);}
                        })
                ],{
                        'class': 'btn-move'
                },{
                        'className': 'btn-move'
                }),
                td([
                        strong([songModel.getAttribute('ar')]),
                        ' - ' + songModel.getAttribute('n')
                ]),
                td([
                        a([
                                img({
                                        'src': '/hudebni-kanal/on-line-radio/_clip/btn_delete.gif',
                                        'alt': 'x',
                                        'title': 'Vymazat z playlistu'
                                })
                        ],{
                                href: 'javascript: noop()'
                        },{
                                onclick: function(e) {playlist.remove(view);}
                        })
                ],{
                        'class': 'btn'
                },{
                        'className': 'btn'
                })
        ], {}, {model: songModel});
}


/**
 * @return DOMElement element
 */
PlaylistController.prototype.playlistViewElement = function playlistViewElement()
{
        return table([
                tbody([
                        tr([
                                th([
                                        'TVŮJ PLAYLIST'
                                ],{
                                        'colspan': '3'
                                },{
                                        'colSpan': '3'
                                })
                        ])
                ])
        ],{
                'class': 'playlist floatRight'
        },{
                'className': 'playlist floatRight'
        });
}



/**
 *   Method to append a song at the end of playlist
 *
 * @param Song song
 * @return DOMElement song view
 */
PlaylistController.prototype.append = function append(song)
{
        return this.view.appendChild(this.songView(this.modelNode.appendChild(this.songModel(song))));
//        alert(view.outerHTML);
//        this.view.innerHTML += view.outerHTML;
}


/**
 *    Method to insert a song into playlist at position before another one
 *
 * @param Song     song
 * @param SongView refView
 * @return DOMElement song view
 */
PlaylistController.prototype.insertBefore = function insertBefore(song, refView)
{
        return this.view.insertBefore(this.songView(this.modelNode.insertBefore(this.songModel(song), refView.model)), refView);
}


/**
 *    Method to remove a song from the playlist
 *
 * @return DOMElement song view of removed song
 */
PlaylistController.prototype.remove = function remove(songView)
{
        this.modelNode.removeChild(songView.model);
        return this.view.removeChild(songView);
}


/**
 *    Method to move a song up in the playlist
 *
 * @param SongView songView
 * @return boolean
 */
PlaylistController.prototype.moveUp = function moveUp(songView)
{
        try
        {
                this.modelNode.insertBefore(songView.model, songView.previousSibling.model);
                this.view.insertBefore(songView, songView.previousSibling);
                return true;
        }
        catch(exception)
        {
                return false;
        }
}


/**
 *    Method to move a song down in the playlist
 *
 * @param SongView songView
 * @return boolean
 */
PlaylistController.prototype.moveDown = function moveDown(songView)
{
        return this.moveUp(songView.nextSibling);
}



