$('.item').live('mouseover',function(){
  if ($(this).find('.desc2').size()>0){
     $(this).find('.info').stop().animate({height: '90px'},'fast');
  }
});

$('.item').live('mouseout',function(){
    if ($(this).find('.desc2').size()>0){
     $(this).find('.info').stop().animate({height: '45px'}, 'fast');
    }
});

$('.cambio-mese').live("click",function(){
   var valore = $(this).attr('rel');
   $.get('/inc-calendar.php',{'t':valore},function(msg){
       $('.slider-holder').html(msg);
   });
});

function genera_contenuto_home(created_at,c){
            
            var w=($(window).height()/2);
            
            $('#loading').css('top',($(document).scrollTop()+w)+'px');
            
            $('#loading').show();
            
           $.ajax({
              
               url: '/sfoglia/'+created_at+'/'+c+'/milano/',
              
               async: false,
              
               data: {},
               
               type: 'GET',
               
               cache: false,
               
               success: function(msg){
                
                   
                   $('#home-box').html(msg);
                        
                        $('#home-box').masonry({
                   
                            itemSelector : '.item',
                            
                            columnWidth: 190,
                            
                            isFitWidth:true,
                            
                            isAnimated: false,
                            
                            animationOptions: {
                                
                                 complete: function() {
                                                    
                                                    
                                                    
                                                }
                                        }
                          });
                          
                          
                          $('#home-box').masonry( 'reload' ); 
                          
                          $('#loading').hide();
                        
                          $('.box-img').each(function(){

                                $(this).load(function(){
                                    
                                    $(this).prev().hide();
                                    
                                    $(this).show();
                                    
                                })

                          });
                        
                          $('body').data('created_at',created_at)
                        
                          $('body').data('c',c);

               }
              
            });
}
function genera_contenuto(created_at,c){
            
            var w=($(window).height()/2);
    
            $('.agenda-loader').show();
            
           $.ajax({
              
               url: '/sfoglia/'+created_at+'/'+c+'/milano/',
              
               async: true,
              
               data: {},
               
               type: 'GET',
               
               cache: false,
               
               success: function(msg){
                
                   
                   $('.contenuti-interni-agenda').html(msg);
                        
                    
                          $.get('/inc-giorno-mese.php',{'created_at':created_at},function(data){
                              $('#giorno-mese').html(data);
                          })
                          
                          $.get('/inc-calendar.php',{'created_at':created_at},function(data){
                              $('#agenda-calendario').html(data);
                          })
                    
                           $('.agenda-loader').hide();
                           
                       
                        
                          $('body').data('created_at',created_at)
                        
                          $('body').data('c',c);
                          
                          refreshDataNavigation();  
                          
                        
               }
              
            });

}

$('.filtra-home').live("click",function(e){
    
   e.preventDefault();
   
   var valore=$(this).attr('rel');
   
   jQuery('input[name="h_subsubcategoria"]').val(valore);

   var cat= jQuery('input[name="h_categoria"]').val();
   
   var subcat = jQuery('input[name="h_subcategoria"]').val();
   
   var subsubcat = jQuery('input[name="h_subsubcategoria"]').val();
   
   $.get('/process-filtra-home.php',{'sid':valore},function(msg){
       
             $('#contenuti').html(msg);
             
             refreshMappaHome(cat,subcat,subsubcat);
             
   })
   
});


$('.filtra-dati').live("click",function(e){
    
    e.preventDefault();
    
    
    
    var valore = $(this).attr('rel');
    
    if (valore==0){
        
        $('.item').show();
        
    }else{
    
        $('.item[rel="subcat_'+valore+'"]').show();
        $('.item[rel!="subcat_'+valore+'"]').hide();
    
    }
    
    $('.subcategory a').removeClass('selezionato');
    
    $(this).addClass('selezionato');
    
 
});



