var corpo_ajax;
var cn_del;
$(document).ready(function(){

 
  $(".sandokan-popup a").hover(function() {
  $("#slider").stop(true, true).animate({opacity: "show", top: "0"}, "slow");
  }, function() {
  $("#slider").animate({opacity: "hide", top: "10"}, "fast");
  });

  /*  Navigazione */
  menu("#ar", "#login")
  menu("#pv", "#regioni")
  
  /*  Aggiungo immagine chiusura ai livelli */
  $(".popupmenu").append("<div class='bottom'>&nbsp;</div>")
    
  /* Navigazion dx */
  $(".mnuico").mouseover( function(){
    $(this).children("div").fadeIn("fast")
  });
  $(".mnuico").mouseout( function(){
    $(this).children("div").fadeOut("slow")
  });
  
  /*  Input */
  $(".input").focus( function(){
    $(this).css("background-image", "url(immagini/campo_form_on.png)")
  })
  $(".input").blur( function(){
    $(this).css("background-image", "url(immagini/campo_form.png)")
  })
  /* Ricerca  */
  $("#fldricerca").focus( function(){
    if($("#fldricerca").val() == "cerca nel sito"){
      $("#fldricerca").val("")
    }
  })
  
  $("#fldricerca").blur( function(){
    if($("#fldricerca").val() == ""){
      $("#fldricerca").val("cerca nel sito")
    }
  })

  corpo_ajax = $("#trigger_ajax").overlay({
      effect: 'apple',  
      closeOnClick: false
  });
  
  if(cn_del == true){
    $('.corpo_cn').remove();
    $('.corpo_sx').css({'width':'700px', 'border':'none'});
  }
  
});


$(window).load(function(){

  var sx = 0;
  var cn = 0;
  var dx = 0;
  var h = 0;

  if($(".corpo_sx").length != 0) sx = parseFloat($(".corpo_sx").height())
  if($(".corpo_cn").length != 0) cn = parseFloat($(".corpo_cn").height())
  if($(".corpo_dx").length != 0) dx = parseFloat($(".corpo_dx").height())

  if(sx > cn){ h = sx}else{ h = cn }
  if(h < dx){ h = dx}
  $(".corpo_sx, .corpo_cn, .corpo_dx").css("height", h );

});

var cnt = undefined;

function pv(regione){

    var style= "position:absolute;z-index:1;background:#FFF;opacity:0.7;filter: alpha(opacity=70);left:1px;top:0;width:100%;height:" + $(".corpo_sx").height() + "px;margin:10px 0;"
    $(".corpo_sx").append("<div id='attesa' style='" + style + "'><img src='immagini/loader.gif' style='display:block;position:relative;margin:0 auto;top:50%;'></div>")
  
  $.get("pv.asp?regione=" + regione, function(data){
    
    $(".corpo_sx").slideUp("fast", function(){
      var r = data.split("|")
      if(cnt === undefined) cnt = $(".corpo_sx").html();
      $(".corpo_sx").html(r[0]);
      $(".corpo_sx").css("height","auto")
      $("#attesa").css("display", "none");
      $(".corpo_sx").slideDown("fast");
      $("#attesa").css("display", "none");
      $("area").mouseout();
      $("area[alt=" + regione +"]").mouseover();
    });

  });
  
}

function pv_restore(){
  
  if( cnt !== undefined){
    $(".corpo_sx").slideUp("fast", function(){
      $(".corpo_sx").html(cnt);
      $("#attesa").css("display", "none");
      $(".corpo_sx").slideDown("fast");
    });
  }
  
}

function filtra_pr(o){
  if($(o).val() == "#")
  {
    $(".PV").css("display","block")
  }else{
    var pr= "(" + $(o).val() + ")"
    $(".PV").each( function(){
      ($(this).children(".indirizzo").html().indexOf(pr) == -1) ? $(this).css("display","none") : $(this).css("display","block"); 
    });
  }
}

function prodotto(pr_id, attivaTransizione){
  
  $.get("prodotto.asp?pr_id=" + pr_id, function(data){

    if(attivaTransizione != undefined){
      var anima = 0
       attivaTransizione == "precedente" ? anima = -1000 : anima = 1000
       $("#info_ajax").animate( {left:anima,opacity:0}, 300, 
        function(){ 
          carica( pr_id, data, "attiva" )
        })
    }else{
        carica( pr_id, data )
    }
  });  
}

function carica( pr_id, data, attivaTransizione ){

    var navigazione = ""

    // Prodotto precedente
    if ($("#pr" + pr_id).prev("div.prodotto").length != 0){
      navigazione += "<a id='prev' href='javascript:prodotto(\x22" + $("#pr" + pr_id).prev("div.prodotto:eq(0)").attr("id").replace("pr", "") + "\x22, \x22precedente\x22)'>Prodotto precedente</a>"
    }
    // Prodotto successivo
    if ($("#pr" + pr_id).next("div.prodotto").length != 0){
      navigazione += "<a id='next' href='javascript:prodotto(\x22" + $("#pr" + pr_id).next("div.prodotto:eq(0)").attr("id").replace("pr", "") + "\x22, \x22successivo\x22)'>Prodotto successivo</a>"
    }
    
    if(navigazione.length != 0){
      navigazione = "<div id='navigazioneprodotto'>" + navigazione + "</div>"
    }

    data += navigazione

    $("#info_ajax").html(unescape(data)); 

    if($('#info_ajax img').attr('src') !== undefined ){
      $('#ajax_img')
      .attr('src', $('#info_ajax img').attr('src'))
      .load(function(){
        transizione(attivaTransizione)
      });
    }else{   
        transizione(attivaTransizione)
    }

}

function transizione( attiva ){

    if(attiva == "attiva"){
        $("#info_ajax").css("left",0)

        $("#info_ajax").css("opacity",100)
    }
    
    if(attiva != true){
      $("#trigger_ajax").click();
    }else{
      $('#info_ajax').fadeIn('fast');
    }

}

function linee(ca_id){

  $("#lineespalla .gruppo_on").slideUp("fast")
  $("#lineespalla .gruppo_on").removeClass("gruppo_on")
  $("#GRUPPO_"+ca_id).addClass("gruppo_on")
  $("#GRUPPO_"+ca_id).slideDown("fast")
  
}

function aprilinea(ca_id){
  
  document.location = "prodotti.asp?ca_id=" + ca_id;

}

function immagine(src){
  $("#pic").attr("src", src.replace("/50/", "/300/") )  
}

function menu(trigger, menu){

//  Attiva comportamento menu per link e livello collegato
  
  $(trigger + "," + menu).mouseover( function(){
    $(trigger).css("background", "#000")
//     $(trigger + " a").css("color", "#FFF")
    $(trigger).css("cursor", "pointer")
    $(menu).show()
  })

  $(trigger + "," + menu).mouseout( function(){
    $(trigger).css("background", "none")
//     $(trigger + " a").css("color", "#333")
    $(menu).hide()
  })

}
