function limitText(limitField, limitCount, limitNum) {
	if (limitField.val().length > limitNum) 
	{
		limitField.val(limitField.val().substring(0, limitNum));
		
	} 
	else 
	{
		limitCount.text( limitNum - limitField.val().length );
		//translateText();
	}
}
function translateText()
{
	var twitter_name = $("#twitterUsername").html();
	var twit_length = twitter_name.length;
	var cut_off = (101 - 48 - twit_length); 
	var question_text = $("#question").val();
	
	if (question_text.length >= cut_off)
	{
		var question_text = question_text.substring(0, cut_off) + " ... ";	
	}
	
	if ($("#tweetText").length != 0)
	{
		$("#tweetText").html(question_text);
	}
}

function checkPerms()
{
	FB.Connect.showPermissionDialog("publish_stream", 
		function(){
		FB.Connect.showPermissionDialog("offline_access", 
			function(){
				var url = location.href;
				var base_url = url.split("/");
				var redirect = "facebook.php";
				window.location.href = redirect;
			});
		});
}	

$(document).ready(function(){
	
	if ($("#question").length > 0)
	{
		limitText(jQuery('#question'),jQuery('#myCounter'),250);
	}

	$('#submit').click(function() {
		
		var args = $("#question_form").serialize();
		$("#question").val('');
		
		if ($("#askContainer").length > 0)
		{
			$("#askInside").fadeTo("slow", 0.33);
			$("#askInsideEdit").fadeTo("slow", 0.33);
			$("#askContainer").prepend("<div id='askSending'><p><img src='../core/images/loader.gif'></p></div>");	
		}
		else if ($(".askOverlay").length > 0)
		{
			$(".askForm").fadeTo("slow", 0.33);
			//$(".askOverlay").html("<img src='../core/images/loader.gif'>");
			$(".askOverlay").fadeIn("slow");
		}
		else
		{
			$("#question").mask("Submitting Question...");
			$("#question_form").mask("Submitting Question...");
		}
		
		
		
		$("#submit").hide();
		$.ajax({
			url: "../core/data/store.php",
			type: "POST",
			data: args,
			dataType: "json",
			error: function(timeout, error)
			{
				limitText(jQuery('#question'),jQuery('#myCounter'),250);
				$("#askInside").fadeTo("slow", 1);
				jConfirm('Sorry, there was an unknown error in posting your question. Please try your question again. Error Code: ' + error, 'Oops!', function(r) 
				{
					if (r == "true" || r == true)
					{
						window.location.href = "ask";
					}
					else
					{
						window.location.href = "ask";
					}
				});
				
			},
			success: function(html)
			{
				limitText(jQuery('#question'),jQuery('#myCounter'),250);
				if (html.posttype == "twitter")
				{
					/*if (html.follow.length != 0)
					{
						$(html.follow).each(function(i, items){
							
							if (items.result == "true")
							{
								
							}
							else
							{
								
							}
							
						});
					}*/

					if (html.type == "edit")
					{
						if (html.db_entry == "success")
						{
							window.location.href = "thanks";
						}
						else
						{
							jAlert("There was an error with your entry. " + html.db_entry);
						}
					}
					else
					{
						if (html.db_entry == "success")
						{
							if (html.post == "success")
							{
								window.location.href = "thanks";
							}
							else
							{
								jAlert("We could not post your message to twitter due to the following error: " + html.post);
							}
						}
						else
						{
							jAlert("There was an error with your entry. " + html.db_entry);
						}
					}
				
				}
				else
				{
					if (html.type == "edit")
					{
						if (html.db_entry == "success")
						{
							window.location.href = "thanks";
						}
						else
						{
							jAlert("There was an error with your entry. " + html.db_entry);
						}
					}
					else
					{
						if (html.db_entry == "success")
						{
							if (html.post == "success")
							{
								window.location.href = "thanks";
							}
							else
							{
								jAlert("We could not post your message to Facebook due to the error: " + html.post);
							}
						}
						else
						{
							jAlert("There was an error with your entry. " + html.db_entry);
						}
					}
				}
			
			}
		});
		
		
		return false;
	});
});