// JavaScript Document

//GA
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-2555190-9']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();


$(document).ready(function () {

    //Global functions
    nav();


    if ($.isPage('produzione-standard')) {

        animateBoxes(".boxCategory");

    } else if ($.isPage('carpenteria-disegno')) {

        $('ul.prgThumb a').lightBox();

    } else if ($.isPage('produzione-speciale')) {

        $('ul.listThumbsES li a').lightBox();

    } else if ($.isPage('arredi-speciali')) {

        $('ul.prgThumb li a').lightBox();

    } else if ($.isPage('esempi-di-installazione')) {

        $('ul.listInstallations li a').lightBox();

    } else if ($.isPage('azienda') || $.isPage('')) {

        animateBoxes(".toSection");

    } else if ($.isPage('dove-siamo')) {

        initializeMap();

    } else if ($.isPage('contatti')) {

       validateForm();
    }
})


/*-------------------- FUNCTIONS ---------------------------*/

// @info 	Check current pag
// @usage	if($.isPage('pageName'))
jQuery.isPage = function (checkFor) {

    var url = window.location.pathname;
    var page = url.split('/');
    page.reverse();

    if (checkFor == page[1]) {

        return true;
    } else {
        return false;
    }
}

//Initialize main menu
function nav() {
    $("ul#menu-nav ul").css({
        display: "none"
    }); // Opera Fix
    $("ul#menu-nav li").hover(

    function () {
        $(this).find('ul:first').css({
            visibility: "visible",
            display: "none"
        }).show(300);
    }, function () {
        $(this).stop().find('ul:first').hide(250);
    });
}

//Fade Effects


function animateBoxes(id) {

    var el = id;

    $(el).hover(

    function () {

        $(el).addClass('unselected');
        $(this).removeClass('unselected')

        //FadeOut this voices
        $(el).each(function (i, element) {

            var $this = $(element);

            if ($this.hasClass('unselected')) {

                $this.stop().animate({
                    opacity: 0.60
                }, 450);
            }

        })
    },

    function () {

        $(el).stop().animate({
            opacity: 1
        }, 450);

    })

}

// @info Initialize map
function initializeMap() {

    var myLatlng = new google.maps.LatLng(45.686900, 9.735500);
    var myOptions = {
        zoom: 16,
        center: myLatlng,
        mapTypeId: google.maps.MapTypeId.HYBRID
    }
    var map = new google.maps.Map(document.getElementById("map"), myOptions);

    //var image = '';
    var marker = new google.maps.Marker({
        position: myLatlng,
        map: map
        //icon: image
    });
}

//Validate form
function validateForm() {
	
$("input[name=send]").click(function(){
		var name	= $("input[name=name]").val();
		var surname	= $("input[name=surname]").val();
		var company	= $("input[name=company]").val();
		var address	= $("input[name=address]").val();
		var city	= $("input[name=city]").val();
		var province   = $("input[name=province]").val();
		var email	= $("input[name=email]").val();
		var telephone = $("input[name=telephone]").val();
		var msg 	= $("textarea[name=msg]").val();
	
		var error = false;
		var at = email.indexOf("@");
		var dot = email.indexOf(".");
		
		if(name == ""){
			error = true;
			$("input[name=name]").val("Insert name");
		}
		
		if(surname == ""){
			error = true;
			$("input[name=surname]").val("Insert Cognome");
		}
	
		if(telephone == ""){
			error = true;
			$("input[name=telephone]").val("Completare il campo Telefono");
		}
	
	
		if(name == ""){
			error = true;
			$("input[name=email]").val("Completare il campo Email");
		}
		
		if((email == "") || (at == -1) || (dot == -1) ){
			error = true;
			$("input[name=email]").val("Completare il campo Email");
		}
		
		if(msg == ""){
			error = true;
			$("textarea[name=msg]").val("Completare");
		}
		
		if( error ){
			$('input[name=send]')
				.animate({marginLeft: "3px"},200)
				.animate({marginLeft: "-3px"},200)
				.animate({marginLeft: "3px"},200)
				.animate({marginLeft: "0"},200);
		}else{
			$.post("/wp-content/themes/aldegani/lib/mail/mail.php", { 
				 
				 name: name,
                 surname: surname,
                 company: company,
				 address: address,
				 city: city,
				 province: province,
                 telephone: telephone,
                 email: email,
                 msg: msg 
				   
				 });
				$('.theForm').fadeOut(500);
				setTimeout("$('.thxForm').fadeIn('1000')", 1000);
		}
		
		return false;
	});
}
