var SEO_SETTINGS_TAB_NAME = 'SettingSEO' ;

/***********************************************
* init the web 2.0 style dialog box
**/
function InitSEOSettings()
{
	// the background seo setting panel
	var SettingPan = document.createElement('DIV');
	Element.extend(SettingPan);
	
	var SetW = 650;
	var SetH = 580;
	var a_PageDim = GetPageSize() ;
	
	SettingPan.id = SEO_SETTINGS_TAB_NAME ;
	SettingPan.style.zIndex = 1000 ;
	// SettingPan.style.position = 'absolute' ;
	SettingPan.style.top = ((a_PageDim[3] / 2) - (SetH / 2)) + 'px' ;
	SettingPan.style.left = ((a_PageDim[2] / 2) - (SetW / 2)) + 'px' ;
	// SettingPan.style.background = '#ffffff' ;
	// SettingPan.style.border = '1px solid #444' ;
	SettingPan.style.width = SetW + 'px' ;
	SettingPan.style.height = SetH + 'px' ;
	SettingPan.addClassName('SettingPanel') ;
	SettingPan.hide();
	
	var ABody = document.getElementsByTagName("body") ;
	ABody[0].appendChild(SettingPan) ;
	
	// the title
	var DivTitle = document.createElement('H1');
	Element.extend(DivTitle);
	DivTitle.innerHTML = txt_MySeoParams ;
	DivTitle.addClassName('setting_title') ;
	SettingPan.appendChild(DivTitle) ;
	
	// div to contain the tabs of the dialog box
	var DivTabs = document.createElement('div');
	Element.extend(DivTabs);
	DivTabs.id = 'SEOSettingTabs' ;	// just like MainTabs or SettingDisplayTabs
	SettingPan.appendChild(DivTabs) ;
	
	// div for tab1 = current tab of the main gui
	var DivTab1 = GiveMeATab() ;
	DivTabs.appendChild(DivTab1) ;
	
	// div for tab2 = custom settings
	var DivTab2 = GiveMeATab() ;
	DivTabs.appendChild(DivTab2) ;
	
	// the seo param form for tab 1 (the current tab)
	var FormParamTab1 = document.createElement('form');
	Element.extend(FormParamTab1);
	FormParamTab1.name = 'form_curtab_seo' ;
	FormParamTab1.id = SEO_SETTINGS_TAB_NAME + '_Tab1' ;
	FormParamTab1.addClassName('SEOParam') ;
	DivTab1.appendChild(FormParamTab1) ;
	// no need to init tab1 => it'll be done on the opening
	
	// the seo param form for tab 2 (the settings)
	var FormParamTab2 = document.createElement('form');
	Element.extend(FormParamTab2);
	FormParamTab2.name = 'form_setting_seo' ;
	FormParamTab2.id = SEO_SETTINGS_TAB_NAME + '_Tab2' ;
	FormParamTab2.addClassName('SEOParam') ;
	DivTab2.appendChild(FormParamTab2) ;
	
	// init tab2
	InitSEOParam(FormParamTab2) ;
	if(MemberId != -1)
	{
		var CountingDensityResult = $(FormParamTab2.id).getInputs('hidden', 'CountingDensitySliderResult') ;
		form_widget_amount_slider('SettingSEO_Tab2_CountingDensitySlider', CountingDensityResult[0], 200, 0,100,"UpdateCountingDensity('"+FormParamTab2.id+"')");
	}
	
	var i_ActiveTab = 0 ;	// the first tab is active
	initTabs('SEOSettingTabs', Array(txt_CurrentTab, txt_CustomSettings), i_ActiveTab,SetW,'auto',Array(false, false));
}

