/**
 * location.js - Custom scripting for Interactive Map
 * $Id$
 */

$(document).ready(function() {

   /**
    * jQuery Map Tool
    * This function finds all parent level map toggles (radio buttons), and allows the user to change the map shown.
    */  

     /* Load Google Map before anything else */
   
    $('.toggle-radio').click(function () {
       
        /* Get the value of the toggle that was clicked */
        selectedMap = $(this).val();
        
        /* If map is already active then  do nothing, if it isn't then set it */
        if ($("#map ."+selectedMap).is('.active')) {
            return;
        } else {
            /* Fade out active map, and then run callback function */
            $('#map .active').fadeOut("slow", function () {
                    /* Remove .active class from current map */
                    $('#map .active').removeClass("active");
                    
                    /* Show newly selected map */
                    $('#map .'+selectedMap).fadeIn("slow");
                    
                    /* Add the .active class to newly selected map */
                    $('#map .'+selectedMap).addClass("active");
                    
                    /* If Google Map button is checked, show map */
                    if(selectedMap == 'googleMap') {
                        $("#googleMap").css('visibility','visible');
                    }
                    
                    /* If the local radio button is checked, show local toggles and map layers */
                    if (selectedMap == 'local') {
                        $('.local-toggles').addClass("active");
                        $('.local-toggles').fadeIn("slow");
                        $('#map-layers').css("display", "block");
                        /* We have to have it set the attributes "checked" in order to make the trigger work, and then set them back since the trigger makes it go away. */
                        $('.toggle-checkbox[name="airports"], .toggle-checkbox[name="highways"], .toggle-checkbox[name="railways"]').attr("checked", true);
                        $('.toggle-checkbox[name="airports"], .toggle-checkbox[name="highways"], .toggle-checkbox[name="railways"]').trigger('click');
                        $('.toggle-checkbox[name="airports"], .toggle-checkbox[name="highways"], .toggle-checkbox[name="railways"]').attr("checked", true);
                    } else if ($(".local-toggles").is('.active')) {
                        /* If a main toggles is checked and the local toggles are showing, then hide / uncheck local toggles, and hide map layers */
                        $('.local-toggles').removeClass("active");
                        $('.local-toggles').fadeOut("slow");
                        $('.local-toggles input:checkbox').attr("checked", false);
                        $('#map-layers div').css("display", "none");
                    }
                    
            });
        }
    });
    
    /* This function finds all child level map toggles (checkboxes), and allows the user to turn on and off map layers */
    $(".toggle-checkbox").click(function () {
       
        /* Get the value of the toggle that was clicked */
        selectedLayer = $(this).val();
       
        /* If a layer toggle is checked, show associated map and imagemap */
        if ($(this).is(":checked")) {
            $('#map-layers .'+selectedLayer).css("display", "block");
            $('#map-layers .spacer').css("display", "block");
            $('#map-layers .'+selectedLayer).pngFix(); 
            
            /* If a layer toggle is checked, show associated map, imagemap and run the tooltip function */
            $.get("maps/imagemaps/"+selectedLayer+".php",
                function(newImageMap) { 
                    document.getElementById('imagemap').innerHTML = document.getElementById('imagemap').innerHTML + newImageMap; 
                    $('map area').Tooltip({ track: true, showURL: false });
                }
            );                     
        } else if ($(this).attr("checked", false)) {
            /* Else If a layer toggle is unchecked, hide associated map and imagemap */
            $('#imagemap .'+selectedLayer+'-imagemap').remove();
            $('#map-layers .'+selectedLayer).css("display", "none");
        }

    });

}); /* END $(document).ready() */


$(document).unload(function() { 
    GUnload(); 
});
