// JavaScript Document
// Custom vars
var comment_opened = slided = false;
var isValid = nameValid = emailValid = true;
var edit_current_id;
var name;
var email;
var comment;
var write_comment_html = ''+
'<table width="460" border="0" cellspacing="0" cellpadding="5" id="comment_table">'+
'	<tr>'+
'		<td align="right" valign="top">Name</td>'+
'		<td>'+
'			<input type="text" name="comment_name" id="comment_name" />'+
'		</td>'+
'	</tr>'+
'	<tr>'+
'		<td align="right" valign="top">E-mail:</td>'+
'		<td>'+
'			<input name="comment_email" type="text" id="comment_email" size="30" />'+
'		</td>'+
'	</tr>'+
'	<tr>'+
'		<td align="right" valign="top">Comment:</td>'+
'		<td>'+
'			<textarea name="comment_text" id="comment_text" cols="45" rows="5" ></textarea>'+
'		</td>'+
'	</tr>'+
'	<tr>'+
'		<td align="right">&nbsp;</td>'+
'		<td>'+
'			<input type="button" name="submitcomment" id="submitcomment" value="Submit comment" />&nbsp;'+
'			<input type="button" name="cancelcomment" id="cancelcomment" value="Cancel" />'+
'		</td>'+
'	</tr>'+
'</table>';
$(document).ready(function(){
	$("#search_error").hide();
	$("#contacterror").hide();
	if ($(".commentspace").html() == '') {
		$(".commentspace").hide();
	}
	$(".writecomment").click(function(event){
		event.preventDefault();
		if (!comment_opened) {
			comment_opened = true;
			var current_id = $(this).attr("id").substr(1);
			edit_current_id = current_id;
			$("#commentspace"+current_id).append(write_comment_html).slideDown('fast');
			$("#cancelcomment").click(function(){
				$("#commentspace"+edit_current_id).slideUp('fast',function() {
					$("#commentspace"+edit_current_id).html('');
					comment_opened = false;
				});
			});
			$("#submitcomment").click(function(){
				name = $("#comment_name").val();
				email = $("#comment_email").val();
				comment = $("#comment_text").val();
				// Name validation
				var Regex = /^[a-zA-Z\ ']*$/;
				if(!Regex.test(name) || name.length === 0 || name==' ' || name=='  '){
					$("#comment_name").addClass('invalid'); isValid = false;
					//$("#comment_name").qtip({ content: 'Your name is invalid.', position: { corner: { target: 'topRight', tooltip: 'bottomLeft' } } });
					return false;
				} else {
					$("#comment_name").removeClass('invalid');
					//$("#comment_name").qtip("destroy");
				}
				//alert("name "+isValid);
				// Email validation
				var Regex = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
				if(!Regex.test(email) || email.length === 0 || email==' ' || email=='  '){
					$("#comment_email").addClass('invalid'); isValid = false;
					//$("#comment_email").qtip({ content: 'E-mail address is invalid.', position: { corner: { target: 'topRight', tooltip: 'bottomLeft' } } });
					return false;
				} else {
					$("#comment_email").removeClass('invalid');
					//$("#comment_email").qtip("destroy");
				}
				//alert("email "+isValid);
				// Comment validation
				if(comment.length === 0 || comment==' ' || comment=='  '){
					$("#comment_comment").addClass('invalid'); isValid = false;
					//$("#comment_comment").qtip({ content: 'Comment is invalid.', position: { corner: { target: 'topRight', tooltip: 'bottomLeft' } } });
					return false;
				} else {
					$("#comment_comment").removeClass('invalid');
					//$("#comment_comment").qtip("destroy");
				}
				//alert("comment "+isValid);
				// Submit data
				if (isValid) {
					$.post('js/post_comment.php',{ cname:name, cemail:email, ccomment:comment, parent:edit_current_id });
					$("#commentspace"+edit_current_id).slideUp('fast',function() {
						$("#commentspace"+edit_current_id).html('<span class="late">Your comment is waiting moderation.</span>');
						$("#commentspace"+edit_current_id).slideDown('slow');
					});
					comment_opened = false;
				}
			});
		}
	});
	
	/* Book search */
	$(".ftl_jump").change(function() {
		if ($(this).val() !='') {
			parent.location='/'+$(this).val();
		}
	});
	
	/* Newsletter */
	$('#newsletter_name').focus(function() {
		var current = $(this);
		if (current.val()=='Your name') { current.val('').css({ color: '#000' }); }
		nameValid = true;
	});
	$("#newsletter_name").blur(function() {
		var current = $(this);
		if (current.val()=='') { current.val('Your name').css({ color: '#ccc' }); }
		if (current.val()=='' || current.val()=='Your name') {
			nameValid = false;
		}
	});
	$('#newsletter_email').focus(function() {
		var current = $(this);
		if (current.val()=='E-mail address') { current.val('').css({ color: '#000' }); }
		emailValid = true;
	});
	$("#newsletter_email").blur(function() {
		var current = $(this);
		if (current.val()=='') { current.val('E-mail address').css({ color: '#ccc' }); }
		var Regex = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		if (!Regex.test(current.val()) || current.val()=='' || current.val()=='E-mail address') {
			emailValid = false;
		}
	});
	$("#newsletter_button").click(function() {
		if (nameValid && emailValid) {
			$.post('js/post_newsletter.php', { name: $('#newsletter_name').val(), email: $("#newsletter_email").val(), language: $("#language").val() }, function(data) {
				if (data) {
					var d = data.split('|');
					if (d[0]=='ok') {
						$("#newsletter_data").slideUp('slow',function() { 
							$("#newsletter_data").html(d[1]).slideDown('slow'); 
						});
					}
				}
			});
		}
	});
	
	/* Search */
	$("#kw").focus(function() { if (slided) { $("#search_error").slideUp('slow'); } });
	
	$("#searchform").submit(function() {
		var current = $('#kw').val();
		if (current=='' || current==' ' || current=='  ' || current.length <= 3) {
			$("#search_error").slideDown('slow');
			slided = true;
			return false;
		}
		return true;
	});
	
	/* Contact form */
	
	$("#contactform input").focus(function() { $("#contacterror").slideUp('slow'); });
	$("#contactform textarea").focus(function() { $("#contacterror").slideUp('slow'); });
	$("#contactform").submit(function() {
		isValid = true;
		var cName = $("#name").val();
		var cEmail = $("#email").val();
		var rEmail = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		var cPhone = $("#phone").val();
		var cSubject = $("#subject").val();
		var cMessage = $("#message").val();
		if (cName.length===0 || cName==' ' || cName=='  ') {
			isValid = false;
			$("#errmsg").html('The name field cannot be empty.'); $("#contacterror").slideDown('slow');
			return false;
		} else if (!rEmail.test(cEmail) || cEmail.length===0) {
			isValid = false;
			$("#errmsg").html('The email is invalid.'); $("#contacterror").slideDown('slow');
			return false;
		} else if (cPhone.length===0 || cPhone==' ' || cPhone=='  ') {
			isValid = false;
			$("#errmsg").html('The phone number is invalid.'); $("#contacterror").slideDown('slow');
			return false;
		} else if (cSubject.length===0 || cSubject==' ' || cSubject=='  ') {
			isValid = false;
			$("#errmsg").html('The subject cannot be empty.'); $("#contacterror").slideDown('slow');
			return false;
		} else if (cMessage.length===0 || cMessage==' ' || cMessage=='  ') {
			isValid = false;
			$("#errmsg").html('The message field cannot be empty.'); $("#contacterror").slideDown('slow');
			return false;
		}
		if (isValid) {
			return true;
		}
		return false;
	});
	
	
	/* Images */
	$("#imagegal, a.iframe").fancybox({ 'zoomSpeedIn': 300, 'zoomSpeedOut': 300, 'overlayShow': true });
	
});
