// On Error
function showError(responseText, statusText){
	alert("Something broke in the CFM! " + responseText + statusText); //something broke in the CFM
}

function fCheckAvailable(vString){
		$.ajax({
		  type: 	"post",
		  url: 		"/blogs/fncCheckAvailable.cfm",
		  data: 	{fldBlogname: vString},
		  success: 	showResult,
		  error: 	showError
		});	
}

function showResult(vResult){
	if (vResult == 0){
		var vOut = "<img src='/images/icons/tick.png' alt='Available' title='Available' align='absmiddle' hspace='4'><span style='color:##7CB135;'>Available!</span>";
		$("#btnSubmit").removeAttr("disabled");
	}else{
		var vOut = "<img src='/images/icons/cross.png' alt='Not Available' title='Not Available' align='absmiddle' hspace='4'><span style='color:##b30000;'>Not Available!</span>";
		$("#fldBlogname").focus().select();
		$("#btnSubmit").attr("disabled", "true");
	}	
	$("#jqResult").html(vOut);
}

function fDeleteBlog(id){
	fConfirm("Deleting this Blog and all Entries. Are you sure?",function(){
		location.href='/blogs/fncDeleteBlog.cfm?blogid=' + id;																		 
	});
}

// Color picker defaults
        $.colorInput.defaults.cells = 16;
        $.colorInput.defaults.cellSize = 10;
        $.colorInput.defaults.hueWidth = 22;
        $.colorInput.defaults.hideInput = false;
        $.colorInput.defaults.hoverSelect = false;
        $.colorInput.defaults.cancelOnClick = [];
        $.colorInput.defaults.acceptCancelButtons = true;
        $.colorInput.defaults.showLeft = false;

/* DOCUMENT READY */
$(document).ready(function() { 
						   
	// set up the color input
	var $input = $(".colorInput").colorInput({
		change: function() {
			$("#Status").text("changed ".concat(this.value));
		},
		colorSelected: function(e) {
			$("#Status").text("selected ".concat(this.value));
		}
	});
						   
	
	// Validate 
	$("#formBlog").validate({
		rules: {
			fldBlogname: "required",
			fldTitle: "required",
			fldDescription: "required",
			fldKeywords: "required",
			fldAgree: "required"
		}							   
	});
	
	// Init jq Table Sorter
	if ($("#jqTable").length > 0){
		$("#jqTable").tablesorter(); 
	}
   	$('#jqTable table tr').hover(
        function() {$(this).addClass('highlight');},
        function() {$(this).removeClass('highlight');
    }); 	
	
	// only accept alpha characters!
	$("#fldBlogname").alpha();

	//Bind on change, check blogname availability
	$("#fldBlogname").change(function(){
		fCheckAvailable(this.value);
	});
	
	//Bind key up, put in blogname
	$("#fldBlogname").keyup(function(){
		$("#jqBlogAddress").html(this.value);
	});
	
	//Bind jqdeleteblog
	$(".jqDeleteBlog").click(function(){
		fDeleteBlog(this.rel);									  
	});
	
	// Bind click to jqTerms, to display Terms of Use Agreement
	$("a.jqAgreeTerms").click(function(oEvent){
		$.blockUI({ message: $('.jqTerms') }); 
		$("#jqOKTerms").click(function() { 
			// remove the block message and check the Agreement checkbox
			$.unblockUI();
			$("#fldAgree").attr('checked', true);
			return false;
		});
		$("#jqNoTerms").click(function() { 
			$.unblockUI();
			$("#fldAgree").attr('checked', false);
			return false; 						
		});		
		return false;
	});
});