//GA
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-473767-53']);
_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);
})();

//Functions
$(document).ready(function () {

    $('.scrollpane').jScrollPane({
        showArrows: true
    });

    //Animate menu
    animateMenu();

    //
    productsMenu();

    if ($.isPage('') || $.isPage('index')) {

        moveBoxes();
        swfWrite('header_home.swf', 'headerImage', '960', '350');

    } else if ($.isPage('chi-siamo')) {

        swfWrite('header_azienda.swf', 'headerImage', '960', '350');

    } else if ($.isPage('contatti')) {

        swfWrite('header_azienda.swf', 'headerImage', '960', '350');
        validateForm();

    } else if ($.isPage('dove-trovare-i-nostri-prodotti')) {

        swfWrite('header-dove-trovare-i-prodotti.swf', 'headerImage', '960', '350');
        

    } else if ($.isPage('note-legali')) {

        swfWrite('header_azienda.swf', 'headerImage', '960', '350');
		initializeMap();
	}
});


// @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[0]) {

        return true;
    } else {
        return false;
    }
}


//FUNCTION animateMenu
function animateMenu() {

    //Hide all submenus
    $("ul#nav ul").css({
        display: "none"
    });

    //Li hover
    $("ul#nav li").hover(function () {

        //Show all submenus
        $(this).children('ul:first').css({
            visibility: "visible",
            display: "none"
        }).show(400);

        //Animate voices
        if (!$(this).children('a').hasClass('none')) {

            $(this).children('a').find('img').stop().animate({
                marginTop: "-40px"
            }, 250);

        }

    }, function () {

        //Animate back
        $(this).children('ul:first').css({
            visibility: "hidden"
        });

        //Animate back
        $(this).find('img').stop().animate({
            marginTop: "0"
        }, 300);


    });

}

//FUNCTION productsMenu
function productsMenu() {

    var url = window.location.pathname;
    var page = url.split('/');
    page.reverse();


    //Li hover
    $("#productsMenu ul li a").each(function () {



        if ($(this).attr('id') == page[0]) {


            $(this).css({
                backgroundPosition: "0 -120px"
            });

        }


    })
}

//FUNCTION animate boxes products
function moveBoxes() {

    $(".product").hover(

    function () {

        $(".thumbProduct").addClass('selected');
        $(this).stop().animate({
            paddingTop: "10px"
        }, 300);

    },

    function () {

        $(this).stop().animate({
            paddingTop: "0px"
        }, 300);

    })

}

// @info Initialize map
function initializeMap() {

    var myLatlng = new google.maps.LatLng(45.840588, 9.356194);

    var myOptions = {
        zoom: 13,
        center: myLatlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    var map = new google.maps.Map(document.getElementById("map"), myOptions);

    //var image = '';
    var marker = new google.maps.Marker({
        position: myLatlng,
        map: map,
        title: 'ciao'
        //icon: image
    });

    var infowindow = new google.maps.InfoWindow({
        content: '<p><span>Opac.it</span><br />Via Casnedi 96<br />23868 Valmadrera (LC) - Italy</p>'
    });

    /*google.maps.event.addListener(marker, 'click', function() {*/
    infowindow.open(map, marker); /*});*/
}

// @info Write Swf
function swfWrite(name, target, width, height) {

    var path = "/swf/" + name;
    var so = new SWFObject(path, '', width, height, '9');

    so.addParam('menu', 'false');
    so.addParam("wmode", "opaque");
    so.write(target);

}

//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 zip = $("input[name=zip]").val();
        var province = $("input[name=province]").val();
        var telephone = $("input[name=telephone]").val();
        var email = $("input[name=email]").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("Completare il campo nome");
        }

        if (surname == "") {
            error = true;
            $("input[name=surname]").val("Completare il campo 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("Inserire una richiesta");
        }

        if (error) {
            $('input[name=send]').animate({
                marginLeft: "310px"
            }, 200).animate({
                marginLeft: "305px"
            }, 200).animate({
                marginLeft: "310px"
            }, 200).animate({
                marginLeft: "305"
            }, 200);
        } else {
            $.post("/lib/mail/mail.php", {

                name: name,
                surname: surname,
                company: company,
                address: address,
                city: city,
                zip: zip,
                province: province,
                telephone: telephone,
                email: email,
                msg: msg

            });
            $('.theForm').fadeOut(500);
            setTimeout("$('.thxForm').fadeIn('1000')", 1000);
        }

        return false;
    });
}
