(function($,sr){
  var debounce = function (func, threshold, execAsap) {
      var timeout;
      return function debounced () {
          var obj = this, args = arguments;
          function delayed () {
              if (!execAsap)
                  func.apply(obj, args);
              timeout = null; 
          };
          if (timeout)
              clearTimeout(timeout);
          else if (execAsap)
              func.apply(obj, args);
          timeout = setTimeout(delayed, threshold || 100); 
      };
  }
  jQuery.fn[sr] = function(fn){  return fn ? this.bind('resize', debounce(fn)) : this.trigger(sr); };
})(jQuery,'smartresize');
(function($,sr){
  var debounce = function (func, threshold, execAsap) {
      var timeout;
      return function debounced () {
          var obj = this, args = arguments;
          function delayed () {
              if (!execAsap)
                  func.apply(obj, args);
              timeout = null; 
          };
          if (timeout)
              clearTimeout(timeout);
          else if (execAsap)
              func.apply(obj, args);
          timeout = setTimeout(delayed, threshold || 100); 
      };
  }
  // smartresize 
  jQuery.fn[sr] = function(fn){  return fn ? this.bind('scroll', debounce(fn)) : this.trigger(sr); };
})(jQuery,'smartscroll');
(function($){
     $.fn.extend({
          centraPopup: function (options) {
               var options =  $.extend({ // Default values
                    inside:window, // element, center into window
                    transition: 300, // millisecond, transition time
                    minX:0, // pixel, minimum left element value
                    minY:140, // pixel, minimum top element value
                    offsetX:0, // pixel
                    offsetY:0, // pixel
                    withScrolling:false, // booleen, take care of the scrollbar (scrollTop)
                    vertical:true, // booleen, center vertical
                    horizontal:true // booleen, center horizontal
               }, options);
               return this.each(function() {
                    var props = {};//position:'fixed'
                    if (options.vertical) {
                         var top = ($(options.inside).height() - $(this).outerHeight()) / 2;
                         if (options.withScrolling) top += $(options.inside).scrollTop() || 0;
                         top += options.offsetY;
                         top = (top > options.minY ? top : options.minY);
                         $.extend(props, {top: top+'px'});
                    }
                    if (options.horizontal) {
                          var left = ($(options.inside).width() - $(this).outerWidth()) / 2;
                          if (options.withScrolling) left += $(options.inside).scrollLeft() || 0;
                          left += options.offsetX;
                          left = (left > options.minX ? left : options.minX);
                          $.extend(props, {left: left+'px'});
                    }
                    if (options.transition > 0) $(this).animate(props, options.transition);
                    else $(this).css(props);
                    return $(this);
               });
          }
     });
})(jQuery);

function FullScreenBackground(theItem){
    var winWidth=$(window).width();
    var winHeight=$(window).height();
    var imageWidth=$(theItem).width();
    var imageHeight=$(theItem).height();
    var picHeight = imageHeight / imageWidth;
    var picWidth = imageWidth / imageHeight;
    if ((winHeight / winWidth) < picHeight) {
        $(theItem).css("width",winWidth);
        $(theItem).css("height",picHeight*winWidth);
    } else {
        $(theItem).css("height",winHeight);
        $(theItem).css("width",picWidth*winHeight);
    };
    $(theItem).css("margin-left",winWidth / 2 - $(theItem).width() / 2);
    $(theItem).css("margin-top",winHeight / 2 - $(theItem).height() / 2);
}
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-27928425-1']);
  _gaq.push(['_trackPageview']);
  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();
  
$(document).ready(function(){
    FullScreenBackground('#bgimg');
    $('.contenuto').lionbars();
    $('#logocnt').centraPopup({vertical:false,transition:0});
    $('#maincont').centraPopup({vertical:true,transition:0});
    $(window).smartresize(function(){ 
    $('#logocnt').centraPopup({vertical:false});
    $('#maincont').centraPopup({vertical:true});
    });
    $(window).smartscroll(function(){ 
    $('#logocnt').centraPopup({vertical:false});
    $('#maincont').centraPopup({vertical:true});
    });
    var h=$('.submenu').height();
    $('.submenu').height(0);
    $('#sub_'+sezione).stop().animate({height:h+'px'}, 1000,"easeInOutQuart");
    //.delay(750)
    //$('.contenuto').hide().delay(800).show(350);
    $(".riquadro span.txt").click(function(event){
      var href=$(this).find("a.mainlink").attr('href');
      event.preventDefault();
      event.stopPropagation();
      location.href=href;
    });
    /*
    $(".riquadro").each(function(i) {
      $(this).delay(170*(i+1)).css({opacity:0}).animate({opacity: 1}, 200);
    });
    $(".subcontenuto").hide();
    */
    function refreshSub(){
        $(".voceint").each(function(i) {
            if($(this).hasClass("selezionato")){
                var href=$(this).find("a").attr('href');
                var rel=$(this).find("a").attr('rel');
                $(href).delay(350).show(350);
                //$.backstretch(rel,{speed:400});
                $('#bgimg').hide(250);
                $('#bgimg').attr('src', rel);
                $('#bgimg').load(function() {
                    FullScreenBackground('#bgimg');
                    $('#bgimg').show(250);
                });
                
            }
        });
    }
    $(".voceint a").click(function(event){
        $(".voceint").each(function(i) {
            if($(this).hasClass("selezionato")){
                var href=$(this).find("a").attr('href');
                $(href).hide(350);
            }
            $(this).removeClass("selezionato");
        });
      $(this).parent().addClass("selezionato");
      refreshSub();
      event.stopPropagation();
      
    });
    var myFile = document.location.toString();
    if (myFile.match('#')) { // the URL contains an anchor
      var myAnchor = '#' + myFile.split('#')[1];
      $('.voceint a[href="' + myAnchor + '"]').click();
    } else {
      $('.voceint.predefinito a').click();
    }
    $('a[rel*=external],a[href^=http]').click( function() {
        var href=$(this).attr('href');
        var serv=href.split('.')[1];
        if(serv=="fiatcamper"){
            location.href=href;
        }else{
            window.open(href);
        }
        return false;
    });
});
$(window).resize(function() {
    FullScreenBackground('#bgimg');
});

