$(document).ready(function() {
	
	/*setInterval(function() {
		var randomC = "#"+Math.floor(Math.random()*16777215).toString(16);
		$("#logo").animate({"backgroundColor": randomC}, "slow");
	}, 1000);*/
	
	// Load More Posts
	$(".loadMorePosts").live("click", function() {
		var selectedRow = $(this).attr("id");
		$.post("./ajax/more-posts.php", {
			dateVar: selectedRow
		}, function(response) {
			postResults('post-list', escape(response));
		});
	});
	
	// Preset CSS
	$(".notification").css("z-index", "100");
	$("#header-container").css("z-index", "101");
	$(".notification").css("margin-top", "-65px");
	$(".dropdown ul").each(function() {
		$(this).css("opacity", 0.95);
	});
	
	// Update Now Playing
	setInterval(function() {
		$.get("./ajax/now-playing.php", {
		}, function(response) {
			setTimeout("returnNowPlaying('now-playing-box', '"+escape(response)+"')", 400);
		});
	}, 20000);
	
	// Update Latest Tweet
	setInterval(function() {
		$.get("./ajax/latest-tweet.php", {
		}, function(response) {
			setTimeout("returnLatestTweet('latest-tweet-box', '"+escape(response)+"')", 400);
		});
	}, 600000);
	
	// Smooth Back To Top
	$("#backToTop").click(function() {
		$("html, body").animate({scrollTop:0}, "fast");
	});
	
	// Active Navigation
	$("#mainNav a, #mainNav a:visited").each(function() {
		$(this).removeClass("active");
	});
	$("#mainNav li a, #mainNav li a:visited").each(function() {

      var currentURL = window.location.toString().split("/");
      var currentHref = $(this).attr("href");
      var single_path = currentURL[currentURL.length-1];
      var with_params = currentURL[currentURL.length-2];
      var parentLi = $(this).parent();

      if(currentHref == single_path && $(parentLi+":not").hasClass("dropdown")) {
    	  $(parentLi).addClass("active");
      } else if(currentHref.search(with_params) >= 0) {
    	  $(parentLi).addClass("active");
      } 
   });
	
	// Active Navigation - Code Repository
	$("#cat-header .code-header, #cat-header .code-header:visited").each(function() {
		$(this).removeClass("active");
	})
	$("#cat-header .code-header, #cat-header .code-header:visited").each(function() {

	  var currentURL = window.location.toString().split("/");
	  var currentHref = $(this).attr("href");
	  var single_path = "./code-repository/"+currentURL[currentURL.length-1];
	  var with_params = currentURL[currentURL.length-2];
	  var parentLi = $(this).parent();
	
	  if(currentHref == single_path) {
		  $(this).addClass("active");
	  } 
	  //$("#content-inside").html(single_path);
   });
	
	// Active Navigation - Code Tags
	$("#tag-page .code-tag, #tag-page .code-tag:visited").each(function() {
		$(this).removeClass("active");
	})
	$("#tag-page .code-tag, #tag-page .code-tag:visited").each(function() {
		
		var currentURL = window.location.toString().split("/");
		var currentHref = $(this).attr("href");
		var single_path = "./code-repository/tags/"+currentURL[currentURL.length-1];
		var with_params = currentURL[currentURL.length-2];
		var parentLi = $(this).parent();
		
		if(currentHref == single_path) {
			$(this).addClass("active");
		} 
		//$("#content-inside").html(single_path);
	});
	
	$("#blog-page .code-tag, #blog-page .code-tag:visited").each(function() {
		$(this).removeClass("active");
	})
	$(".code-tag").each(function() {
		
		var currentURL = window.location.toString().split("/");
		var currentHref = $(this).attr("href");
		var single_path = "./blog/tags/"+currentURL[currentURL.length-1];
		var with_params = currentURL[currentURL.length-2];
		var parentLi = $(this).parent();
		
		if(currentHref == single_path) {
			$(this).addClass("active");
		} 
		//$("#content-inside").html(single_path);
	});
	
	// Active Navigation - Portfolio Page
	$("#port-page .code-tag, #port-page .code-tag:visited").each(function() {
		$(this).removeClass("active");
	})
	$("#port-page .code-tag, #port-page .code-tag:visited").each(function() {
		
		var currentURL = window.location.toString().split("/");
		var currentHref = $(this).attr("href");
		var single_path = "./work/"+currentURL[currentURL.length-1];
		var with_params = currentURL[currentURL.length-2];
		var parentLi = $(this).parent();
		
		if(currentHref == single_path) {
			$(this).addClass("active");
		} 
		//$("#content-inside").html(single_path);
	});
	
	// Open Social Share Link In New Small Window
	$(".social-share, .social-share:visited").click(function(e) {
		var toShare = $(this).attr("rel");
		var itsVia = $(this).text();	
		var middleX = (window.screen.width-480)/2;
		var middleY = (window.screen.height-320)/2;
		window.open(toShare, "", "width=480, height=320, left="+middleX+", top="+middleY);
	});
	
	// Open External Links In New Window/Tab
	$('a').each(function() {
		if($(this).attr("href") != "" && $(this).attr("href") != "javascript:void(0);") {
			var a = new RegExp('/' + window.location.host + '/');
			if(!a.test(this.href)) {
				$(this).click(function(event) {
					event.preventDefault();
					event.stopPropagation();
					window.open(this.href, '_blank');
				});
			}
		}
	});
	
	// SlideFadeToggle
	jQuery.fn.slideFadeToggle = function(speed, easing, callback) {
	  return this.animate({opacity: 'toggle', height: 'toggle'}, speed, easing, callback);  
	};
	
	// Dropdown Menu
	$("#mainNav li").hover(function() {
		 $("ul", this).slideFadeToggle("fast");
		 $(this).css("background", "#434654");
	}, function() {
		 $("ul", this).css("display", "none");
		 $(this).css("background", "");
	});
	
	// Mouse Interactions - Hover/Click
	$("#logo").hover(function() {
		$(this).stop().animate({"opacity": ".5"}, "fast");
	},
	function() {
		$(this).stop().animate({"opacity": "1"}, "fast");
	});
	
	$(".code-table tr, .admin-code-table tr").hover(function() {
		oldColor = $(this).find("td").css("background-color");
		$(this).find("td").stop().animate({ backgroundColor : "#ffffff"}, "fast");
		$(this).find("td").css("color", "#c300cf");
	}, function() {
		$(this).find("td").stop().animate({ backgroundColor : "#ededed"}, "fast");
		$(this).find("td").css("color", "#444");
	});
	
	$(".social-list li a").hover(function() {
		$(this).parent("li").stop().animate({ backgroundColor : "#ffffff"}, "fast");
	}, function() {
		$(this).parent("li").stop().animate({ backgroundColor : "#ededed"}, "fast");
	});
	
	$(".admin_panel li a").hover(function() {
		$(this).parent("li").stop().animate({ backgroundColor : "#ffffff"}, "fast");
	}, function() {
		$(this).parent("li").stop().animate({ backgroundColor : "#ededed"}, "fast");
	});
	
	$("#twitterLink").hover(function() {
		$(".over").fadeIn("fast");
	}, function() {
		$(".over").fadeOut("fast");
	});
	
	// Preset Input Attributes
	$('input:text, textarea').each(function() {
		var default_value = $(this).attr("title");
		
		if(default_value) {	       
		       
			$(this).val(default_value);
		    $(this).css("color", "#cccccc");
		       
			$(this).focus(function() {
				if(this.value == default_value) {
					this.value = '';
					$(this).css("color", "#666666");
				}
			});
		       
			$(this).blur(function() {
				if(this.value == '') {
					this.value = default_value;
					$(this).css("color", "#cccccc");
				}
			});
		}	
	});
	
	// Assign Enter Key when Search field focused
	$("#qSearch").focus(function() {
		$("#qSearch").live("keyup", function(e) {
			if(e.keyCode == 13) {
				var term = $("#qSearch").val();
			    if(term != "" && term != "Enter keyword(s) here...") {
			    	window.location = "./code-repository/search/" + escape(term);
			    }
			}
		});	
	});
	
	// Assign Enter Key when Search field focused
	$("#qBSearch").focus(function() {
		$("#qBSearch").live("keyup", function(e) {
			if(e.keyCode == 13) {
				var term = $("#qBSearch").val();
				if(term != "" && term != "Enter keyword(s) here...") {
					window.location = "./blog/search/" + escape(term);
				}
			}
		});	
	});
	
	// Fade In Notification Bar
	if($(".notification-inside p").html() != "") {
		$(".notification").delay(100).animate({"margin-top" : "0px"}, 500);
	}
	
	// Close Notification
	$("#closeNotification").click(function() {
		$(".notification").delay(100).animate({"margin-top" : "-65px"}, 500);
	});
	
	
	 
});

