
$(document).ready(function(){

 //   tuneHeight();
    espanditore();
    gestioneFrecce();
    $(".view-macchine .views-fluid-grid-inline:last").css("background-image", "none");
    //$(".page-miscela-master-club-coffee .pane-node:first").css("background-image", "none");
	
    $(".section-miscela .pane-node h2:last, .section-cialde-capsule-al-bar .pane-node h2:last").css("background-image", "none");
    
    /*TRADUZIONE*/
    var dove_ti_trovi ="";
    var torna_al_carrello ="";
    var acquista_online= "ACQUISTA ON LINE";
    var a_meno_di= "a meno di";
    var vedi= "VEDI";
    var risultati_della_ricerca="RISULTATI DELLA RICERCA";
    switch(languageActive()){
        case "en":
            dove_ti_trovi ="where are you?";
            torna_al_carrello ="Torna al carrello";
            acquista_online= "ACQUISTA ON LINE";
            a_meno_di= "Less than";
            risultati_della_ricerca="SEARCH RESULTS";
            vedi= "VIEW"; 
            if($("body").hasClass('page-coffee-lab-locator')){
                $("h1").text('Find your nearest coffee lab');
            }
            break;
        case "it":
            dove_ti_trovi ="dove ti trovi";
            torna_al_carrello ="Torna al carrello";
            acquista_online= "ACQUISTA ON LINE";
            a_meno_di= "a meno di";
            break;
        case "fr":
            dove_ti_trovi ="where are you?";
            torna_al_carrello ="Torna al carrello";
            acquista_online= "ACQUISTA ON LINE";
            a_meno_di= "à moins de";
            vedi= "VOIR"; 
            risultati_della_ricerca="SEARCH RESULTS";
            if($("body").hasClass('page-coffee-lab-locator')){
                $("h1").text('Trouver le coffee lab le plus proche');
            }
            break;
        case "de":
            dove_ti_trovi ="where are you?";
            torna_al_carrello ="Torna al carrello";
            acquista_online= "ACQUISTA ON LINE";
            a_meno_di= "Weniger als";
            vedi= "SIEHE"; 
            risultati_della_ricerca="SEARCH RESULTS";
            if($("body").hasClass('page-coffee-lab-locator')){
                $("h1").text('Wo finde ich das nächste Coffee Lab');
            }
            break;
    }
    
    
    /*openlocator*/
    $("#edit-circle-value-wrapper span.field-prefix").text(a_meno_di);
    $(".view-display-id-panel_pane_2 .view-filters").after("<div style='margin-bottom:10px;'><b>"+risultati_della_ricerca+"</b></div>");
    $(".view-display-id-panel_pane_2 .views-field-field-indirizzo-completo-value").after("<div class='btn-grigio ombra right btn-vedi'>"+vedi+"</div><div class='clear'></div>");
    
    
    
    
    
    $("#edit-circle-location").val(dove_ti_trovi);
    $("#edit-circle-location").focus(function(){
        if($(this).val()==dove_ti_trovi){
            $(this).val('');
        }
    });
    $("#edit-circle-location").blur(function(){
        if($(this).val()==""){
            $(this).val(dove_ti_trovi);
        }
    });
    $("#edit-circle-location").attr("title", dove_ti_trovi);
    $("#edit-circle-value").attr("title", "Km");

    $("#cart-form-products a").removeAttr("href");
    $("#cart-form-products a").css("cursor","default");
    $("#cart-form-products a").attr("onclick", "return false;");
    $("#cart-form-products a").css("text-decoration","none");
    /*$("#edit-cancel").click(function(){
        $("#edit-cancel").removeAttr("name");
        window.location.href = "cart";
     
    });*/
    $("#checkout-form-bottom").append("<a id='btn-indietro-cart' href='/cart'>"+torna_al_carrello+"</a>");

    $("#btn-indietro-cart").insertBefore("#edit-cancel")
    $("#edit-cancel").hide();

    /*acquista online*/
    if(languageActive()=="it"){
        $(".page-sistema-a-capsule-costadoro-coffee-lab .pane-prodotti h2").after('<span class="field-content right"><a class="btn-acquista" href="http://www.costadoro.it/e-shop/capsule" style="font-size:21px">'+acquista_online+'</a></span>');
        $(".page-sistema-a-cialde-costadoro-coffee-lab .pane-prodotti h2").after('<span class="field-content right"><a class="btn-acquista" href="http://www.costadoro.it/e-shop/cialde" style="font-size:21px">'+acquista_online+'</a></span>');
        $(".page-linea-moka-espresso .pane-prodotti h2").after('<span class="field-content right"><a class="btn-acquista" href="http://www.costadoro.it/e-shop/caffe" style="font-size:21px">'+acquista_online+'</a></span>');
        $(".page-miscela-coffee-lab .pane-prodotti h2").after('<span class="field-content right"><a class="btn-acquista" href="http://www.costadoro.it/e-shop/caffe" style="font-size:21px">'+acquista_online+'</a></span>');
        $(".page-miscela-master-club-coffee .pane-prodotti h2").after('<span class="field-content right"><a class="btn-acquista" href="http://www.costadoro.it/e-shop/caffe" style="font-size:21px">'+acquista_online+'</a></span>');
        $(".page-miscela-caffe-costadoro .pane-prodotti h2").after('<span class="field-content right"><a class="btn-acquista" href="http://www.costadoro.it/e-shop/caffe" style="font-size:21px">'+acquista_online+'</a></span>');
        $(".page-miscela-san-francisco .pane-prodotti h2").after('<span class="field-content right"><a class="btn-acquista" href="http://www.costadoro.it/e-shop/caffe" style="font-size:21px">'+acquista_online+'</a></span>');
    }
});
 
