// -------------------------------------------------------------------------

if(typeof checkHash == 'undefined')
{
	var _window = $(window);
	var checkHash = function()
	{
		var hash = window.location.hash;

		if(hash != this.prevHash)
		{
			this.prevHash = hash;
			_window.trigger('hashChanged', hash.substring(1));
		}
	};

	if('onhashchange' in window)
	{
		_window.bind('hashchange', checkHash);
	}
	else if('onpopstate' in window)
	{
		_window.bind('popstate', checkHash);
	}
	else
	{
		window.setInterval('checkHash()', 100);
	}
}

// -------------------------------------------------------------------------

$(window).bind('hashChanged', function(event, hash)
{
	hash = 's_' + hash;
	if(hash == 's_sale')
	{
		sdpsmPageScroller.scrollToId(hash, {top:200, left:200});
	}
	else
	{
		sdpsmPageScroller.scrollToId(hash, {top:75, left:75});
	}
});


// -------------------------------------------------------------------------

var sdpsmPageScroller =
{
	// -------------------------

	t: false, // timer
	ew: false, // engine_works
	sS: {top:0, left:0}, // startScrolling
	cS: {top:0, left:0}, // currentScrolling
	sBS: {top:0, left:0}, // stepBackScrolling
	fS: {top:0, left:0}, // finishScrolling

	// -------------------------

	gS: function(x)
	{
		return parseInt(x/3) || 1;
	},

	// -------------------------

	eg: function()
	{
		this.cS.left = parseInt(document.documentElement.scrollLeft || document.body.scrollLeft || 0);
		this.cS.top = parseInt(document.documentElement.scrollTop || document.body.scrollTop || 0);

		var mustIwork = false;

		if(this.cS.top != this.fS.top && this.sBS.top != this.cS.top)
		{
			this.sBS.top = this.cS.top;

			var sign = this.cS.top - this.fS.top > 0 ? -1 : 1;

			var past = Math.abs(this.sS.top - this.cS.top);
			var future = Math.abs(this.fS.top - this.cS.top);

			this.cS.top += sign * this.gS(Math.min(past, future));

			mustIwork = true;
		}

		if(this.cS.left != this.fS.left && this.sBS.left != this.cS.left)
		{
			this.sBS.left = this.cS.left;

			var sign = this.cS.left - this.fS.left > 0 ? -1 : 1;

			var past = Math.abs(this.sS.left - this.cS.left);
			var future = Math.abs(this.fS.left - this.cS.left);

			this.cS.left += sign * this.gS(Math.min(past, future));

			mustIwork = true;
		}

		if(mustIwork) window.scrollTo(this.cS.left, this.cS.top);
		else this.stopeg();
	},

	// -------------------------

	stopeg: function()
	{
		window.clearInterval(this.t);
		this.ew = false;
	},

	// -------------------------
	
	scrollToObj: function(obj, offset)
	{
		if(obj == null)
		{
			return;
		}
		if(obj == '')
		{
			this.fS = {top:0, left:0};
		}
		else
		{
			this.fS = {top:0, left:0};
			do
			{
				this.fS.top += obj.offsetTop  || 0;
				this.fS.left += obj.offsetLeft || 0;
				obj = obj.offsetParent;
			}
			while (obj);
			
			offset = offset || {top:0, left:0};
			this.fS.top -= offset.top;
			this.fS.left -= offset.left;
		}

		if(this.fS)
		{
			if(!this.ew)
			{
				this.sS.left = parseInt(document.documentElement.scrollLeft || document.body.scrollLeft || 0);
				this.sS.top = parseInt(document.documentElement.scrollTop || document.body.scrollTop || 0);
			}
			this.sBS.left = this.sBS.top = -1;

			window.clearInterval(this.t);
			this.t = window.setInterval('sdpsmPageScroller.eg()', 10);
			this.ew = true;
		}

		return false;
	},
	
	// -------------------------

	scrollToId: function(id, offset)
	{
		return this.scrollToObj(document.getElementById(id), offset);
	}

	// -------------------------
};