function postResults(id, response) {
	$(".loadMorePosts").remove();
	$('#'+id).append(unescape(response));
}

function goSearch() {
    var term = $("#qSearch").val();
    if(term != "" && term != "Enter keyword(s) here...") {
    	window.location.href = "./code-repository/search/" + escape(term);
    }
}

function goBlogSearch() {
	var term = $("#qBSearch").val();
	if(term != "" && term != "Enter keyword(s) here...") {
		window.location.href = "./blog/search/" + escape(term);
	}
}

function returnNowPlaying(id, response) {
	$('#'+id).html(unescape(response));
	$('#'+id).fadeIn("fast");
}

function returnLatestTweet(id, response) {
	$('#'+id).html(unescape(response));
	$('#'+id).fadeIn("fast");
}

function toggleExpand(list) {
	
	/* jQuery Smooth Expansion */
	var element = "div#"+list;
	
	jQuery.fn.slideFadeToggle = function(speed, easing, callback) {
	  return this.animate({opacity: 'toggle', height: 'toggle'}, speed, easing, callback);  
	};
	
	jQuery(element).slideFadeToggle('fast');
}

function charsRemaining() {
	
	var currentLength = $("#inputMsg").val().length;
	var remaining = 500-currentLength;
	$('#remainingChars').html(remaining);
	if(remaining >= 0) {
		$('#remainingChars').css("color", "#444");
		$("#inputBtn").removeAttr("disabled");
		$("#inputBtn").removeClass("disabled-input-btn");
	} else {
		$('#remainingChars').css("color", "#c300cf");
		$("#inputBtn").attr("disabled", "disabled");
		$("#inputBtn").addClass("disabled-input-btn");
	}
	
}

