$(document).ready(function() {
	$(function(){
		$('#datepicker').datepicker({
			createButton:false,
			displayClose:true,
			closeOnSelect:false,
			selectMultiple:true,
			onSelect:function(dateText, inst) 
			{
				var d = dateText.split("/");
				var d = d[0] + ',' + d[1] + "," + d[2];
				location.href = "?d=" + d;
			},
			onChangeMonthYear:function(year, month, inst) 
			{
		
			}
		});
	});
	
	textfieldEffect = function(elementID, from, to) {
		$(elementID).fade({ duration: 0.5, from: from, to: to});
	}
	
	/*
	//@group Submit Comments
	*/
	submitComment = function() {
		var URL = "Comments.php";
		var ID = $("#comment_id").val();
		var FullName = $("#comment_name").val();
		var Email = $("#comment_email").val();
		var Content = $("#comment_content").val();
		var RefferURL = $("#comment_url").val();
		var reg = new RegExp(/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/);
		if (FullName == '') {
			alert('Please enter your name.');
			$("#comment_name").select();
			return false;
		}else if(Email == '') {
			alert('Please enter your email address.');
			$("#comment_email").select();
			return false;
		}
		else if( !(reg.test(Email)) ) {
			alert('Invalid email address.');
			$("#comment_email").select();
			return false;
		}else if(Content == '') {
			alert('Please enter comment.');
			$("#comment_content").select();
			return false;
		}
		
		$.ajax(
			{
				type:"POST",
				url:URL,
				data:"do=submitComment&ID="+ID+"&RefferURL="+RefferURL+"&FullName="+ FullName + "&Email="+ Email +"&Content="+Content,
				beforeSend:function() {
					$("#ajaxSpinner").fadeOut('fast');
					$("#ajaxSpinner").fadeIn('slow');
					$("#ajaxSpinner").show();
				},
				complete:function() {
					$("#ajaxSpinner").fadeOut('fast');
				},
				success:function(responseString) {
					$("#comment_name").val('');
					$("#comment_email").val('');
					$("#comment_content").val('');
					displayComments(ID, 'comments');
				},
				error:function() {
					$("#ajaxSpinner").fadeOut('fast');
					$("#ajaxSpinner").fadeIn('normal');
				}
			}
		);
	}
	/*
	//@end
	*/
	/*
	//@group Display Comments
	*/
	displayComments = function(intID, stringElementID) {
		var URL = "http://www.vivoviajando.com.br/Blog/Comments.php";
		$.ajax(
			{
				dataType:"json",
				type:"GET",
				url:URL,
				data:"CommentID="+intID,
				beforeSend:function() {
					$("#ajaxProgress").fadeOut('fast');
					$("#ajaxProgress").fadeIn('slow');
					$("#ajaxProgress").show();
				},
				success:function(json) {
					$("#"+stringElementID).html('');
					if (json == 0) {
							$("#"+stringElementID).html('<li class="empty"></li>');
					}else{
						for(i in json) {
							$("#"+stringElementID).append('<li><p><b>'+json[i]['Name']+'</b> <span style="color:#999999">'+json[i]['Date']+'</span></p><p>'+json[i]['Content']+'</p></li>');
						}
					}
				},
				complete:function() {
					$("#ajaxProgress").fadeOut('fast');
				},
				error:function() {
					$("#ajaxProgress").fadeOut('fast');
					$("#ajaxProgress").fadeIn('normal');
				}
			}
		);
	}
	/*
	//@end
	*/
	/*
	//@group Display Dig
	*/
	displayDig = function(stringDigAction, intID) {
		var URL = "Comments.php";
		$.ajax(
			{
				type:"POST",
				url:URL,
				data:"do=displayDig&dig="+stringDigAction+"&id="+intID,
				beforeSend:function() {
				},
				complete:function() {
				},
				success:function(value) {
					$("#"+stringDigAction).html(value);
				},
				error:function() {
				}
			}
		);
	}
	/*
	//@end
	*/
	/*
	//@group Submit Dig
	*/
	submitDig = function(stringDigAction, intID) {
		var URL = "Comments.php";
		$.ajax(
			{
				type:"POST",
				url:URL,
				data:"do=submitDig"+stringDigAction+"&id="+intID,
				beforeSend:function() {
					$("#dig"+stringDigAction).hide();
					$("#dig"+stringDigAction+"Progress").show();
				},
				complete:function() {
					displayDig('dig'+stringDigAction,intID);
				},
				success:function(json) {
					$("#dig"+stringDigAction+"Progress").hide();
					$("#dig"+stringDigAction).show();
				},
				error:function() {
				}
			}
		);
	}
	/*
	//@end
	*/
});