// this file contains all of the js functions
// that we'll need to use specifically for our
// "shows" page/s

// this is the div that includes our inviter
// form etc...
var invite_box  = dojo.byId("invite_box");
var invite_bg   = dojo.byId("invite_bg");
var invite_text = dojo.byId("invite_text");
// center our invite box
dojo.addOnLoad(function(){
    if( invite_box )
    {
        dojo.html.setOpacity(invite_bg, 0.55);
        Nifty("div#invite_bg","all fixed-height");
    }
});

// invite a friend!
function invite()
{
    var args    = invite.arguments;
    var show_id = (args[0] > 0) ? args[0] : false;

    // sanity
    if( !show_id ) { return; }

    // center the box
    center(invite_box);
    // display the "invite" window
    invite_box.style.visibility = "visible";
    dojo.byId("show_id").value = show_id;
}
function close_invite_box() { invite_box.style.visibility = "hidden"; }

// submit the invite friend form
function __invite_friend()
{
loading(true);
    var id = generate_id();
    set_required_fields("Email=invite_email",id);
    __submit("invite_friend.php",function(){ invite_friend(id); },id);
}
function invite_friend()
{
    var args = invite_friend.arguments;
    var id   = args[0];
    var r    = _response[id];

    // make sure we have a response
    if( !r ) { loading(false); return; }

    // display the message anyway...
    display_message(r.error_message);

    // check the errorcode and continue!
    if( r.errorcode == 0 ) { close_invite_box(); }
}

// search for shows based on keyword
function __search_shows()
{
    var args  = __search_shows.arguments;

loading(true);
    // otherwise, submit it!
    var id = generate_id();
    __submit("search_shows.php",function(){ search_shows(id); },id);
}
function search_shows()
{
    var args = search_shows.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 template = (dojo.byId("admin")&&(dojo.byId("admin").value==1)) ? "html/shows_list_admin.html" : "html/shows_list.html";
        var html = "";
        var list = dojo.byId("showlist_container");
        // we have matches... open the template file
        dojo.io.bind({
            url:        template,
            load:       function( type, data )
                        {
                            var d;              // the data from the db
                            var show_dates;     // the "SHOW_DATES" (admin)
                            var date_month;     // the "SHOW_MONTH"
                            var date_days;      // the "SHOW_DAY"
                            var location;       // the "LOCATION"
                            var tmp_html;       // temp html for our loop
                            // "data" is our html
                            // check to see how many matches we have
                            if( r.num_rows == 1 )
                            {
                                d = r.data;
                    // decide what our "month" is
                    if( d.StartMonth.toLowerCase() == d.StopMonth.toLowerCase() ) { date_month = d.StartMonth.toLowerCase(); }
                    else { date_month = d.StartMonth.toLowerCase()+" &nbsp; &nbsp; "+d.StopMonth.toLowerCase(); }
                                // get our "days"
                                if( d.StartDay == d.StopDay ) { date_days = d.StartDay; }
                                else { date_days = d.StartDay+"-"+d.StopDay; }
                    show_dates = d.StartMonth.toLowerCase()+" "+d.StartDay+" - "+d.StopMonth.toLowerCase()+" "+d.StopDay;
                                // get our "location"
                                location = unescape(d.City+", "+d.State);

                                // do the replace on our html
                                data = data.replace(/{SHOW_MONTH}/g,date_month);
                                data = data.replace(/{SHOW_DAY}/g,date_days);
                                data = data.replace(/{SHOW_DATES}/g,show_dates);
                                data = data.replace(/{LOCATION}/g,location);
                                data = data.replace(/{VENUE}/g,unescape(d.Venue));
                                data = data.replace(/{NOTES}/g,unescape(d.Notes));
                                data = data.replace(/{MAP}/g,unescape(d.Map));
                                data = data.replace(/{SHOWID}/g,d.ID);
                                html = data;
                            }
                            else
                            {
                                for( var i=0; i<r.num_rows; i++ )
                                {
                                    d = r.data[i];
                    // decide what our "month" is
                    if( d.StartMonth.toLowerCase() == d.StopMonth.toLowerCase() ) { date_month = d.StartMonth.toLowerCase(); }
                    else { date_month = d.StartMonth.toLowerCase()+" &nbsp; &nbsp; "+d.StopMonth.toLowerCase(); }
                                // get our "days"
                                if( d.StartDay == d.StopDay ) { date_days = d.StartDay; }
                                else { date_days = d.StartDay+"-"+d.StopDay; }
                    show_dates = d.StartMonth.toLowerCase()+" "+d.StartDay+" - "+d.StopMonth.toLowerCase()+" "+d.StopDay;
                                // get our "location"
                                location = unescape(d.City+", "+d.State);

                                // do the replace on our html
                                temp_html = data;
                                temp_html = temp_html.replace(/{SHOW_MONTH}/g,date_month);
                                temp_html = temp_html.replace(/{SHOW_DAY}/g,date_days);
                                temp_html = temp_html.replace(/{SHOW_DATES}/g,show_dates);
                                temp_html = temp_html.replace(/{LOCATION}/g,location);
                                temp_html = temp_html.replace(/{VENUE}/g,unescape(d.Venue));
                                temp_html = temp_html.replace(/{NOTES}/g,unescape(d.Notes));
                                temp_html = temp_html.replace(/{MAP}/g,unescape(d.Map));
                                temp_html = temp_html.replace(/{SHOWID}/g,d.ID);
                                html     += temp_html;
                                }
                            }
                            list.innerHTML = html;
loading(false);
                        },
            mimetype:   "text/html"
        });
    }
}