function validateRequired(field) {
	with(field) {
		if(value == null || value == "Your Name"  || value == "Your Name"  || value == "your@email.com"  
			|| value == "Subject of your enquiry"  || value == "Type your message here" || value == 0
			|| value == "Enter keyword(s) here...") {
			return false;
		} else {
			return true;
		}
	}
}

function validateEnquiryForm(form) {
	with(form) {
		if(validateRequired(cName) == false) {
			cName.focus();
			$(cName).css("background", "url(./imgs/error.gif) no-repeat #ffe5f5");
			$(cName).css("background-position", "380px 12px");
			return false;
		} else if(validateRequired(cEmail) == false) {
			cEmail.focus();
			$(cEmail).css("background", "url(./imgs/error.gif) no-repeat #ffe5f5");
			$(cEmail).css("background-position", "380px 12px");
			return false;
		} else if(validateRequired(cSubject) == false) {
			cSubject.focus();
			$(cSubject).css("background", "url(./imgs/error.gif) no-repeat #ffe5f5");
			$(cSubject).css("background-position", "380px 12px");
			return false;
		} else if(validateRequired(cMessage) == false) {
			cMessage.focus();
			$(cMessage).css("background", "url(./imgs/error.gif) no-repeat #ffe5f5");
			$(cMessage).css("background-position", "380px 12px");
			return false;
		} 
	}
}

function validateSearch(form) {
	with(form) {
		if(validateRequired(q) == false) {
			return false;
		}
	}
}

function addAnotherUpload() {
	var holder = document.getElementById('inputs');
	var file = document.createElement('input');
	file.setAttribute('type', 'file');
	file.setAttribute('name', 'adminfile[]');
	file.setAttribute('style', 'font-family:verdana;font-size:11px;');
	holder.appendChild(file);
}

function numbersonly(myfield, e, dec) {
	var key;
	var keychar;
	
	if (window.event) {
	   key = window.event.keyCode;
	} else if (e) {
	   key = e.which;
	} else {
	   return true;
	}
	keychar = String.fromCharCode(key);
	
	if ((key == null) || (key == 0) || (key == 8) || (key == 9) || (key == 13) || (key == 27)) {
		// control keys
	   return true;
	
	} else if ((("0123456789.").indexOf(keychar) > -1)) {
		// numbers
	   return true;
	
	} else if (dec && (keychar == ".")) {
		// decimal point jump
	   myfield.form.elements[dec].focus();
	   return false;
	   
	} else {
	   return false;
	}
}