/***********************************************
*
**/
function InitSEOParam(pFormParamObj)
{
	// in public audits, tab2 is not needed => display message instead
	if( (pFormParamObj.id == 'SettingSEO_Tab2') && (MemberId == -1) )
	{
		var DivText1 = document.createElement('div');
		Element.extend(DivText1);
		DivText1.innerHTML = txt_SeoUserPrefsNotAvailable ;
		DivText1.addClassName("ico_info_top") ;
		DivText1.style.border = "1px solid #f60" ;
		DivText1.style.fontSize = "14px" ;
		pFormParamObj.appendChild(DivText1) ;
		
		var DivBtn = document.createElement('div');
		Element.extend(DivBtn);
		DivBtn.innerHTML = '<a href="#" onclick="HideSEOSettings(); return false" class="bt_cancel bt_small" style="margin: 0">' + txt_Close + '</a>' ;
		DivBtn.style.height = "25px" ;
		DivBtn.style.marginTop = "10px" ;
		pFormParamObj.appendChild(DivBtn) ;
		
		return ;
	}

	// add fieldset 1 of the form
	var FieldSet1 = document.createElement('fieldset');
	Element.extend(FieldSet1);
	FieldSet1.addClassName("SettingFieldSet") ;
	pFormParamObj.appendChild(FieldSet1) ;
	
	// ... and its legend
	var Legend1 = document.createElement('legend');
	Element.extend(Legend1);
	Legend1.innerHTML = txt_CountingAndDensity ;
	FieldSet1.appendChild(Legend1) ;
	
	// ... some text in it
	var DivText1 = document.createElement('p');
	Element.extend(DivText1);
	DivText1.innerHTML = txt_SeoParamsTxt1 ;
	DivText1.setStyle({textAlign: 'justify'});
	FieldSet1.appendChild(DivText1) ;

	// adding the counting / density slider
	var KCounting ;
	var KDensity ;
	
	if(pFormParamObj.id == 'SettingSEO_Tab2')
	{
		KCounting = DefaultSettings.GetCountingWeight() ;
		KDensity = DefaultSettings.GetDensityWeight() ;
	}
	else
	{
		KCounting = TabData['SettingSEO_Tab1'].GetCountingWeight() ;
		KDensity = TabData['SettingSEO_Tab1'].GetDensityWeight() ;
	}
	
	// ...the hidden input field
	var InputSliderResult = document.createElement('INPUT') ;
	Element.extend(InputSliderResult);
	InputSliderResult.type = "hidden" ;
	InputSliderResult.name = "CountingDensitySliderResult" ;
	InputSliderResult.addClassName('CountingDensitySliderResult') ;
	InputSliderResult.value = KDensity * 10 ;
	FieldSet1.appendChild(InputSliderResult) ;
	
	// this is a div that contains the slider
	var DivSlider = document.createElement('DIV') ;
	DivSlider.style.width = '400px' ;
	//DivSlider.style.border = '1px solid #000' ;
	DivSlider.style.background = '#eee' ;
	DivSlider.style.paddingTop = '5px' ;
	DivSlider.style.marginLeft = 'auto' ;
	DivSlider.style.marginRight = 'auto' ;
	DivSlider.style.height = '20px' ;
	FieldSet1.appendChild(DivSlider) ;
	
	// ...the counting label
	var DivCounting = document.createElement('DIV') ;
	Element.extend(DivCounting);
	DivCounting.addClassName('SliderLabel');
	DivCounting.innerHTML = '<span class="set_counting" style="margin: 0px 10px;">'+KCounting+'</span>' + txt_Counting ;
	DivSlider.appendChild(DivCounting) ;
	
	// ...the slider
	var DivSliderBar = document.createElement('DIV') ;
	Element.extend(DivSliderBar);
	DivSliderBar.id = pFormParamObj.id + '_CountingDensitySlider' ;
	DivSliderBar.addClassName('FloatLeft') ;
	DivSliderBar.style.width = '200px';
	DivSlider.appendChild(DivSliderBar) ;
	
	// ...the density label
	var DivDensity = document.createElement('DIV') ;
	Element.extend(DivDensity);
	DivDensity.addClassName('SliderLabel');
	DivDensity.style.marginLeft = '20px';
	DivDensity.innerHTML = txt_Density + '<span class="set_density" style="margin: 0px 10px;">'+KDensity+'</span>' ;
	DivSlider.appendChild(DivDensity) ;
	
	// fieldset 2 of the tab
	var FieldSet2 = document.createElement('fieldset');
	Element.extend(FieldSet2);
	FieldSet2.addClassName("SettingFieldSet") ;
	pFormParamObj.appendChild(FieldSet2) ;
	
	// ... and its legend
	var Legend2 = document.createElement('legend');
	Element.extend(Legend2);
	Legend2.innerHTML = txt_TagWeights ;
	FieldSet2.appendChild(Legend2) ;
	
	// ... some text in it
	var DivText2 = document.createElement('p');
	Element.extend(DivText2);
	DivText2.innerHTML = txt_SeoParamsTxt2 ;
	DivText2.setStyle({textAlign: 'justify'});
	FieldSet2.appendChild(DivText2) ;
	
	var H_TagList ;
	if(pFormParamObj.id == 'SettingSEO_Tab2')
		H_TagList = DefaultSettings.GetTags() ;
	else
		H_TagList = TabData['SettingSEO_Tab1'].GetTags() ;
	
	// UL tag in order to put the tag LIs in
	var DivUl = document.createElement('div');
	DivUl.style.background = "#eee";
	FieldSet2.appendChild(DivUl) ;
	
	var TagUList = document.createElement('ul');
	Element.extend(TagUList);
	TagUList.addClassName('TagList') ;
	DivUl.appendChild(TagUList) ;
	
	// fill the tag list with the current tab tags & weights
	var s_Checked ;
	H_TagList.each(function(pair)
	{
		var TagItem = document.createElement('li');
		Element.extend(TagItem);
		
		s_Checked = '' ;
		if(pair.value > 0)		s_Checked = 'checked' ;
		
		// each item has a checkbox...
		var s_Code = '<input type="checkbox" class="'+pair.key+'" name="chk_'+pair.key+'"' ;
		s_Code += ' onclick="ToggleWeight(\''+pFormParamObj.id+'\', \''+pair.key+'\');" value="1" '+s_Checked+' /> ' ;
		// ... a listbox to choose the weight of the tag
		// s_Code += '<select class="'+pair.key+'" name="w_'+pair.key+'" onclick="UpdateHtmlTagWeight(\''+pTabId+'\', \''+pair.key+'\', this.value);">' ;
		s_Code += '<select style="font-size: 1.1em;" class="'+pair.key+'" name="w_'+pair.key+'">' ;
		for(j = 1 ; j <= 10 ; j++)
		{
			var s_Selected = '' ;
			if(j == pair.value)	s_Selected = ' selected' ;
			s_Code += '<option value="'+j+'"'+s_Selected+'>'+j+'</option>' ;
		}
		s_Code += '</select> ' ;
		// ... and the name of the tag (before it was in span & label tags)
		// s_Code += '<span id="TL_tag_content_'+i+'">' ;
		// s_Code += '<label for="TL_tag_'+pair.key+'">'+pair.key+'</label>' ;
		// s_Code += '</span>' ;
		s_Code += eval('txt_Tag_'+pair.key) ;
		
		TagItem.innerHTML = s_Code ;
		TagItem.setStyle({margin:0,padding:0});
		TagUList.appendChild(TagItem);
		if(s_Checked == '')
		{
			var SelectCombos = $$('#'+pFormParamObj.id+' select[name="w_'+pair.key+'"]');
			// je sais pas pkoi, mais si on retire cette ligne ça marche plus !
			// on a alors : message: Statement on line 256: Type mismatch (usually a non-object value used where an object is required)
			SelectCombos.inspect();
			if(SelectCombos.length > 0)
				SelectCombos[0].disable() ;
		}
	}) ;
	
	// add [apply] button
	var DivButtons = document.createElement('div');
	Element.extend(DivButtons);
	// DivButtons.style.clear = 'both' ;
	DivButtons.setStyle({paddingTop:'10px'});
	// DivButtons.style.width = '500px' ;
	var DivButtonsContent = '' ;
	if(pFormParamObj.id == 'SettingSEO_Tab2')
	{
		DivButtonsContent = '<div style="float: left; height: 20px;"><a href="#" onclick="SaveTabSEOParamAsSettings(\''+pFormParamObj.id+'\'); return false" class="bt_simple2 bt_small">' + txt_SaveAsSettings + '</a>' ;
		DivButtonsContent += '<span id="'+pFormParamObj.id+'_SetSaveResult" style="margin: 10px; width: 400px; float: left;">&nbsp;</span></div>' ;
		DivButtonsContent += '<div style="float: right; height: 20px;"><a href="#" onclick="HideSEOSettings(); return false" class="bt_cancel bt_small" style="margin: 0">' + txt_Close + '</a></div><div style="height: 20px;">&nbsp;</div>' ;
	}
	else
	{
		DivButtonsContent = '<div style="float: left; height: 20px;"><a href="#" onclick="ApplySEOParams(\''+pFormParamObj.id+'\'); return false" class="bt_simple2 bt_small" style="margin-right: 5px">' + txt_ApplyToCurrentTab + '</a>' ;
		DivButtonsContent += '<a href="#" onclick="RestoreTagSettingsInTab(\''+pFormParamObj.id+'\'); return false" class="bt_simple2 bt_small" style="margin: 0px 5px">' + txt_RestoreSettings + '</a>' ;
		DivButtonsContent += '<a href="#" onclick="ApplyTagParamToAllTags(\''+pFormParamObj.id+'\'); return false" class="bt_simple2 bt_small" style="margin: 0px 5px">' + txt_ApplyToAllTabs + '</a>&nbsp;</div>' ;
		DivButtonsContent += '<div style="float: right; height: 20px;"><a href="#" onclick="HideSEOSettings(); return false" class="bt_cancel bt_small" style="margin: 0">' + txt_Close + '</a></div><div style="height: 20px;">&nbsp;</div>' ;
	}
	DivButtons.innerHTML = DivButtonsContent ;
	
	pFormParamObj.appendChild(DivButtons);
}

