$(document).ready(function() {
		$("#commandMain").keypress(function(e) { 
			if (e.which == 13) {
			$("#commandSuggest").slideUp();
			var command = $("#commandMain").val();
			$.post("http://kwyno.com/request/", { command: command }, function(data){
   				 $("#commandResult").html(data);
   				 $("#commandTitle").text("What would you like to do next?");
			});
			}
		});
		
		$("#specialMessage").click(function() { 
			$("#specialMessage").fadeOut("slow");
		});			
});


$(document).ajaxStart(function() { 
	$("#ajaxLoading").fadeIn();
});

$(document).ajaxSuccess(function() { 
	$("#ajaxLoading").fadeOut();
});

function goAway(id) {
	$(id).fadeOut("slow");
}

function characterCounter(object) {
	var charLength = $(object).val().length;
	var charToGo = 140 - charLength;
	if(charToGo > 50 ) {
		var htmlDisplay = '<font color="green">' +charToGo +' characters to go</font>';
	} else if(charToGo > 0) {
		var htmlDisplay = '<font color="red">' +charToGo +' characters to go</font>';
	} else {
		var htmlDisplay = '<font color="red">Kwyno will auto-adjust this reply to 139 characters</font>';
	}
	$("#counterDisplay").html(htmlDisplay);
}

function clearText() {
	var currentValue = $("#commandMain").val();
	if(currentValue == "Run your first command! Type in 'check news' and press enter") {
		$("#commandMain").val("");
	} else if(currentValue == "calculate 54 * 43") {
		$("#commandMain").val("");
	}
}

function tweetReplyExact(twitterURL, statusID) {
	$("#replyError").fadeOut("fast");
	var replyLen = $("#tweetReplyContent").val().length;
	if(replyLen < 16) { 
		$("#replyError").html('<font color="red">That\'s a rather short reply! Try again.</font>');
		$("#replyError").fadeIn("slow");
	} else {
		var replyValue = $("#tweetReplyContent").val();
		$.post("http://kwyno.com/tweetexact.php", { twitterID: twitterURL, statusID: statusID, reply: replyValue }, function(data) { 
			$("#specialMessage").html("Your reply to " +twitterURL +" has been made with success");
			$("#specialMessage").fadeIn("slow");
			closeLight();
		});
	}
}

function youtubeWatch(requestString) {
	$.post("http://kwyno.com/request/", { command: "youtubeexact " +requestString }, function(data) { 
		$("#commandResult").html(data);
	});
}

function stopFollowingDisplay(request) {
	$("#mainlight").jqm({ajax: "http://kwyno.com/tweetstopfollow.php?request=" +request, ajaxText: '<div style="padding:10px;">Just one second...loading everything</div>', modal: true});
	$("#mainlight").jqmShow();
}

function bugreport() {
	$("#mainlight").jqm({ajax: "http://kwyno.com/bugreport.php", ajaxText: '<div style="padding:10px;">Just one second...loading everything</div>', modal: true});
	$("#mainlight").jqmShow();
}

function submitBug() {
	$("#whatHappenedDisplay").html('');
	var whatHappened = $("#whatHappened").val();
	if(whatHappened == "") {
		$("#whatHappenedDisplay").html('<font color="red">Let us know exactly what happened!</font> <br />');
	} else {
		$.post("http://kwyno.com/reportbug.php", { message: whatHappened }, function(data) { 
			$("#specialMessage").html("Thanks for reporting! We'll take a look at it shortly.");
			$("#specialMessage").fadeIn("slow");
			closeLight();			
		});
	}
}

function feedbackdisplay() {
	$("#mainlight").jqm({ajax: "http://kwyno.com/featurerequest.php", ajaxText: '<div style="padding:10px;">Just one second...loading everything</div>', modal: true});
	$("#mainlight").jqmShow();
}

function submitfeedback() {
		var kwynoLike = $("#kwynoLike").val();
		var kwynoDislike = $("#kwynoDislike").val();
		var kwynoFeature = $("#kwynoFeature").val();
		var kwynoMessage = $("#kwynoMessage").val();
		var errors = 0;
			
		if(kwynoMessage == "") {
			$("#kwynoMessageDisplay").html('<font color="red">Let us know a bit more, send us a message!</font>');
			var errors = 1;
		} else {
			$("#kwynoMessageDisplay").html('Thank you');
		}
		
  		if(errors == 0) {
		$.post("http://kwyno.com/feedbackprocess.php", { like: kwynoLike, dislike: kwynoDislike, feature: kwynoFeature, message: kwynoMessage }, function(data) { 
			$("#specialMessage").html("Thanks for the feedback! We'll take a look at it shortly.");
			$("#specialMessage").fadeIn("slow");
			closeLight();			
		});  		
	}
 }


