$(window).load(function(){
  $('.boxfloor').removeClass('loading');
})

$(document).ready(function(){

  $('.boxfloor').addClass('loading');

  var indexPage = 0;
  var vel = 250;
  //ARROW MANAGER

  $('#arrow-left').hide();

  $('#arrow-left').click(function(e){
    e.preventDefault();

    $('#arrow-right').show();
    $('#goto').hide();

    if (indexPage > 0){

      var fixedImage = $($('.boxfloor')[indexPage]).find('.fixed-img');

      if (fixedImage.length > 0){
        fixedImage.fadeOut('slow', function(){
          slideLeft();
        });
      }else{
        slideLeft();
      }
    }
  })

  $('#arrow-right').click(function(e){
    e.preventDefault();

    $('#arrow-left').fadeIn();

    if (indexPage < $('#nav-bar ul li a').length - 1){

      var fixedImage = $($('.boxfloor')[indexPage]).find('.fixed-img');

      if (fixedImage.length > 0){
        fixedImage.fadeOut('slow', function(){
          slideRight();
        });
      }else{
        slideRight();
      }
    }
    return;
  })

  $('.landing-img').click(function(e){
    e.preventDefault();
    slideRight();
  })

  function slideRight(){

    indexPage++;

    if (indexPage == $('#nav-bar a').length - 1){
      $('#arrow-right').hide();
      $('#goto').show();
    }

    var nextAnchor = $($('#nav-bar a')[indexPage]);
    nextAnchor.click();

    return;
  }

  function slideLeft(){

    indexPage--;

    if (indexPage <= 0){
      $('#arrow-left').fadeOut();
    }

    var prevAnchor = $($('#nav-bar a')[indexPage]);
    prevAnchor.click();

    return;
  }

  //NAVBAR MANAGER

  $('#nav-bar ul li a').hover(function(){

    var li = $(this).parent();

    if (!li.hasClass('hover')){
      li.addClass('hover');
    }
  }, function(){

    var li = $(this).parent();
    if (li.hasClass('hover')){
      li.removeClass('hover');
    }
  })

  $('#nav-bar a').click(function(e){

    e.preventDefault();
    e.stopPropagation();

    var fixedImage = $($('.boxfloor')[indexPage]).find('.fixed-img');
    var clickedAnchor = $(this);

    indexPage = ($('#nav-bar a').index(clickedAnchor));

    $('#nav-bar ul li.selected').removeClass('selected');

    clickedAnchor.parent().addClass('selected');

    if (clickedAnchor.parent().hasClass('submenu-parent')){

      var submenu = $('#nav-bar ul li a[name=' + clickedAnchor.parent().attr('id') + ']').parent();

      if (!submenu.is(':visible')){
        $('#nav-bar ul li.submenu').hide(vel);

        if ($.browser.msie && $.browser.version < 8) {
          submenu.show(vel).css({
            'display' : 'inline'
          });
        }else{
          submenu.show(vel).css({
            'display' : 'inline-block'
          });
        }
      }

    }else if (clickedAnchor.parent().hasClass('submenu')){
      $('#nav-bar ul li.selected').removeClass('selected');
      clickedAnchor.parent().addClass('selected');
      $('#' + clickedAnchor.attr('name')).addClass('selected');

      if ($.browser.msie && $.browser.version < 8) {
        $('#nav-bar ul li a[name=' + clickedAnchor.attr('name') + ']').parent().show(vel).css({
          'display' : 'inline'
        });
      }else{
        $('#nav-bar ul li a[name=' + clickedAnchor.attr('name') + ']').parent().show(vel).css({
          'display' : 'inline-block'
        });
      }


      $('.submenu-parent').each(function(){

        var parent = $(this);
        if (!parent.hasClass('selected')){
          $('#nav-bar ul li.submenu a[name=' + parent.attr('id') + ']').parent().hide(vel);
        }
      })

    }else{
      $('#nav-bar ul li.submenu').hide(vel);
    }

    $('#arrow-left').show();
    $('#arrow-right').show();
    $('#goto').hide();

    if (indexPage == 0){
      $('#arrow-left').hide();
      $('#bottom-logo').fadeIn('slow');
    }else if (indexPage == $('#nav-bar a').length - 1){
      $('#arrow-right').hide();
      $('#goto').show();
      $('#bottom-logo').fadeOut('slow');
    } else {
      $('#bottom-logo').fadeOut('slow');
    }

    $('.house').css({
      'overflow-y' : 'hidden'
    });

    if (fixedImage.length > 0){
      fixedImage.stop(true, true).fadeOut('slow', function(){
        $('#house').trigger('manualslide', clickedAnchor);
      });
    }else{
      $('#house').trigger('manualslide', clickedAnchor);
    }


  })

  /*MENU MANAGER*/
  $('#footer-left-button').hover(function(){
    $('#menu-button').stop().animate({
      width: $('#menu-list').width() + 80
    }, 400);
  }, function(){
    $('#menu-button').stop().animate({
      width: 80
    }, 400);
  })
    
  
  

  $("#footer-right-button a").click(function(e){
    e.preventDefault();   
   
    $(this).parent().addClass("selected");
    $("#bottom-logo").fadeIn('slow');

    $('#main').fadeOut('slow', function(){

      var $footer = $('#footer');

      $('html').css({
        'overflow-y' : 'scroll'
      })

      $footer.animate(
      {
        'bottom' : ($(window).height()) - $footer.height()
      },
      800,
      'easeInOutExpo',
      function(){

        $footer.css({
          'top' : '0px',
          'bottom' : 'none',
          'position' : 'absolute',
          'background-color' : '#fff'
        })


        $('head').append('<link href="css/foradori-info.css" rel="stylesheet" type="text/css" />');
        $('body').addClass("loading");
        $('#info-ajax').load('../../index.php?mod=&func=&id=21 #info-ajax-content', function(){
          $(this).fadeIn(800);
          $('#info-ajax').append('<script type="text/javascript" src="js/jquery/jquery.placeholder.js"></script>');
          $('#info-ajax').append('<script type="text/javascript" src="colorbox/jquery.colorbox-min.js"></script>');
          $('#info-ajax').append('<script type="text/javascript" src="js/foradori-info.js"></script>');
          $('body').removeClass("loading");
        });



        $('html').css({
          'overflow-y' : 'scroll'
        })
      });
    });
    $(this).parent().fadeOut('slow');
    $("#footer-left-button").fadeOut('slow');
  });

  if ($.browser.msie&&$.browser.version<=8) {
    $("#footer-right-button a").unbind();
    $("#footer-right-button").click(function(){
      location.href = $(this).find('a').attr('href');
    })
  }
  
  
  function imageLoading(image){
    
    adjustFixedImage(image);
    verticalCenter();

    image.css({
      'display' : 'none',
      'visibility' : 'visible'
    })

    if (image.hasClass('landing-img')){

      var windowHeight = ($(window).height() * image.attr('rel')) / 100;
      var windowWidth = ($(window).width() * image.attr('rel')) / 100;
      var imageWidth = image.width();
      var imageHeight = image.height();
      var heightRatio = imageHeight / windowHeight;
      var widthRatio = imageWidth / windowWidth;

      if (heightRatio > widthRatio){
        image.height(windowHeight);
        image.width((imageWidth * windowHeight) / imageHeight);
            
      }else{
        image.width(windowWidth);
        image.height((windowWidth * imageHeight) / imageWidth);
      }
          
      image.show();
      $(window).resize();
      image.hide(); 
        
      image.fadeIn('slow', function(){
        $(window).resize();
      });
    }else{
      image.width(image.parent().width());
      verticalCenter();
    }  
  }
  
  function fixedImagePreload(){
	
    $('.landing-img, .fixed-img').each(function(){

      var image = $(this);
      var src = image.attr('src');
      
      if (!image[0].complete){
        //console.log("not cached");
        image.load(function(){
          imageLoading(image);
        }).attr('src', src);
      }else{
        //console.log("just cached");
        imageLoading(image);
      }
    });
  }

  function adjustFixedImage(){

    $('.house').each(function(){

      var house = $(this);
      var images = house.find('.fixed-img');

      if (images.length > 0){
        images.each(function(){
          var img_parent = $(this).parent();
          var left = img_parent.position().left;
          var width = img_parent.width();

          $(this).css({
            'width' : width + 'px'
          })

          if (!$(this).hasClass('no-left-adjust')){
            $(this).css({
              'left' : left + house.scrollLeft() + 'px'
            })
          }
        })
      }
    })
  }

  function verticalCenter(){
    $('.boxfloor').each(function(){

      var children = $(this).children();

      children.each(function(){
        var gap = 180;
        var child = $(this);
        var $window = $(window);
        var max_height = $window.height() - gap;
        var topkey = false;

        var fixedImage = child.find('.fixed-img');

        if (fixedImage.length > 0){
          child = fixedImage;
          max_height += gap;

          if (!fixedImage.hasClass('no-height')){
            child.parent().height(child.height());
          }

          topkey = true;
        }

        if (!child.hasClass('no-central')){

          if (child.height() <= max_height){

            var top = (max_height - child.height()) / 2

            if (top > 0){

              if (topkey){
                child.css({
                  'top' : top + 'px'
                })
              }else{
                child.css({
                  'marginTop' : top + 'px'
                })
              }
            }

          }else{
            child.css({
              'top' : 'auto',
              'marginTop' : '0px'
            })
          }
        }
      })

    })
  }

  $(window).resize(function(){
    var image = $('.landing-img');

    var windowHeight = ($(window).height() * image.attr('rel')) / 100;
    var windowWidth = ($(window).width() * image.attr('rel')) / 100;
    var imageWidth = image.width();
    var imageHeight = image.height();
    var heightRatio = imageHeight / windowHeight;
    var widthRatio = imageWidth / windowWidth;

    if (heightRatio > widthRatio){
      image.height(windowHeight);
      image.width((imageWidth * windowHeight) / imageHeight);        
    }else{
      image.width(windowWidth);
      image.height((windowWidth * imageHeight) / imageWidth);
    }
    
    adjustFixedImage();
    verticalCenter();

    $('.boxfloor').css({
      'margin' : '80px'
    });
  });

  $('#bg-home').click(function(e){
    e.preventDefault();
    location.href = $('#bg-home').attr("href");
  });

  $(document).bind('keydown',function(e){
    //left key arrow
    if (e.keyCode == 37) {
      var leftArrow = $('#arrow-left');
      if (leftArrow.is(':visible')){
        leftArrow.click();
      }
      return false;
    }

    //right key arrow
    if (e.keyCode == 39) {
      var rightArrow = $('#arrow-right');
      if (rightArrow.is(':visible')){
        rightArrow.click();
      }
      return false;
    }
  });

  fixedImagePreload();

});


