/**Helper Javascript Functions **/

/**Time**/


function formatTime(datetime)
{	
	var date = parseDatetime(datetime);
	return formatTimeFromDate(date);
}

//JS Date Object - 4:12pm
function formatTimeFromDate(date)
{
	var hours = date.getHours();
	var minutes = date.getMinutes();
	
	 var suffix = "am";
	  if (hours >= 12) {
	  suffix = "pm";
	  	hours = hours - 12;
	  }
	  if (hours == 0) {
		  hours = 12;
	  }

	  if (minutes < 10) minutes = "0" + minutes;
	  
	  //if earlier than today show day
	  // var today = new Date();
	  // var day = today.getDay();
	  return hours+":"+minutes + suffix;
}

//Takes a mysql datetime string in the format '2010-12-12 12:12:12';
//Turns it into a javascript datetime object
function parseDatetime(string) {  
  var date = new Date();  
  var parts = String(string).split(/[- :]/);  

  date.setFullYear(parts[0]);  
  date.setMonth(parts[1] - 1);  
  date.setDate(parts[2]);  
  date.setHours(parts[3]);  
  date.setMinutes(parts[4]);  
  date.setSeconds(parts[5]);  
  date.setMilliseconds(0);  
  var retDate = new Date();
  retDate.setTime(date.getTime()+timeOffset*1000);
  return retDate;  
};  


function formatTimeAsTimeAgo(datetime) {
	
	var date = parseDatetime(datetime);
	
	var current = new Date();
	var minute = 1000*60; 
	var timePassed = Math.floor((current.getTime() - date.getTime()) / minute);
	
	//In minutes
	var hour = 60;
	var day = hour * 24; 
	var week = day * 7;
	var year = week * 52;
	var months = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];	
	
	if (timePassed <= 1) {
		return "just now";
	
	} else if (timePassed < 2) {
	
		return "a minute ago";
	} else if (timePassed < 55) {
		return timePassed + " minutes ago";
		
	} else if (timePassed < 61) {
		return "an hour ago";
	
	}
	//Today
	else if(timePassed < day)
	{
		//5:15am
		return formatTimeFromDate(date);
	}
	else if (timePassed < week) { 
		//Sun 5:45pm
		var weekdays= ["Sun","Mon","Tue","Wed","Thu","Fri","Sat"];
		return weekdays[date.getDay()] + ' ' + formatTimeFromDate(date);
	} else if (timePassed < year) {
		//Apr 17 5:45pm	
		return months[date.getMonth()] + ' ' + date.getDate() + ' ' + formatTimeFromDate(date);
	} else {
		//Apr 17, 2007 5:45pm	
		return months[date.getMonth()] + ' ' + date.getDate() + ', ' + date.getFullYear() + ' ' + formatTimeFromDate(date);
	} 
}

function encodeLocationInQueryString(mapLocation)
{
	var lat = encodeURIComponent(mapLocation.lat());
	var lng = encodeURIComponent(mapLocation.lng());
	return 'lat=' + lat + '&long=' + lng;
}
function distanceBetween(coord1, coord2)
{
	//Radius of earth
	var R = 6371;
	
	var lat1 = degreesToRadians(coord1.lat());
	var long1 = degreesToRadians(coord1.lng());
	var lat2 = degreesToRadians(coord2.lat());
	var long2 = degreesToRadians(coord2.lng());
	
	var d = Math.acos(Math.sin(lat1)*Math.sin(lat2) + 
					Math.cos(lat1)*Math.cos(lat2) *
					Math.cos(long2-long1)) * R;
	
	//To Miles
	return 0.6214 * d;
}

function degreesToRadians(degrees)
{
	return degrees*Math.PI/180;
}

//FhConstants
function FhConstant(){}
FhConstant.JPEG = 1;
FhConstant.ACTION_TYPE_LOGIN = 0;
FhConstant.ACTION_TYPE_INTERACTION = 1;
FhConstant.ACTION_TYPE_LOGOUT = 2;
FhConstant.ACTION_TYPE_ALLOW_LOCATION = 3;
FhConstant.ACTION_TYPE_DENY_LOCATION = 4;
FhConstant.ACTION_TYPE_BROWSER_LOCATION_INCAPABLE = 5;
FhConstant.ACTION_TYPE_ERROR_GETTING_LOCATION = 6;

FhConstant.BROWSER_TYPE_FIREFOX = 0;
FhConstant.BROWSER_TYPE_CHROME = 1;
FhConstant.BROWSER_TYPE_SAFARI = 2;
FhConstant.BROWSER_TYPE_IE = 3;
FhConstant.BROWSER_TYPE_UNKNOWN = 4;

function FhGeoCoordinate(lat,lng){
    this.type = "FhGeoCoordinate";
    this.latitude = lat;
    this.longitude = lng;
}

FhHop.HOP_TYPE_CHECK_IN = 0;
FhHop.HOP_TYPE_MESSAGE = 1;
FhHop.HOP_TYPE_PHOTO = 2;

function FhHop(id,userId,venueTypeId,venueId, hopTypeId) {  
    this.type = "FhHop";
    this.id = id;
    this.userId = userId;
    this.venueTypeId = venueTypeId;
    this.venueId=venueId;
    this.hopTypeId = hopTypeId;
    //add these
    this.file = null;
    this.filename = null;
    this.data = null;
}

//FhId object
function FhId(id,idType){
	this.type="FhId";
	this.idType = idType;
	this.id = id;
	this.isValid="true";
	this.equals= function (other){
		return (other.id==this.id && other.idType==this.idType);
	};
}

function FhUser(firstName, lastName, latitude, longitude)
{
	this.type = "FhUser";
	this.firstName = firstName;
	this.lastName = lastName;
	this.latitude = latitude;
	this.longitude = longitude;
}


$(document).ready(function(){
	$('#leader_info_answer').addClass('hidden');
});
var hidden = true;
function toggleDiv(){
	if(hidden)$('#leader_info_answer').removeClass('hidden');
	else $('#leader_info_answer').addClass('hidden');
	hidden = !hidden;
}




var defaultText = "Add to the conversation";

function  texthop_validate(){
	var text = $("#textHopInput").val();
	if(text.length==0 || text==defaultText){
		alert("You need to write something!");
		return false;
	}
}

function flagResult(data){
	alert("Thanks for flagging! We'll look into it soon.")
}
function initFlagButton(userId,objectId,objectTypeId){
	$("#flag_button").click(function (evt){
		// send ajax request for flag
		var params = JSON.stringify([userId,objectId, objectTypeId]);
		$.ajax({
			  url:interfaceUrl,
			  type:'GET',
			  dataType:'jsonp',
			  jsonp:'callback',
			  data:{"serviceName":'fh_flagItem',"params":params,"auth":authToken},
			  jsonpCallback: 'flagResult'
			});
		
		
		$("#flag_button").addClass("hidden");
		
	});
}

function initTextHop(){
	//Default Text for hop box
	 $("#textHopInput").focus(function(srcc)
	    {
	        if ($(this).val() == defaultText)
	        {
	            $(this).removeClass("defaultText");
	            $(this).val("");
	        }
	    });
			    
   $("#textHopInput").blur(function()
   {
       if ($(this).val() == "")
       {
           $(this).addClass("defaultText");
           $(this).val(defaultText);
       }
   });
			    
	$("#textHopInput").blur(); 
}