/*
 **/
Drupal.behaviors.pippo = function (context){

    var data = $(context).data("openlayers");
    if(data){
        var i = 0;
        $(".view-openlayer .views-row").click(function(event){
            i++;
            event.preventDefault();
            var nome = $(this).find(".views-field-title a").html();
            var indirizzo = $(this).find(".views-field-field-indirizzo-completo-value").text();
            var lat = $(this).find(".views-field-field-latitudine-value").text();
            lat+=0.4234;
            var lon = $(this).find(".views-field-field-longitudine-value").text();
            lon+=0.1234;
            var point = new OpenLayers.LonLat(lon,lat).transform(new OpenLayers.Projection('EPSG:4326'), new OpenLayers.Projection('EPSG:900913'));
            popup = new OpenLayers.Popup.FramedCloud(
                'popup',
                point,
                null,
                nome+"<p>"+indirizzo+"</p>",
                null,
                true,
                null
                );
            for(var j = 1; j<i;j++){
                $("#popup").remove();
            }
            var map = data.openlayers;
            map.addPopup(popup);
            map.setCenter(point, 15);
            return false;
        });
    }
/*$(".view-openlayer .views-field-title a").click(function(event){
        i++;
        event.preventDefault();
        var nome = $(this).html();
        var indirizzo = $(this).parents(".views-row").find(".views-field-field-indirizzo-completo-value").text();
        var lat = $(this).parents(".views-row").find(".views-field-field-latitudine-value").text();
        lat+=0.4234;
        var lon = $(this).parents(".views-row").find(".views-field-field-longitudine-value").text();
        lon+=0.1234;
        var point = new OpenLayers.LonLat(lon,lat).transform(new OpenLayers.Projection('EPSG:4326'), new OpenLayers.Projection('EPSG:900913'));
        popup = new OpenLayers.Popup.FramedCloud(
            'popup',
            point,
            null,
            nome+"<p>"+indirizzo+"</p>",
            null,
            true,
            null
          );
        for(var j = 1; j<i;j++){
            $("#popup").remove();
        }
        map.addPopup(popup);
        map.setCenter(point, 15);
        return false;
    });*/
  
}



function gestioneFrecce(){
    /*frecce next*/
    $("#btn-next").hide();
    $("#btn-prev").hide();
    if ($("body").hasClass("page-miscela-coffee-lab") == true) {
        $("#btn-next").show();
        $("#btn-next").attr("href", "master-club-coffee");
        $("#btn-prev").show();
        if(languageActive()=="it"){
            $("#btn-prev").attr("href", "san-francisco");
        }
        else{
            $("#btn-prev").attr("href", "caffe-costadoro");
        }
    }
    if ($("body").hasClass("page-miscela-master-club-coffee") == true) {
        $("#btn-next").show();
        $("#btn-next").attr("href", "caffe-costadoro");
        $("#btn-prev").show();
        $("#btn-prev").attr("href", "coffee-lab");
    }
    
    if ($("body").hasClass("page-miscela-caffe-costadoro") == true) {
        $("#btn-next").show();
        if(languageActive()=="it"){
            $("#btn-next").attr("href", "san-francisco");
        }
        else{
            $("#btn-next").attr("href", "coffee-lab");
        }
        $("#btn-prev").show();
        $("#btn-prev").attr("href", "master-club-coffee");
    }
    
    if ($("body").hasClass("page-miscela-san-francisco") == true) {
        $("#btn-next").show();
        $("#btn-next").attr("href", "coffee-lab");
        $("#btn-prev").show();
        $("#btn-prev").attr("href", "caffe-costadoro");
    }
	
	
    if ($("body").hasClass("page-sistema-a-capsule-costadoro-coffee-lab") == true) {
        $("#btn-next").show();
        $("#btn-next").attr("href", "sistema-a-cialde-costadoro-coffee-lab");
        $("#btn-prev").show();
        $("#btn-prev").attr("href", "sistema-a-cialde-costadoro-coffee-lab");
    }
    if ($("body").hasClass("page-sistema-a-cialde-costadoro-coffee-lab") == true) {
        $("#btn-next").show();
        $("#btn-next").attr("href", "sistema-a-capsule-costadoro-coffee-lab");
        $("#btn-prev").show();
        $("#btn-prev").attr("href", "sistema-a-capsule-costadoro-coffee-lab");
    }
    if (($("body").hasClass("page-altri-prodotti") == true)||($("body").hasClass("page-linea-moka-espresso"))) {
        
        avanti=$(".pager-next a").attr("href");
        indietro=$(".pager-previous a").attr("href");
		
        if(avanti){
            $("#btn-next").show();
            $("#btn-next").attr("href", avanti);
        }
        else{
            if ($("body").hasClass("page-altri-prodotti") == true) {
                $("#btn-next").show();
                $("#btn-next").attr("href", "altri-prodotti");
            }
            if ($("body").hasClass("page-linea-moka-espresso") == true) {
                $("#btn-next").show();
                $("#btn-next").attr("href", "linea-moka-espresso");
            }
        }
		
        if(indietro){
            $("#btn-prev").show();
            $("#btn-prev").attr("href", indietro);
        }
        else{
            if ($("body").hasClass("page-altri-prodotti") == true) {
                $("#btn-prev").show();
                $("#btn-prev").attr("href", "altri-prodotti?page=1");
            }
            if ($("body").hasClass("page-linea-moka-espresso") == true) {
                $("#btn-prev").show();
                $("#btn-prev").attr("href", "linea-moka-espresso?page=1");
            }
        }
    }
	
	
/*  if ($("body").hasClass("front") == true) {
        $("#btn-next").show();
        $("#btn-prev").show();
    }
  if ($("body").hasClass("node-id-12") == true ||
    $("body").hasClass("node-id-13") == true ||
	 $("body").hasClass("node-id-86") == true ||
    $("body").hasClass("node-id-14") == true) {
        $("#btn-next").show();
        $("#btn-prev").show();
    }*/
}