// get the upcoming gigs for the side bar...
function __get_sideshows()
{
loading(true);
    var id = generate_id();
    __submit("search_upcoming_shows.php",function(){ get_sideshows(id); },id);
}
function get_sideshows()
{
    var args = get_sideshows.arguments;
    var id   = args[0];
    var r    = _response[id];

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

    // check the errorcode and act accordingly
    if( r.errorcode != 0 ) { }//display_message(r.error_message); }
    else
    {
        var html = "";
        var list = dojo.byId("gig_list");
        dojo.io.bind({
            url:        "html/shows_side.html",
            load:       function( type, data )
                        {
                            var d;
                            var temp_html;
                            var show_dates;
                            var location;
                            // "data" is our html... no formatting necessary

                            // check the num rows
                            if( r.num_rows == 1 )
                            {
                                d = r.data;
                                // decide what our "month" is
                                if( d.StartMonth.toLowerCase() == d.StopMonth.toLowerCase() )
                                {
                                    show_dates  = d.StartMonth.toLowerCase()+" "+d.StartDay;
                                    show_dates += (d.StartDay!=d.StopDay) ? "-"+d.StopDay : "";
                                }
                                else
                                {
                                    show_dates  = d.StartMonth.toLowerCase()+" "+d.StartDay+" - ";
                                    show_dates += d.StopMonth.toLowerCase()+" "+d.StopDay;
                                }
                                // get our "location"
                                location = unescape(d.City+", "+d.State);

                                data = data.replace(/{SHOW_DATES}/g,show_dates);
                                data = data.replace(/{LOCATION}/g,location);
                                data = data.replace(/{VENUE}/g,unescape(d.Venue));
                                html = data;
                            }
                            else
                            {
                                for( var i=0; i<r.num_rows; i++ )
                                {
                                    d = r.data[i];
                                // decide what our "month" is
                                if( d.StartMonth.toLowerCase() == d.StopMonth.toLowerCase() )
                                {
                                    show_dates  = d.StartMonth.toLowerCase()+" "+d.StartDay;
                                    show_dates += (d.StartDay!=d.StopDay) ? "-"+d.StopDay : "";
                                }
                                else
                                {
                                    show_dates  = d.StartMonth.toLowerCase()+" "+d.StartDay+" - ";
                                    show_dates += d.StopMonth.toLowerCase()+" "+d.StopDay;
                                }
                                // get our "location"
                                location = unescape(d.City+", "+d.State);

                                // set up our html
                                temp_html = data;
                                temp_html = temp_html.replace(/{SHOW_DATES}/g,show_dates);
                                temp_html = temp_html.replace(/{LOCATION}/g,location);
                                temp_html = temp_html.replace(/{VENUE}/g,unescape(d.Venue));
                                html     += temp_html;
                                }
                            }
                            list.innerHTML = html;
loading(false);
                        },
            mimetype:   "text/html"
        });
    }
}

// this will display our "edit" screen for shows in the admin
function __edit_show()
{
    var args    = __edit_show.arguments;
    var show_id = args[0];
loading(true);

    dojo.byId("show_id").value = show_id;
    // generate a form id and submit
    var id = generate_id();
    __submit("details_show.php",function(){ edit_show(id); },id);
}
function edit_show()
{
    var args = edit_show.arguments;
    var id   = args[0];
    var r    = _response[id];

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

    // check the error code and act accordingly
    if( r.errorcode != 0 ) { display_message(r.error_message); }
    else { dojo.byId("showlist_container").innerHTML = unescape(r.html); }
loading(false);
}

// throw up the template to add a show
function add_show()
{
loading(true);
    // we have to set the show_id to 0 so we don't
    // accidentally EDIT a previously edited show
    dojo.byId("show_id").value = 0;
    dojo.io.bind({
        url:      "html/shows_details.php",
        load:     function( type, data )
                  {
                    dojo.byId("showlist_container").innerHTML = data;
                    // SPECIAL!!
                    // we have to make sure we get our form field vars again!
                    loading(false);
                  },
        mimetype: "text/plain"
    });
}

// submit our form to the server
function __update_details()
{
loading(true);

    // generate an id, set the required fields and submit
    var id = generate_id();
    set_required_fields("City=City","Venue=Venue",id);
    __submit("update_show.php",function(){ update_details(id); },id);
}
function update_details()
{
    var args = update_details.arguments;
    var id   = args[0];
    var r    = _response[id];

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

    // check the errorcode and act accordingly
    display_message(r.error_message);
    if( r.errorcode == 0 ) { __search_shows(); __get_sideshows(); }
}