/***********************************************
* apply tab1 seo settings to current tab (data structure + current tab refresh)
**/
function ApplySEOParams(FormId)
{
	// update the data structure
	UpdateSEOSettingsDataStructure(FormId) ;
	
	// then apply to the current tab
	UpdateCurrentTab() ;
}

/***********************************************
* update TabData[CurrentTab] if tab1 OR DefaultSettings if tab2
**/
function UpdateSEOSettingsDataStructure(FormId)
{
	var SEOParamForm = $(FormId) ;
	// update counting / density in data structure
	var aCounting = SEOParamForm.getElementsByClassName('set_counting') ;
	var aDensity = SEOParamForm.getElementsByClassName('set_density') ;
	if(FormId == 'SettingSEO_Tab2')
		DefaultSettings.SetCountingDensityWeights(aCounting[0].innerHTML, aDensity[0].innerHTML) ;
	else
		TabData[CurrentTab].SetCountingDensityWeights(aCounting[0].innerHTML, aDensity[0].innerHTML) ;
	
	// update tags in data structure
	var s_Tag = '' ;
	SEOParamForm.getElements().each(function(elt)
	{
		if((elt.type == 'select-one') || (elt.type == 'select'))
		{
			s_Tag = elt.name.replace(/^w_/, "") ;
			// CheckBoxes = SEOParamForm.getInputs('checkbox', 'chk_' + s_Tag) ;
			// c pareil que la ligne en dessous mais ça fait moins j'me la joue prototype ;-)
			CheckBoxes = $$('#'+FormId+' input[name="chk_'+s_Tag+'"]');
			
			if(FormId == 'SettingSEO_Tab2')
				DefaultSettings.Tags[s_Tag] = (CheckBoxes[0].checked) ? elt.value : 0 ;
			else
				TabData[CurrentTab].Tags[s_Tag] = (CheckBoxes[0].checked) ? elt.value : 0 ;
		}
	}) ;
}

