/**
 * Storage Handler - manages backend storage of playlists
 *
 * Copyright: (c)2007 CK Web Technologies
 * Author:    Chris Knowles <chris.knowles@ckweb.com.au>
 * Version:   $Id: Storage.js 20 2007-11-12 07:21:13Z Chris $
 */

var Storage = function(app, autoSaveInterval)
{
    /**
     *  Reference to the main app
     */
    this.app = app; 

    /**
     *  The interval between auto saves
     */
    this.autoSaveInterval = autoSaveInterval;
    
    /**
     *  The current autosaved playlist
     */
    this.list = '';
    
    /**
     *  Reference to the notification element
     */
    this.html = $D.id('autosave');
    
    /**
     * If the user is not logged in, challenges them to do so or sign up 
     * If user logged in and the playlist is in an unsaved state, it 
     * posts the playlist to the server and calls the finish save function
     */
    this.save = function(action)
    {
        // challenge if not logged in
        if (!this.app.User.loggedIn && this.app.Playlist.saveButton.className != 'saved') {
            this.app.User.showChallenge('all');
            
        // if logged in, save playlist iff it's in an unsaved state
        } else {
            if (!this.app.Playlist.saved || action == 'force') {
                var self = this;
                var http = new $H;
                if (action == 'logout') {
                    func = function(){self.app.User.finishLogout()}
                } else {
                    func = function(){self.finishSave()};
                }
                http.post(
                    'update_playlist/', 
                    'user=' + this.app.User.email + '&tracks=' + this.app.Playlist.list, 
                    func
                );
            }
        }
    };

    /**
     *  Sets the playlist state to 'saved'
     */
    this.finishSave = function()
    {
        this.app.Playlist.saved = true;
        this.app.Playlist.hideSaveButton();
    };
    
    /**
     * Sets the window to periodically call the auto save function 
     */
    this.setAutoSave = function()
    {
        var self = this;
        var ms = this.autoSaveInterval * 1000;
        window.setInterval(function(){self.autoSave()}, ms, true);
    };
    
    /**
     * If the user is logged in and the playlist is in an 'unsaved' state 
     * it posts the playlist to the server and calls the finish auto save
     * function when saved
     */
    this.autoSave = function()
    {
        if (!this.app.Playlist.saved && this.list != this.app.Playlist.list && this.app.User.email && !this.app.User.loggingOut) {
            //this.html.className = 'autounsaved';
            $D.setContent(this.html, 'Saving backup...');
            this.list = this.app.Playlist.list;
            var http = new $H;
            var self = this;
            http.post(
                'update_playlist/', 
                'user=' + this.app.User.email + '&tracks=' + this.app.Playlist.list + '&autosave=true',
                function(){self.finishAutoSave()}
            );
        }
    };
        
    /**
     * After a short interval this clears the auto save notification
     * from the screen 
     */
    this.finishAutoSave = function()
    {
        var self = this;
        var t = window.setTimeout(function(){
            //self.html.className = 'autosaved';
            $D.setContent(self.html, '');
        }, 
        1000);
    };
    
    this.clearDb = function(user)
    {
        var http = new $H;
        var self = this;
        http.post('clear_db/', 'user=' + user, function(){self.finishClearDb('save')});
    };
    
    this.finishClearDb = function(action)
    {
        if (action == 'save' && this.app.Playlist.totalSongs > 0) {
            this.save();
        }
    };
    
    this.clearAutosave = function(user)
    {
        var http = new $H;
        var self = this;
        http.post('clear_autosave/', 'user=' + user, null);
    };
    
}