// Page Specific JavaScript Document

$(function() {
/**
 * Enter description here...
 */
	var queryString = encodeURI(document.location.href).replace(/^[^¥?]+\/search\/universal\/results¥??/,'');	
	var params = _parseNamedQuery(queryString);

	var $tabs = $('#Tabs').tabs();

	$tabs.bind('tabsshow', function(e, ui) {});

	$('#Tabs > ul > li > a').unbind().click(function() {
		var href = $(this).attr('href');
		var tabKey = href.replace(/#Section_/,'');
		var q = $("#FormQ").val();
		window.location.href = '/search/universal/results/Form.q:' + q + '/Form.tab:' + tabKey + '/hl:ja';
		return false;
	});
	
	//alert(ui.panel.id);
	var tabIndex = 0, tabKey = params['Form.tab'];
	
	if (tabKey) {
		if (tabKey == 'People') {
			tabIndex = 1;
		} else if (tabKey == 'Groups') {
			tabIndex = 2;
		} else if (tabKey == 'Events') {
			tabIndex = 3;
		} else if (tabKey == 'BlogPosts') {
			tabIndex = 4;
		} else if (tabKey == 'SellnbuyItems') {
			tabIndex = 5;
		} else {
			tabIndex = 0;
		}
		if (tabIndex != 0) {
			$tabs.tabs('select', tabIndex); // switch to third tab
		}
	}
	
	var selected = $tabs.tabs('option', 'selected');
	//alert(selected);
	
	var html = $("#searchResults").html();
	$("#Section_" + params['Form.tab']).append(html);
	$('#Tabs').removeClass('hiddenElem');
	
/**
 * Enter description here...
 */
	var currentFocus = null;
	$(':text').focus( function() {
		currentFocus = this;
		$(currentFocus).css("background-color", "#FFFAC1");
	}).blur( function() {
		$(currentFocus).css("background-color", "#ffffff");
		currentFocus = null;
	}); 
});

/**
 * Enter description here...
 */
	function _parseNamedQuery(query) {
		var Params = {};
		if (!query) {return Params;}// return empty object
		var Pairs = query.split(/[/]/);
		for (var i=0; i<Pairs.length; i++) {
			var KeyVal = Pairs[i].split(':');
			if (! KeyVal || KeyVal.length != 2) {continue;}
			var key = unescape(KeyVal[0]);
			var val = unescape(KeyVal[1]);
			val = val.replace(/¥+/g, ' ');
			Params[key] = val;
		}
		return Params;
	};
