$(document).ready(function() {
  
    //HOME PAGE
    
    $('#slider').easySlider({
      
      speed:      800,
      auto:       true,
      pause:      6000,
      continuous: true,
      numeric:    true,
      numericId:  'controls'
      });
    
    // SMOOTH SCROLL
    
    //inizializzo ogni ancora della pagina (per smoothscroll)
    
    $('a').each(function(index) {
        if ($(this).attr("name")) $(this).after('<span style="margin-left:-5px;" id="'+$(this).attr("name")+'">&nbsp;</span>');
        });
    
    //smooth scroll al click del menu
    
    $('ul.in-page-menu a').smoothScroll();
    
    // smooth scroll to top
    
    $('p.goto-top a').click(function(e) {
        
        e.preventDefault();
        
        $.smoothScroll({
          scrollTarget: "#top"
          });
        });    

    
    // SEARCH BOX
    
    var searcValue = "";
    
    $('#search').focus(function() {
      
      currentSearchValue = $(this).attr("value");
      searcValue         = currentSearchValue;
      
      $(this).attr("value","");
      });

    $('#search').blur(function() {
      
      if ($(this).attr("value")=="") $(this).attr("value",searcValue);
      });
    
    //FANCYBOX
    
    //per formattare il titolo della lightbox corrente
    function format_fbTitle( title, currentArray, currentIndex, currentOpts) {
      
      var fbTitle  = '<div id="fb-title-container">';
          fbTitle += '<span id="fb-title"><b>'+title+'</b></span>';
          fbTitle += '<span id="fb-close"><a href="javascript:;" onclick="parent.$.fancybox.close();">chiudi la finestra</a></span>';
          fbTitle += '</div>';

      return fbTitle;
      }

    //per eseguire fancybox

    function exec_fancybox( url, title, contentType, dims ) {

        var winwidth  = $(window).width();
        var winheight = $(window).height();

        switch (dims) {
          case "small": width=450; height=300; break;
          case "medium": width=550; height=450; break;
          default: width=550; height=450;
          }

        //var url   = url+"&w="+winwidth+"&h="+winheight;
        //var url   = url;
        //var title = title;

        parent.$.fancybox({
          'href'               : url,
          'type'               : contentType,
          'title'              : title,
          'titlePosition'      : 'inside',
          'titleFormat'        : format_fbTitle,
          'autoDimensions'     : false,
          'width'              : width,
          'height'             : height,
          'modal'              : false,
          'showCloseButton'    : false,
          'enableEscapeButton' : true,
          'showNavArrows'      : false,
          'overlayOpacity'     : 0.8,
          'overlayColor'       : '#000',
          'hideOnContentClick' : false,
          'padding'            : 10,
          'margin'             : 30
          });

        }

    //esegue fancybox dal link

    $('a.utils-small').live('click', function(e) {
        
        e.preventDefault();
        
        var url   = $(this).attr("href");
        var title = $(this).attr("title");

        exec_fancybox( url, title, "iframe", "small" );

        });

    $('a.utils-audio').live('click', function(e) {
        
        e.preventDefault();
        
        var url   = $(this).attr("href");
        
        $.fancybox({
          'modal' : true,
          'href'  : url,
          'type'  : 'inline',
          'title'              : "&nbsp;",
          'titlePosition'      : 'inside',
          'titleFormat'        : format_fbTitle
          });

        });

    $('a.utils-video').live('click', function(e) {
        
        e.preventDefault();
        
        var url   = $(this).attr("href");
        var title = $(this).attr("title");
        
        $.fancybox({
          'modal'         : true,
          'href'          : url,
          'type'          : 'inline',
          'title'         : title,
          'titlePosition' : 'inside',
          'titleFormat'   : format_fbTitle,
          'width'         : 720,
          'height'        : 400
          });

        });

    $('a.utils-findomestic').live('click', function(e) {
        
        e.preventDefault();
        
        var url   = $(this).attr("href");
        var title = $(this).attr("title");
        
        $.fancybox({
          'modal' : true,
          'href'  : url,
          'type'  : 'inline',
          'title'              : title,
          'titlePosition'      : 'inside',
          'titleFormat'        : format_fbTitle
          });

        });

//contatti-mappa

    $('a.utils-map').live('click', function(e) {
        
        e.preventDefault();
        
        var url   = $(this).attr("href");
        var title = $(this).attr("title");
        
        $.fancybox({
          'modal'         : true,
          'href'          : url,
          'type'          : 'iframe',
          'title'         : title,
          'titlePosition' : 'inside',
          'titleFormat'   : format_fbTitle,
          'width'         : 700,
          'height'        : 500
          });

        });

//dati on line

    $('a.utils-dati-on-line').live('click', function(e) {
        
        e.preventDefault();
        
        var url   = $(this).attr("href");
        var title = $(this).attr("title");
        
        $.fancybox({
          'modal'         : true,
          'href'          : url,
          'type'          : 'iframe',
          'title'         : title,
          'titlePosition' : 'inside',
          'titleFormat'   : format_fbTitle,
          'width'         : 700,
          'height'        : 500
          });

        });
	
//richiesta abbonamento federauto time

    $('a.utils-req-abbonamento').live('click', function(e) {
        
        e.preventDefault();
        
        var randomnumber=Math.random();
        
        var url   = $(this).attr("href")+"?r="+randomnumber;
        var title = $(this).attr("title");
        
        $.fancybox({
          'modal'         : true,
          'href'          : url,
          'type'          : 'iframe',
          'title'         : title,
          'titlePosition' : 'inside',
          'titleFormat'   : format_fbTitle,
          'width'         : 700,
          'height'        : 580,
          'margin'        : 10
          });

        });

//richiesta associazione

    $('a.utils-req-associazione').live('click', function(e) {
        
        e.preventDefault();
        
        var randomnumber=Math.random();
        
        var url   = $(this).attr("href")+"?r="+randomnumber;
        var title = $(this).attr("title");
        
        $.fancybox({
          'modal'         : true,
          'href'          : url,
          'type'          : 'iframe',
          'title'         : title,
          'titlePosition' : 'inside',
          'titleFormat'   : format_fbTitle,
          'width'         : 700,
          'height'        : 580,
          'margin'        : 10
          });

        });

//richiesta associazione - <select> provincia change

    $('#provincia').change(function() {
      
      $('.comContainer').remove();
      var value = $(this).val();
      
      if ( value != 'ZZ' ) {
        
        //i comuni della provincia selezionata
        
        $.get('utils/get-comuni', { prov: value }, function(data) {
          
          var comContainer = '<div class="comContainer"><label for="codistat"> Comune: </label><select id="codistat" name="codistat"><option value="ZZ" >Scegli un comune</option>'+data+'</select></div>';
          $('#provincia').parent('div').after(comContainer);
          });
        
        }
      
      });
	  
//richiesta abbonamento - <select> tipologia change

    $('#tipologia_id').change(function() {
      
      $('.tipContainer').remove();
      var value = $(this).val();
      
      if ( value != 'ZZ' ) {
        
        //tipologia Concessionario (solo se Tipologia Id == Concessionario)
        
        $.get('utils/get-tipologia-concessionario', { tipologia_id: value }, function(data) {
          
          //lo snippet Modx ritorna "nc" se Tipologia != Concessionario
          if (data != 'nc') {
            var tipContainer = '<div class="tipContainer"><label for="marca_conce"> Marche Rappresentate: </label><select id="marca_concessionario" name="marca_concessionario[]" multiple="multiple" size=5>'+data+'</select></div>';
            $('#tipologia_id').parent('div').after(tipContainer);
            }
          });
        
        }
      
      });

//richiesta associazione - <select> macrocategoria change

    $('#macrocat_id').change(function() {
      
      $('.ocContainer').remove();
      var value = $(this).val();
      
      if ( value != 'ZZ' ) {
        
        //tipologie Operatore Commerciale (solo se Macrocat Id == Operatore Comm.le)
        
        $.get('utils/get-tipologia-oc', { macrocat_id: value }, function(data) {
          
          //lo snippet Modx ritorna "nc" se Macrocat != Operatore Comm.le
          if ( data != "nc" ) {
            
            var ocContainer = '<div class="ocContainer"><label for="tipo_oc"> Tipo OC: </label><select id="tipo_oc" name="tipo_oc"><option value="ZZ" >Scegli il tipo di operatore</option>'+data+'</select></div>';
            $('#macrocat_id').parent('div').after(ocContainer);
            }
          });
        
        }
      
      });

//richiesta associazione - impongo utente = email

    $('#email').blur(function() {
      
      var emailValue = $(this).attr("value");
      
      if (emailValue!="") $('#utente').attr("value",emailValue);
      });

    });

