
(function ($) {
	SBM.tpl.tabs = {
		hash:    /#(\w+)$/.test(location.href) ? RegExp.$1 : null,
		content: null,
		
		/**
		 * @return  {object}
		 */
		init: function () {
			$(this.bind(this.ready));
			return this;
		},
	
	/**
	 * @param   {object}  self
	 */
	ready: function (self) {
		$('#tabs li').each(function () {
			$('#container-top').prepend('<div id="' + this.className + '"></div>');
		});
		
		var tab;
		if (this.hash && $('#tabs li.' + this.hash).size())
			tab = $('#tabs li.' + this.hash);
		else
			tab = $('#tabs li:first');
		
		this.open(tab.find('a').get(0));
		$('#tabs li a').click(this.bind(this.open));
	},
	
	/**
	 * @param   {element}  el
	 * @return  {boolean}
	 */
	open: function (el) {
		$('#tabs li a.on').removeClass('on');
		if (this.content)
			this.content.hide();
		
		var tab      = $(el).parent();
		var hash     = this.hash;
		this.hash    = tab.attr('class');
		this.content = $('#layout div.content[id=' + this.hash + ']');
		this.content.show();
		el.className = 'on';
		
		if (hash)
			this.scroll(this.hash);
		
		return false;
	},
	
	/**
	 * @param   {string}  hash
	 */
	scroll: (function () {
		var timer;
		var clear = function () {
			if (timer)
				clearInterval(timer);
		};
		
		return function (hash) {
			clear();
			
			var from = $(window).scrollTop();
			var to   = $('#tabs').position().top;
			
			timer = setInterval(function () {
				from -= Math.floor((from - to) / 3);
				$(window).scrollTop(from);
				if (from == to) {
/*‚±‚±					if (SBM.env.ua != 'OP')
						location.hash = hash;*/
					clear();
				}
			}, 50);
			
/*‚±‚±		if (SBM.env.ua == 'FF')
				document.body.addEventListener('DOMMouseScroll', function () {
					clear();
					this.removeEventListener('DOMMouseScroll', arguments.callee, false);
				}, false);
			else
				document.onmousewheel = function () {
					clear();
					document.onmousewheel = null;
				};*/
		};
	})(),

	bind: function (fn) {
		var self = this;
		return function () {
			return fn.apply(self, [this].concat(Array.prototype.slice.call(arguments)));
		};
	}
}.init()})(jQuery);