window.MCS=window.MCS||{}

MCS.selectedMenu=null;
MCS.menuWidth=0;

MCS.mapsLoaded=function(){

    //centro mappa differente da posizione marker 45.496722,9.895935
    MCS.latlng = new google.maps.LatLng(45.701541,10.36338);//45.71412, 10.34345
    var myOptions = {
        zoom: 10,//originale a 15
        center: new google.maps.LatLng(45.627484,10.343628),
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    MCS.mappa = new google.maps.Map(document.getElementById("googleMaps"), myOptions);
    MCS.dirService= new google.maps.DirectionsService;
    MCS.dirRenderer= new google.maps.DirectionsRenderer();
    MCS.dirRenderer.setMap(MCS.mappa);

    MCS.marker = new google.maps.Marker({
        position: MCS.latlng,
        map: MCS.mappa,
        title:"MCS"
    });



    var contentString = '<div id="scrittaInsernaInfoWindow"><h3>M.C.S. Facchetti Srl</h3></div>';

    MCS.infowindow = new google.maps.InfoWindow({
        content: contentString
    });
    // MCS.infowindow.open(MCS.mappa,MCS.marker);

    google.maps.event.addListener(MCS.marker, 'click', function() {
        MCS.infowindow.open(MCS.mappa,MCS.marker);
    });
};

MCS.getDirections=function(where){
    var request = {
        origin:where,
        destination:MCS.latlng,
        waypoints: [
        {
            location:new google.maps.LatLng(45.702204, 10.387042),
            stopover:false
        }],
        travelMode: google.maps.DirectionsTravelMode.DRIVING
    };
    MCS.dirService.route(request, function(result, status) {
        if (status == google.maps.DirectionsStatus.OK) {
            MCS.infowindow.close();
            MCS.dirRenderer.setDirections(result);
            //stampo sotto la mappa e inidicazioni per raggiungere la posizione
            
            var indicazioni=result.routes[0].legs[0];
            htmlElenco='<ol>';
            for(var x=0;x<indicazioni.steps.length;x++){
                htmlElenco+='<li>'+indicazioni.steps[x].instructions+'</li>';
            }
            htmlElenco+='</ol>';
            $('#indicazioniSteps').html(htmlElenco);
            $('#distanza').text(indicazioni.distance.text);
            $('#durata').text(indicazioni.duration.text);
            $('#indicazioniGoogleMaps').slideDown('fast');
            
        }
    });
};
MCS.sendTextToDirection=function(e){
    e.preventDefault();
    testo=$('#testoGoogleMaps').val();
    if(testo==''){
        return false;
    }else{
        var win = window.open('','map')
        win.location = 'http://maps.google.it/maps?f=d&hl=it&saddr='+testo+'&daddr=mcs facchetti';
    }
};


$(document).ready(function(){
    $('.btnMenu').mouseenter(function() {
        if(MCS.selectedMenu==null){
            MCS.selectedMenu=$(".selected")[0];
        }
        $('.btnMenu').removeClass("selected");
        $(this).addClass("selected");
    }).mouseleave(function() {
        $('.btnMenu').removeClass("selected");
        $(MCS.selectedMenu).addClass("selected");
    });

    //$("#carrousel").CloudCarousel({
    //  xPos: 450,
    //  yPos: 30
    //});
    $("#featureCarousel").featureCarousel({
        smallFeatureWidth:0.8,
        smallFeatureHeight:0.8,
        smallFeatureOffset:20,
        topPadding:12,
        autoPlay:0
    });

    //controllo la presenza della mappa e carico l'api
    if($('#googleMaps').length>0){
        MCS.mapsLoaded();
    }

    //eventi per ricerca direzione google maps
    $('#sendLocation').click(MCS.sendTextToDirection);
    $('#testoGoogleMaps').keypress(function(e){
        var keycode;
        if (window.event) {
            keycode = window.event.keyCode;
        }
        else if (e) {
            keycode = e.which;
        }
        else {
            return true;
        }

        if (keycode == 13) {
            MCS.sendTextToDirection(e);
            return false;
        }
    });
});

jQuery(window).load(function(){
    $("a[rel='lightbox']").fancybox();
    //correzione altezze content
    totHeight=$("#box-footer").height()+$("#box-header").height()+$("#divisoria").height()+$('#bottomMiddlePage').height();
    $("#box-middle").css("minHeight",($(document).height()-totHeight-2));

    //correzione menu
    $('.btnMenu').each(function(){
        MCS.menuWidth+=$(this).outerWidth(true);
    });
    $('#menu').width(MCS.menuWidth+2);//aggiungo 2 pixel per correggere i possibili problemi risultati da arrotondamenti

    //ridimensionamento divisorie content
    $('.contentRow').each(function(){
        MCS.maxHeightContent=0;
        $(this).children('.contentBlk').each(function(){
            if(MCS.maxHeightContent<$(this).height()){
                MCS.maxHeightContent=$(this).height()
            }
        });
        $(this).children('.contentDiv').height(MCS.maxHeightContent);
    });
});

   