function espanditore(){
    //nascondi tutti gli elementi
    $(".expand-body").hide();
    //toggle gli elementi figli
    $(".expand-header").click(function(){
        $(this).next(".expand-body").slideToggle("slow");
        $(this).parent("li").toggleClass("open");
    });
}

function tuneHeight(){
    var hmax = 0;
    $(".view-Prodotti .views-field-body").each(function(){
        if (hmax < $(this).height()) {
            hmax = $(this).height();
        }
    });
    $(".view-Prodotti .views-field-body").height(hmax);
    
    
    hmax = 0;
    $(".view-Prodotti .views-field-title").each(function(){
        if (hmax < $(this).height()) {
            hmax = $(this).height();
        }
    });
    $(".view-Prodotti .views-field-title").height(hmax * .6);
    
    hmax = 0;
    $(".view-macchine .views-field-title").each(function(){
        if (hmax < $(this).height()) {
            hmax = $(this).height();
        }
    });
    $(".view-macchine .views-field-title").height(hmax * .6);
    
    hmax = 0;
    $(".view-miscele .views-field-title").each(function(){
        if (hmax < $(this).height()) {
            hmax = $(this).height();
        }
    });
    $(".view-miscele .views-field-title").height(hmax * .6);
}


function loadCss(filename){
    var fileref = document.getElementById("fontcheck")
    fileref.setAttribute("href", filename)
}
function datiAzienda(){
    var orig_div_cont=document.getElementById("content").innerhtml;
    var myWin=window.open("","myWin","left=230px,top=80px,height=240px,width=320px");
    myWin.document.write('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'+
        '<html><head><title>COSTADORO</title>'+
        '<link type="text/css" rel="stylesheet" media="all" href="/sites/all/themes/costadoro/css/default.css?O" />'+
        '<link type="text/css" rel="stylesheet" media="all" href="/sites/all/themes/costadoro/css/layout.css?O" />'+
        '<link type="text/css" rel="stylesheet" media="all" href="/sites/all/themes/costadoro/css/style.css?O" />'+
        '</head><body><div style="padding:16px"><div id="dest_div">'+
        '<b>COSTADORO SPA</b> – CAP.SOC. € 6.000.000 INT.VERS.<br>'+
        'CODICE FISCALE e PARTITA IVA IT 00703260018<br>'+
        'R.E.A. TO 459869 - REG.IMPR. TO 00703260018<br><br>'+
        'Sede Legale:<br>'+
        'LUNGO DORA COLLETTA 113/6 <br/>'+
        '10153 - TORINO (ITALIA)<br />Tel. +39.011.2483804<br />Fax. +39.011.2483828<br />E-mail: <a href="mailto:infoespresso@costadoro.it">infoespresso@costadoro.it</a>'+
        '</div></div></body></html>');
    myWin.document.close();
    myWin.document.getElementById("dest_div").innerhtml+=orig_div_cont;
}


function languageActive(){
    if($("body").hasClass('i18n-en')){
        return "en";
    }
    if($("body").hasClass('i18n-it')){
        return "it";
    }
    if($("body").hasClass('i18n-fr')){
        return "fr";
    }
    if($("body").hasClass('i18n-de')){
        return "de";
    }
    return "it";
}