$('.filtra-macro').live("click",function(e){
    
      e.preventDefault();
    
    
    
    var valore = $(this).attr('rel');
    
    if (valore==0){
        
        $('.item').show();
        
    }else{
    
        $('.item[rel="subcat_'+valore+'"]').show();
        $('.item[rel!="subcat_'+valore+'"]').hide();
    
    }
    
    $('.subcategory a').removeClass('selezionato');
    
    $(this).addClass('selezionato');
    
   /* e.preventDefault();
   
   var valore=$(this).attr('rel');
   
   jQuery('input[name="h_subsubcategoria"]').val(valore);

   var cat= jQuery('input[name="h_categoria"]').val();
   
   var subcat = jQuery('input[name="h_subcategoria"]').val();
   
   var subsubcat = jQuery('input[name="h_subsubcategoria"]').val();
   
   $.get('/contenuti-home.php',{'sid':valore,'cat':cat,'subcat':subcat},function(msg){
       
           
            $('#contenuti').html(msg);
            
            refreshMappaHome(cat,subcat,subsubcat);
                   
       
   })
   */
});

$('.filtra-agenda').live("click",function(e){
    
   e.preventDefault();
   
   var valore=$(this).attr('rel');
   var created_at = $('body').data('created_at');
   var c = $('body').data('c');
      
           $.ajax({
              
               url: '/sfoglia/'+created_at+'/'+c+'/milano/',
              
               async: false,
              
               data: {'sid':valore},
               
               type: 'GET',
               
               cache: false,
               
               success: function(msg){
                
                   
                   $('.contenuti-interni-agenda').html(msg);     
                          
                          $('#loading').hide();
   
               }
              
            });
   
});


function genera_sub_contenuto(sub){
            
            var created_at = $('body').data('created_at')
                        
            var c = $('body').data('c');
            
            var w=($(window).height()/2);
            
            $('#loading').css('top',($(document).scrollTop()+w)+'px');
            
            $('#loading').show();
            
            if (sub>0){
            
                var url='/filtro/'+created_at+'/'+c+'/'+sub+'/';
                       
            }else{
                
                genera_contenuto(created_at,c);
                
                return;
            }
            
       
            
            $.get(url, '',function(msg){

                        $('#home-box').html(msg);
                        
                        $('#home-box').masonry({
                   
                            itemSelector : '.item',
                            
                            columnWidth: 190,
                            
                            isFitWidth:true,
                            
                            isAnimated: false,
                            
                            animationOptions: {
                                
                                 complete: function() {
                                                    
                                                    
                                                    
                                                }
                                        }
                          });
                        
                          $('#home-box').masonry( 'reload' ); 
                     
                          
                          $('#loading').hide();
  
                        
            }); 

}


function refreshDataNavigation(){
    
    var c = $('body').data('c');
    
    var created_at = $('body').data('created_at');
    
    $.get('/refresh-navigation.php',{'created_at':created_at,'c':c},function(msg){
        
        $('#data-info').html(msg);
        
    });
    
}

function refreshSubmenu(){
    
    var c = $('body').data('c');
    
    var created_at = $('body').data('created_at');
    
    $.get('/refresh-submenu.php',{'created_at':created_at,'c':c},function(msg){
        
        $('#submenu').html(msg);
        
    });
    
  
}


$('.sub-no-selected').live("click",function(e){

        e.preventDefault(e);
        
        var valore=$(this).attr('rel').split('_');
        
        $('#submenu a').each(function(){
            $(this).removeClass('sub-selected');
            $(this).addClass('sub-no-selected');
        })
        
        $(this).removeClass('sub-no-selected').addClass('sub-selected');   
        
        genera_sub_contenuto(valore[1]);
   
});

$('.sub-no-selected-scroller').live("click",function(e){
         $('html,body').animate({scrollTop: 500+'px'}, 1000,function(){
               
               
           });
        e.preventDefault(e);
        
        var valore=$(this).attr('rel').split('_');
        
        $('#submenu-scroller a').each(function(){
            $(this).removeClass('sub-selected-scroller');
            $(this).addClass('sub-no-selected-scroller');
        })
        
        $(this).removeClass('sub-no-selected-scroller').addClass('sub-selected-scroller');   
        
        genera_sub_contenuto(valore[1]);
   
});


$('.box-link').live("click", function(e){
    
    
    e.stopPropagation();
    
    
    
    var at=$(this).attr('rel');
    
    if (at=='magazine'){
     
        $('.sfogliabile-catalogo').trigger('click');
     
    }else{
    
        document.location.href=at;
   
    }
   
});

    
    $('.share-over').live('mouseover',function(){
            
        $(this).children().css('opacity','1');
        
    });
    
    $('.share-over').live('mouseout',function(){
        
       $(this).children().css('opacity','0.5');
        
    })
    
    $('.share-over').live('click',function(e){
        
        e.stopPropagation();
        
        var link=$(this).attr('rel');
        
        window.open(link);
        
    })

