/**
 * handle_error
 * Displays an error message to the user neatly
 *
 * @param String. The error message to display
 * @return Boolean.
 *
 * @author Greg Saunderson
 **/
var handle_error = function( message )
{

    // Make sure to stop the directions timeout check so that we don't see the
    // timeout message because the error message is more important
    __directions_timeout = false;

    $('error_message').update(message);
    center_element_in_viewport('error');
    show_error();

    return true;

};

/**
 * show_error
 * Handle the display of error messages
 *
 * @return Boolean
 *
 * @author Greg Saunderson
 **/
var show_error = function()
{

    Effect.Appear('error');
    return true;

};

var hide_error = function()
{

    Effect.Fade('error');
    return true;

};

/**
 * handle_message
 * Displays a message to the user neatly
 *
 * @param String. The error message to display
 * @return Boolean.
 *
 * @author Greg Saunderson
 **/
var handle_message = function( message )
{

    $('message_content').innerHTML = message;
    center_element_in_viewport('message');
    show_message();

    return true;

};

/**
 * show_message
 * Handle the display of info messages
 *
 * @return Boolean
 *
 * @author Greg Saunderson
 **/
var show_message = function()
{

    Effect.Appear('message');
    Effect.Appear('dismiss_message');

    return true;

};

/**
 * hide_message
 * Clears the message contents and hides both the message dismissal button and
 * the message div itself
 *
 * @return Boolean
 *
 * @author Greg Saunderson
 **/
var hide_message = function()
{

    $('message').hide();
    $('message_content').innerHTML = '';
    $('dismiss_message').hide();

    return true;

};

/**
 * handle_timeout_message
 * Handles the button events for the timeout message
 *
 * @param String. The message to show the user
 * @return Boolean.
 *
 * @author Greg Saunderson
 **/
var handle_timeout_message = function( message )
{

    $('timeout_message_content').innerHTML = message;
    center_element_in_viewport('timeout_message');
    Effect.Appear('timeout_message');

    return true;

};

/**
 * hide_timeout_message
 * Clears the timeout message contents and hides the timeout message div
 *
 * @return Boolean
 *
 * @author Greg Saunderson
 **/
var hide_timeout_message = function()
{

    $('timeout_message').hide();
    $('timeout_message_content').innerHTML = '';

    return true;

};

/**
 * show_map_switcher
 * After all the maps are loaded, we then show the buttons to allow switching
 * between them
 *
 * @return Boolean
 *
 * @author Greg Saunderson
 **/
var show_map_switcher = function()
{
    // Add mouse event functionality to the map switcher buttons
    Event.observe("show_0","click",function(){
        switch_map(0);
    });
    Event.observe("pl_show_0","click",function(){
        switch_map(0);
    });

    Event.observe("show_1","click",function(){
        switch_map(1);
    });
    Event.observe("pl_show_1","click",function(){
        switch_map(1);
    });

    Event.observe('show_2','click',function(){
        switch_map(2);
    });
    Event.observe('pl_show_2','click',function(){
        switch_map(2);
    });

    // Map switcher arrows
    Event.observe('module_arrow_left','click',function(){
        switch_map(handle_map_arrow('left'));
    });
    Event.observe('module_arrow_right','click',function(){
        switch_map(handle_map_arrow('right'));
    });

    Effect.Appear('map_switcher');
    Effect.Appear('playlist_tabs');

    return true;
};

/**
 * handle_map_arrow
 * Figures out which map to show if the user uses the arrows on the map switcher
 *
 * @param String The direction the arrow points
 * @return Int. The number of the map to switch to
 *
 * @author Greg Saunderson
 **/
handle_map_arrow = function(direction)
{

    var map_id = 0;
    if ( direction == 'left' ) {
        ( (__current_module_id - 1) < 0 ) ? map_id = ( __module_count - 1 ) : map_id = __current_module_id - 1;
    } else {
        ( (__current_module_id + 1) >= __module_count ) ? map_id = 0 : map_id = __current_module_id + 1;
    }

    return map_id;

};

/**
 * switch_map
 * Provides funcitonality to show or hide the different maps
 *
 * @param Int. Number of the map to show
 * @return Boolean
 *
 * @author Greg Saunderson
 **/
var switch_map = function(map_id)
{

    for ( var i = 0; i < __map.length; i++ ) {
        if ( i == map_id ) {
            $('map' + i).show();
            $('playlist' + i).show();
            $('playlist' + i).scrollTop = $('user_playlist').scrollTop;
            $('show_' + i).addClassName('focus' + i);
            $('pl_show_' + i).addClassName('focus' + i);

            // Hide the border around the button
            $('pl_show_' + i).blur();

            // Update the module tracking variable
            __current_module_id = i;
        } else {
            $('map' + i).hide();
            $('playlist' + i).hide();
            $('show_' + i).removeClassName('focus' + i);
            $('pl_show_' + i).removeClassName('focus' + i);
        }
    }

    // Hide the more_info_container div
    $('more_info_container').hide();
    $('more_info_container').innerHTML = '';

    map_id = null;
    i      = null;

    return true;

};