/***********************************************
* display seo settings dialog box
*	=> copy current tab seo settings to seo settings tab 1 before showing
**/
function ShowSEOSettings()
{
	UpdateParamsFromCurrentTab() ;

	HideMathSelectBoxes();
	ShowVeil();
	$(SEO_SETTINGS_TAB_NAME).show() ;
}

/***********************************************
* hide seo settings dialog box
**/
function HideSEOSettings()
{
	ShowMathSelectBoxes();
	$(SEO_SETTINGS_TAB_NAME).hide() ;
	HideVeil();
}


/***********************************************
* enable or disable to weight of a html tag
**/
function ToggleWeight(ps_FormId, ps_Tag)
{
	var CheckBoxes = $$('#'+ps_FormId+' input[name="chk_'+ps_Tag+'"]');
	var SelectCombos = $$('#'+ps_FormId+' select[name="w_'+ps_Tag+'"]');
	
	if((CheckBoxes.length == 0) || (SelectCombos.length == 0))
	{
		debug_alert("WARNING : no checkbox found or no select combo found for "+ps_TabId+"/"+ps_Tag) ;
		return ;
	}
	
	// alert(CheckBoxes[0].inspect());
	// il faut laisser cette ligne, je sais pas pkoi
	SelectCombos[0].inspect();
	
	if(CheckBoxes[0].checked)
		SelectCombos[0].enable() ;
	else
		SelectCombos[0].disable() ;
}


/***********************************************
* apply seo setting tab 1 to all main tabs of the gui
**/
function ApplyTagParamToAllTags(FormId)
{
	UpdateSEOSettingsDataStructure(FormId) ;
	
	var SetCountingDensityResult = $(FormId).getElementsByClassName('CountingDensitySliderResult') ;

	tabView_headerTabs[MainTabSet].each(
		function(item)
		{
			// do nothing for the current tab of course neither the setting panel
			// if( (item.key == CurrentTab) || (item.key == SEO_SETTINGS_TAB_NAME) )	return ;
			if(item.key == CurrentTab)	return ;
			
			// alert('applying to ' + item.key) ;
			// apply counting & density
			TabData[item.key].SetCountingDensityWeights(
				TabData[CurrentTab].GetCountingWeight(), 
				TabData[CurrentTab].GetDensityWeight()
			) ;
			
			// apply tags
			TabData[CurrentTab].GetTags().each(function(pair) {
				TabData[item.key].Tags[pair.key] = pair.value ;
			});
		}
	) ;
	
	UpdateAllTabs();
}


