/* Copyright (c) 2011 mezz, Inc.  All Rights Reserved
*/

/* Return distance string such as "1.2 miles".
 * First, calculate distance and then append appropriate string.
 * inMiles = true if we want distance in miles.  false for kilometers.
 */
function Create2DArray(cols) 
{
	var arr = [];

	for (var i=0; i<cols; i++) {
		arr[i] = [];
		arr[i][0] = 0;
	}

	return arr;
}

function reparseSocialMediaLinks()
{
	FB.XFBML.parse();
	twttr.widgets.load();
}

(function($) {
	  $.fn.outerHTML = function() {
	    return $(this).clone().wrap('<div></div>').parent().html();
	  }
	})(jQuery);


function distance(lat1, lon1, lat2, lon2, inMiles) {
  
	  var Rk = 6371; // Radius of earth in km
	  var Rm = 3958.7558657440545; // Radius of earth in Miles 
	  var dLat = (lat2-lat1).toRad();
	  var dLon = (lon2-lon1).toRad();
	  lat1 = lat1.toRad();
	  lat2 = lat2.toRad();

	  var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
	          Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(lat1) * Math.cos(lat2); 
	  var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
	  
	  var R = Rm;
	  var suffix = " mi";
	  
	  if (!inMiles) {
		  R = Rk;
		  suffix = " km";
	  }
	  
	  var dist = R*c;
	  
	  if (dist < 10) {
		  // one decimal digit if distance less than 10
		  // like 10.2 mi
		  return Math.round(dist*10)/10 + suffix;
	  }
	
	  // Otherwise round off the number and return.
	  return Math.round(dist) + suffix;
}    

function radiusToSlider(r)
{
	var s = 0;
	
	if (r <= 5) {
		s = parseInt(r * 100 / 5);
	} else if (r <= 10) {
		s = 100 + parseInt((r - 5) * 100 / 5);
	} else if (r <= 25) {
		s = 200 + parseInt((r - 10) * 100 / 15);
	} else if (r <= 50) {
		s = 300 + parseInt((r - 25) * 100 / 25);
	} else if (r <= 100) {
		s = 400 + parseInt((r - 50) * 100 / 50);
	} else {
		s = 500 + parseInt((r - 100) / 100);
		if (s > 600) s = 600;
	}
	
	return s;
}

function sliderToRadius(n)
{
	var r = 0;
	
	if (n <= 100) {
		r = n * 5 / 100;
		if (r < 1) {
			r = 1;
		}
	} else if (n <= 200) {
		r = 5 + (n - 100) * 5 / 100;
	} else if (n <= 300) {
		r = 10 + parseInt((n - 200) * 15 / 100);
	} else if (n <= 400) {
		r = 25 + parseInt((n - 300) * 25 / 100);
	} else if (n <= 500) {
		r = 50 + parseInt((n - 400) * 50 / 100);
	} else {
		r = 100 + (n - 500) * 100;
		if (r > 10000) radius = 10000;
	}
	return r;
}


/** Converts numeric degrees to radians */
if (typeof(Number.prototype.toRad) === "undefined") {
  Number.prototype.toRad = function() {
    return this * Math.PI / 180;
  };
}

/* getUrlVars.  Usage:
 * Get object of URL parameters
 * var allVars = $.getUrlVars();
 *
 * Getting URL var by its nam
 * var byName = $.getUrlVar('name');
*/
$.extend({
	  getUrlVars: function(url){
	    var vars = {};
	    var hash;
	    if (varIsEmpty(url)) url = window.location.href;
	    if (url.indexOf('?') < 0) return vars;
	    
	    var hashes = url.slice(url.indexOf('?') + 1).split('&');
	    for(var i = 0; i < hashes.length; i++)
	    {
	      hash = hashes[i].split('=');
	      //vars.push(hash[0]);
	      vars[hash[0]] = hash[1];
	    }
	    return vars;
	  },
	  getUrlVar: function(name){
	    return $.getUrlVars()[name];
	  },
	  getUrlPath: function(url){
		  if (varIsEmpty(url)) url = window.location.href;
		  if (url.indexOf('?') < 0) return url;
		  return url.slice(0, url.indexOf('?'));
	  }
	});


/*
 * This will return true for
 * - undefined  // Because undefined == null
 * - null
 * - []
 * - ""
 * - and zero argument functions since a function's length is the number of declared parameters it takes.
 */
function varIsEmpty(value)
{
	return (typeof value === "undefined" || value === null || value.length === 0);
}

/*
 * String.format function.
 */
String.prototype.format = function() {
    var formatted = this;
    for (var i = 0; i < arguments.length; i++) {
        var regexp = new RegExp('\\{'+i+'\\}', 'gi');
        formatted = formatted.replace(regexp, arguments[i]);
    }
    return formatted;
};

String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g, "");
};

// http://www.simonwhatley.co.uk/parsing-twitter-usernames-hashtags-and-urls-with-javascript
String.prototype.parseURL = function() {
	return this.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&~\?\/.=]+/g, function(url) {
		return '<a href="' + url + '" target="_blank">' + url + '</a>';
	});
};

String.prototype.parseUsername = function() {
	return this.replace(/[@]+[A-Za-z0-9-_]+/g, function(u) {
		var username = u.replace("@","");
		return '<a href="http://twitter.com/' + username + '" target="_blank">' + u + '</a>';
	});
};

String.prototype.parseHashtag = function() {
	return this.replace(/[#]+[A-Za-z0-9-_]+/g, function(t) {
		var tag = t.replace("#","%23");
		return '<a href="http://search.twitter.com/search?q=' + tag + '" target="_blank">' + t + '</a>';
	});
};


