var Point = function(x,y)
{
	this.x = x || 0;
	this.y = y || 0;
}

// ====================================================================================

var sdpsmPageScroller =
{
	// ====================================================================================

	timer: false,
	debug: false,
	engine_works: false,
	startScrolling: new Point(),
	currentScrolling: new Point(),
	stepBackScrolling: new Point(),
	finishScrolling: new Point(),

	// ====================================================================================

	getSpeed: function(x)
	{
		return parseInt(x/5) || 1;
	},

	// ====================================================================================

	engine: function()
	{
		var screenHeight = Math.min(document.documentElement.clientHeight != 0 ? document.documentElement.clientHeight : Infinity, document.body.clientHeight != 0 ? document.body.clientHeight : Infinity);
		var screenWidth = Math.min(document.documentElement.clientWidth != 0 ? document.documentElement.clientWidth : Infinity, document.body.clientWidth != 0 ? document.body.clientWidth : Infinity);

		this.currentScrolling = new Point();
		this.currentScrolling.x = parseInt(document.documentElement.scrollLeft || document.body.scrollLeft) || 0;
		this.currentScrolling.y = parseInt(document.documentElement.scrollTop || document.body.scrollTop) || 0;

		if((this.currentScrolling.x != this.finishScrolling.x || this.currentScrolling.y != this.finishScrolling.y) && (this.stepBackScrolling.x != this.currentScrolling.x || this.stepBackScrolling.y != this.currentScrolling.y))
		{
			// ====================================================================================

			this.stepBackScrolling = new Point(this.currentScrolling.x, this.currentScrolling.y);

			// ====================================================================================

			if(this.currentScrolling.y != this.finishScrolling.y)
			{
				// ====================================================================================

				var sign = this.currentScrolling.y - this.finishScrolling.y > 0 ? -1 : 1;

				var past = this.startScrolling.y - this.currentScrolling.y;
				past = past > 0 ? past : -past;
				var future = this.finishScrolling.y - this.currentScrolling.y;
				future = future > 0 ? future : -future;

				this.currentScrolling.y = this.currentScrolling.y + sign * this.getSpeed(Math.min(past, future));

				// ====================================================================================
			}

			if(this.currentScrolling.x != this.finishScrolling.x)
			{
				// ====================================================================================

				var sign = this.currentScrolling.x - this.finishScrolling.x > 0 ? -1 : 1;

				var past = this.startScrolling.x - this.currentScrolling.x;
				past = past > 0 ? past : -past;
				var future = this.finishScrolling.x - this.currentScrolling.x;
				future = future > 0 ? future : -future;

				this.currentScrolling.x = this.currentScrolling.x + sign * this.getSpeed(Math.min(past, future));

				// ====================================================================================
			}

			// ====================================================================================

			window.scrollTo(this.currentScrolling.x, this.currentScrolling.y);

			// ====================================================================================
		}
		else
		{
			this.stopEngine();
		}
	},

	// ====================================================================================

	stopEngine: function()
	{
		window.clearInterval(this.timer);
		if(this.debug) alert("Остановили таймер!");
		this.engine_works = false;
	},

	// ====================================================================================

	scrollToId: function(id)
	{
		if(id == "") this.finishScrolling = new Point(0, 0);
		else
		{
			var obj = document.getElementById(id);
			if(!obj) return true;

			var offsetTrail = obj;
			var offsetLeft = 0;
			var offsetTop = 0;
			while (offsetTrail)
			{
				offsetLeft += offsetTrail.offsetLeft;
				offsetTop += offsetTrail.offsetTop;
				offsetTrail = offsetTrail.offsetParent;
			}
			this.finishScrolling = new Point(offsetLeft, offsetTop);
		}

		if(!this.engine_works)
		{
			this.startScrolling = new Point();
			this.startScrolling.x = parseInt(document.documentElement.scrollLeft || document.body.scrollLeft) || 0;
			this.startScrolling.y = parseInt(document.documentElement.scrollTop || document.body.scrollTop) || 0;
		}

		this.stepBackScrolling = new Point(-1,-1);

		window.clearInterval(this.timer);
		this.timer = window.setInterval('sdpsmPageScroller.engine()', 21);
		this.engine_works = true;
		if(this.debug) alert("Запустили таймер! " + id);

		return false;
	},

	init: function()
	{
		var header_ul = document.getElementsByTagName("ul");
		if(!header_ul) return false;
		header_ul = header_ul[0]
		if(!header_ul) return false;
		var healer_lis = header_ul.getElementsByTagName("a");
		if(!healer_lis) return false;
		for(var i = 0, length = healer_lis.length; i < length; i ++)
		{
			var href = healer_lis[i].href;
			href = href.substr(href.indexOf("#")+1);
			switch(href)
			{
				/*			case "to_studios_and_freelancers":
				healer_lis[i].onclick = function(link){sdpsmXpander.xpand("orange_two");return sdpsmPageScroller.scrollToId(this.href.substr(this.href.indexOf("#")+1))};
				break;
				case "to_managers":
				healer_lis[i].onclick = function(link){sdpsmXpander.xpand("orange_three");return sdpsmPageScroller.scrollToId(this.href.substr(this.href.indexOf("#")+1))};
				break;
				case "to_direct_clients":
				healer_lis[i].onclick = function(link){sdpsmXpander.xpand("orange_four");return sdpsmPageScroller.scrollToId(this.href.substr(this.href.indexOf("#")+1))};
				break;*/
				default:
				healer_lis[i].onclick = function(link){return sdpsmPageScroller.scrollToId(this.href.substr(this.href.indexOf("#")+1))};
				break;
			}
		}
		return true;
	}

	// ====================================================================================
}
sdpsmPageScroller.init();