/***********************************************
* save seo settings to the server
**/
function SaveTabSEOParamAsSettings(FormId)
{
	// update on the server side
	var Url = 'index.php?module=audit&action=SaveSEOSettings' ;
	params = 'data=' + DataFileName ;
	params+= '&' + $(FormId).serialize() ;
	params+= '&member_id=' + MemberId ;
	params+= '&member_key=' + MemberKey ;
	// alert(params);
	var myAjax = new Ajax.Updater(FormId + '_SetSaveResult', Url, 
	{	method: 'post', 
		parameters: params
	});
	$(FormId + '_SetSaveResult').innerHTML = 'saving...' ;
	
	// update on the client side (DefaultSettings data scruture)
	UpdateSEOSettingsDataStructure(FormId) ;
}


/***********************************************
* restore default settings in the tab 1 of the seo settings
**/
function RestoreTagSettingsInTab(FormId)
{
	// copy from default to tab data structure
	DefaultSettings.GetTags().each(function(pair)	{
		TabData['SettingSEO_Tab1'].Tags[pair.key] = pair.value ;
	});
	TabData['SettingSEO_Tab1'].SetCountingDensityWeights(
		DefaultSettings.GetCountingWeight(), 
		DefaultSettings.GetDensityWeight()
	) ;
	
	// remove all the childs of the seo param form of the tab
	var SEOParamForm = $(FormId) ;
	SEOParamForm.immediateDescendants().each(function(childElt)
	{
		SEOParamForm.removeChild(childElt) ;
	});
	
	InitSEOParam(SEOParamForm) ;
	var CountingDensityResult = SEOParamForm.getInputs('hidden', 'CountingDensitySliderResult') ;
	CountingDensityResult[0].value = TabData['SettingSEO_Tab1'].GetDensityWeight() * 10 ;
	form_widget_amount_slider(FormId + '_CountingDensitySlider', CountingDensityResult[0], 200, 0,100,"UpdateCountingDensity('"+FormId+"')");
}

/*******************************************************
* copy current tab seo settings to seo settings tab 1 before showing
*	then re-init the form to take the new values
**/
function UpdateParamsFromCurrentTab()
{
	var s_SettingFormId = 'SettingSEO_Tab1' ;

	TabData[CurrentTab].GetTags().each(function(pair)	{
		TabData[s_SettingFormId].Tags[pair.key] = pair.value ;
	});
	
	TabData[s_SettingFormId].SetCountingDensityWeights(
		TabData[CurrentTab].GetCountingWeight(), 
		TabData[CurrentTab].GetDensityWeight()
	) ;
	
	
	// remove all the childs of the seo param form of the tab
	var SEOParamForm = $(s_SettingFormId) ;
	SEOParamForm.immediateDescendants().each(function(childElt)
	{
		SEOParamForm.removeChild(childElt) ;
	});
	
	InitSEOParam(SEOParamForm) ;
	var CountingDensityResult = SEOParamForm.getInputs('hidden', 'CountingDensitySliderResult') ;
	CountingDensityResult[0].value = TabData[CurrentTab].GetDensityWeight() * 10 ;
	form_widget_amount_slider(s_SettingFormId + '_CountingDensitySlider', CountingDensityResult[0], 200, 0,100,"UpdateCountingDensity('"+s_SettingFormId+"')");
}


/***********************************************
* update both counting & density infos when moving the slider of a seo setting tab (either tab 1 or 2)
**/
function UpdateCountingDensity(FormId)
{
	var a_Couting = $(FormId).getElementsByClassName('set_counting') ;
	var a_Density = $(FormId).getElementsByClassName('set_density') ;
	var CountingDensityResult = $(FormId).getInputs('hidden', 'CountingDensitySliderResult') ;
	
	if(a_Couting.length > 0)	a_Couting[0].innerHTML = Math.min(10, Math.round(10 - (CountingDensityResult[0].getValue() / 10))) ;
	if(a_Density.length > 0)	a_Density[0].innerHTML = Math.min(10, Math.round(CountingDensityResult[0].getValue() / 10)) ;
}