﻿jQuery(function( $ ){

// var footerheight = $('#FooterMaster').css("height");
// var footer2height = $('#FooterMaster2').css("height");
// var headerheight = $('#HeaderMaster').css("height"); 
// var FixedDivs = footerheight+footer2height+headerheight;

    var FixedDivs = 25+25+50; 
	var isResizing;
	var isResizingCollectionsx;
	var CollectionElemtHeight;
	var isswfhome = false;
	
	//*************
    //DOM ready
    //*************
    $(document).ready(function(){
    
    
            var uri = document.location.href;
            //alert (uri);
            
            
            //alert (uri.toLowerCase().indexOf("www.natuzzi.es/#/dealers"));
            if (uri.toLowerCase().indexOf('www.natuzzi.es/#/dealers')>-1)
            {
                location.replace('http://www.natuzzi.es/4/17/puntos-de-venta/173/es-es/');
            }
            
            if (uri.toLowerCase().indexOf('www.natuzzi.com.au/#/dealers_new')>-1)
            {
                location.replace('http://www.natuzzi.com.au/4/17/dealer-locator/54/en-au/');
            }
            
            
    
            var offsetY = 22;
            var hLang = CalcHeightLangs();
            var wLang= $('#languages').width();
            $('#languages').click(
               function () {
                  if ($('#down-list').is(":visible"))
                  {
                    $('#down-list').hide(100);  
                  }
                  else
                  {
                    var posLang = $('#languages').position();
                    $('#down-list').css({'width':+wLang+'px','height':+hLang+'px','left':posLang.left+'px','top':(posLang.top+offsetY)+'px'});
                    $('#down-list').show(100);
                  }
               }
            );


            $(".tofriend").colorbox({iframe:true, innerWidth:260, innerHeight:200});
            $(".toaddmylist").colorbox({iframe:true, innerWidth:260, innerHeight:200});
            $("#txt_search").alphanumeric({allow:" "});            
            

    }); 
    //*************
    //WIN ready, also IMAGES...
    //*************
    $(window).load(function(){
          var minWidthBlock =  CalcMinWidth();
          var ContentH = $(window).height()- FixedDivs;
          $('#ContentMaster').css({'height': ContentH +'px'});
          $('#bgcontainer').css({'height': ContentH +'px'});
          $('.centerd').css({'height':(($(window).height())-FixedDivs)+'px'});
          $('#HeaderMaster').css({'min-width':(minWidthBlock)+'px'});
          $('#ContentMaster').css({'min-width':(minWidthBlock)+'px'});
          $('#FooterMaster').css({'min-width':(minWidthBlock)+'px'});
          $('#FooterMaster2').css({'min-width':(minWidthBlock)+'px'});
          if (document.getElementById('PaneScroll_collectionsx'))
          {
              CollectionElemtHeight = $('.level2').height() //CalcMinHeightCollection();
              var Menu3H = ContentH - 70 - $('.menu1').height() - $('.level1').height();
              if (Menu3H<100){Menu3H=100;}
              if (CollectionElemtHeight>Menu3H)
              {
                 $('.wrapper_collectionsx').css({'height':+Menu3H +'px'});
                 $('ul#menucontent3>li>a').css({'padding-left':'15px'});
              }
              else
              {
                  $('.wrapper_collectionsx').css({'height':+CollectionElemtHeight+'px'});
                  $('ul#menucontent3>li>a').css({'padding-left':'25px'});
              }
              setPaneScrollCollectionsx();
             

          }
          setPaneScroll();
          
          $('img.fade').cross();
          $('#PaneScroll_privacy').jScrollPane({scrollbarWidth:5,scrollbarOnLeft:true});
          //$('#PaneScroll_combox').jScrollPane({scrollbarWidth:5,scrollbarOnLeft:false});
          //$('#PaneScroll_collectionsx').jScrollPane({scrollbarWidth:5,scrollbarOnLeft:true});
          $(".fullBg").fullBg();
         
          //CHECK IF BOXES
          if ($('.boxContent').length)
          {
              setBoxes();
              $(".boxContent").jCarouselLite({
                   btnNext: ".next",
                   btnPrev: ".prev",
                   visible: 4,
                   circular: true,
                   auto: 3000,
                   speed: 3000,
                   easing: "linear",
                   afterBuilt: LoadAllImagesBoxes   
              });
              BoxesEvents();
          }

          if ($('.swfhome').find('object').length || $('.swfhome').find('iframe').length)
          {
              isswfhome = true;
          }
          //opacity BG
          if (isswfhome)
          {
              $(".fullBg").animate({opacity:'0.05'}, 100);
          }
          else
          {
              $(".fullBg").animate({opacity:'0.7'}, 100);
          }
          
           //effects: easein, easeinout, easeout, expoin, expoout, bouncein, bounceout, bounceinout, elasin, elasout, elasinout, backin, backout, backinout, linear
          
          

    });
    //*************
    //resize listener
    //*************
    $(window).resize(function(){     
          var ContentH = $(window).height()- FixedDivs;   
          $('#ContentMaster').css({'height': ContentH +'px'});
          $('#bgcontainer').css({'height': ContentH +'px'});
          $('.centerd').css({'height':(($(window).height())-FixedDivs)+'px'});
          $('#down-list').hide(100);
          
          if (document.getElementById('PaneScroll_collectionsx'))
          {
              var Menu3H = ContentH - 70 - $('.menu1').height() - $('.menu2').height();
              if (Menu3H<100){Menu3H=100;}
              if (CollectionElemtHeight>Menu3H)
              {
                  $('.wrapper_collectionsx').css({'height':+Menu3H+'px'});
                  $('ul#menucontent3>li>a').css({'padding-left':'15px'});
              }
              else
              {
                  $('.wrapper_collectionsx').css({'height':+CollectionElemtHeight+'px'});
                  $('ul#menucontent3>li>a').css({'padding-left':'25px'});
              }
              setPaneScrollCollectionsx();
          }

          setPaneScroll();
    });
    //*************
    // FUNC GENERIC
    //*************
	var setPaneScroll = function()
	{
		// IE triggers the onResize event internally when you do the stuff in this function
		// so make sure we don't enter an infinite loop and crash the browser
		if (!isResizing) { 
			isResizing = true;
			$container = $('#ContentMaster');
			$pane = $('#PaneScroll');
			//$('#ContentMaster>.jScrollPaneContainer').css({'height': $container.height() + 'px', 'width': $container.width() + 'px'});
			$('#ContentMaster>.jScrollPaneContainer').css({'height': $container.height() + 'px'});
			$pane.jScrollPane({scrollbarWidth : '10',enableKeyboardNavigation : false, showArrows: true, arrowSize: '10'});	
			isResizing = false;	
		}
	}
	
	var setPaneScrollCollectionsx = function()
	{
	
		// IE triggers the onResize event internally when you do the stuff in this function
		// so make sure we don't enter an infinite loop and crash the browser
		if (!isResizingCollectionsx) { 
			isResizingCollectionsx = true;
			$container = $('.wrapper_collectionsx');
			$pane = $('#PaneScroll_collectionsx');
			$('.wrapper_collectionsx>.jScrollPaneContainer').css({'height': $container.height() + 'px'});
			$pane.jScrollPane({scrollbarWidth:5,scrollbarOnLeft:true,enableKeyboardNavigation:false,showArrows: true, arrowSize: '6'});	
			//$pane.jScrollPane({scrollbarWidth:'5',scrollbarOnLeft:true,enableKeyboardNavigation:false});	
			isResizingCollectionsx = false;	
		}
	}
	
	function LoadAllImagesBoxes(){               
        $('.box').each(function(){       
            var loader = $(this);
            LoadImageBoxes(loader);
        });
    };
            
            
    function LoadImageBoxes(loader){
    
        var image_src = $(loader).attr("title");
        
        if (image_src)
        {                
            var img = new Image();
            $(img).hide();
            $(img).addClass('imgbox');

         
            $(img).load(function() {
                img.height = 116;
                img.width = 226;
                $(loader).prepend($(img));
                $(loader).removeClass("loadingbox4");
                $(loader).removeAttr("title");
                $(img).show();
            
            });
            
            img.src = image_src;

        
        }
        else
        {
          $(loader).removeClass("loadingbox4");
        }
    };

    
    function CalcMinWidth()    {
            var widthToReturn = 950;
            var children = $("ul#topnav").children("li");
            var logo = $("div#logo"); 
            var childrenWidth = 0;
            for (var i = 0; i < children.length; i++){
                      childrenWidth += $(children[i]).width();
            }
            var widthMenu = childrenWidth+$(logo).width();
            if (widthMenu>950)
            {
                widthToReturn = widthMenu;
            }
            return widthToReturn;
    }  
    
    
    function CalcMinHeightCollection()    {
            var children = $("ul#menucontent3").children("li");
            var childrenHeight = 0;
            for (var i = 0; i < children.length; i++){
                      childrenHeight += $(children[i]).height();
            }
            return childrenHeight;
    }  
    
    function CalcHeightLangs()    {
            var children = $("ul.langlist").children("li");
            var childrenHeight = 0;
            for (var i = 0; i < children.length; i++){
                      childrenHeight += 18;
            }
            return childrenHeight;
    }  
    
    //******************************** BOXES ATTRIBUTES START width ******************
    var setBoxes = function()
	{   
	    $container =$('.boxContent');
	    var children = $('.boxContent').children("ul").children("li");
	    var childrenWidth = 0;
	    for (var i = 0; i < children.length; i++){
                      childrenWidth += $(children[i]).find(".box").outerWidth();

        }
        $container.css({'width':childrenWidth+'px'});  
        
        $('.boxes').css({'height':'120px'});  
        $('.boxLayer').css({'height':'120px'});  
        $('.boxContent').css({'height':'120px'});  
        $('#listbox').css({'height':'120px'});
        $('.singlebox').css({'height':'120px'});
	}
	
	//******************************** BOXES EVENTS ******************************
	var BoxesEvents = function()
	{
	        $('.boxDeta').hide();
	        var selected;
	        var closedHeight;
	        var swfstyle = $(".swfhome").attr("style");

            //******************************** BOX PREVIEW HOVER ******************************
			$('.boxAction').hover(function(){
			        var currli = $(this).parent("li");
                    var box = $(currli).find('.box');
 
				    if(!(box.hasClass("boxFixed")))
				    {
                        $(".imgbox", box).animate({opacity:'1.0'}, 100);
                        $(".objbox", box).css({opacity:'1.0'});
				    }

	
			    }, function() {
                    var currli = $(this).parent("li");
                    var box = $(currli).find('.box');
                    
                    if(!(box.hasClass("boxFixed")))
				    {   
                        $(".imgbox", box).animate({opacity:'0.5'}, 100);
                        $(".objbox", box).css({opacity:'0.5'});				    
				    }           
			});
			
    
		   //******************************** BOX CLOSE CLICK ******************************
		   $(".boxHeaderClose").click(function() {
	
		         var par = $(this).parent(".boxDetaHeader").parent(".boxDeta").parent("li");
		         var currentbox = $(par).find(".box");
		         
		         $(".swfhome").removeClass('swfhomeFixed');
		         $(".swfhome").find("Object").show();
		         $(".swfhome").find("iframe").show();
		         if (!isswfhome)
                 {
                    $(".fullBg").animate({opacity:'0.7'}, 100); 
                 }

                 $(".boxtitle").show();
                      
                 $(".boxDeta").hide();
                 $(".imgbox", currentbox).css({'opacity':'0.5'});
                 $(".objbox", currentbox).css({'opacity':'0.5'}); 
                               
                 $(currentbox).removeClass("boxFixed");
                 
                 $(".swfhome").attr("style", swfstyle);
                 
                $('.boxes').css({'height':'120px'});  
                $('.boxLayer').css({'height':'120px'});  
                $('.boxContent').css({'height':'120px'});  
                $('#listbox').css({'height':'120px'});
                $('.singlebox').css({'height':'120px'});

                 setPaneScroll();
                 selected = null;		   
		   });    
                   
            
           //******************************** BOX PREVIEW CLICK ******************************
           $(".boxAction").click(function() {
            
                var currli = $(this).parent("li");
                var box = $(currli).find('.box');
                var currentid = $(box).attr('id');
                
                //memo l'ultima height 'buona' per riaprire con stesse impostazioni
                closedHeight = $("#PaneScroll").outerHeight();
   
                
                if (currentid==selected)
                {
                    //close
                    
                    $("#PaneScroll").css({'height':closedHeight+'px'});
                    $(".swfhome").removeClass('swfhomeFixed');
                    $(".swfhome").find("Object").show();
                    $(".swfhome").find("iframe").show();
                    if (!isswfhome)
                    {
                        $(".fullBg").animate({opacity:'0.7'}, 500);  
                    }
                    
                    $(".boxDeta").hide();
                    
                    $(box).removeClass("boxFixed");
                    $(".imgbox", box).removeClass("selected");
                    
                    $(".boxtitle", box).show();
                    $(".imgbox", box).css({'opacity':'1.0'});
                    $(".objbox", box).css({'opacity':'1.0'});
                    
                    $(".swfhome").attr("style", swfstyle);
                    
                    
                $('.boxes').css({'height':'120px'});  
                $('.boxLayer').css({'height':'120px'});  
                $('.boxContent').css({'height':'120px'});  
                $('#listbox').css({'height':'120px'});
                $('.singlebox').css({'height':'120px'});
                    
                    selected = null;
                }
                else
                {
                   //open

                    $("#PaneScroll").css({'height':closedHeight+'px'});
                    $(".swfhome").find("Object").hide();
                    $(".swfhome").find("iframe").hide();
                    $(".swfhome").addClass('swfhomeFixed');
                    if (!isswfhome)
                    {
                        $(".fullBg").animate({opacity:'0.05'}, 500);
                    }
                    
                   
                    $(".imgbox").removeClass("selected");
                    $(".imgbox", box).addClass("selected");
                    
                    $(".objbox").removeClass("selected");
                    $(".objbox", box).addClass("selected");
                    
                    $(".boxtitle").show();
                    $(".boxDeta").hide();

                    $(".imgbox:not(.selected)").css({'opacity':'0.5'});
                    $(".objbox:not(.selected)").css({'opacity':'0.5'});
                    
                    $(".box").removeClass("boxFixed");
                    
                    $(box).parent("li").find(".boxDeta").show(); 
                    
                    $(box).addClass("boxFixed");
                    $(".boxtitle", box).hide();
                    
                    $(".imgbox", box).css({'opacity':'1.0'});
                    $(".objbox", box).css({'opacity':'1.0'});
                    
                    $(".swfhome").removeAttr("style");
                    
                    
                $('.boxes').css({'height':'420px'});  
                $('.boxLayer').css({'height':'420px'});  
                $('.boxContent').css({'height':'420px'});  
                $('#listbox').css({'height':'420px'});
                $('.singlebox').css({'height':'420px'});
            
                    selected = currentid;
                }
                
                 setPaneScroll();
           });
	
	}
    //*******************
    // EVENTS
    //******************
    //------------------
    // MENU TOP
    //------------------
    $("#topnav li").hover(
            function() {	//On hover...
                  var tagA =$(this).find("a");
                  if($(this).attr("class") == "active")
                  {
                  }
                  else
                  {
                        $(tagA).css( {backgroundPosition: "0 0px"} );
                        $(tagA).stop().animate({backgroundPosition:"(0px -40px)",color: "#FFFFFF"}, {duration:600});
                  }
            } ,
            function() { //On hover out...
                 var tagA =$(this).find("a");
                  if($(this).attr("class") == "active")
                  {
                  }
                  else
                  {        
                        $(tagA).css( {backgroundPosition: "0 -40px"} );
                        $(tagA).stop().animate({backgroundPosition:"(0px 0px)",color: "#afafaf"}, {duration:600});
                  }
                  
 
            }
    );
	//------------------
	// MENU BOTTOM SX
	//------------------
	$("#bottomnavsx li").hover(function() {	//On hover...
	              var tagA =$(this).find("a");
                  if($(this).attr("class") == "active")
                  {
                  }
                  else
                  {
                        $(tagA).stop().animate({color: "#FFFFFF"}, {duration:250});
                  }
		
	} , function() { //On hover out...
	               var tagA =$(this).find("a");
                  if($(this).attr("class") == "active")
                  {
                  }
                  else
                  {
                        $(tagA).stop().animate({color: "#a3a3a3"}, {duration:250});
                  }
		
	});
	//------------------
	// MENU BOTTOM SX 2
	//------------------
	$("#bottomnavsx2 li").hover(function() {	//On hover...
	              var tagA =$(this).find("a");
                  if($(this).attr("class") == "active")
                  {
                  }
                  else
                  {
                        $(tagA).stop().animate({color: "#FFFFFF"}, {duration:250});
                  }
		
	} , function() { //On hover out...
	               var tagA =$(this).find("a");
                  if($(this).attr("class") == "active")
                  {
                  }
                  else
                  {
                        $(tagA).stop().animate({color: "#9a9a9a"}, {duration:250});
                  }
		
	});
    //------------------
	// MENU BOTTOM DX
	//------------------
    $("#fullscreen").click(function() {
            if (window.opener != null) {
                window.opener.location = window.location.href;
                window.self.close();
            }
            else {
                document.getElementById('SplashFullScreen').style.display = 'block';
                window.open(window.location.href, 'fullscreen',
                            'height=' + screen.height + ' width=' + screen.width + ',left=0,top=0,screenX=0,screenY=0,type=fullWindow,fullscreen=yes,menubar=no,toolbar=no,location=no,directories=no,status=no,scrollbars=auto,resizable=yes');
            }
    });
    //------------------
	// RADIO
	//------------------        
       function RadioPop(u,t,l){
            var top = t + 250;
            var limitLeft = screen.width - 300;
            var left = limitLeft;
            if (l < limitLeft) { left = l ;}
            window.open(u,'Radio','toolbar=no,menubar=no,status=no,scrollbars=no,resizable=yes,width=260px,height=140px,top='+top+',left='+left);
            return false;
         }        

       $(".toradio").click(
       function(e)
           {
            var X = e.pageX;
            var Y = e.pageY;
            var urllocation = $(this).attr('rel');
            RadioPop(urllocation, Y, X);

           }
       );
       
       
        $(".lnksearch").click(
        function(e)
           {
           var keys = $('#txt_search').val().toLowerCase();
           var partsArray = keys.split(' ');
           partsArray = jQuery.grep(partsArray, function(n, i){
              return (n != '');
           });
           var qstring = partsArray.join(",");         
           window.location = $(this).attr("rel") + "&q=" + encodeURI(qstring);
           return false;
           }
       );
       
       $(".gohighlight").click(
       function(e)
           {
           $("#paragraph").highlight(this.innerText);
           return false;
           }
       );
     
      
});




