function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}



$(document).ready(function(){ 
						   
	$("#notificationsText").animate({ opacity: 0 });
						   
	$(function() {
		$("#contentWrapLeft ul").sortable({ opacity: 0.6, cursor: 'move', update: function() {
			var order = $(this).sortable("serialize") + '&action=updateTweetOrders'; 
			$.post("inc/functions.php", order); 															 
		}								  
		});
		$("#contentWrapLeft ul").disableSelection();
	});



	$("#scheduleStartTime").clockpick({
		starthour : 0,
		endhour : 23,
		minutedivisions : 12,
		military : true,
		showminutes : true
	}
	); 
	
	$("#scheduleEndTime").clockpick({
		starthour : 0,
		endhour : 23,
		minutedivisions : 12,
		military : true,
		showminutes : true
	}
	);



	$('.word_count').each(function(){
		// get current number of characters
		var length = $("#tweetDetails").val().length;
		// get current number of words
		//var length = $(this).val().split(/\b[\s,\.-:;]*/).length;
		// update characters
		$(".counter").html('<strong>' + (140 - length) + '</strong> chars. left');
		// bind on key up event
		$("#tweetDetails").keyup(function(){
			// get new length of characters
			var new_length = $("#tweetDetails").val().length;
			// get new length of words
			//var new_length = $(this).val().split(/\b[\s,\.-:;]*/).length;
			// update
			$(".counter").html('<strong>' + (140 - new_length) + '</strong> chars. left');
		});
	});





	$(".hiddenTweetLi").hide();

	$("#tweetSubmit").click(function() {
		
		var tweetDetailsVal = $("#tweetDetails").val();
		
		if (tweetDetailsVal.length > 0) {
			
			$("#notificationsText").empty();
			
			if (tweetDetailsVal.length <= 140) {
				
				$("#notificationsText").empty();
				$("#notificationsLoader").html('<img src="images/loader.gif">');
	
			
				$.ajax({  
				type: "POST",  
				url: "inc/functions.php",  
				data: { tweetDetails: tweetDetailsVal, action: "newTweet" },  
				success: function(theResponse) { 
					$("#contentWrapLeft li:last").after(theResponse);
					$("#contentWrapLeft li:last").hide();
					$("#contentWrapLeft li:last").show("slow");
					$("#notificationsLoader").empty();
					$("#notificationsText").html('<img src="images/ok.png"> Tweet Added!');
					$("#notificationsText").animate({ opacity: 1 }, 1500);
					$("#notificationsText").animate({ opacity: 0 }, 1500);
					$("#tweetDetails").val('');
					$("input#shortURL").val('');
					
	
				}  
				});  
				return false; 
				
			} else {
				$("#notificationsText").html('<img src="images/no.png"> Tweet is > 140 chars!');
				$("#notificationsText").animate({ opacity: 1 }, 1500);
				$("#notificationsText").animate({ opacity: 0 }, 1500);
				return false;
			}

		
		} else {
			$("#notificationsText").html('<img src="images/no.png"> Tweet is not mentioned!');
			$("#notificationsText").animate({ opacity: 1 }, 1500);
			$("#notificationsText").animate({ opacity: 0 }, 1500);
			return false;
		}
	
	});
	
	
	$("#shortURLSubmit").click(function() {
		var shortURLVal = $("input#shortURL").val();
		
		if (shortURLVal.length > 0) {
			
			$("#notificationsText").empty();
			$("#notificationsLoader").html('<img src="images/loader.gif">');
		
			$.ajax({  
			type: "POST",  
			url: "inc/functions.php",  
			data: { shortURL: shortURLVal, action: "shortenURL" },  
			success: function(theResponse) {  
				var temptweetDetailsVal = $("#tweetDetails").val();
				$("#tweetDetails").val(temptweetDetailsVal + trim(theResponse));
				$(".word_count").parent().find('.counter').html( 140 - $(".word_count").val().length);
				$("#notificationsLoader").empty();
				$("#notificationsText").html('<img src="images/ok.png"> URL Shortened!');
				$("#notificationsText").animate({ opacity: 1 }, 1500);
				$("#notificationsText").animate({ opacity: 0 }, 1500);

				
			}  
			});  
			return false;  
		
		} else {
			$("#notificationsText").html('<img src="images/no.png"> URL is not mentioned!');
			$("#notificationsText").animate({ opacity: 1 }, 1500);
			$("#notificationsText").animate({ opacity: 0 }, 1500);
			return false;
		}
	
	});
	
	
	$("#newScheduleSubmit").click(function() {
		var scheduleStartTimeVal = $("input#scheduleStartTime").val();
		var scheduleEndTimeVal = $("input#scheduleEndTime").val();
		var scheduleDayVal = $("#scheduleDay").val();
		var scheduleIntervalVal = $("input#scheduleInterval").val();
		
		if (scheduleStartTimeVal.length > 0 && scheduleEndTimeVal.length > 0 && scheduleIntervalVal.length > 0)  {
			
			$("#notificationsText").empty();
			$("#notificationsLoader").html('<img src="images/loader.gif">');
	
			$.ajax({  
			type: "POST",  
			url: "inc/functions.php",  
			data: { scheduleDay: scheduleDayVal, action: "newSchedule", scheduleStartTime: scheduleStartTimeVal, scheduleEndTime: scheduleEndTimeVal, scheduleInterval: scheduleIntervalVal },  
			success: function(theResponse) {
				$("#schedulesWrap li:first").before(theResponse);
				$("#schedulesWrap li:first").hide();
				$("#schedulesWrap li:first").show("slow");  
				$("#notificationsLoader").empty();
				$("#notificationsText").html('<img src="images/ok.png"> Schedule Added!');
				$("#notificationsText").animate({ opacity: 1 }, 1500);
				$("#notificationsText").animate({ opacity: 0 }, 1500);
	
			}  
			});  
			return false;  
			
		} else {
			$("#notificationsText").html('<img src="images/no.png"> Schedule is not mentioned!');
			$("#notificationsText").animate({ opacity: 1 }, 1500);
			$("#notificationsText").animate({ opacity: 0 }, 1500);
			return false;
		}
	
	});		
	
	
	
	$("#stopStartSystem").click(function() {
		
			$.ajax({  
			type: "POST",  
			url: "inc/functions.php",  
			data: { action: "stopStartSystem" },  
			success: function(theResponse) {
				$("#stopStartSystem").empty();
				$("#stopStartSystem").html(theResponse);
				$("#notificationsText").animate({ opacity: 1 }, 1500);
				$("#notificationsText").animate({ opacity: 0 }, 1500);
	
			}  
			});  
			return false;  

	
	});		
	
	
	
	
	$("#installSubmit").click(function() {
									   
		$("#installNotifications").empty();
		var h = $("body").height();
		
		var installDBNameVal 			= $("input#installDBName").val();
		var installDBUserVal 			= $("input#installDBUser").val();
		var installDBPassVal 			= $("input#installDBPass").val();
		var installDBHostVal 			= $("input#installDBHost").val();
		var installTwitterUsernameVal 	= $("input#installTwitterUsername").val();
		var installTwitterPasswordVal 	= $("input#installTwitterPassword").val();
		var installSTUsernameVal 		= $("input#installSTUsername").val();
		var installSTPasswordVal 		= $("input#installSTPassword").val();
		
		
		$("#installLoader").html('<img src="images/loader.gif">');

		$.ajax({  
		type: "POST",  
		url: "inc/functions.php",  
		data: { installDBName: installDBNameVal, action: "install", installDBUser: installDBUserVal, installDBPass: installDBPassVal, installDBHost: installDBHostVal, installTwitterUsername: installTwitterUsernameVal, installTwitterPassword: installTwitterPasswordVal, installSTUsername: installSTUsernameVal, installSTPassword: installSTPasswordVal },  
		success: function(theResponse) {
			
			$("#installLoader").empty();
			if (theResponse == 0) {
				alert ('Can not connect to MySQL!');
			}
			if (theResponse == 1) {
				alert('Can not write to config.php!');
			}
			if (theResponse == 2) {
				
				$("#loginFormWrap").hide("slow");
				$("#loginFormWrap").empty();
				$("#loginFormWrap").slideDown("slow");
				$("#loginFormWrap").html('<img src="images/ok.png"> ScheduledTweets Installed!<br /><br /> <a href="index.php">Click here to login</a>');
				
			}
			


		}  
		});  
		return false;  
			
	
	});		
	


	
});


function deleteTweet(deleteTweetVal) 
{ 
	$.ajax({  
	type: "POST",  
	url: "inc/functions.php",  
	data: { deleteTweetID: deleteTweetVal, action: "deleteTweet" },  
	success: function(theResponse) {  
		$("#contentWrapLeft ." + deleteTweetVal).hide("slow");
	}  
	});

};


function deleteSchedule(deleteScheduleVal) 
{ 
	$.ajax({  
	type: "POST",  
	url: "inc/functions.php",  
	data: { deleteScheduleID: deleteScheduleVal, action: "deleteSchedule" },  
	success: function(theResponse) {  
		$("#schedulesWrap ." + deleteScheduleVal).hide("slow");
	}  
	});

};