$('.right-m-agenda, .left-m-agenda ,.right-d-agenda, .left-d-agenda, .agenda-full-date').live("click",function(e){
   
    
    e.preventDefault();
   
   var c = $('body').data('c');
 
   var r = $(this).attr('rel');
   
   genera_contenuto(r, c);
   
   if ($(this).hasClass('agenda-full-date')){
        
        $('#agenda-lista-giorni a').each(function(){
            $(this).removeClass('agenda-full-date-selected');
            $(this).addClass('agenda-full-date');
        })
        
        $(this).removeClass('agenda-full-date');   
        $(this).addClass('agenda-full-date-selected');
   }
   
   if ($(this).hasClass('left-m-agenda')||$(this).hasClass('right-m-agenda')||$(this).hasClass('left-d-agenda')||$(this).hasClass('right-d-agenda')){
    
    
       var obj = $('#agenda-lista-giorni div a[rel="'+r+'"]');
        
        
        $('#agenda-lista-giorni div a').each(function(){
            $(this).removeClass('agenda-full-date-selected');
            $(this).addClass('agenda-full-date');
        })
        
        if (obj.size()>0){
        
                obj.removeClass('agenda-full-date').addClass('agenda-full-date-selected');   
       
        }
        
   }
   
   if ($('#mappa').size()>0){
      
       refreshMappa(r,c);
       
   }
   
});


function refreshMappa(r,c){
     
      $.ajax({
              
              url: "/gmap-home-logic.php",
              
              async: false,
              
              data: {'created_at':r, 'categoria':c},
              
              cache: false,
              
              success: function(msg){
                
                $('#gmap').gmap3({action:'clear'})
                $("#mappa").append(msg);
                
              }
              
            });   
    
}


function refreshMappaHome(cat,subcat,sid){
     
     if ($('#mappa').size()>0){
         
         $.ajax({
              
              url: "/gmap-home.php",
              
              async: false,
              
              data: {'cat':cat,'subcat':subcat,'sid':sid},
              
              type: "POST",
              
              cache: false,
              
              success: function(msg){
                
                $('#gmap').gmap3({action:'clear'});
                $("#mappa").append(msg);
                
              }
              
            }); 
         
         
     }

}

 $('.left-date, .right-date').live("mouseover",function(){
        
        $(this).addClass('date-over');
        
    });
    
    $('.left-date, .right-date').live("mouseout",function(){
        
        $(this).removeClass('date-over');
        
    });

 var total_menu_height=0;
 var tag_height=0;

$('.tool-cerca').live("click",function(e){
    
    e.preventDefault();
    
    $('.cerca-holder').slideToggle(500);
    
})

$('.cosa-cerchi-link').live('click',function(){
    
    if ($('.tag-content').is(':visible')){
        
        $('.tag-content').slideUp(function(){
            $('.scroller_menu_new').height(total_menu_height+10+'px');
        });
        
    }else{
         $('.scroller_menu_new').height(total_menu_height+tag_height+10+'px');
         $('.tag-content').slideDown(function(){
            
        });
       
    }

});

function genera_oggi(){
    
    $.get('/inc-oggi.php',function(msg){
       
       $('.today-side-holder').html(msg);
        $('.oggi-loader img').hide()
    });
    
}

function genera_top10(){
    
    var h_cat = $('input[name="h_categoria"]').val();
    
  
    
    $.get('/inc-top10.php',{'c':h_cat},function(msg){
        
        $('.today-side-holder').html(msg);
        
        $('.oggi-loader img').hide();
        
    })
    
    
}


