jQuery.fn.delay = function(time,func){
	this.each(function(){
		setTimeout(func,time);
	});
	
	return this;
};

function decodeGeocoderResponse(response) {

    var output_city = "";
    var output_state = "";
    var output_zip = "";

   place = response.Placemark[0].AddressDetails;
    
    if (place.Accuracy > 1) {
	var state = place.Country.AdministrativeArea;	     		       
	output_state = state.AdministrativeAreaName;
	if (place.Accuracy > 3) {
	    var city = place.Country.AdministrativeArea.SubAdministrativeArea;
	    output_city = city.SubAdministrativeAreaName;
	    for (a in city) {
		if (a == "Locality") {
		    output_city = city.Locality.LocalityName;
		    for (b in city.Locality) {
			if (b == "PostalCode") {
			    output_zip = city.Locality.PostalCode.PostalCodeNumber;
			} else if (b == "DependentLocality") {
			    alert(b)
				output_city = city.Locality.DependentLocality.DependentLocalityName;
			    try {
				output_zip = city.Locality.DependentLocality.PostalCode.PostalCodeNumber;
			    } catch(err) {}
			}
		    }   
		} else if (a == "DependentLocality") {
		    output_city = city.DependentLocality.DependentLocalityName;
		    try {
			output_zip = city.DependentLocality.PostalCode.PostalCodeNumber;
		    } catch (err) {}
		} else if (a == "PostalCode") {
		    output_zip = city.PostalCode.PostalCodeNumber;
		}
	    }
	}
    }
       
    var decodedLocation = new Object;
    decodedLocation.city = output_city;
    decodedLocation.state = output_state;
    decodedLocation.zip = output_zip;
    
    return decodedLocation
    
}



function fadeInOverlay(element, parentSelector, backgroundColor, speed, callbackFn) {
    fadeInOverlayExact(element, parentSelector, backgroundColor, speed, callbackFn,
		       0,0,0,0);
}

/* Makes simple fade in for element by constructing an overlay over it */
function fadeInOverlayExact(element, parentSelector, backgroundColor, speed, callbackFn, 
			    top_offset, left_offset, width_adj, height_adj) {                                       
  var overlay = makeOverlayDivExact(element, parentSelector, backgroundColor, 
                                    top_offset, left_offset, width_adj, height_adj);

 
  overlay.fadeOut(speed, function () {

    $(this).remove();
    if (callbackFn) {
      callbackFn();
    }

	});

    
}

/* Takes in a jQuery overlay element, fades it out with a delay */
function fadeOverlayOutWithDelay(overlay, speed, delay) {

  function fadeOut() {
    fadeOverlayOut(overlay, speed);
  }

  setTimeout(fadeOut, delay);

}

/* Fades out jQuery overlay element */
function fadeOverlayOut(overlay, speed) {
 
    overlay.fadeOut(speed, function () {
	    $(this).remove();

	});

}

/* Makes an overlay div element, returns the jQuery element created */
function makeOverlayDivExact(element, parentSelector, backgroundColor,
			     top_offset, left_offset, width_adj, height_adj, 
                             opacity, hidden)
{

    var overlay = $("<div></div>");
    
    //overlay = $("<div></div>").attr("id", "overlay_" + index);

    
    var position = element.position();
    var width = element.width();
    var height  = element.height();

    overlay.css('background-color', backgroundColor );
    overlay.css('width', (width + width_adj) + "px" );
    overlay.css('height', (height + height_adj) + "px");
    overlay.css('position', 'absolute' );
    overlay.css('zIndex', '10000' );
    overlay.css("top", Math.round(position.top) + top_offset + "px");
    overlay.css("left", Math.round(position.left) + left_offset + "px");
    if (opacity) {
      overlay.fadeTo(0, opacity);
    }
    if (hidden) {
      overlay.hide();
    }

    overlay.appendTo(parentSelector);

   
    return overlay;
  
}



