/***********************************************
* 
**/
function UpdateKwInfos(TabId)
{
	// alert("UpdateKwInfos("+TabId+")");
	var Url = 'index.php?module=audit&action=ShowKwInfos' ;
	params = 'data=' + DataFileName ;
	params+= '&tags=' + TabData[TabId].GetSerializedTags() ;
	params+= '&expr=' + GetCurrentTabExpr() ;
	params+= '&agreg=' + AgregatorObj.GetSerializedData() ;
	params+= '&comp1=' + TabData[TabId].GetCompetitor1() ;
	params+= '&comp2=' + TabData[TabId].GetCompetitor2() ;
	params+= '&kcounting=' + TabData[TabId].GetCountingWeight() ;
	params+= '&kdensity=' + TabData[TabId].GetDensityWeight() ;
	params+= '&min_car=' + i_AuditMinCar ;
	params+= '&blacklist=' + GetBlacklist('audit') ;
	params+= '&member_id=' + MemberId ;
	params+= '&member_key=' + MemberKey ;
	params+= '&audit_id=' + AuditId ;
	params+= '&tag_url_nb=' + TabData[TAB1].GetTagUrlNb() ;
	params+= '&tt_cols=' + TabData[TAB1].GetSerializedTTCols() ;
	params+= '&median=' + (($('b_Median').checked) ? 1 : 0) ;
	params+= '&blacklist_changed=' + ((BlacklistChanged) ? 1 : 0) ;
	
	// alert(params);
	var myAjax = new Ajax.Updater(TabId + '_KwStats', Url, 
	{	method: 'get', 
		parameters: params,
		onComplete: function() {InitKwInfos(TabId);}
	});
	
	// set flag off
	if(BlacklistChanged)	BlacklistChanged = false;
	
	// display loading icon
	SetDivAsLoading(TabId + '_KwStats') ;
}

/***********************************************
* get the expression of the current tab
**/
function GetCurrentTabExpr()
{
	return tabView_headerTabs[MainTabSet][CurrentTab];
	// return escape(tabView_headerTabs[MainTabSet][CurrentTab]);
}

/***********************************************
* 
**/
function InitKwInfos(TabId)
{
	var aDivs = ['Tips', 'Graph', 'Stats'] ;
	
	aDivs.each(function(DivClass){
		var DivTip = $$('#'+TabId+' div[class="'+DivClass+'"]') ;
		if(DivTip[0] != null)
		{
			if(readCookie("hb_kw_info_" + DivClass) == null)
			{
				// by default, only open the "Tips" div
				if(DivClass == 'Tips')
					DivTip[0].show();
			}
			else
			{
				if(readCookie("hb_kw_info_" + DivClass) == '1')
					DivTip[0].show();
				else
					DivTip[0].hide();
			}
		}
	});

	// alert("InitKwInfos("+TabId+")") ;
	// set the id of the tip table
	var TipTable = $$('#'+TabId+' table[class="TipTable"]') ;
	if(TipTable.length > 0)
		TipTable[0].id = TabId + '_TipTable' ;
	
	// set the id of the stat table
	var StatTable = $$('#'+TabId+' table[class="StatTable"]') ;
	if(StatTable.length > 0)
		StatTable[0].id = TabId + '_StatTable' ;
	
	// init the sortable tables
	if(TipTable.length > 0)
		initTableWidget(TabId + '_TipTable','100%',250, TabData[TabId].GetSortTypes());
	if(StatTable.length > 0)
		initTableWidget(TabId + '_StatTable','100%',250,Array('S', 'N', 'N', 'N', 'N', 'N'));
	
	// init sorts
	if(TipTable.length > 0)
	{
		if(readCookie('hb_tip_table_sort') == null)
			sortTable(TabId + '_TipTable', 1, 'ascending');
		else
		{
			var s_Tmp = readCookie('hb_tip_table_sort');
			var a_Tmp = s_Tmp.split(',');
			sortTable(TabId + '_TipTable', a_Tmp[1], a_Tmp[2]);
		}
	}
	
	if(StatTable.length > 0)
	{
		if(readCookie('hb_stat_table_sort') == null)
			sortTable(TabId + '_StatTable', 0, 'ascending');
		else
		{
			var s_Tmp = readCookie('hb_stat_table_sort');
			var a_Tmp = s_Tmp.split(',');
			sortTable(TabId + '_StatTable', a_Tmp[1], a_Tmp[2]);
		}
	}
}


/***********************************************
* 
**/
function UpdateKwGraph(TableName, ColNum, Direction)
{
	//alert("TableName="+TableName + ", ColNum="+ColNum+", Direction=" + Direction) ;

	var a_Tmp = TableName.split('_');
	var TableType = a_Tmp[ a_Tmp.length-1 ] ;
	
	var Sorts = new Hash();
	Sorts['TipTable'] = ['tag', 'tag', 'tag', 'efficiency', 'difficulty', 'tag'] ;
	Sorts['StatTable'] = ['tag', 'counting_1', 'counting_2', 'density_1', 'density_2', 'global_1', 'global_2'] ;
	
	if(Sorts[ TableType ][ ColNum ] == '')
		return ;
	
	var ImgObj = $(CurrentTab).getElementsByClassName('GraphImg') ;
	// alert(ImgObj[0].src) ;
	
	a_Tmp = ImgObj[0].src.split('?') ;
	a_TmpB = a_Tmp[1].split('&') ;
	var H_Params = new Hash() ;
	var a_TmpC ;
	a_TmpB.each(function(item){
		a_TmpC = item.split('=') ;
		H_Params[ a_TmpC[0] ] = a_TmpC[1] ;
	});
	
	H_Params['sort'] = Sorts[ TableType ][ ColNum ] ;
	H_Params['sort_dir'] = Direction ;	// ascending or descending
		
	var NewSrc = a_Tmp[0] + '?' ;
	H_Params.each(function(item){
		NewSrc += item.key + '=' + item.value + '&' ;
	});
	
	NewSrc = NewSrc.substr(0, NewSrc.length - 1) ;
	
	// alert(NewSrc) ;
	
	ImgObj[0].src = NewSrc ;
}


/***********************************************
* 
**/
function ToggleKwInfoPanel(ps_Type)
{
	var Outlet = $(CurrentTab).getElementsByClassName(ps_Type);
	
	if(Outlet[0].style.display == 'none')
		createCookie("hb_kw_info_" + ps_Type, 1, false) ;	// false = cookie expires at the end of the session
	else
		createCookie("hb_kw_info_" + ps_Type, 0, false) ;	// false = cookie expires at the end of the session
	
	if(Outlet[0] != null)
	{
		Outlet[0].toggle();
		return ;
	}
		
	debug_alert("no '"+ps_Type+"' outlet in the tab '"+CurrentTab+"'");
}

/***********************************************
*
**/
function GetAjaxUrlForNewTab()		{return 'index.php?module=audit&action=ShowKwInfos';}