$(document).ready(function(){
    
    $('.cerca-sender').bind("click",function(){
            
            $('#cerca-form').submit();
        
    })
        
    
    
    genera_oggi();
    
    $('.today-voce').bind("click",function(){
       
       var valore = $(this).attr('rel');
       $('.oggi-loader img').show()
       switch(valore){
           case '1':
              
               
               $('li[rel="lista2"]').removeClass('today-current');
               $('.voce2').hide();
               
               
               $('li[rel="lista1"]').addClass('today-current');
               $('.voce1').show();
               
               genera_oggi();
              
           break;
           case '2':
              
              
               $('li[rel="lista1"]').removeClass('today-current');
               $('.voce1').hide();
               
               
               $('li[rel="lista2"]').addClass('today-current');
               $('.voce2').show();
               
               genera_top10();
               
           break;    
       }
       
    });
    
    $('#reg-commento').bind("click",function(e){
       
       $('.commento-loader').show();
       
       e.preventDefault();
       
       var stringa = $('#reg-form').serialize();
       
       $.post('/process-commento.php',stringa,function(msg){
          
          $('#stato-messaggio').html(msg).slideDown();
          
          $('.commento-loader').hide();
       });
       
    });
    
    
    $('.lascia-commento').bind("click", function(e){
       
       e.preventDefault();
       
       $('.commento-box').toggle(400);

       
    });
    
    
    $('#newsletter-registra').bind("click",function(){
        
        var l = $('<img>');
        
        l.attr('src','/images/agenda-loader.gif');
        
        $('.newsletter-feedback').html(l);
        
        $('#newsletter')
        
        var valore= $('#newsletter').val();
        $.ajax({
                                   type: "POST",
                                   url: "/inc-newsletter-register.php",
                                   async: true,
                                   data: {'h':valore},
                                   dataType: "json",
                                   success: function(msg){
                                        
                                        $('.newsletter-feedback').html(msg.messaggio);
                                        
                                   }
                                 });
        
       
    });
    
    

   
   
   $('.recover-button').bind("click",function(e){
       
       $('.reg-loader').show();
       
       e.preventDefault();
       
      var valore = $('#forgot').val();
      
      var stringa = $('#reg-form').serialize();
      
      $.post('/process-rec-psw.php',stringa,function(msg){
         
         $('.reg-loader').hide();
         
         $('.status-msg-error2').html(msg);
         
      });
      
   });
   
   $('.reg-button').bind("click",function(e){
       document.location.href='/registrazione/#modulo'
   });
    

   
   
   $('.profile-button').bind("click",function(e){
     
     e.preventDefault();
     
     var top=$('.scroller_menu_new').position().top+33;
     
     $('.activate-container').hide();

     $('.profile-container').toggle();
     
   });
   
   $('.registrazione').bind("click",function(e){
       
       e.preventDefault();
       
       var stringa = $('#reg-form').serialize();
       
       $('.reg-loader').show();
       
       $.post('/process-registrati.php',stringa,function(msg){
           
           $('.status-msg-error2').html(msg);
           
           $('.reg-loader').hide();
           
       })
       
   })
   
   $('.edit-profile').bind("click",function(e){
       
       e.preventDefault();
       
       var stringa = $('#reg-form').serialize();
       $('.reg-loader').show();
       
       
       $.post('/process-edit-profile.php',stringa,function(msg){
           
           $('.status-msg-error2').html(msg);
           
           $('.reg-loader').hide();
           
       })
       
   })
    
    
   total_menu_height=$('.scroller_menu_new').height()+20;
      
   tag_height=$('.tag-content').height()+25;
     
   $('.login-button').bind("click",function(e){
            
            $('.reg-loader').show();
            
           e.preventDefault();
           
           var stringa = $('#reg-form').serialize();
           
           $.post('/process-login.php',stringa,function(msg){
              
            if (msg==1){
                
                $('.status-msg-error2').html('Email o passoword errati!');
                
                $('.reg-loader').hide();
            }else{
                
                document.location.href='/wp-log.php';
                
            }
               
           });
      
   })
   
   $('.view-login').bind("click",function(e){
       e.preventDefault();
       $('.login-div').toggle(500);
   })


 
   $(window).bind("scroll",function(){
       
    
       var top=$('.scroller_menu_new').position().top+33;
     
 
   });
    
    
        
   $('#send-button').bind("click",function(e){
       
       e.preventDefault();
       
       var dati = $('#contatti').serialize();
       
       $.ajax({
              
               url: "/process-contatti.php",
              
               async: false,
              
               data: dati,
               
               type: 'POST',
               
               cache: false,
               
               success: function(msg){
                
                   $('#dialog-con').html(msg);
                   
               }
              
            });
       
   });
    

    
      $('.tool-maps-catalogo').bind("click",function(e){
       
        e.preventDefault();
        
        if ($('#spazio-condiviso-catalogo').size()>0){
            
            if ($('#mappa').size()>0){
                
                $('#mappa').remove();
            }else{
            
            
            $('#loading').show();
            
            var cat= jQuery('input[name="h_categoria"]').val();
             
            var subcat = jQuery('input[name="h_subcategoria"]').val();
      
            $('#contenuti-condivisi').html(''); 
            
            $('html,body').animate({scrollTop: '300px'}, 1000,function(){
                
                var mappa=$('<div id="mappa"><div id="gmap"></div></div>');
                
                    $('#contenuti-condivisi').html(mappa);
                     
                    if ($('#ag-box').size()>0){
                     
                       var created_at = $('body').data('created_at');
                        
                       var categoria = $('body').data('c');       

                       $.ajax({

                          url: "/gmap-home-logic.php",

                          async: false,

                          data: {'created_at':created_at, 'categoria':categoria},

                          cache: false,

                          success: function(html){

                             $("#mappa").append(html);

                              $('#loading').hide();




                          }

                        }); 
                     
            }else{
                         $.ajax({

                                  url: "/gmap-home.php",

                                  async: false,

                                  type: "POST",

                                  data: {'cat':cat,'subcat':subcat},

                                  cache: false,

                                  success: function(html){

                                    $("#mappa").append(html);

                                    $('#loading').hide();

                                  }

                            }); 
             
                }
            });
            
            } 
            
        }else if($('#spazio-condiviso-ricerca').size()>0){
            
            
            if ($('#mappa').size()>0){
                
                $('#mappa').remove();
            }else{
            
            
            
             $('#loading').show();
            
            $('#contenuti-condivisi').html(''); 
            
            $('html,body').animate({scrollTop: '300px'}, 1000,function(){
                
                var mappa=$('<div id="mappa"><div id="gmap"></div></div>');
                
                     $('#contenuti-condivisi').html(mappa);
                     
                     $.ajax({

                              url: "/gmap-home-ricerca.php",

                              async: false,
                              
                              type: "POST",
                              
                              data: {},

                              cache: false,

                              success: function(html){

                                 $("#mappa").append(html);
                                
                                 $('#loading').hide();
                                
                              }

                        }); 
             
                
            });
            
            }
        }else if($('#spazio-condiviso-tag').size()>0){
             if ($('#mappa').size()>0){
                
                $('#mappa').remove();
            }else{
                
                var valore= $('#h_tag').val();
                
             $('#loading').show();
            
            $('#contenuti-condivisi').html(''); 
            
            $('html,body').animate({scrollTop: '300px'}, 1000,function(){
                
                var mappa=$('<div id="mappa"><div id="gmap"></div></div>');
                
                     $('#contenuti-condivisi').html(mappa);
                     
                     $.ajax({

                              url: "/gmap-home-tag.php",

                              async: false,
                              
                              type: "POST",
                              
                              data: {'tag_id':valore},

                              cache: false,

                              success: function(html){

                                 $("#mappa").append(html);
                                
                                 $('#loading').hide();
                                
                              }

                        }); 
             
                
            });
                
                
            }
            
        }else{    
            document.location.href='/mappa-on.php';
        }
        
   
    });

        var nome=$('#nome').val();
        var cognome=$('#cognome').val();
        var telefono=$('#telefono').val();
        var email=$('#email').val();
        var email2=$('#email2').val();
        var sesso=$('#sesso').val();
        var newsletter=$('#newsletter').val();
 

    $('.info-aggiuntive h4').bind("click",function(){
        $(this).blur();
        $('.info-aggiuntive-content').toggle(500);
        if ($(this).hasClass('info-aggiuntive-bg-down')){
            $(this).removeClass('info-aggiuntive-bg-down');
        }else{
            $(this).addClass('info-aggiuntive-bg-down');
        }
    });



    $('a').bind("click",function(e){
        $(this).blur();
    });


  

    $('#language').bind("click",function(e){

        $('#lingua-select').toggle(200);

    });

    $('#navigation-menu').bind("click",function(e){
        e.preventDefault();
        $('#navigation-select').toggle(200);

    });

    $('#navigation-select').bind('mouseleave',function(e){

                $('#navigation-menu').trigger('click');

    });

 
     
     
   
     $('.calendario-agenda').live("click",function(e){
         e.preventDefault();
         $('#calendario-logic-scroller').trigger("click");
     }); 
     
  
     
 

   
     
     $('#cerca, #cerca_scroller').bind('focus',function(e){

        var valore = $(this).val();

        if (valore=='Cerca in MyMI'){
            $(this).val('');
        }

    });
    
    $('.link-calendario-logic').bind("click",function(e){
       
        $('#calendario-logic').trigger('click');

    })
    
  
  $('.link-calendario-logic-scroller').bind("click",function(e){
       
        $('#calendario-logic-scroller').trigger('click');

    })
    $('.link-calendario').bind("click",function(e){
       
        $('#calendario').trigger('click');

    });

    $('#cerca, #cerca_scroller').bind('blur',function(e){

        var valore = $(this).val();

        if (valore==''){
            $(this).val('Cerca in MyMI');
        }

    });

    $('#nome').bind("focus",function(){

        if ($(this).val()==nome){
            $(this).val('');
        }
        
    });

    $('#nome').bind("blur",function(){

        if ($(this).val()==''){
            $(this).val(nome);
        }

    });

    

    $('#cognome').bind("focus",function(){

        if ($(this).val()==cognome){
            $(this).val('');
        }

    });

    $('#cognome').bind("blur",function(){

        if ($(this).val()==''){
            $(this).val(cognome);
        }

    });

     $('#sesso').bind("focus",function(){

        if ($(this).val()==sesso){
            $(this).val('');
        }

    });

    $('#sesso').bind("blur",function(){

        if ($(this).val()==''){
            $(this).val(sesso);
        }

    });

    $('#telefono').bind("focus",function(){

        if ($(this).val()==telefono){
            $(this).val('');
        }

    });

    $('#telefono').bind("blur",function(){

        if ($(this).val()==''){
            $(this).val(telefono);
        }

    });

    $('#email').bind("focus",function(){

        if ($(this).val()==email){
            $(this).val('');
        }

    });

    $('#email').bind("blur",function(){

        if ($(this).val()==''){
            $(this).val(email);
        }

    });
    
     $('#email2').bind("focus",function(){

        if ($(this).val()==email2){
            $(this).val('');
        }

    });

    $('#email2').bind("blur",function(){

        if ($(this).val()==''){
            $(this).val(email2);
        }

    });


    $('.italiano').bind('click',function (e){

        document.location.href='set-lang.php?l=_it';

    });

    $('.inglese').bind('click',function (e){

        document.location.href='set-lang.php?l=_en';

    });

    $('#username2').bind('focus',function(e){

        var valore = $(this).val();

        if (valore=='e-mail'){
            $(this).val('');
        }

    });

    $('#username2').bind('blur',function(e){

        var valore = $(this).val();

        if (valore==''){
            $(this).val('e-mail');
        }

    });

    $('#psw2').bind('focus',function(e){

        var valore = $(this).val();

        if (valore=='********'){
            $(this).val('');
        }

    });
    
    $('#newsletter').bind('focus',function(e){

        var valore = $(this).val();

        if (valore==newsletter){
            $(this).val('');
        }

    });
    
    $('#newsletter').bind('blur',function(e){

        var valore = $(this).val();

        if (valore==''){
            $(this).val(newsletter);
        }

    });

    $('#psw2').bind('blur',function(e){

        var valore = $(this).val();

        if (valore==''){
            $(this).val('********');
        }

    });

    $('#psw3').bind('focus',function(e){

        var valore = $(this).val();

        if (valore=='********'){
            $(this).val('');
        }

    });

    $('#psw3').bind('blur',function(e){

        var valore = $(this).val();

        if (valore==''){
            $(this).val('********');
        }

    });


    if ($('.contenuti-interni').size()>0){
      var cat= jQuery('input[name="h_categoria"]').val();
      var subcat = jQuery('input[name="h_subcategoria"]').val();
      
  
      
      
      jQuery.ajax({
       url: '/home/1/',
         type: 'get',
         data: {'cat':cat,'subcat':subcat},
           success: function(data) {
               jQuery('.contenuti-interni').html(data);
            }
         });

    }

});
