$(document).ready(function() {


    //#######################################################################
    //SEARCH MENU UTILITY
    $('#cerce-nel-sito').click(function(){
        $('#cerce-nel-sito').hide();
        $('#seach_text').fadeIn(500);
        $('#seach_text').focus();
    });
    $('#seach_text').focusout(function(){
        $('#seach_text').hide();
        $('#cerce-nel-sito').fadeIn(500);
    });

    
    
    //#######################################################################
    // SLIDE IMMAGINI
    //Show the paging and activate its first link
        $(".paging").show();
        $(".paging a:first").addClass("active");
        
          ChangeDescription = function(CurrSlide){
            
            var Title = $('#img-'+CurrSlide).attr('data-title');
            var Descr = $('#img-'+CurrSlide).attr('data-text');
                $('#slide-info h1').empty();
                $('#slide-info p').empty();
                $('#slide-info h1').html(Title);
                $('#slide-info p').html(Descr);
                return false;
        };
        
        ChangeDescription(1);
          
//        $('#slide-info').empty();
//        $('#slide-info').append('<h1>'+Title+'<h1>');
//        $('#slide-info').append('<p>'+Descr+'</p>');
//                
        //Get size of the image, how many images there are, then determin the size of the image reel.
        var imageWidth = $(".window").width();
        var imageSum = $(".image_reel img").size();
        var imageReelWidth = imageWidth * imageSum;

        //Adjust the image reel to its new size
        $(".image_reel").css({'width' : imageReelWidth});
        
        //Paging  and Slider Function
        rotate = function(){
            var triggerID = $active.attr("data-rel") - 1; //Get number of times to slide
            var image_reelPosition = triggerID * imageWidth; //Determines the distance the image reel needs to slide

            $(".paging a").removeClass('active'); //Remove all active class
            $active.addClass('active'); //Add active class (the $active is declared in the rotateSwitch function)

            //Slider Animation
            $(".image_reel").animate({
                left: -image_reelPosition
            }, 500, function(){
                var CurrSlide = $active.attr("data-rel");
                ChangeDescription(CurrSlide);
            });
            
            

        }; 

        //Rotation  and Timing Event
        rotateSwitch = function(){
            play = setInterval(function(){ //Set timer - this will repeat itself every 7 seconds
                $active = $('.paging a.active').next(); //Move to the next paging
                if ( $active.length === 0) { //If paging reaches the end...
                    $active = $('.paging a:first'); //go back to first
                }
                rotate(); //Trigger the paging and slider function
            }, 7000); //Timer speed in milliseconds (7 seconds)
        };

        rotateSwitch(); //Run function on launch
        
        //On Hover
        $(".image_reel a").hover(function() {
            clearInterval(play); //Stop the rotation
        }, function() {
            rotateSwitch(); //Resume rotation timer
        });	

        //On Click
        $(".paging a").click(function() {
            $active = $(this); //Activate the clicked paging
            //Reset Timer
            clearInterval(play); //Stop the rotation
            rotate(); //Trigger rotation immediately
            rotateSwitch(); // Resume rotation timer
            return false; //Prevent browser jump to link anchor
        });
        
        
        
       
       //#######################################################################
       // DATEPICKER
       
        // Use a custom theme named android
        $("#date2").glDatePicker(
        {
            cssName: "android"
        });

       
       //#######################################################################
       // SOTTOFAMIGLIA
       $('.FamilyLink').click(function(){
           
           $('.SottoFamigliaProd').hide();
           $('.SottoFamiglia h1').removeClass('active');
           
           var FamilyNum = $(this).attr('data-family');
           var position = $('#SottoFamiglia'+FamilyNum).position();
           //alert("Top: "+position.top+" - Left: "+position.left);
           
           $('#SottoFamigliaProd'+FamilyNum).css('top',position.top+36+'px');
           $('#SottoFamigliaProd'+FamilyNum).css('left','0px');
           
//           if(position.left > 350){
//               $('#SottoFamigliaProd'+FamilyNum).css('left','-187px');
//           }
//          
//           if(position.left > 500){
//               $('#SottoFamigliaProd'+FamilyNum).css('left','-374px');
//           }
//           
//           if(position.left > 700){
//               $('#SottoFamigliaProd'+FamilyNum).css('left','-561px');
//           }
           
           $('#SottoFamiglia'+FamilyNum+' h1').addClass('active');
           $('#SottoFamigliaProd'+FamilyNum).fadeIn(500);
           
       });
       
       
       // SOTTOFAMIGLIA CHIUDI PANNELLO
       $('.SottoFamigliaProd .ClosePanel').click(function(){
           $('.SottoFamigliaProd').hide();
           $('.SottoFamiglia h1').removeClass('active');
       });
       
       
       //#######################################################################
       // RED BOX CONTACT FORM PRIVACY CHECKBOX
       $('#ContactRedBoxForm .checkbox').click(function(){
           $('#ContactRedBoxForm .checkbox').removeClass('active');
           $(this).addClass('active');
           var ValoreCheck = $(this).attr('data-checkvalue');
           
           if(ValoreCheck == 1){
            $('input[name=privacy]').attr('checked', true);
           }else{
               $('input[name=privacy]').attr('checked', false);
           }
       });
       
       
       
       
       //#######################################################################
       // SCHEDA TECNICA PRODOTTO
//       $('.SchedaTecnicaContent').hide();
//       var DefaultActive = $('#SchedaTecnicaMenu li.active').attr('data-scheda');
//       $('#'+DefaultActive).fadeIn(1000);
//       
//       
//       $('#SchedaTecnicaMenu li').click(function(){
//           
//           $('#SchedaTecnicaMenu li').removeClass('active');
//           $('.SchedaTecnicaContent').hide();
//           $(this).addClass('active');
//           var SelectedValue = $(this).attr('data-scheda');
//           $('#'+SelectedValue).fadeIn(500);
//       });
       
       
       
       //#######################################################################
       // GALLERY SEARCH
       $('#GallerySearchContainer :radio').attr('checked',false);
       
       $('.RadioGallery').click(function(){
          var RadioName = $(this).attr('data-id-domanda') ;
          var RadioValue = $(this).attr('data-risposta') ;
         
          $('.RadioGallery[data-id-domanda='+RadioName+']').removeClass('active');
          $(this).addClass('active');
         
//         alert(RadioName +' - '+RadioValue);
          $(':radio[name='+RadioName+'][value='+RadioValue+']').attr('checked',true);
       });
       
       
       //#######################################################################
       // GALLERY SEARCH AJAX CALL ON SUBMIT
       
       $('#GallerySearch').click(function(){
          
        var classe_impianto = $("input[name='classe_impianto']:checked").val();
        var tipologia_edificio = $("input[name='tipologia_edificio']:checked").val();
        var bisogno = $("input[name='bisogno']:checked").val();
        var dimensionamento = $("input[name='dimensionamento']:checked").val();
        var fonte_energetica = $("input[name='fonte_energetica']:checked").val();
        var dove = $("input[name='dove']:checked").val();
        
        $.ajax({
			type : 'POST',
			url : 'AjaxGallerySearch.php',
			dataType : 'html',
			cache: false,
			data: {
				classe_impianto : classe_impianto,
				tipologia_edificio : tipologia_edificio,
                                bisogno : bisogno,
                                dimensionamento : dimensionamento,
                                fonte_energetica : fonte_energetica,
                                dove : dove

			},
			success : function(Html_result){
//                            alert("SUCCESSO");
				$('#AjaxContent').html(Html_result);
                                $('#GallerySearchResults').fadeIn(300);
				delete Html_result;
			},
			error : function(XMLHttpRequest, textStatus, errorThrown) {
//                            alert("ERRORE");
				delete Html_result;
			}
		});

		return false;
          
       });
       
       
       //#######################################################################
       // GALLERY SEARCH RESULT CLOSE PANEL
       $('#GallerySearchResults .ClosePanel').click(function(){
           $('#GallerySearchResults').hide();
       });
       
       
      
       //#######################################################################
    // SLIDE IMMAGINI
    //Show the paging and activate its first link
        $(".ref_paging").show();
        $(".ref_paging a:first").addClass("active");
        
                       
        //Get size of the image, how many images there are, then determin the size of the image reel.
        var imageWidthRef = $(".ref_window").width();
        var imageSumRef = $(".RefContainer").size();
        var imageReelWidthRef = imageWidthRef * imageSumRef;

        //Adjust the image reel to its new size
        $(".ref_image_reel").css({'width' : imageReelWidthRef});
        
        //Paging  and Slider Function
        rotateReference = function(){
            var triggerID = $active.attr("data-rel") - 1; //Get number of times to slide
            var image_reelPosition = triggerID * imageWidthRef; //Determines the distance the image reel needs to slide

            $(".ref_paging a").removeClass('active'); //Remove all active class
            $active.addClass('active'); //Add active class (the $active is declared in the rotateSwitch function)

            //Slider Animation
            $(".ref_image_reel").animate({
                left: -image_reelPosition
            }, 500, function(){
                var CurrSlide = $active.attr("data-rel");
            });
            
            

        }; 

        //On Click
        $(".ref_paging a").click(function() {
            $active = $(this); //Activate the clicked paging
            //Reset Timer
//            clearInterval(play); //Stop the rotation
            rotateReference(); //Trigger rotation immediately
//            rotateSwitchReference(); // Resume rotation timer
            return false; //Prevent browser jump to link anchor
        });
        
       
       //#######################################################################
       // GESTIONE CONTENUTI REFERENZA / VIDEO 360°
       $('.Video360, .Video').hide();
       
       $('.RedBoxReferenzeBox').click(function(){
          var idContainer = $(this).attr('data-id-content');
          
          $('.RedBoxReferenzeBox').removeClass('active');
          $(this).addClass('active');
          
          $('.Video360, .Video, .AbstractReferenza').hide();
          $('#'+idContainer).fadeIn(500);
          
       });
       
       
});
