function log(data)
{
	if(window.console != undefined)
		console.log(data);
}

(function($){

	function handleThumbsHover(event)
	{
		var $img = $(this);
		var $blocco = $img.parent();
		var $hover = $blocco.find('.hover');
		var hoverInited = false;

		if(event.type == 'mouseenter')
		{
			$hover.show();
		}
		else
		{
			$hover.hide();
		}
	}

	$.fn.thumbsHover = function()
	{
		this.each(init);
		function init()
		{
			var $blocco = $(this);
			var $thumb = $blocco.find('.thumb');
			var $hover = $blocco.find('.hover');
			var $link = $hover.find('a');

			var hoverInited = false;

			$thumb.mouseenter(showHover);
			$hover.mouseleave(hideHover);
			$link.mouseleave(hideHover);

			function showHover()
			{
				//if(! hoverInited)
					setHeight();

				$hover.stop().fadeTo('fast', .9);
			}
			function hideHover()
			{
				$hover.fadeOut('fast');
			}

			function setHeight()
			{
				$hover.css
				({
					height:$thumb.height(),
					lineHeight:$thumb.height() + 'px'
				})
				hoverInited = true;
			}
		}
	}

	function ajaxForm()
	{
		var $form = $(this.currentForm);

		var $invia = $form.find('input.invia');
		var $loading = $form.find('.loading');

		$invia.hide();
		$loading
			.html('Invio messaggio...')
			.removeClass('hide');

		var params = {
			url:'email',
			type:'post',
			data:$form.serialize(),
			success:ajaxSuccess,
			error:ajaxError
		}
		$.ajax(params);

		return false;

		function ajaxSuccess(data)
		{
			if(data == 'success')
			{
				$loading.text('Messaggio inviato.')
			}
			else
			{
				var errorCode = '(cod. 0)';
				if(data.length == 7)
					errorCode = '(cod. ' + data.replace('error:') + ')';

				$loading.html('Si &egrave; verificato un errore ' + errorCode + '.<br>Si prega di scrivere a <a href="mailto:info@terotero.com">info@terotero.com</a>');
			}
		}
		function ajaxError(error)
		{
			errorCode = '(cod. ' + error.status + ')';
			$loading
				.html('Si &egrave; verificato un errore ' + errorCode + '.<br>Si prega di scrivere a <a href="mailto:info@terotero.com">info@terotero.com</a>')
				.removeClass('hide');
		}
	}

    $(function()
    {
        /* $(document).ready() */

		/*$('.modal').modalGallery
		({
			overlayAlpha:1,
			width:600,
			height:450
		});*/

		$('.modal').colorbox({opacity:1});

		$('#gallery').inlineGallery
		({
			width:600,
			height:450
		});

		$('.blocco').thumbsHover();

		$('#contact').validate
		({
			submitHandler:ajaxForm
		});

    });     //  closing $(document).ready()

})(jQuery);
