﻿/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
if (!Function.prototype.bind) {

  Function.prototype.bind = function (oThis) {

    if (typeof this !== "function") // closest thing possible to the ECMAScript 5 internal IsCallable function
      throw new TypeError("Function.prototype.bind - what is trying to be fBound is not callable");

    var aArgs = Array.prototype.slice.call(arguments, 1),
        fToBind = this,
        fNOP = function () {},
        fBound = function () {
          return fToBind.apply(this instanceof fNOP ? this : oThis || window, aArgs.concat(Array.prototype.slice.call(arguments)));
        };

    fNOP.prototype = this.prototype;
    fBound.prototype = new fNOP();

    return fBound;

  };
}

var eventType = (/Firefox/i.test(navigator.userAgent))?"DOMMouseScroll":"mousewheel";

function myScroller(object, width, height, scrollSize){
    this.width = width;
    this.height = height;
    this.scrollSize = 30;
    if(scrollSize){
        this.scrollSize = scrollSize;
    }
    this.content = object;
    var parent = this.content.parentNode;
    this.scrollPane = document.createElement('div');
    this.scrollBar = document.createElement('div');
    $(this.scrollBar).addClass('scroll-bar')
    this.scrollUp = document.createElement('div');
    $(this.scrollUp).addClass('arrow-up');
    this.scrollDown = document.createElement('div');
    $(this.scrollDown).addClass('arrow-down');
    this.scrollPad = document.createElement('div');
    $(this.scrollPad).addClass('scroll-pad');
    

    this.scrollBar.appendChild(this.scrollUp);
    this.scrollBar.appendChild(this.scrollPad);
    this.scrollBar.appendChild(this.scrollDown);

    this.scrollPane.appendChild(this.content);
    this.scrollPane.appendChild(this.scrollBar);

    parent.appendChild(this.scrollPane);
    $(this.scrollPane).width(width);
    $(this.scrollPane).height(height);
    $(this.scrollPane).css('position', 'relative');
    $(this.scrollPane).css('overflow', 'hidden');
    $(this.scrollBar).height(height);
    $(this.content).width(width - $(this.scrollBar).outerWidth() - $(this.scrollBar).css('margin-left').split('px')[0] - $(this.scrollBar).css('margin-right').split('px')[0]);
    $(this.content).css('position', 'absolute');
    $(this.content).css('top', '0px');
    this.contentHeight = $(this.content).height();
    $(this.scrollPad).css('top', $(this.scrollUp).outerHeight() + 'px');
    $(this.scrollDown).css('top', height - $(this.scrollDown).outerHeight() + 'px');
    this.scrollPadMax = height - $(this.scrollPad).outerHeight() - $(this.scrollUp).outerHeight() - $(this.scrollDown).outerHeight();
    this.scrollPadStart = $(this.scrollUp).outerHeight();
    this.contentHeight = $(this.content).outerHeight();

    $(this.scrollPane).hover(this.scrollPaneOver.bind(this), this.scrollPaneOut.bind(this));
    
	if (this.scrollPane.addEventListener)
		this.scrollPane.addEventListener(eventType, this.scrollMouse.bind(this), false);
	else
		this.scrollPane.attachEvent("onmousewheel", this.scrollMouse.bind(this));
		
    $(this.scrollUp).click(this.scrollArrow.bind(this));
    $(this.scrollDown).click(this.scrollArrow.bind(this));
    $(this.scrollBar).mousedown(this.scrollDragStart.bind(this));
    //$(this.scrollPane).mouseout(this.scrollDragStop.bind(this));
    $(this.scrollPane).mouseup(this.scrollDragStop.bind(this));

    //$(this.scrollPane).scroll(this.scrollContent.bind(this));
	
	//
	var cont = $(this.content);
	if(cont.height() > 370)
	{
		cont.append("<div style='text-align:right;font-size:12px;float:right;'><br /><a href='#' onclick=\"$('div.to-scroll').css('top', '0');$('div.scroll-pad').css('top', '16px'); return false;\"><b><i>повратак на врх</i></b></a></div>");
		this.contentHeight = cont.height();
	}
	else
	{
		$(this.scrollBar).hide();
	}
	
}

