/**
 * Sidebar
 * 
 * Pos argument takes one of the values: 'left', 'right'
 * Max_width is a width of the sidebar
 *  
 * @todo Sidebar width should be calculated instead of beeing passed as max_width
 * 
 * @param {Object} object
 * @param {Object} pos
 * @param {Object} max_width
 */
function Sidebar(object, pos, max_width)
{
	this.object = $(object);							/* Sidebar container object */
	this.object_inner = $('.inner', this.object);		/* Sidebar content object */
	this.state = 0;										/* Sidebar state: 1 - visible, 0 - hidden */
	
	this.pos = (pos == 'right' ? 'right' : 'left');
	this.margin_start = (typeof max_width != 'string' ? max_width + 'px' : max_width);
	this.unit = 'px';
	
	var _self = this;
	
	this.object.click(function (e) {
		eventBuble(e);
	});

	this._init();
};
/**
 * Class constructor
 */
Sidebar.prototype._init = function () {
	if (this.pos == 'right')
	{
		var w = this.object.parent().width() - parseInt(this.margin_start);
		this.object.css({marginLeft: w + 'px'});
	}
	
	this.unit = (this.margin_start.indexOf('px') != -1 ? 'px' : this.margin_start.indexOf('em') != -1 ? 'em' : '%');
	this.object_inner.css({left: this.margin_start});
	this.object.css({display: 'none'});
};
/**
 * Show sidebar
 */
Sidebar.prototype.show = function () {
	if (this.state == 0)
	{
		this.state = 1;
		this.object.css({display: 'block'});
		this.object_inner.animate({left: '0' + this.unit});
	}
};
/**
 * Hide sidebar
 */
Sidebar.prototype.hide = function () {
	if (this.state == 1)
	{
		this.state = 0;
		var _self = this;
		this.object_inner.animate({left: this.margin_start}, function () {
			_self.object.css({display: 'none'});
		});
	}
};


/* Initialize JS for page */
$(document).ready(function () {
	/* Sidebar */
		var sb = new Sidebar($('#sidebar'), 'right', '270px');
		
		/* Show links */
		$('#content .heading .link a').click(function (e) {
			var h = $('#content').height() + 'px';
			$('#sidebar').css({height: h});
			$('#sidebar .inner').css({height: h});
			
			sb.show();
			eventBuble(e);
			return false;
		});
		
		/* Close link */
		$('#content #sidebar .controls a').click(function (e) {
			sb.hide();
			return false;
		});
	
	/* Tabs */
		/**
		 * @todo: Create reusable object Tabs instead of following
		 */
		var openTab = null;
		var openTabIndex = -1;
		
		var tabLinks = $('a[rel=contents]');
		var tabs = $('#content .projects .right .text');
		var tabHeights = [];
		
		/* Calculating size of the tabs */
		tabs.each(function (i){
			/* tabsIndex will allow to identify tabs */
			this.tabsIndex = i;
			var _self = $(this);
			if (!_self.hasClass('hidden'))
			{
				/* Active tab */
				tabHeights[i] = _self.height();
				openTab = _self;
			}else{
				/* Height can be calculated only if block is visible */
				_self.css({display: 'block'});
				tabHeights[i] = _self.height();
				_self.css({display: 'none'});
			}
		});
		
		/* Setting animation for tabs; listeners and effects for links */
		tabLinks.each(function () {
			/* Tab click event */
			$(this).click(function () {
				
				sb.hide();	//Hidding sidebar when tab is changed
				
				/* Getting tab element which will be opened */
				var href = $(this).attr('href');
				var href = (href.indexOf('#') != -1 ? href.substr(href.indexOf('#') + 1) : '');
				
				var tr_el = $(this).parent().parent();
				tr_el.siblings().removeClass('active');
				tr_el.addClass('active');
				
				if (href != '')
				{
					/* Tab content have title attribute the same as link hash value */
					var el = $('*[title=' + href + ']');
					el = el[0];

					if (el && el.tabsIndex != openTab[0].tabsIndex)
					{
						var tmp = openTab;
						openTab = $(el);						
						
						/* Resizing text container */
						var new_h = tabHeights[openTab[0].tabsIndex];
						$('#content .projects .right .inner').animate({height: new_h + 'px'});
						
						/* Slide old tab out, new in */
						jQuery.synchronize.start();
						
						openTab.slideDown();
						tmp.slideUp();
						
						jQuery.synchronize.end();
					}
				}
				
				return false;
			});
		});
});