﻿Type.registerNamespace("CyberCoders");

CyberCoders.ScrollBehavior = function(element) {
	CyberCoders.ScrollBehavior.initializeBase(this, [element]);

	this._scrollSpeed = 1;
	this._cycle = true;
	this._autoStart = true;
	this._scrollDiv = null;
	this._position = 0;
	this._startPosition = 0;
	this._pauseLength = 0;

	this._scrollDivHeight = 0;

	this._scrollDelegate = null;
}

CyberCoders.ScrollBehavior.prototype = {
	get_scrollSpeed: function() {
		return this._scrollSpeed;
	},

	set_scrollSpeed: function(value) {
		this._scrollSpeed = value;
	},

	get_cycle: function() {
		return this._cycle;
	},

	set_cycle: function(value) {
		this._cycle = value;
	},

	get_autoStart: function() {
		return this._autoStart;
	},

	set_autoStart: function(value) {
		this._autoStart = value;
	},

	get_pauseLength: function() {
		return this._pauseLength;
	},

	set_pauseLength: function(value) {
		this._pauseLength = value;
	},

	start: function() {
		window.setTimeout(this._scrollDelegate, 100);
	},

	stop: function() {

	},

	initialize: function() {
		CyberCoders.ScrollBehavior.callBaseMethod(this, 'initialize');

		this._scrollDelegate = Function.createDelegate(this, this._scrollHandler);

		var e = this.get_element();
		this._scrollDiv = document.createElement('div');

		var wrapper = document.createElement('div');

		var b = $common.getBounds(e);

		for (var i = e.childNodes.length - 1; i >= 0; i--) {
			if (e.childNodes[i].nodeType == 1) {
				e.childNodes[i].style.width = b.width + 'px';
			}

			this._scrollDiv.insertBefore(e.childNodes[i], this._scrollDiv.firstChild);
		}

		wrapper.appendChild(this._scrollDiv);
		e.appendChild(wrapper);

		this._scrollDiv.style.position = 'absolute';
		wrapper.style.position = 'absolute';
		wrapper.style.height = b.height + 'px';
		wrapper.style.width = b.width + 'px'

		//this._scrollDiv.style.width = b.width + 'px';

		this._clip(wrapper, 0, 'auto', b.height, 'auto');
		this._move(this._scrollDiv, 0, b.height);

		this._position = this._startPosition = b.height;

		b = $common.getSize(this._scrollDiv);
		this._scrollDivHeight = b.height;

		e.style.visibility = 'visible';

		if (this._autoStart)
			this.start();
	},

	dispose: function() {
		delete this._scrollDelegate;

		CyberCoders.ScrollBehavior.callBaseMethod(this, 'dispose');
	},

	_scrollHandler: function() {
		var delta = parseInt(2 * this._scrollSpeed);
		var pause = false;

		if (this._pauseLength) {
			var y = $common.getLocation(this.get_element()).y;

			for (var i = 0; i < this._scrollDiv.childNodes.length && !pause; i++) {
				if (this._scrollDiv.childNodes[i].nodeType == 1) {
					var l = $common.getLocation(this._scrollDiv.childNodes[i]);

					if (y >= l.y - delta && y < l.y) {
						pause = true;
						delta = l.y - y;
					}
				}
			}
		}

		this._position -= delta;
		this._move(this._scrollDiv, 0, this._position);

		var c = true;

		if (this._position < -this._scrollDivHeight) {
			if (this._cycle) {
				this._position = this._startPosition;
			}
			else {
				c = false;
			}
		}

		if (c) {
			var that = this;
			var f = function() {
				window.setTimeout(that._scrollDelegate, 100);
			};

			if (pause) {
				window.setTimeout(f, this._pauseLength);
			}
			else {
				f();
			}
		}
	},

	_clip: function(e, t, l, b, r) {

		e.style.clip = String.format('rect({0},{1},{2},{3})', this._a(t), this._a(r), this._a(b), this._a(l));
	},

	_move: function(e, x, y) {
		$common.setLocation(e, { x: x, y: y });
	},

	_a: function(v) {
		return v == null || v === 'auto' ? 'auto' : v + 'px';
	}
}
CyberCoders.ScrollBehavior.registerClass('CyberCoders.ScrollBehavior', Sys.UI.Behavior);
