/**
 * Tabs class
 * @param {Object} tabs
 */
function Tabs (container, tabs) {
	this.container = $(container);					/* Tab container element */
	this.tabs = [];									/* Associative array of tabs */
	this._tab_heights = [];							/* Associative array of tab heights */
	this._tab = null;								/* Reference to active tab */
	
	this.init(tabs);
}
/**
 * Open tab instantly, used when opening tab first time
 * @param {Object} tab_name
 */
Tabs.prototype.setTab = function (tab_name) {
	
	if (this.tabs[tab_name])
	{
		var old_tab = this._tab;

		if (old_tab !== null)
		{
			if (tab_name == old_tab.tab_name)
			{
				old_tab = null;
			}else{
				old_tab.css({zIndex: '2', position: 'absolute', left: '0'});
			}
		}
		
		var branch = this.tabs[tab_name].branch;
		if (window.branches[branch])
		{
			for(var i in window.branches)
			{
				if (i != branch && window.branches.hasOwnProperty(i)) {
					window.branches[i].css({'width': '0px'});
					$('div.list_container', window.branches[i]).css({'paddingLeft': '0px', 'width': '0px', 'display': 'none'});
				}
			}
			
			$('.invisible', window.branches[branch]).css('visibility', 'visible');

			if (window.branches_count > 1) {
				container = $('div.list_container', window.branches[branch]).css({'width': '206px'}).animate({'paddingLeft': '87px'});
			}
				
			window.branches[branch].css({'width': '306px'});
		}
		
		this._tab = this.tabs[tab_name];
		this._tab.css({left: '400px', zIndex: '3'});

		this.container.animate({height: this._tab_heights[tab_name] + 'px'});
		this._tab.css({'left': 0});
		
		if (old_tab !== null)
		{
			old_tab.css({position: 'absolute', left: '400px'});
		}
		
		return true;
	}else{
		return (tab_name == this._tab.tab_name);
	}
}
/**
 * Open tab
 * @param {Object} tab_name Tab name (from title)
 */
Tabs.prototype.openTab = function (tab_name) {
	
	if (this.tabs[tab_name] && tab_name != this._tab.tab_name)
	{
		var old_tab = this._tab;
		old_tab.css({zIndex: '2', position: 'absolute', left: '0'});
		
		this._tab = this.tabs[tab_name];
		this._tab.css({left: '400px', zIndex: '3'});
		
		this.container.animate({height: this._tab_heights[tab_name] + 'px'});
		this._tab.animate({'left': 0}, 500, function () {
			old_tab.css({position: 'absolute', left: '400px'});
		});
		
		return true;
	}else{
		return (tab_name == this._tab.tab_name);
	}
};
/**
 * Class constructor
 * @param {Object} tabs Tab list
 */
Tabs.prototype.init = function (tabs) {
	var title = '';
	for(var i = 0, j = tabs.length; i < j; i++)
	{
		tabs[i] = $(tabs[i]);
		title = tabs[i].attr('title');
		
		tabs[i].tab_name = title;
		tabs[i].branch = null;
		
		for(var b in window.branches) {
			if (window.branches.hasOwnProperty(b) && tabs[i].hasClass(b))
			{
				tabs[i].branch = b;
			}
		}
		
		this.tabs[title] = tabs[i];
		this._tab_heights[title] = tabs[i].height();
		
		if (tabs[i].hasClass('initial'))
		{
			this._tab = tabs[i];
		}
	}
};

/**
 * Returns hash part from the url
 * "/we_hire/#tab-QA-Specialist" will return "tab-QA-Specialist"
 */
function getUrlHash(url)
{
	var url_hash = url.toString();
	var pos = url_hash.indexOf('#');

	if (pos == -1)
	{
		url_hash = '';
	}else{
		url_hash = url_hash.substr(pos + 1);
	}
	
	return url_hash;
}

/* Initialize */
$(window).bind('load', function () {
	var group_state = 0;		//0 - "We hire start page", 1 - vacancy listing
	
	var group_content = $('div.inner');
	var tab_outter_container = $('div.inner td.right');
	var tab_inner_container = $('div #tab_container');
	
	var objTabs = new Tabs(tab_inner_container, $('*[title~="tab-"]'));
	var tab_links = $('a[href~="#"]');
	
	/* Attaching event handling to the links */
	tab_links.click(function() {
		var anchor = this.href;
		var pos = anchor.indexOf('#');
		
		if (pos > -1)
		{
			anchor = 'tab-' + anchor.substr(pos + 1);

			if (group_state == 0) {
				objTabs.setTab(anchor);
			} else {
				objTabs.openTab(anchor);
			}
			
			group_state = 1;
			
			tab_links.css('fontWeight', 'normal');
			
			group_content.animate({marginLeft: '-352px'});
			tab_outter_container.animate({opacity: 1});
			
			$('a[href="#group-default"]').parent().slideDown();
			$(this).css('fontWeight', 'bold');
		}
	});
	
	$('a[href="#group-default"]').click(function () {
		group_content.animate({marginLeft: '0px'});
		tab_outter_container.animate({opacity: 0});
		tab_inner_container.animate({height: '200px'});
		tab_links.css('fontWeight', 'normal');
		
		for(var i in window.branches)
		{
			if (window.branches[i] && window.branches.hasOwnProperty(i))
			{
				if (window.branches_count > 1)
				{
					var obj = $('div.list_container', window.branches[i]);
					
					obj.animate({'width': '153px', 'paddingLeft': '0px'})				//Resizing content
					   .css({'display': 'block'});
						
					window.branches[i].css({'width': '153px'});							//Resizing container
					$('.invisible', window.branches[i]).css('visibility', 'hidden');	//Hiding elements, which should be invisible
				}
				else
				{
					$('div.list_container', window.branches[i]).css({'overflow': 'visible', 'width': 'auto'});
					window.branches[i].css({'width': '220px'});							//Resizing container
					$('.invisible', window.branches[i]).css('visibility', 'hidden');	//Hiding elements, which should be invisible
				}
			}
		}
		
		group_state = 0;
		
		$('a[href="#group-default"]').parent().slideUp();
	});
	
	var current_hash = getUrlHash(document.location);
	if (objTabs.setTab('tab-' + current_hash))
	{
		group_state = 1;
			
		tab_links.css('fontWeight', 'normal');
		
		group_content.css({marginLeft: '-352px'});
		tab_outter_container.css({opacity: 1});
		
		$('a[href="#group-default"]').parent().slideDown();
		$('a[href="#' + current_hash + '"]').css('fontWeight', 'bold');
	}
});