myScroller.prototype.scrollDragStart = function(event){
    $(this.scrollBar).mousemove(this.scrollDrag.bind(this));
    $(this.scrollPad).mousemove(this.scrollDrag.bind(this));
    this.startY = event.pageY;
    this.startTop = parseInt($(this.scrollPad).css('top').split('px')[0]);
    toDisplay('start drag');
}

myScroller.prototype.scrollDragStop = function(event){
    $(this.scrollBar).unbind('mousemove');
    $(this.scrollPad).unbind('mousemove');
    toDisplay('stop drag');
}

function toDisplay(text){
    //$('#display').html(text);
}

myScroller.prototype.scrollDrag = function(event){    
    var newTop = this.startTop + event.pageY - this.startY;
    if(newTop < this.scrollPadStart){
        newTop = this.scrollPadStart;
    }
    if(newTop > this.scrollPadStart + this.scrollPadMax){
        newTop = this.scrollPadStart + this.scrollPadMax;
    }
    $(this.scrollPad).css('top', newTop + 'px');
    $(this.content).css('top', ((newTop-$(this.scrollUp).outerHeight())/this.scrollPadMax)*(this.height - this.contentHeight) + 'px');
}

myScroller.prototype.scrollMouse = function(event){
    var dy = event.detail ? (-event.detail) : (event.wheelDelta);
    if(dy < 0){
        dy = -this.scrollSize;
    }else{
        dy = this.scrollSize
    }
    var oldTop = parseInt(this.content.style.top.split('px')[0]);
    var newTop = oldTop + dy;
    if(newTop > 0){
        newTop = 0;
    }
    if(newTop + this.contentHeight <= this.height){
        newTop = this.height - this.contentHeight;
    }
    this.content.style.top = newTop + 'px';
    this.adjustPad();
}

myScroller.prototype.scrollArrow = function(event){
    var dy = this.scrollSize;
    if(event.target == this.scrollDown){
        dy = -this.scrollSize;
    }
    
    var oldTop = parseInt(this.content.style.top.split('px')[0]);
    var newTop = oldTop + dy;
    if(newTop > 0){
        newTop = 0;
    }
    if(newTop + this.contentHeight <= this.height){
        newTop = this.height - this.contentHeight;
    }
    this.content.style.top = newTop + 'px';
    this.adjustPad();
}

var autoSnap = null;

myScroller.prototype.adjustPad = function(){
    var contentTop = parseInt(this.content.style.top.split('px')[0]);

    $(this.scrollPad).css('top', this.scrollPadStart + parseInt(this.scrollPadMax*((-contentTop)/(this.contentHeight - this.height))) + 'px');
	if(autoSnap != null)
	{
		var obj = this;
		var oldTop = $(this.content).position().top;
		oldTop = Math.round(oldTop / autoSnap) * autoSnap;
		$(this.content).animate(
		{top:oldTop}, 500, function() {
			obj.adjustPad2(obj);
		});
	}
}

myScroller.prototype.adjustPad2 = function(obj){
    var contentTop = parseInt(obj.content.style.top.split('px')[0]);
    $(obj.scrollPad).css('top', obj.scrollPadStart + parseInt(obj.scrollPadMax*((-contentTop)/(obj.contentHeight - obj.height))) + 'px');
}

myScroller.prototype.scrollPaneOver =  function (event){
    disable_scroll();
}

myScroller.prototype.scrollPaneOut = function (event){
    enable_scroll();
}

// left: 37, up: 38, right: 39, down: 40,
// spacebar: 32, pageup: 33, pagedown: 34, end: 35, home: 36
var keys = [37, 38, 39, 40];

function preventDefault(e) {
    e = e || window.event;
    if (e.preventDefault){
        e.preventDefault();
    }
    e.returnValue = false;
}

function keydown(e) {
    for (var i = keys.length; i--;) {
        if (e.keyCode === keys[i]) {
            preventDefault(e);
            return;
        }
    }
}

function wheel(e) {
    preventDefault(e);
}

function disable_scroll() {
    if (window.addEventListener) {
        window.addEventListener(eventType, wheel, false);
    }
    window.onmousewheel = document.onmousewheel = wheel;
    document.onkeydown = keydown;
}

function enable_scroll() {
    if (window.removeEventListener) {
        window.removeEventListener(eventType, wheel, false);
    }
    window.onmousewheel = document.onmousewheel = document.onkeydown = null;
}