function deleteTweetDisplay(request) {
	$("#mainlight").jqm({ajax: "http://kwyno.com/tweetdelete.php?request=" +request, ajaxText: '<div style="padding:10px;">Just one second...loading everything</div>', modal: true});
	$("#mainlight").jqmShow();
}

function deleteTweet(tweetID) {
		var hide = "." +tweetID;
		$.post("http://kwyno.com/deletetweet.php", { twitterID: tweetID }, function(data) { 
			$("#specialMessage").html("Your tweet has been successfully deleted");
			$("#specialMessage").fadeIn("slow");
			$(hide).hide();
			closeLight();
		});
}

function stopFollowing(twitterUsername) {
		var hide = "." +twitterUsername;
		$.post("http://kwyno.com/stopfollow.php", { twitterID: twitterUsername }, function(data) { 
			$("#specialMessage").html("You are no longer following " +twitterUsername);
			$("#specialMessage").fadeIn("slow");
			$(hide).hide();
			closeLight();
		});
}

function closeEmailLightbox() {
	$("#emailSend").jqmHide();
	$("#friendsname").val("");
	$("#friendsemail").val("");
	$("#friendsubject").val("");
	$("#friendMessage").val("");
	$("#requestValue").html("");
	$("#friendnameDisplay").html("");
	$("#friendemailDisplay").html("");
	$("#friendsubjectDisplay").html("");
	$("#friendMessageDisplay").html("");
	
}

function youtubeComments() {
	$("#youtubeComments").slideToggle();
}

function maplocation(requestString) {
	$.post("http://kwyno.com/request/", { command: "mapexact " +requestString }, function(data) { 
		$("#commandResult").html(data);
	});
}

function sendFriendEmail(requestValue) {
	$("#requestValue").html(requestValue);
	$('#emailSend').jqm({modal: true});
	$("#emailSend").jqmShow(); 
}

function setMeetup(requestValue) {
	$("#lightSandbox").html(requestValue);
	$('#mainlight').jqm({ajax: "http://kwyno.com/friendmeetup.php", ajaxText: '<div style="padding:10px;">Just one second...loading everything</div>' , modal: true});
	$("#mainlight").jqmShow();
}

function closeLight() {
	$("#lightSandbox").html("");
	$("#mainlight").jqmHide();	
}

function tweetReply(request) {
	$("#mainlight").jqm({ajax: "http://kwyno.com/tweetreply.php?request=" +request, ajaxText: '<div style="padding:10px;">Just one second...loading everything</div>', modal: true});
	$("#mainlight").jqmShow();
}

function showwipeeLight() {
	$("#mainlight").jqm({ajax: "http://kwyno.com/newtask.php", ajaxText: '<div style="padding:10px;">Just one second...loading everything</div>', modal: true });
	$("#mainlight").jqmShow();
}

function aboutLight() {
	$("#mainlight").jqm({ajax: "http://kwyno.com/userhelp.php", ajaxText: '<div style="padding:10px;">Just one second...loading everything</div>', modal: true });
	$("#mainlight").jqmShow();
}

function newTask() {
	$("#titleError").html("");
	var taskTitle = $("#taskTitle").val();
	
	if(taskTitle == "") {
		$("#titleError").html("What do you need to do?");
	} else {
		var builtCommand = "task " +taskTitle;
		runcommand(builtCommand, '0', "");
		closeLight();
	}
}

function sendInvitation() {
	var friendsName = $("#friendsname2").val();
	var friendsEmail = $("#friendsemail2").val();
	var friendSubject = $("#friendsubject2").val();
	var friendMessage = $("#friendMessage2").val();
	var friendtime = $("#friendtime").val();
	var requestValue = $("#lightSandbox").html();
	var errors = 0;
	
	if(friendsName == "") {
		$("#friendnameDisplay2").html('<font color="red">What is your friend\'s name?</font>');
		var errors = 1;
	} else {
		$("#friendnameDisplay2").html('Ok! Good to go');
	}
	
	if(friendsEmail == "") {
		$("#friendemailDisplay2").html('<font color="red">What is your friend\'s eMail?</font>');
		var errors = 1;
	} else {
		$("#friendemailDisplay2").html('Looks good..');
	}
	
	if(friendSubject == "") {
		$("#friendsubjectDisplay2").html('<font color="red">What should the message\'s subject be?</font>');
		var errors = 1;
	} else {
		$("#friendsubjectDisplay2").html('Alrighty...');
	}
	
	if(friendtime == "") {
		$("#friendtimeDisplay").html('<font color="red">What time should your friend meet you?</font>');
		var errors = 1;
	} else {
		$("#friendtimeDisplay").html('Sweet!');
	}
	
	if(friendMessage == "") {
		$("#friendMessageDisplay2").html('<font color="red">You need a message!</font>');
		var errors = 1;
	} else {
		$("#friendMessageDisplay2").html('Looks good!');
	}
	
	if(errors == 0) {
		$.post("http://kwyno.com/sendinvite.php", { name: friendsName, email: friendsEmail, time: friendtime, subject: friendSubject, message: friendMessage, requestValue: requestValue }, function(data){
			$("#specialMessage").html("Your invite has been sent with success. Click to disappear");
			$("#specialMessage").fadeIn("slow");
			closeLight();
		});
	}
}


