var gmarkers=[]; //global to enable synch of map to our search result list.

$(document).ready(function () {
    
    $(".clockpick").clockpick({
    starthour : 0,
    endhour : 23 
    }
    );

    $('.tip-w').tipsy({gravity: 'w', fade: true}); // n | s | e | w
    $('.tip-n').tipsy({gravity: 'n', fade: true});

    $(".dialog").dialog();

    $("#city_state").Watermark("City, State or Zip", "#999")
    //$("#bridging_request_event_details").Watermark("Contact, Phone Number, IP/ISDN Number", "#999")

    $('.datepicker').datepicker({
        inline: true
    });

    $('.timepicker').timepickr({
        convention: 12 
    });

    $(".timeentry").timeEntry({
        spinnerImage: '/images/time_spinner.png',
        spinnerSize: [16, 16, 16],
        showSeconds: false,
        ampmPrefix: ' ',
        timeSteps: [1, 15, 0]
    })

    if ($('#gmap').length > 0) {
        load_map();
    }

    function load_map() {
        if (GBrowserIsCompatible()) {
            map = new GMap2($('#gmap').get(0));
            map.addControl(new GSmallMapControl());
            zoomAndCenter(origin, markers);
            jQuery.each(markers, function () {
                addMarker(this.latitude, this.longitude, this.description, this.label);
            })
        }
    }

    function addMarker(latitude, longitude, description, label) {
        var marker = createMarker(new GLatLng(latitude, longitude), label);
        gmarkers.push(marker);
        GEvent.addListener(marker, 'click', function () {
            marker.openInfoWindowHtml(description);
        });
        map.addOverlay(marker);
    }

    // Creates a marker whose info window displays the letter corresponding
    // to the given index.
    function createMarker(point, label) {
        var icon = new GIcon();
        icon.image = '/images/map-marker-bg.png';
        icon.iconAnchor = new GPoint(8,32);
        icon.infoWindowAnchor = new GPoint(25, 7);

        opts = {
            "icon": icon,
            "clickable": true,
            "labelText": label,
            "labelOffset": new GSize(-2, -31)
        };

        var marker = new LabeledMarker(point, opts);

        return marker;
    }


    function zoomAndCenter(origin, markers) {
        var bounds = new GLatLngBounds
        if (origin != null) bounds.extend(origin);
        jQuery.each(markers, function () {
            bounds.extend(new GLatLng(this.latitude, this.longitude));
        })
        map.setCenter(bounds.getCenter(), Math.min(15, map.getBoundsZoomLevel(bounds)));
    }

    // For ALL <form> tags on your page
    $('form').submit(function(){
        // On submit disable its submit button
        $('input[type=submit]', this).attr('disabled', 'disabled');
    });


}); //end docready




//Nested form support.
function insert_fields(link, method, content) {
  var new_id = new Date().getTime();
  var regexp = new RegExp("new_" + method, "g")
  //gives the inputs a unique name so they're not overwritten on submit
  $(content.replace(regexp,new_id)).insertBefore($(link))
  
}

function remove_fields(link) {
  var hidden_field = $(link).prev("input[type=hidden]");
  if (hidden_field) {
    hidden_field[0].value = '1';
  }
  var fields_div = $(link).parent(".fields");
  fields_div.hide();
}
