/**
 * Playlist Application - loads the app
 *
 * Copyright: (c)2007 CK Web Technologies
 * Author:    Chris Knowles <chris.knowles@ckweb.com.au>
 * Version:   $Id: App.js 22 2007-11-13 04:31:39Z Chris $
 */

var PlaylistApp = function()
{
    /**
     * Initialises the app components 
     */
    this.load = function(songlistCacheSize, autoSaveInterval)
    {
        var self = this; 
        
        // build the app
        this.build(songlistCacheSize, autoSaveInterval);
        
        // load the palyer
        //this.Player.load();
                
        // set initial messages
        this.Songlist.setMessage($D.id('ui-songlist-intro').innerHTML + $D.id('ui-system-reqs').innerHTML);
        this.Playlist.setMessage($D.id('ui-playlist-intro').innerHTML + $D.id('ui-help-email').innerHTML);

        // set listeners
        $E.listen('playlist', 'click', function(e){self.Playlist.moveRow(e)});
        $E.listen('saveButton', 'click', function(){self.Storage.save()});
        $E.listen('clearButton', 'click', function(){self.Playlist.clear()});
        $E.listen('printButton', 'click', function(){self.Printer.print()});
        $E.listen('menu', 'click', function(e){self.Songlist.fetch(e)});
        $E.listen('artist', 'click', function(e){self.Songlist.fetchArtist(e)});
        $E.listen('title', 'click', function(e){self.Songlist.fetchTitle(e)});
        $E.listen('searchButton', 'click', function(e){self.Songlist.search(e)});
        $E.listen('introLogin', 'click', function(){self.User.showChallenge("login")});
        $E.listen('introPwd', 'click', function(){self.User.showChallenge("forgottenPassword")});
        $E.listen('songlist', 'click', function(e){self.Songlist.handleClick(e)});
        $E.listen('playlist', 'click', function(e){self.Playlist.handleClick(e)});
        $E.listen(window, 'unload', function(){self.Storage.autoSave()});
        $E.listen('help-button', 'click', function(){self.help()});
        this.tmp = $D.id('songlist').innerHTML;
    };
    
    /**
     * Creates all the components of the app 
     */
    this.build = function(songlistCacheSize, autoSaveInterval)
    {
        this.Menu     = new $W.Menu('menu');
        this.User     = new User(this);
        this.Player   = new Player(this);
        this.Songlist = new Songlist(this, songlistCacheSize);
        this.Playlist = new Playlist(this);
        this.Printer  = new Printer(this);
        this.Storage  = new Storage(this, autoSaveInterval);
        this.Content  = new Content;
    };

    this.help = function() {
        var self = this;
        if ($('#challenge').css('display') != 'none') {
            $('#challenge').hide();
        }
        $('#songlist').hide();
        if (!this.tmp) {
            this.tmp = $D.id('songlist').innerHTML;
        }
        this.Songlist.setMessage($D.id('ui-full-help').innerHTML);
        $('#songlist').fadeIn();
        $('.closehelp').click(function(){
            self.closeHelp();
        });
    };

    this.closeHelp = function() {
        $D.setContent('songlist', this.tmp);
        this.tmp = null;
    };
    
}
