var __track_details_backup  = '';

/**
 * check_array_contents
 * Checks an array for an occurrence of a string or number
 * 
 * @param Array. The array we want to inspect
 * @param Number or String. The item whose existence in the array we want to 
 * check
 * @return Boolean
 *
 * @author Greg Saunderson
 **/
var check_array_contents = function(arr,item)
{
    for ( var i = 0; i < arr.length; i++ ) {
        if ( arr[i] === item ) {
            return false;
        }
    }

    return true;

};

/**
 * center_element_in_viewport
 * Centers the selected element horizontally in the browser viewport
 *
 * @param String. The name of the element
 * @return Boolean
 *
 * @author Greg Saunderson
 **/
var center_element_in_viewport = function(e)
{

    var viewport_dimensions = document.viewport.getDimensions();
    var element_dimensions  = $(e).getDimensions();

    // var top  = viewport_dimensions.height / 2 - element_dimensions.height / 2;
    var top  = 80;
    var left = viewport_dimensions.width / 2 - element_dimensions.width / 2;

    $(e).setStyle({
        'top' : parseInt(top,10) + 'px',
        'left' : parseInt(left,10) + 'px',
        'zIndex' : 100000
    });

    return true;

};

/**
 * build_minimaps
 * Builds the mini map for each step of the route
 *
 * @param String. Which map to build the minimaps for
 * @return Boolean
 *
 * @author Greg Saunderson
 **/
var build_minimaps = function(mode)
{

    // Array used to store the minimaps in the DB for later retrieval
    var journey_map_tracker = [];

    var r                = __directions.getRoute(__directions.getNumRoutes() - 1);
    var route_step_count = r.getNumSteps();

    var divisions       = __step_div.split(',');
    var divisions_count = divisions.length;
    for ( var i = 0; i < divisions_count; i++ ) {
        var div = i + 1;
        var mini_map_div = $(mode + '_minimap_' + div.toString());

        var map_size = new google.maps.Size(120,80);
        var mini_map = new google.maps.Map2(mini_map_div,{size:map_size});

        // Disable map panning
        mini_map.disableDragging();

        // Now we have to set the zoom level and the map bounds to show us all
        // the steps involved in this division
        mini_map.setCenter(new google.maps.LatLng(0,0),0);
        var bounds = new google.maps.LatLngBounds();

        var steps             = divisions[i].split(':');
        var first_step_in_div = steps[0];
        var last_step_in_div  = steps[steps.length - 1];

        // A difficult situation arises when the last div only has 1 step.
        // Here we attempt to get around it
        if ( first_step_in_div == last_step_in_div && first_step_in_div == route_step_count ) {
            bounds.extend(r.getEndLatLng());
        } else {
            bounds.extend(r.getStep(first_step_in_div).getLatLng());
        }

        if ( last_step_in_div != route_step_count ) {
            bounds.extend(r.getStep(last_step_in_div).getLatLng());
        } else {
            bounds.extend(r.getEndLatLng());
        }

        var center = bounds.getCenter();
        var zoom   = mini_map.getBoundsZoomLevel(bounds);

        mini_map.setCenter(center);
        mini_map.setZoom(zoom);

        var map_data = [center.lat(),center.lng(),zoom];

        if ( mode == 'directions' ) {
            journey_map_tracker.push(map_data);
        }

        if ( __is_ie ) {
            var map_url = 'http://maps.google.com/staticmap?center=' + center.lat() + ',' + center.lng() + '&zoom=' + zoom + '&size=120x80&key=' + __google_maps_key;
            $(mini_map_div).update('<img src="' + map_url + '" width="120" height="80" alt="" title="" />');

            // VERY IMPORTANT!
            // Google Maps adds a 'position: relative' style to the div. This
            // results in the mini map not scrolling in IE6+. VERY ANNOYING. So we
            // have to explicitly remove the position style here
            mini_map_div.setStyle({'position':''});
        } else {
            // Draw the polyline
            // First we have to copy the existing polyline and then add it to our
            // new map
            var this_poly = __directions.getPolyline().copy();
            if ( mode != 'print' ) {
                mini_map.addOverlay(this_poly);
            }
            
            // Hide copyright
            hide_copyright(mini_map_div);
            
            // Set the dimensions on the minimap div's 1st child to make sure the map scrolls
            mini_map_div.firstDescendant().setStyle({'width':'120px','height':'80px','position':'relative'});
        }
    }

    // Only save the maps once
    if ( mode == 'directions' ) {
        var d = journey_map_tracker.toJSON();
        new Ajax.Request('journey_manager.php', {
            method: 'post',
            parameters: 'task=update&field=static_maps&data=' + d,
            onSuccess: function(transport) {
                // La.... Nothing really to do here
            }
        });
    }

    return true;

};

/**
 * hide_copyright
 * This function disappears the copyright notices at the bottom of the Google maps
 *
 * @param Object. The div element containing the map
 * @return Boolean
 *
 * @author Greg Saunderson
 **/
var hide_copyright = function(d)
{

    var CopyrightDiv = d.firstChild.nextSibling;
    var CopyrightImg = d.firstChild.nextSibling.nextSibling;

    CopyrightDiv.style.display = 'none';
    CopyrightImg.style.display = 'none';

    return true;

};

/**
 * open_new_window
 * Opens a link in a new window. Used to guarantee XHTML Strict compliance
 * because the href target attribute does not exist in XHTML
 *
 * @param String. The URL of the window to open
 * @return Boolean
 *
 * @author Greg Saunderson
 **/
var open_new_window = function(url)
{

    window.open(url);
    return true;

};