/**
 * AJAX Javascript
 *
 * Page to initiate all AJAX functions
 *
 * @author gUncle Development [http://www.guncle.com]
 * @version 1/14/2010
 */


/**
 * Sets contact as viewed
 * @param id = id of contact
 */
function contactViewed(id) {
	$.ajax({
		type: "POST",
		url: "/ajax.php",
		data: ({action: "contactViewed",id: id}),
		cache: false,
		dataType: "text",
		success: function(response){
			$("#viewed_"+response).fadeOut("slow");
		}
	});
}


/**
 * Removes comment from blog
 * @param id = id of comment to remove
 */
function deleteComment(id) {
	if( confirm('Are you sure you want to delete this comment?') ) {
		$.ajax({
			type: "POST",
			url: "/ajax.php",
			data: ({action: "deleteComment",id: id}),
			cache: false,
			dataType: "text",
			success: function(response){
				$("#blog_"+response).fadeOut("slow");
			}
		});
	}
}


/**
 * Removes contact
 * @param id = id of contact to remove
 */
function deleteContact(id) {
	if( confirm('Are you sure you want to delete this contact?') ) {
		$.ajax({
			type: "POST",
			url: "/ajax.php",
			data: ({action: "deleteContact",id: id}),
			cache: false,
			dataType: "text",
			success: function(response){
				$("#contact_"+response).fadeOut("slow");
			}
		});
	}
}


/**
 * Removes message
 * @param id = id of message to remove
 */
function deleteMessage(id) {
	if( confirm('Are you sure you want to delete this message?') ) {
		$.ajax({
			type: "POST",
			url: "/ajax.php",
			data: ({action: "deleteMessage",id: id}),
			cache: false,
			dataType: "text",
			success: function(response){
				if( response > 0 ) {
					$("#mb_"+response).fadeOut("slow");
				}
			}
		});
	}
}


/**
 * Edits message board post
 * @param id = id of post to edit
 */
function editMessage(id) {
	$.ajax({
		type: "POST",
		url: "/ajax.php",
		data: ({
			action: "editMessage",
			id: id
		}),
		cache: false,
		dataType: "text",
		success: function(response){
			if( response ) {
				var res = splitResponse(response);
				$("#mb_id")[0].value = res['id'];
				$("#mb_name")[0].value = res['name'];
				$("#mb_message")[0].value = res['message'];
				$('input:radio[name=icon]').filter('[value='+res['icon']+']').attr('checked', true);
				//$("#mb_button")[0].value = 'Edit';
				$("#commentbox").slideToggle("slow");
			}
		}
	});
}


/**
 * Removes item row
 * @param type = type of item to remove
 * @param id = id of item to remove
 */
function itemRemove(type,id) {
	if( confirm('Are you sure you want to delete this item?') ) {
		$.ajax({
			type: "POST",
			url: "/ajax.php",
			data: ({action: "itemRemove",type: type,id: id}),
			cache: false,
			dataType: "text",
			success: function(response){
				var res = splitResponse(response);
				$("#"+res['type']+"_"+res['id']).fadeOut("slow");
			}
		});
	}
}


/**
 * Processes blog comment post
 */
function processComment() {
	$.ajax({
		type: "POST",
		url: "/ajax.php",
		data: ({
			action: "processComment",
			name: $("input#comment_name")[0].value,
			message: $("textarea#comment_message")[0].value,
			blog_id: $("input#blog_id")[0].value
		}),
		cache: false,
		dataType: "text",
		success: function(response){
			$("#comments").fadeOut("slow",function(){
				$("#comments").html(response);
			});
			$("#comments").fadeIn("slow",function(){
				$("form[name=comment]")[0].reset();
				$("#commentbox").slideToggle("normal");
			});
		}
	});
}


/**
 * Processes message board post
 */
function processMessage() {
	$.ajax({
		type: "POST",
		url: "/ajax.php",
		data: ({
			action: "processMessage",
			id: $("input#mb_id")[0].value,
			name: $("input#mb_name")[0].value,
			message: $("textarea#mb_message")[0].value,
			icon: $("input[name=icon]:checked").val(),
			mbdate: $("input#mbdate")[0].value
		}),
		cache: false,
		dataType: "text",
		success: function(response){
			$("#messages").fadeOut("slow",function(){
				$("#messages").html(response);
			});
			$("#messages").fadeIn("slow",function(){
				$("form[name=mb]")[0].reset();
				$("#commentbox").slideToggle("normal");
				document.getElementById('commentbox').reset();
			});
		}
	});
}


/**
 * Selects gallery folder
 */
function selectGallery() {
		$.ajax({
		type: "POST",
		url: "/ajax.php",
		data: ({
			action: "selectGallery",
			gallery: $("select#gallery")[0].value
		}),
		cache: false,
		dataType: "text",
		success: function(response){
			var res = splitResponse(response);
			$("#name")[0].value = res['name'];
			$("#date")[0].value = res['date'];
			$("#thumbnails").html(res['thumbnails']);
		}
	});
}


/**
 * Selects year to search folders for gallery
 */
function selectGalleryYear() {
		$.ajax({
		type: "POST",
		url: "/ajax.php",
		data: ({
			action: "selectGalleryYear",
			year: $("select#year")[0].value
		}),
		cache: false,
		dataType: "text",
		success: function(response){
			var res = splitResponse(response);
			$("#galleryDropdown").html(res['content']);
		}
	});
}


/**
 * Splits response from ajax processing page
 * @param response = echoed text to place into array
 */
function splitResponse(response) {
	if( response ) {
		var responseArray = response.split('|~');
		var newArray = new Array();
		for( var i=0; i<responseArray.length; i++ ) {
			tempArray = responseArray[i].split(':~');
			newArray[tempArray[0]] = tempArray[1];
		}
		return newArray;
	}
	else {
		return false;
	}
}

