String.prototype.startsWith = function(str) 
{return (this.match("^"+str)==str)}

String.prototype.endsWith = function(str) 
{return (this.match(str+"$")==str)}

$.fn.wait = function(time, type) {
        time = time || 1000;
        type = type || "fx";
        return this.queue(type, function() {
            var self = this;
            setTimeout(function() {
                $(self).dequeue();
            }, time);
        });
    };
    
   
function submitTreForm(e, link)
{
	var url = "/searches/tre_search";
	var history='';
	if(typeof link != 'undefined' && link)
	{
		// use passed in link as url and do not collect form data
		url = link;
		history = link;
	}
	else
	{
		var data = jQuery(this).serialize();	
		history = url+"?"+data;
	}
    $.history.load("ajax_load"+history);
	
	
	return false;
	
}

var last_call_was_ajax = false;

function loadPage(hash)
{
	// hash doesn't contain the first # character.
	if(hash) 
	{
		if(hash.startsWith("ajax_load"))
		{
			hash = hash.substring("ajax_load".length);
			$('#container').mask('');
			
			last_call_was_ajax = true;
			jQuery.ajax({
			   type: "GET",
			   url: hash,
			   complete: function(XMLHttpRequest, textStatus)
			   {
			   		$('#container').unmask();
			   },
			   success: function(msg){
			     $("#right-side-content").html(msg);
			   }
			 });
			
			return false;
		}
		else
		{
			if(last_call_was_ajax)
			{
				last_call_was_ajax = false;
				location.reload();
				return false;
			}
			return true;
		}
	} else {
		if(last_call_was_ajax)
		{
			last_call_was_ajax = false;
			location.reload();
			return false;
		}
		else
		{
			//start page
			return true;
		}
	}
}

function showGoogleMap(config)
{
	/*
	 * show on the map
	 */
	$('#googlemap-container').show();
	$('#googlemap').gMap(config); 		
	//store config to be used in popup window
	var config_str =  $.toJSON(config);
	$('#googlemap').attr('config', config_str);
}


function openGoogleMapWin2(key, height,width) {
    var offset_x,offset_y, win_width, win_height, padding;

    var config = getCurrentMapConfig();

	$('#googlemap_popup').modal({
		onOpen: function (dialog) {
		dialog.overlay.fadeIn('slow', function () {
			dialog.container.slideDown('slow', function () {
				 $('#googlemap_popup').show();
                 $('#googlemap_popup').gMap(config);
                 dialog.data.fadeIn('slow');
			});
		});
	},
	onClose: function (dialog) {
		dialog.data.fadeOut('slow', function () {
			dialog.container.slideUp('slow', function () {
				dialog.overlay.fadeOut('slow', function () {
					$.modal.close(); // must call this!
					$('#googlemap_popup').hide();
				});
			});
		});
	},
	containerCss:{
		backgroundColor: "#333333",
		borderWidth: "8px",
		borderStyle: "solid",
		borderColor: "#444444",
		padding: "12px",		
		height:450,
		width:450,
		color: "#000",
		fontFamily: "Lucida Grande",
		fontSize: ".9em",
		paddingBottom: "4px",
		borderRadius: "1em"
	},
	
	closeClass: "modal-close"
	
	});
    //debugger;
    
    /*showModal("/googlemap_popup");
    
    
    var config = getCurrentMapConfig();
    var maps = $('#modal-dialog').contents().find('#googlemap_popup');
    maps.width(450);
    maps.height(450);
    maps.gMap(config);
    */
    /*
     * var url = "http://maps.google.com/maps/api/staticmap?zoom="+config.zoom+"&size="+width+"x"+height;
    
    for(var i = 0; i < config.markers.length; i++)
    {
    	var label = String.fromCharCode(65+i);
    	url +="&markers=color:red|label:"+label+"|"+config.markers[i].latitude+","+config.markers[i].longitude;
    }
    url+="&sensor=false&key="+key; 
    alert(url);
    */
    /*
    win_width  = width;
    win_height = height;  
    padding=0;

    offset_x=(screen.availWidth - win_width)/2;
    offset_y=(screen.availHeight - win_height)/2;
    
    
    var my_wnd = window.open("/googlemap_popup", "FatPassport","titlebar=0,fullscreen=0,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0,height=" +win_height+ ",width=" +win_width+ ",screenX=" +offset_x+ ",screenY=" +offset_y+ ",left=" +offset_x+ ",top=" +offset_y+ "");
    */
}

function getCurrentMapConfig()
{
	var config = $('#googlemap').attr('config');
    config = $.evalJSON(config);
 
    return config;
}