/* 
    Created on : 20-apr-2011
    Author     : Vasile Botoroga
    Description: Javascript nel frontend
*/

$(document).ready(function() {

	// ** album page
	$('.picture').live('mouseover', function(){
		$(this).children('.pic_hover_info').fadeIn(200);
    });
	$('.picture').live('mouseleave', function(){
		$(this).children('.pic_hover_info').fadeOut(200);
    });
	
	// ** gallery page
	$('.album').live('mouseover', function(){
		$(this).children('.album_hover_info').fadeIn(200);
    });
	$('.album').live('mouseleave', function(){
		$(this).children('.album_hover_info').fadeOut(200);
    });
	
	// textarea del login
	$('input[type=text], textarea').each(function() {
		var campotxt = this.value;
		$(this).focus(function() {
			if(this.value == campotxt) {
				this.value = '';
			}
		});

		$(this).blur(function() {
			if(this.value == '') {
				this.value = campotxt;
			}
		});
	});
	
	
	// animazione logo e servizi
	$("#logo").children('img').css({'width':'0px','height':'0px'});
	$("#servizi a").hide();
	$("#logo").children('img').animate({ 
		width: "350px",
		height: "256px",
	}, 2000, function(){
		var showInSequence = function($el){
			if($el)
				$el.eq(0).fadeIn(300, function(){ showInSequence($(this).next()) } );
		}
		showInSequence( $("#servizi a") );
	});
	
	// hover su servizi
	$('#servizi a').live('mouseover', function(){
		//$(this).animate({'background-color':'#AE4444'}, 500);
	});
	$('#servizi a').live('mouseleave', function(){
		//$(this).animate({'background-color':'#000000'}, 500);
	});
	
	// visualizza i submenu
	$('#vetrine_submenu, #servizi_submenu').live('mouseover', function(){
		$(this).children('div:hidden').slideDown(300);
	});
	$('#vetrine_submenu, #servizi_submenu').live('mouseleave', function(){
			$(this).children('div:visible').slideUp(300);
	});
	
	
    /* -------------------------------------------------------------- */
    /* ---------------------- Form Newsletter ----------------------- */
    /* -------------------------------------------------------------- */

    $('#newsletter-form').submit(function() {

        var first_name = $('input[name="newsletter_first_name"]').val(),
        	last_name = $('input[name="newsletter_last_name"]').val(),
            company = $('input[name="company"]').val(),
            email = $('input[name="newsletter_email"]').val();

/*        
// Validazione degli elementi obligatori
        if (first_name <= 0 || first_name == "Nome") {
            alert('Il campo "Nome" è obligatorio.');
            return false;
        }
        
        if (last_name <= 0 || last_name == "Cognome") {
            alert('Il campo "Cognome" è obligatorio.');
            return false;
        }
*/

        email = $('#newsletter-email').val().match(/^([a-zA-Z0-9_\-\.]+)@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i) ? $('#newsletter-email').val() : null;

        if (email === null) {
            alert('L\'indirizzo email inserito non è valido.');
            return false;
        }

        var data = {
            'first_name': 	first_name,
            'last_name': 	last_name,
            'company': 		company,
            'email': 		email
        };

        $.ajax({
            url: $('#newsletter-form').attr('action'),
            type: 'POST',
            data: data,
            success: function(result) {
            	if(result) {
                	alert('Grazie per esserti iscritto alla newsletter del nostro sito.');
                	$('input[name="newsletter_first_name"]').val('Nome');
                	$('input[name="newsletter_last_name"]').val('Cognome');
                	$('input[name="newsletter_email"]').val('Indirizzo E-mail');
                }else {
                	alert('L\'indirizzo email inserito è gia presente nei nostri database.');
                }
            }
        });
        
        return false;

    });
});

