// Mute JS

// Init function - should only be fired by index page

// Jquery INIT function

// Need to apply this only to the index page
$(document).ready(function(){
	// 
	// xmas class
	$("#comments textarea").focus(function(){
		if($(this).val() == "") {
			$(this).addClass("xmas");
		}
	})
	$("#comments textarea").blur(function(){
		if($(this).val() == "") {
			$(this).removeClass("xmas");
		}
	})
	//
	// change form action
	if($("body").attr("id") == "index"){
		realAction();
	}
	// archive form
	$("#archiveForm").submit(function(event){
		event.preventDefault();
		window.location = "archive.php?lamonth="+$("#selectMonth").val()+"&layear="+$("#selectYear").val();
	});
	// email obfuscation
	emailName = "mute";
	emailDomain = "rigent";
	emailExt = "com";
	$("span.emailLink").html("<a href=\"mailto:"+emailName+"@"+emailDomain+"."+emailExt+"\" title =\"Send an email\">"+emailName+"@"+emailDomain+"."+emailExt+"</a>");
	// 
	// calendar (grab current post dates first)
	//
	if($("#entryform #date").length > 0) {
		dateStr = $("#entryform #date").val();
		dateStrr = dateStr.split("-");
		curDate = dateStrr[0] + "," + dateStrr[1] + "," + dateStrr[2];
	} else {
		curDate = "";
	}
	$.ajax({
		type: "POST",
		url: "getpostdates.php",
		data: "y=2009&m=07",
		success: function(dataBack){
			postDays = eval(dataBack),
			$("#datepicker")
				.datepicker({
					inline: true,
					changeMonth: true,
					changeYear: true,
					dateFormat: "yy-mm-dd", 
					defaultDate: new Date(curDate),
					onChangeMonthYear: function(year, month) { 
						$.ajax({
						   type: "POST",
						   url: "getpostdates.php",
						   data: "y="+year+"&m="+month,
						   success: function(dataBack){
							 postDays = eval(dataBack);
							 $("#datepicker").datepicker("refresh"); 
						   }
						});
					}, 
					beforeShowDay: function(thedate) {
						var theday = thedate.getDate();
						if( $.inArray(theday,postDays) == -1 ){
							return [true,""];
						} else {
							return [true, "ui-postOnDay"];
						}
					},
					onSelect: function(dateText, inst) {
						window.location = "muteme.php?ladat="+dateText;
					}
			});
		}
	});
}); 

//grabNewPostDates: function(year, month) { 
//	$.ajax({
//	   type: "POST",
//	   url: "getpostdates.php",
//	   data: "y="+year+"&m="+month,
//	   success: function(dataBack){
//		 postDays = eval(dataBack);
//		 $("#datepicker").datepicker("refresh"); 
//	   }
//	});
//}

// form validation

var clearFlag = false

//

function validateForm(){
	if(clearFlag) {
		return false;
		}
	if (document.getElementById("comment").value == ""){
		document.getElementById("comment").value = "Anonymous comments are fine but you have to type something!"
		document.getElementById("comment").style.backgroundColor = "#ffffcc"
		clearFlag = true
		return false;
		}
		else {
		return true;
		}
}

//
function clickNclear(){
	if(clearFlag) {
		document.getElementById("comment").value = ""
		document.getElementById("comment").style.backgroundColor = "#fff"
		clearFlag = false
		}
}

// change form action
function realAction(){
   trueAction = "http://mute.rigent.com/now!add!comment.php";
   trueAction = trueAction.replace(/!/g, "_");
   document.forms.garblegook.action = trueAction;
}