// this file contains all of the functions used in
// our songlist page

// search the songs in our db
function __search_songs()
{
    var args = __search_songs.arguments;
loading(true);

    var id = generate_id();
    __submit("search_songs.php",function(){ search_songs(id); },id);
}
function search_songs()
{
    var args = search_songs.arguments;
    var id   = args[0];
    var r    = _response[id];

    // sanity
    if( !r ) { loading(false); return; }

    // check the errorcode and continue
    if( r.errorcode != 0 ) { display_message(r.error_message); }
    else
    {
        var html      = "";
        var copy_html = "";
        var the_list  = dojo.byId("songlist");

        // load in our template file and get all results
        dojo.io.bind({
            url:        "html/songlist_list.html",
            load:       function( type, data )
                        {
                            // check the number of matches
                            if( r.matches == 1 )
                            {
                                html = data;
                                html = html.replace(/{ID}/g,0);
                                html = html.replace(/{TITLE}/g,unescape(r.data.Title));
                                html = html.replace(/{ARTIST}/g,unescape(r.data.Artist));
                            }
                            else
                            {
                                // we have to loop the result
                                for( var i=0; i<r.matches; i++ )
                                {
                                    var d     = r.data[i];
                                    copy_html = data;
                                    // do the replace
                                    copy_html = copy_html.replace(/{ID}/g,d.ID);
                                    copy_html = copy_html.replace(/{TITLE}/g,unescape(d.Title));
                                    copy_html = copy_html.replace(/{ARTIST}/g,unescape(d.Artist));
                                    html     += copy_html;
                                }
                            }
                            the_list.innerHTML = html;

                            if( dojo.byId("admin") )
                            {
                                // NOW we can make the list sortable...
                                var sort_opts = {
                                    tag:        "div",
                                    dropOnEmpty: true,
                                    onUpdate:   function()
                                                {
                                                    // we have to set the form vars to our serialized value
                                                    var formvars = Sortable.serialize("songlist",{tag:"div",name:"song_id"});
                                                    // now send it off to the server
                                                    dojo.io.bind({
                                                        url:         "data/update_song_order.php",
                                                        postContent: formvars,
                                                        mimetype:    "text/plain"
                                                    });
                                                }
                                };
                                Sortable.create("songlist",sort_opts);
                            }
loading(false);
                        },
            mimetype:   "text/html"
        });
    }
}

// add a new song to the db
function __add_song()
{
    var args = __add_song.arguments;
loading(true);

    var id = generate_id();
    __submit("update_song.php",function(){ add_song(id); },id);
}
function add_song()
{
    var args = add_song.arguments;
    var id   = args[0];
    var r    = _response[id];

    // sanity
    if( !r ) { display_message("Unknown error."); return; }

    // don't care about the code, just display the message
    if( r.errorcode != 0 ) { display_message(r.error_message); }
    else
    {
        // clear the fields and search songs again!
        dojo.byId("Title").value  = "";
        dojo.byId("Artist").value = "";
        __search_songs();
        display_message(r.error_message);
    }
}

// submit a song to us ... aka "request"
function __submit_song()
{
    var args = __submit_song.arguments;
loading(true);

    var id = generate_id();
    set_required_fields("Your Email=Email","Song Title=Title","Artist=Artist",id);
    __submit("request_song.php",function(){ submit_song(id); },id);
}
function submit_song()
{
    var args = submit_song.arguments;
    var id   = args[0];
    var r    = _response[id];

    // sanity
    if( !r ) { loading(false); return; }

    // check the errorcode and continue
    if( r.errorcode !=0 ) { display_message(r.error_message); }
    else
    {
        dojo.byId("Email").value  = "";
        dojo.byId("Title").value  = "";
        dojo.byId("Artist").value = "";
        display_message(r.error_message);
    }
}