
jQuery(function($) {

/*  -----------------------------------------------------------------------
	Google Analytics - New Version ---------------------------------------- */
	var pageTracker = _gat._getTracker("UA-796315-12");
	pageTracker._initData();
	pageTracker._trackPageview();

/*  ---------------------------------------------------
	Comment form handler ------------------------------ */
	
	$('li.share').show();

	$('a.external').attr('target', '_blank');

/*  ---------------------------------------------------
	Contact form handler ------------------------------ */
	$('#contact a, #thanks a, a.b_email').click(function () {
		$('#contact, #thanks').hide();
		$('#frm_contact').show();
		$('#col3').get(0).scrollIntoView(false);
		$('#contact-name').focus();
		return false;
	});

	$('#frm_contact, #frm_contact2').submit(function (event) {
		$self = $(this);
													  
		// Reset the validation
		$('.error', this).removeClass('error');
		$('label span.msg', this).remove();
		var error = 0;
		
		// Field validation
		$(".v-mandatory", this).each(function() {
			if ( !$(this).val() ) { error++; flagError(this, 'is mandatory'); }
		});
		$(".v-email", this).each(function() {
			if ( !validateEmail($(this).val()) ) { error++; flagError(this, 'is invalid'); }
		});
		$("textarea.v-maxlenght", this).each(function () {
			if ( !validateLenght($(this).val(), 500) ) { error++; flagError(this, ''); }
		});
		
		// Sending request
		if (error) {
			return false;
		} else if ($self.attr('id') == 'frm_contact') {
			event.preventDefault();
			var href = $self.attr('action');
			var method = $self.attr('method');
			var data = {
						'contact-name'		: escape($('input[@name="contact-name"]', $self).val()),
						'contact-email'		: escape($('input[@name="contact-email"]', $self).val()),
						'contact-comments'	: escape($('textarea[@name="contact-comments"]', $self).val()),
						'contact-ajax'		: 1
			};
			$.ajax({
				type: method,
				url: href,
				data: data,
				success: function(resp){
					if (resp == '') {
						$('#thanks').show();
						$self.hide();
					} else {
						alert(resp);
					}
				},
				error: function(req) {}
			});
		}
	});

	$('textarea.v-maxlenght').keyup(function() {
		var $label = $('label[@for='+ $(this).attr('id') +']');
		$('span.count', $label).html(this.value.length+' / ');
	});

/*	$('textarea.v-maxlenght').focus(function() {
		var $textarea = $(this);
		var $label = $('label[@for='+ $(this).attr('id') +']');
		$().bind('keyup.textcount', function () {
			var value = $textarea.val();
			$('span.count', $label).html(value.length+' / ');
		});
	});
	$('#frm_contact textarea').blur(function() { $().unbind('keyup.textcount'); });*/

/*  ---------------------------------------------------
	Comment form handler ------------------------------ */

	$('#frm_comment').submit(function (event) {
		// Reset the validation
		var $frm = $(this);
		var error = 0;
		$('.error', this).removeClass('error');
		$('label span.msg', this).remove();
		
		if (!$(this).hasClass('logged')) {
			$("input.v-mandatory", $frm).each(function() {
				if ( !$(this).val() ) { error++; flagError(this, 'is mandatory'); }
			});
			$(".v-email", $frm).each(function() {
				if ( !validateEmail($(this).val()) ) { error++; flagError(this, 'is invalid'); }
			});
			$("#comment-tinyturing", $frm).each(function () {
				if ( $(this).val() != $('span.tinyturing', $frm).html()) { error++; flagError (this, ''); }
			});
		}
		$("textarea", $frm).each(function () {
			if ( $(this).val() == 0 || !validateLenght($(this).val(), 2000) ) { error++; flagError(this, ''); }
		});
		
		if (error) { return false }
		else { $frm.attr('action', '/movabletype/' + 'mt-comment-handler.' + 'cgi') }
	});
	
	if ( $('#frm_comment').is('form') ) {
		$('#frm_comment').show();
		if (commenter_name && commenter_name != '') {
			$('#frm_comment').addClass('logged');
		}
	}
});


/*  ---------------------------------------------------
    Validation ---------------------------------------- */	
	
	function validateEmail (value) {
		var reEmail = /^(?:\w+(?:[-.+]?\w+)*@\w+(?:[.-]?\w+)*\.(?:\w{2,4}|museum))?$/;
		return reEmail.test(value);
	}
	function validateLenght (value, length) {
		return (value.length <= length);
	}
	function flagError (self, msg) {
		label = $(self).siblings('label[@for='+ $(self).attr('id') +']');
		label.append('<span class="msg"> '+msg+'</span>')
			 .addClass('error');
	}
