function isset (arg) {
  return typeof(arg) != 'undefined';
}

function isfunc (arg) {
  return typeof(arg) == 'function';
}

function cl () {
  if (isset(console) && isfunc(console.log)) {
    console.log.apply(null, arguments);
  }
  if ($('#dbg').size() == 0) {
    $('<div id="dbg" style="position: absolute; bottom: 0; right: 0; color: gray; background-color: gray;"></div>').appendTo('body');
  }
  var argumentz = '';
  for (var i in arguments) {
    argumentz += arguments[i] + ', ';
  }
  $('#dbg').html(argumentz);
}

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

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

function pct (arg) {
  return arg + '%';
};
$.pct = pct;

function updateScrollerPosition () {
  var st = $('.logo').height()
          + parseInt($('.logo').css('padding-top'))
          + parseInt($('.logo').css('padding-bottom'))
  var sh = $(window).height() - st
          - $('.menu').height()
          - parseInt($('.menu').css('padding-top'))
          - parseInt($('.menu').css('padding-bottom'));
  var sr = $('.content .scroll').offset().left + $('.content .scroll').width();

  $('.scroller')
    .height(sh - 40)
    .css('position', 'absolute')
    .css('left', px(sr + 14))
    .css('top', st + 20);
}

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

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

  // for inner functions
  var self = this;

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

  // removing default HTML scroller if exists
  this.$scroller.css('overflow', 'hidden');

  // using jquery.scrollbar plugin for horizontal scrollbar
  this.scrollbar = $.verticalScroller($('.scroller'), {
    topArrowLocator: '.topArrow',
    scrollLocator: '.tracker',
    bottomArrowLocator: '.bottomArrow',
    onScrollMove: function (percentage) {
      self.$content.css('top', $.px(-percentage * (self.$content.height() - $(window).height())));
    },
    scrollSpeed: (this.$content.height()*0.5 > $(window).height()) ? 0.02 : 0.3,
    scrollAcceleration: 0.002
  });

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

  // updating scrollbar view on window resizing
  $(window).resize(function () {
      if(!$.browser.msie) {
        self.scrollbar.setPosition(self.getPositionAsPercentage());
      }
  });

};

var scroller = null;

$(document).ready(function () {

  $(document).pngFix();

  $(window).resize(function () {
    if ($('.scroll').height() > ($(window).height() - 140)) {
      if (!scroller) {
        scroller = new PageScroller;
        $('.topArrow').unbind('mouseover').mouseover(function () {
          $(this).mousedown();
        }).unbind('mouseout').mouseout(function () {
          $(this).mouseup();
        });

        $('.bottomArrow').unbind('mouseover').mouseover(function () {
          $(this).mousedown();
        }).unbind('mouseout').mouseout(function () {
          $(this).mouseup();
        });


      }

      if (scroller) {
        $('.scroller').show();
        updateScrollerPosition();
        scroller.scrollbar.setPosition(0);
      }
    }

    else {
      if (scroller) {
        $('.scroller').hide();
        scroller.scrollbar.setPosition(0);
      }
    }

  });
  
  $('body').mousewheel(function (event, delta) {
  
    if (delta < 0) {
      $('.bottomArrow:visible').mouseover();
      setTimeout(function () {
        $('.bottomArrow:visible').mouseout();
      }, 500 * Math.abs(delta));
    }
    else if (delta > 0) {
      $('.topArrow:visible').mouseover();
      setTimeout(function () {
        $('.topArrow:visible').mouseout();
      }, 500 * Math.abs(delta));
    }
    
    return false;
  });
  
  $(window).resize();
  
});
