var webRoot = "/";

function getActivities(id)
{
	$("#more_"+id).show();
	//alert("/scripts/get.php?type=record&data="+id);
	$.getJSON(webRoot+"/scripts/get.php?type=record&data="+id,
     function(data)
     {
       	switch(data.result)
		{
			case "got":
				if(typeof(data.data) != "undefined")updateActivities(id,data.data);
				break;
			case "failed":
				//alert(data.msg);
				break;
		}
     });
}

function feedback()
{
	$.fn.fancybox.close();
	var link = webRoot+"/scripts/feedback.php?type="+$("#fancy_div #type").val()+"&feedback_name="+$("#fancy_div #feedback_name").val()+"&feedback_email="+$("#fancy_div #feedback_email").val()+"&feedback_message="+$("#fancy_div #feedback_message").val()+"&record_name="+$("#record_name").val()+"&record_id="+$("#record_id").val();

	$.getJSON(link,
     function(data)
     {
       	if(data.saved)alert("Thank you for your feedback");
       	else alert("failed to save feedback");
     });
}

function sendFeedback(myForm)
{
    subName = myForm.iname.value;
    subEmail = myForm.iemail.value;
    subMessage = myForm.imessage.value;
    subType = myForm.itype.value;
    
    if(subName == "" || subEmail == ""  || subMessage == "" )
    {
        alert("Please complete all parts of the form");
    }
    else
    {
	   $.fn.fancybox.close();
    	var link = webRoot+"/scripts/feedback.php?type="+subType+"&feedback_name=" +subName+"&feedback_email="+subEmail+"&feedback_message="+subMessage+"&record_name="+$("#record_name").val()+"&record_id="+$("#record_id").val();

	   $.getJSON(link,
         function(data)
         {
           	if(data.saved)alert("Thank you for your feedback");
           	else alert("failed to save feedback");
         });
     }
}

function objectLength(obj)
{
	var count = 0;
	if(typeof(obj) != "undefined")$.each(obj,function(){count++;})
	return count;
}

function updateActivities(id,data)
{
	if(id && data)
	{
		//data = jQuery.makeArray(data);
		var html = "";
		if(objectLength(data))
		{
			html += "<ul>";
			$.each(data,function(i,activity)
			{
				html += "<li>";
				html += activity["title"];
				html += "</li>";
			});
			html += "</ul>";
		}
		else html += "<br/><br/>No Activities";
		$("#"+id+"_activities").html(html);
	}
}

function sendto()
{
	$.fn.fancybox.close();
	var link = webRoot+"/scripts/sendto.php?sendto_name="+$("#fancy_div #sendto_name").val()+"&sendto_email="+$("#fancy_div #sendto_email").val()+"&recordID="+$("#fancy_div #recordID").val();

	$.getJSON(link,
     function(data)
     {
       	if(data.sent)alert("This page has been sent to your friend.");
       	else alert("failed to send page.");
     });
}

jQuery.fn.tooltip = function(options)
{		
		var cleared = false;
		
		jQuery.extend(options);
		
		$(this).focus(function(object)
		{
			if(!cleared)
			{
				$(this).val("");
				$(this).removeClass("tooltip");
				cleared = true;
			}
		});
		
		if(typeof(options["tip"]) != "undefined")
		{
			$(this).addClass("tooltip");
			$(this).val(options["tip"]);
		}
		return null;
}

