
var controlTabs = {
	
	getValue: function(key) {
	  var items = window.location.search.substring(1).split('&'); 
		for(var i  = 0; i < items.length; i++) {
			var item = items[i].split('=');
			if (item[0] == key) return item[1];
    }
		return false;
	},	
	
	init: function() {

		var $tabs = $('#tabs');
		var param = controlTabs.getValue('state');
		var $anchors = $tabs.find("li a");

    $tabs.tabs();

		if (param) {
			var current = $anchors.eq(param-1);
			History.pushState({index:param-1},current.attr('title'),current.attr('rel')); 
			$tabs.tabs("option","active", param - 1);
		}

		
		$anchors.each(function(i) {
			var index = i;
			$(this).click(function(event) {
				History.pushState({index:index},$(this).attr('title'),$(this).attr('rel'));
			});
		});

		$(window).on('statechange',function(){ 
			$tabs.tabs("option","active", History.getState().data.index);				
		});

		$(window).on('anchorchange', function() {
			$tabs.tabs("option","active", History.getState().data.index);				
		});
	}
}

$(document).ready(controlTabs.init);	