/**
 * show_playlist
 * Takes the AJAX responseText and displays the HTML in the correct places
 *
 * @param AJAX responseText containing HTML for playlist and directions areas
 * @param String. The map we are working on
 * @return Boolean
 *
 * @author Greg Saunderson
 **/
var show_playlist = function(html,mode)
{

    var div_id = 0;

    switch ( mode ) {
    case 'map' :
    case 'directions' :
        div_id = 0;
        break;
    case 'poi' :
        div_id = 1;
        break;
    case 'band' :
        div_id = 2;
        break;
    }

    var h = html.split('|');
    $('playlist' + div_id).innerHTML = h[1];

    // Add the division and step data to a global array
    if ( __step_div === null ) {
        __step_div = h[0];
    }

    build_minimaps(mode);

    if ( __initial_search === true ) {
        $('user_playlist').innerHTML = h[2];
        $('user_playlist').setStyle({'backgroundColor': '#898E94'});

        // Alert the user to the playlist finally being returned
        var message_text = 'Done! We found <b>' + h[3] + '</b> tracks for you to listen to over the <b>' + __distance + '</b> miles between <b>' + __input_saddr + '</b> and <b>' + __input_daddr + '</b>. Enjoy!';

        if ( __input_artist != '' || __input_artist.toLowerCase() != 'artist' ) {
            message_text += '<br /><br />Please note that for the search by artist functionality we will fill your playlist with as many tracks by the selected artist as possible, but in case the artist does not reveal enough songs to fill your journey or we have no data on the selected artist your playlist will be made up with songs from the default POP genre.';
        }

        handle_message(message_text);

        $('journey_summary').innerHTML = '<div id="summary_track_count"><b>' + h[3] + '</b> tracks returned</div>' + $('journey_summary').innerHTML;

        resize_user_playlist_divs(mode);
        Effect.Appear('user_playlist');
        Effect.Appear('playlist_tools');
        __initial_search = false;
    }

    

    div_id = null;
    h      = null;

    return true;

};

/**
 * show_extended_playlist_info
 * Displays the extended information in the playlist
 *
 * @param Integer. The id of the track
 * @param String. Playlist type
 * @return Boolean
 *
 * @author Greg Saunderson
 **/
var show_extended_playlist_info = function(track_id,type)
{

    var source_div = type + '_playlist_moreinfo_' + track_id;
    var div = 'more_info_container';
    if ( $(div).getStyle('display') == 'none' ) {
        $(div).update($(source_div).innerHTML);
        Effect.Appear(div);
    } else {
        Effect.Fade(div);
        $(div).update('');
    }

    source_div = null;
    div        = null;

    return true;

};

/**
 * resize_user_playlist_divs
 * Resizes the user playlist div heights to match the currently visible
 * playlist
 *
 * @param String. The playlist we want to match
 * @resturn Boolean
 *
 * @author Greg Saunderson
 **/
var resize_user_playlist_divs = function(mode)
{
    // Count how many divs we have
    var div_count = __step_div.split(',').length;
    var h = '';
    for ( var i = 0; i < div_count; i++ ) {
        h = get_playlist_div_height(mode + '_playlist_div_' + (i+1));
        $('user_playlist_div_' + (i+1)).setStyle({'height' : h + 'px'});
    }

    div_count = null;
    h         = null;

    return true;

};

/**
 * get_playlist_div_height
 * Calculates the height of the playlist div that we want the user div to
 * match. Firefox makes this non-trivial
 *
 * @param String. The id of the playlist div whose height we want to match
 * @return Int. The height of the div
 *
 * @author Greg Saunderson
 **/
var get_playlist_div_height = function(div)
{
    var h = 0;

    // Remove 1px to cater for the border.
    h = $(div).getHeight() - 1;

    // Arbitrary number... basically, Firefox normally returns 1 for the
    // height, IE deals with it nicely. But let's not assume that 1 will always
    // be returned
    if ( h < 70 ) {
        // Now we need to get the heights of the contents of the div. Sigh
        var c  = $(div).childElements();

        // Because we know the first child is a <h1> tag and there are only 3
        // children, we don't need to do any major work here
        var h1 = c[1].getHeight();
        var h2 = c[2].getHeight();

        h = ( h2 >= h1 ) ? h2 : h1;

    }

    c  = null;
    h1 = null;
    h2 = null;

    return h;

};

/**
 * scroll_playlist
 * Scrolls the different playlists in synch with the user playlist
 *
 * @return Boolean
 *
 * @author Greg Saunderson
 **/
var scroll_playlist = function()
{
    var scroll = $('user_playlist').scrollTop;

    for ( var i = 0; i < __module_count; i++ ) {
        $('playlist' + i).scrollTop = scroll;
    }

    scroll = null;

    return true;

};