function sendEmailX() {
	var friendsName = $("#friendsname").val();
	var friendsEmail = $("#friendsemail").val();
	var friendSubject = $("#friendsubject").val();
	var friendMessage = $("#friendMessage").val();
	var requestValue = $("#requestValue").html();
	var errors = 0;
	
	if(friendsName == "") {
		$("#friendnameDisplay").html('<font color="red">What is your friend\'s name?</font>');
		var errors = 1;
	} else {
		$("#friendnameDisplay").html('Ok! Good to go');
	}
	
	if(friendsEmail == "") {
		$("#friendemailDisplay").html('<font color="red">What is your friend\'s eMail?</font>');
		var errors = 1;
	} else {
		$("#friendemailDisplay").html('Looks good..');
	}
	
	if(friendSubject == "") {
		$("#friendsubjectDisplay").html('<font color="red">What should the message\'s subject be?</font>');
		var errors = 1;
	} else {
		$("#friendsubjectDisplay").html('Alrighty...');
	}
	
	if(friendMessage == "") {
		$("#friendMessageDisplay").html('<font color="red">You need a message!</font>');
		var errors = 1;
	} else {
		$("#friendMessageDisplay").html('Looks good!');
	}
	
	if(errors == 0) {
		$.post("http://kwyno.com/sendemaillight.php", { name: friendsName, email: friendsEmail, subject: friendSubject, message: friendMessage, requestValue: requestValue }, function(data){
			$("#specialMessage").html("The e-Mail has been sent with success. Click to disappear");
			$("#specialMessage").fadeIn("slow");
			closeEmailLightbox();
		});
	}
}

function paginatesearch(pageValue) {
	var currentvalue = $("#commandMain").val();
	$.post("http://kwyno.com/request/", { command: currentvalue, page: pageValue } , function(data) { 
		$("#commandResult").html(data);
	});
}

function favoriteWebsite(websiteURL, websiteName, websiteDescription) {
	var websiteURL = $.base64.decode(websiteURL);
	var websiteName = $.base64.decode(websiteName);
	var websiteDescription = $.base64.decode(websiteDescription);
	var currentResponse = $("#commandResult").html();
	
	$.post("http://kwyno.com/addtofavorites.php", { websiteURL: websiteURL, websiteName: websiteName, websiteDescription: websiteDescription }, function(data) { 
		$("#commandResult").html(currentResponse);
		$("#specialMessage").html(websiteName +" has been favorited with success. Click to disappear");
		$("#specialMessage").fadeIn("slow");	
	});	
}

function loadTweets() {
	$.post("http://kwyno.com/request/", { command: "check twitter" }, function(data) { 
		$("#commandResult").html(data);	
		$("#commandMain").val("check twitter");
	});
}

function initialize(lat, lon) {
  if (GBrowserIsCompatible()) {
    var map = new GMap2(document.getElementById("mapDirections"));
    map.setCenter(new GLatLng(lat, lon), 15);
	var mapControl = new GMapTypeControl();
	map.addOverlay(new GMarker(new GLatLng(lat, lon)));
	map.addControl(mapControl);
	map.addControl(new GSmallMapControl());
  }
}

function runcommand(command, paginatepage, displaybox) {
	$.post("http://kwyno.com/request/", { command: command, page: paginatepage }, function(data) { 
		if(displaybox == "") {
			$("#commandMain").val(command);
		}
		$("#commandResult").html(data);
	});
}

function learnWebsite(websiteURL) {
	$.post("http://kwyno.com/lwebsite.php", { website: websiteURL }, function(data) { 
		$("#learnX").html(data);
	});
}

function taskManager(taskID, action) {
	$.post("http://kwyno.com/taskstatus.php", { taskid: taskID, action: action }, function(data) { 
		var numTasks = $("#numTasks").html();
		var numTasksFinal = numTasks - 1;
		$("#numTasks").html(numTasksFinal);
		$("#taskAmount").html("You have " + numTasksFinal +" pending tasks");
		$("#" +taskID).fadeOut("slow");
	});
}

function kwyno139update() {
	var currentCommand = $("#commandMain").val();
	var finalCommand = "twkxfix " + currentCommand;
	$.post("http://kwyno.com/request/", { command: finalCommand }, function(data) { 
		$("#commandResult").html(data);	
	});
}