function showTopRatesTable(toshow) {

    //hideAllTopRatesTables(3);

    $(".top_rates_table_box").hide();
    $("#top_rates_nav li").removeClass("selected");
    $("#top_rates_nav li").addClass("inactive");

    $("#top_rates_nav_" + toshow).addClass("selected");
    $("#top_rates_nav_" + toshow).removeClass("inactive");
    $("#top_rates_table_box_" + toshow).show();
    var selectedBox = $("#top_rates_table_" + toshow);

   
    fadeInOverlay(selectedBox, "#top_rates_table_box_" + toshow, "white", 300, null);
  
    

  

    //document.getElementById("top_rates_table_box_" + toshow).style.display = 'block';
    //document.getElementById("top_rates_nav_" + toshow).setAttribute("class", "selected_yes");
    //document.getElementById("top_rates_nav_" + toshow).setAttribute("className", "selected_yes");
		    
}

function showRateTable(toshow) {

 
    $(".rate_table_box").hide();
    $("#rate_table_nav li").removeClass("selected");

    $("#rate_table_box_" + toshow).show();
    $("#nav_item_" + toshow).addClass("selected");

    var selectedTable = $("#rate_table_box_" + toshow + " table tbody");
    var selectedBox = $("#rate_table_box_" + toshow);

 
    // Change the updated rates time too
    $("#prod_category_update_time span").hide();
    $("span#update_time_" + toshow).show();


    if ($.browser.mozilla) {
	fadeInOverlayExact(selectedTable, "#rate_table_box_" + toshow, "white", 300, null, 0, 0, -1, 0);
    } else if ($.browser.msie) {
	fadeInOverlayExact(selectedTable, "#rate_table_box_" + toshow, "white", 300, null, 1, 0, -1, 0);
    } else {
	fadeInOverlay(selectedTable, "#rate_table_box_" + toshow, "white", 300, null);
    }
}


/*
function toggleRateLocationInput() {
 
    var box = $("#new_rate_location");
    if (box.is(":hidden")) {
	box.fadeIn(100);	
    } else {
	box.fadeOut(100);
    }

 
}
*/

function changeLocation() {
    geocoder = new GClientGeocoder();

    var address = document.getElementById('new_location').value;
    geocoder.getLocations(address, submitNewLocation);


}

function submitNewLocation(response) {
    decodedLocation = decodeGeocoderResponse(response);

    document.getElementById("new_state").value = decodedLocation['state'];
    document.getElementById("new_city").value = decodedLocation['city'];
    document.getElementById("new_zip").value = decodedLocation['zip'];

    document.change_location.submit();

}

function changeRateLocation() {
    geocoder = new GClientGeocoder();

    var address = document.getElementById('new_rate_location').value;
    geocoder.getLocations(address, submitNewLocation);


}


function changeMapLocation() {
    geocoder = new GClientGeocoder();

    var address = document.getElementById('map_input_location').value;
    geocoder.getLocations(address, submitNewMapLocation);
}

function submitNewMapLocation(response) {
    decodedLocation = decodeGeocoderResponse(response);

    document.getElementById("map_state").value = decodedLocation['state'];
    document.getElementById("map_city").value = decodedLocation['city'];
    document.getElementById("map_zip").value = decodedLocation['zip'];

    getLocationData();
}

var new_search_type;

function changeSearchLocationAll() {
    new_search_type = "all";

    geocoder = new GClientGeocoder();

    var address = document.getElementById('new_search_location').value;
    geocoder.getLocations(address, submitNewSearchLocation);

}

function changeSearchLocationLocal() {
    new_search_type = "local";

    geocoder = new GClientGeocoder();

    var address = document.getElementById('new_search_location').value;
    geocoder.getLocations(address, submitNewSearchLocation);

}

function submitNewSearchLocation(response) {
    decodedLocation = decodeGeocoderResponse(response);

    document.getElementById("new_state").value = decodedLocation['state'];
    document.getElementById("new_city").value = decodedLocation['city'];
    document.getElementById("new_zip").value = decodedLocation['zip'];
    document.getElementById("search_type").value = new_search_type;
    document.change_location.submit();

}

