var current_overlay = '';
var hidden_field = '';

$(document).ready(function(){

	$(".button_hide").click(function (event) {
	
		$(this).parent().parent().next().toggle('normal');
		$(this).hide();
		$(this).siblings().show();
	});

	$(".button_show").click(function (event) {
	
		$(this).parent().parent().next().toggle('normal');
		$(this).hide();
		$(this).siblings().show();
	});

	
	/* This is basic - uses default settings */

	$(".popup_overlay").click(function(event) {

		// we'll set the proper height here
		current_overlay = '#' + $(this).attr('data-target');
		$('#main_overlay').height($(document).height() + 'px');
		$(current_overlay).css('margin-top', 25 + $(window).scrollTop() + 'px');
		$(current_overlay).show();
		$("#main_overlay").show();
		
		if ($(this).attr('data-field')) {
			var field_name = $(this).attr('data-field');
			var field_value = $(this).attr('data-field-value');
			$(current_overlay + ' form').append('<input id="'+ field_name + '" type="hidden" name="' + field_name + '" value="' + field_value + '" />');
			hidden_field = $(this).attr('data-field');
		}
	});

	$(".person_overlay").click(function(event) {

		// we'll set the proper height here
		current_overlay = '#' + $(this).attr('data-target');
		$(current_overlay).css('top: ' + $(this).css('top') + 'px');
		$(current_overlay).css('left:' + $(this).css('left') + 'px');
		$(current_overlay).show();
	});
	
	$(".person_overlay_link").click(function(event) {
		
		current_overlay = '.' + $(this).attr('data-target');
		$(current_overlay).hide();
		$(this).siblings(current_overlay).css('top: ' + $(this).css('top') + 'px');
		$(this).siblings(current_overlay).css('left:' + $(this).css('left') + 'px');
		$(this).siblings(current_overlay).show();
	});
	
	$(".btn_cancel").click(function(event) {

		close_overlay();		
	});

	$(".person_overlay_close").click(function(event) {

		$(this).parent().hide();
	});
	
	$(window).scroll(function(event) {
		
		if (current_overlay != '' && current_overlay.search('author') == -1 && current_overlay.search('illustrator') == -1)  {
			//$(current_overlay).hide();
			$(current_overlay).css('margin-top', 25 + $(window).scrollTop() + 'px');
			//$(current_overlay).show();
		}
	});

	
});

function close_overlay() {

	$(current_overlay).hide();
	$('#main_overlay').hide();
	current_overlay = '';
	$('#' . hidden_field).remove();
	clear_form_elements(current_overlay + ' form');
}

function do_login() {
	
	$.post('/ajax/login', $('#signup_form form').serialize(), function(data, textStatus) {
		if (data.code == 0) {
			
			alert(data.msg);
		}
		else {
			clear_form_elements('#main_overlay form');
			window.location.reload();
		}
	}, 'json');
}

function do_register() {

	$.post('/ajax/register', $('#register_form form').serialize(), function(data, textStatus) {
		if (data.code == 0) {
			
			alert(data.msg);
		}
		else {
			alert('Thank you for joining Tiger Tales Books.  Kindly login with your email address and password.');
			clear_form_elements('#main_overlay form');
		}
	}, 'json');
}

function do_forgot() {

	$.post('/ajax/forgot', $('#signup_form form.forgot').serialize(), function(data, textStatus) {
		if (data.code == 0) {
			
			alert(data.msg);
		}
		else {
			alert('Your password has been e-mailed to the address specified.');
			clear_form_elements('#main_overlay form');
		}
	}, 'json');
}

function do_comment() {
	
	$.post('/ajax/comment', $('#comment_form form').serialize(), function(data, textStatus) {

		if (data.code == 0) {
			
			alert(data.msg);
		}
		else {
			alert("Thank you for your comments, but this is a moderated site, and we will evaluate your post shortly.");
			close_overlay();
		}
	}, 'json');
	
}

function do_review() {
	
	$.post('/ajax/review', $('#review_form form').serialize(), function(data, textStatus) {

		if (data.code == 0) {
			
			alert(data.msg);
		}
		else {
			alert("Thank you for your review, but this is a moderated site, and we will evaluate your post shortly.");
			close_overlay();
		}
	}, 'json');
	
}

function do_email() {
	
	$.post('/ajax/email', $('#email_form form').serialize(), function(data, textStatus) {

		if (data.code == 0) {
			
			alert(data.msg);
		}
		else {
			alert("Your email has been sent, thanks for sharing our site with your friends.");
			close_overlay();
		}
	}, 'json');
	
}


function clear_form_elements(ele) {

    $(ele).find(':input').each(function() {
        switch(this.type) {
            case 'password':
            case 'select-multiple':
            case 'select-one':
            case 'text':
            case 'textarea':
                $(this).val('');
                break;
            case 'checkbox':
            case 'radio':
                this.checked = false;
        }
    });

}

function fancy() {

	$("#book_gallery a").attr('rel', 'gallery').fancybox();	
	$('#book_gallery a:first-child').trigger('click');
}
