/**
 * Copyright 2011 Francesco Disperati
 *
 * what are you doing here? U mad?
 */

/** dovoUI: manage user interface */
var dovoUI = {

  spinner: {
    degree: 0,

    _rotate: function() {
      this.degree = this.degree + 360;
      $('#spinner').css('-webkit-transform', 'rotate(' + this.degree + 'deg)');
      $('#spinner').css('-moz-transform', 'rotate(' + this.degree + 'deg)');
      $('#spinner').css('-o-transform', 'rotate(' + this.degree + 'deg)');
    },

    start: function() {
      if( dovoUI.spinner.x == undefined ) {
        dovoUI.spinner._rotate();
        dovoUI.spinner.x = window.setInterval("dovoUI.spinner._rotate()", 1000);
      }
    },

    stop: function() {
      window.clearInterval( dovoUI.spinner.x );
      dovoUI.spinner.x = undefined;
    }
  },

  // DEPRECATED
  columnsResize: function() {
    var cl = $('#content-left');
    var cr = $('#content-right');
    cl.height(''); cr.height('');
    if ( cr.height() > cl.height() ) { cl.height(cr.height()); }
  },

  /**
   * Display the arrow below nav item in the right position
   */
  placeNavArrow: function() {
      var base_x = -265;
      var x = $( 'nav a.selected' ).position().left + base_x;

      $('header').append( $('<div>', {
          id: 'nav-arrow',
          style: 'margin-left:' + x + 'px;'
      }) );
  },

  /**
   * Fix an element on his vertical position, without escaping the parent box
   */
  stickySidebar: function( obj ) {
      var prev_style = {
          position: $(obj).css('position'),
          top:  parseFloat( $(obj).css('top').replace(/auto/,0) ),
          left: parseFloat( $(obj).css('left').replace(/auto/,0) )
      };
      var obj_top, obj_left;

      $(window).scroll(function() {
          var y = $(this).scrollTop();
          var x = $(this).scrollLeft();

          // If element is still non-sticky, set position infos
          if ( $(obj).attr('data-sticky') == 'false' ) {
              obj_top  = $(obj).offset().top;
              obj_left = $(obj).offset().left;
          }

          if ( y > 0 ) {
              var box = $(obj).parent();
              var box_height = box.height();
              var obj_height = $(obj).height();
              if ( obj_height >= box_height ) { return; }

              $(obj).attr('data-sticky', 'true');
              $(obj).css({
                  'position': 'fixed',
                  'top': obj_top,
                  'left': (obj_left-x)
              });
              // prevent object from going outside the box
              var overflow_bottom = (box_height + box.offset().top) - (obj_height + y + obj_top);
              if ( overflow_bottom <= 0 ) {
                  $(obj).css({
                      'position': 'relative',
                      'top': (box_height-obj_height),
                      'left': prev_style.left
                  });
              }
          }
          else {
              $(obj).attr('data-sticky', 'false');
              $(obj).css({
                  'position': prev_style.position,
                  'top': prev_style.top,
                  'left': prev_style.left
              });
          }
      });

      // init
      $(obj).attr('data-sticky', 'false');
  },

  /**
   * Keep position:fixed elements only vertically fixed
   */
  stickyY: function(obj, left, new_margin_left) {
      var prev_style = {
          left: left,
          margin_left: $(obj).css('margin-left')
      };
      var min_width = parseInt( $('body').css('min-width') );

      var fitContent = function() {
          if ( $(this).width() <= min_width ) {
              var x = $(this).scrollLeft();
              $(obj).css( {'left': 0, 'margin-left': (new_margin_left - x)} );
          }
          else {
              $(obj).css( {'left': prev_style.left, 'margin-left': prev_style.margin_left} );
          }
      };

      $(window).resize( fitContent );
      $(window).scroll(function() {
          if ( $(this).width() <= min_width ) {
              var x = $(this).scrollLeft();
              $(obj).css( {'margin-left': (new_margin_left - x)} );
          }
      });

      // initialization calls
      fitContent();
  },

};


/**
 * Simple jQuery extensions
 */
$.fn.slideFadeToggle  = function(speed, easing, callback) {
    return this.animate( {opacity: 'toggle', height: 'toggle'}, speed, easing, callback );
};


$(document).ready(function() {
    dovoUI.placeNavArrow();
    dovoUI.stickyY( '#sidebar-left', "50%", 10 );
    dovoUI.stickyY( 'header', 0, 0 );
});

