jQuery(document).ready(function() {

	/** Cookie Checking Function (*/
	function readCookie(cookieName) {
		var theCookie=""+document.cookie;
		var ind=theCookie.indexOf(cookieName);
		if (ind==-1 || cookieName=="") return ""; 
		var ind1=theCookie.indexOf(';',ind);
		if (ind1==-1) ind1=theCookie.length; 
		return unescape(theCookie.substring(ind+cookieName.length+1,ind1));
	}

	/** Like This Functionality */
	$('a.like').click(function(event) {
		event.preventDefault();
		var jokeID = $(this).attr('alt');
		var userID = $('.user').attr('alt');
		$.post("/php/like.php", { id: jokeID, uID: userID, cookie: "set" });
		var toggleClass = ".toggle-" + jokeID;
		$(toggleClass).toggle();
	});
	$('a.unlike').click(function(event) {
		event.preventDefault();
		var jokeID = $(this).attr('alt');
		var userID = $('.user').attr('alt');
		$.post("/php/like.php", { id: jokeID, uID: userID, cookie: "delete" });
	});
	$('a.unlike-toggle').click(function(event) {
		event.preventDefault();
		var jokeID = $(this).attr('alt');
		var toggleClass = ".unlike-toggle-" + jokeID;
		$(toggleClass).toggle();
	});
	
	/** Profile Box Functionality */
	$('.pc').click(function(event) {
		event.preventDefault();
		$(".pe").slideToggle("medium");

	});
	$('.bc').click(function(event) {
		event.preventDefault();
		$(".be").slideToggle("medium");
	});
	
	/** Focus on First Field on Load */
	$(".focus").focus();
	
	

	/** Tips Functionality */
	$('.text-field').focus(function(event) {
		var fieldID = $(this).attr('alt');
		var tipClass = ".tip-" + fieldID;
		var fieldClass = ".field-" + fieldID;
		$(tipClass).removeClass("none");
		$(fieldClass).addClass("highlight-neut");
	});
	$('.text-field').blur(function(event) {
		var fieldID = $(this).attr('alt');
		var tipClass = ".tip-" + fieldID;
		var fieldClass = ".field-" + fieldID;
		var inputtedText = $("input.url-replace").val();
		$(tipClass).addClass("none");
		$(fieldClass).removeClass("highlight-neut");
	});
	
	/** Register URL Functionality */
	$("input.url-replace").keyup(function(event) {
		var inputtedText = $("input.url-replace").val();
		$(".directory").addClass("pink");
		$(".directory").text(inputtedText);
	});
	
	/** Check Username Availability Functionality */
	$('.url-replace').blur(function(){
		var inputtedText = $("input.url-replace").val();
		if (inputtedText != '') {
			$.ajax({
				url: '/php/checkUserName.php',
				data: 'userName=' + inputtedText,
				dataType: 'script',
				type: 'post',
				success: function (data) {
					if (res == 'false') {
						$(".tip-1").html('<span class="message-neg">Sorry, but that username is already taken.</span>');
						$(".tip-1").removeClass("none");
						$(".field-1").addClass("highlight-neg");
						$(".field-1").removeClass("highlight-neut");
					} else {
						$(".tip-1").html('<span class="message-pos">Congratulations! That username is available.</span>');
						$(".tip-1").removeClass("none");
						$(".field-1").addClass("highlight-pos");
						$(".field-1").removeClass("highlight-neg");
					}
				}
			});
		}
	});
	
	/** Custom Catgeory Functionality */
	$('.cat-select').change(function(event) {
		var cat = "Funny " + $('.cat-select :selected').attr('alt');
		if (cat == 'Funny undefined') {
			var cat = "What's So Funny?";
		}
		$(".cat-h3").html(cat);
	});
	
	/** Check Entry Availability and Profanity Functionality */
	$('.entry').blur(function(){
		var pgText = $("p.jokeTextCheck").html();
		pgText = pgText.toLowerCase();
		var entText = $("input.entry").val();
		entText = entText.toLowerCase();
		if (pgText != entText){
			$(".field-2").removeClass("highlight-neg");
			var inputtedText = $("input.entry").val();
			if (inputtedText != '') {
				$.ajax({
					url: '/php/checkEntry.php',
					data: 'entry=' + inputtedText,
					dataType: 'script',
					type: 'post',
					success: function (data) {
						if (res == 'false') {
							$(".tip-2").html(msg);
							$(".tip-2").removeClass("none");
							$(".field-2").addClass("highlight-neg");
							$(".field-2").removeClass("highlight-neut");
						} else {
							$(".tip-2").addClass("none");
							$(".tip-2").html('Be funny, but keep it clean.');
						}
					}
				});
			}
		} else {
			$(".field-2").removeClass("highlight-neg");
			$(".tip-2").html('Be funny, but keep it clean.');
						
		}
		if (inputtedText != '') {
			$.ajax({
				url: '/php/checkProfanity.php',
				data: 'entry=' + inputtedText,
				dataType: 'script',
				type: 'post',
				success: function (data) {
					if (res == 'false') {
						var profCookie = readCookie('profanityModal');
						if (profCookie != 1) {
							var modalLeft = ($(window).width() - $('#modal-prof').width()) /2 + 'px';
							var modalTop = ($(window).height() - $('#modal-prof').height()) /2 + 'px';
							$('#modal-prof').css("left", modalLeft);
							$('#modal-prof').css("top", modalTop);
							$('#modal-prof').fadeIn(333);
							$('.close').click(function(event){
								event.preventDefault();
								$('#modal-prof').fadeOut(333);
							});
							$('.close-permanently').click(function(event){
								event.preventDefault();
								$('#modal-prof').fadeOut(333);
								var date = new Date();
								date.setTime(date.getTime()+(365*24*60*60*1000));
								var expires = "; expires="+date.toGMTString();
								document.cookie = "profanityModal=1"+expires+"; path=/";
							});
						}
					}
				}
			});
		}
		
	});

	/** Modal Functionality */
	var contributeCookie = readCookie('contributeModal');
	if (contributeCookie != 1) {
		$(window).load(function(){
			var modalLeft = ($(window).width() - $('#modal').width()) /2 + 'px';
			var modalTop = ($(window).height() - $('#modal').height()) /2 + 'px';
			$('#modal').css("left", modalLeft);
			$('#modal').css("top", modalTop);
			$('#modal').fadeIn(666);
		});
		$('.close').click(function(event){
			event.preventDefault();
			$('#modal').fadeOut(333);
		});
		$('.close-permanently').click(function(event){
			event.preventDefault();
			$('#modal').fadeOut(333);
			var date = new Date();
			date.setTime(date.getTime()+(365*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
			document.cookie = "contributeModal=1"+expires+"; path=/";
		});
	}
	
	/** Type-Ahead Functionality */
	var timer;
	var actionCount = 0;
	$(".tag").keyup(function(event) {
		var tagName = $(this).attr('name');
		var tagID = "#"+tagName;
		if (event.keyCode < 32 || (event.keyCode >= 33 && event.keyCode <= 46) || (event.keyCode >= 112 && event.keyCode <= 123)) {
		} else {
			actionCount++;
			clearTimeout(timer);
			timer=setTimeout(function(){
				var entry = $(tagID).val(); 
				if (entry.length > 0) { 
					$.ajax({ 
						type: "POST", 
						url: "/php/checkTag.php", 
						data: "entry=" + entry + "&actionCount=" + actionCount,
						dataType: 'script',
						success: function(data) { 
							if ((check != "false") && (entryLength != totalLength) && (actionCount == replyCount)) { 
								replace = entry + suggest;
								$(tagID).val(replace);
								var oTextbox = document.getElementById(tagName);
								if (oTextbox.createTextRange) {
									var oRange = oTextbox.createTextRange();
									oRange.moveStart("character", entryLength);
									oRange.moveEnd("character", totalLength);
									oRange.select();
								} else if (oTextbox.setSelectionRange) {
									oTextbox.setSelectionRange(entryLength, totalLength);
								}
							};
						} 
					});
				}
			}, 50);
		}
	});
	
});