/*1*/
var products = {
	name :'Product and Services',
	lien :  config_products.link_xml,
	nameDiv : 'conteneur_global_products',
	config : config_products
}

// fonction pout tout les outils
// initialisation du gabarit de l'outil
function initConfig(outil_config){
	if (outil_config.libelle != "") {
		var id_libelle = 'libelle_' + outil_config.id_outil;
		$(id_libelle).setHTML(outil_config.libelle);
	}
	if (outil_config.name_link != "") {
		var id_more = 'more_' + outil_config.id_outil;
		
		if ($(id_more) != false) {
			if ($(id_more) != null) {
			
				if (outil_config.the_link == "#") {
					$(id_more).setProperty('href', outil_config.the_link + outil_config.id_outil);
				}
				else {
					$(id_more).setProperty('href', outil_config.the_link);
				}
				$(id_more).setHTML(outil_config.name_link);
			}
		}
	}
}

//initialisation de la select box des countries

function initSelect_products_countries(responseXML)
{
	var index=0;
	
	//Pinouf // 16/01/2008 // rajout select box dynamic des libell?s
	var tab_product_libelle = [select_services_product,select_site_product];
	$('products_contenu').getElements('option').each(function(item,index){
		item.setHTML(tab_product_libelle[index]);
	});
	// fin rajout
	var opac_button = new Fx.Styles('valide_go_products', {duration:750, wait:false});
	opac_button.start({'opacity':0.3});
	
	
	initSelect_products_services_link (responseXML);
}

//initialisation de la select box des liens et des services quand on choisi un pays
function initSelect_products_services_link(responseXML)
{
	var opac_button = new Fx.Styles('valide_go_products', {duration:750, wait:false});
	opac_button.start({'opacity':0.3});
	$('select_services').empty();
	$('select_websites').empty();

	var elem_option_vide = new Element('option',{'value':0});
	elem_option_vide.setHTML(select_services_product);
	$('select_services').appendChild(elem_option_vide);
	var elem_option_vide_link = new Element('option',{'value':0});
	elem_option_vide_link.setHTML(select_site_product);
	$('select_websites').appendChild(elem_option_vide_link);
	var xml_countries = responseXML.getElementsByTagName("country");
	xml_countries = $A(xml_countries);
	$('select_services').disabled=false;
	$('select_websites').disabled=false;

	var verif = $A([]);
	xml_countries.each(function(item,index){
				//console.log(item)		
		if(item.previousSibling.firstChild != null){
			var service = item.previousSibling.firstChild.nodeValue;
			if(!verif.contains(service))
			{
				verif.include(service);
				var elem_option = new Element('option',{'value':service});
				elem_option.setHTML(service);
				$('select_services').appendChild(elem_option);
			}
		}
		//si la balise du website n'est pas vide
		if(item.parentNode.lastChild.firstChild != null){
			var webSite =  item.parentNode.lastChild.firstChild.nodeValue;
			if(!verif.contains(webSite))
			{
				verif.include(webSite);
				var elem_option_link = new Element('option',{'value':webSite});
				elem_option_link.setHTML(webSite);
				$('select_websites').appendChild(elem_option_link);
			}
		}
	});
		
}

// choix du services , filtre sous la select du website
// la select du website peut rester vide
function select_services(responseXML)
{
	if($('select_services').value != 0 )
	{			
		var opac_button = new Fx.Styles('valide_go_products', {duration:750, wait:false});
		opac_button.start({'opacity':1});
		$('valide_go_products').disabled=false;
		var xml_websites = responseXML.getElementsByTagName("website");
		xml_websites = $A(xml_websites);
		var select_service = $('select_services').value;
		$('select_websites').empty();
		var elem_option_vide_link = new Element('option',{'value':0});
		elem_option_vide_link.setHTML(select_site_product);
		$('select_websites').appendChild(elem_option_vide_link);
		xml_websites.each(function(item,index){
			if(item.parentNode.firstChild.firstChild != null) // service no null
			{
				if(select_service == item.parentNode.firstChild.firstChild.nodeValue){
					if(item.firstChild != null) // website no null
					{
						var webSite = item.firstChild.nodeValue;
						var elem_option_link = new Element('option',{'value':webSite});
						elem_option_link.setHTML(webSite);
						$('select_websites').appendChild(elem_option_link);
					}
				}
			}
		});
		
	}
	else if($('select_services').value == 0) {
		$('valide_go_products').disabled=true;
		this.sendRequest(products.lien,"GET",initSelect_products_services_link);
	}
}

// choix du website , filtre sous la select du service
// la select du service peut rester vide
function select_websites(responseXML)
{
	if($('select_websites').value != 0)
	{
		var opac_button = new Fx.Styles('valide_go_products', {duration:750, wait:false});
		opac_button.start({'opacity':1});
		$('valide_go_products').disabled=false;
	}
	else if($('select_websites').value == 0) {
		$('valide_go_products').disabled=true;
		this.sendRequest(products.lien,"GET",initSelect_products_services_link);
	}
}

function valide_link_product(responseXML)
{
	var select_website = $('select_websites').value;
	var select_service = $('select_services').value;
	var xml_products = responseXML.getElementsByTagName("product");
	xml_products = $A(xml_products);
	
	//Pinouf // 16/01/2008 // Modif pour link popup
	if(select_website == 0){
		xml_products.each(function(item,index){
			if(item.childNodes[0].firstChild){
				if(select_service == item.childNodes[0].firstChild.nodeValue && !item.childNodes[3].firstChild)
				{
					link_open = item.attributes.getNamedItem('link').nodeValue;
					return window.location = (item.attributes.getNamedItem('link').nodeValue);
				}
				else
				{
					link_open = '#';
				}
			}
		});

	}
	else if(select_service == 0)
	{
		xml_products.each(function(item,index){
			if(item.childNodes[3].firstChild){
				if(select_website == item.childNodes[3].firstChild.nodeValue)
				{
					return window.location = item.attributes.getNamedItem('link').nodeValue;
					link_open = item.attributes.getNamedItem('link').nodeValue;
				}
				else
				{
					link_open = '#';
				}
			}
		});

		//window.open(link_open);
	//	window.open(link_open,'',params_popup_bubble);
		
	}
	else if(select_service !=0 && select_website !=0 )
	{
		xml_products.each(function(item,index){
			if(select_website == item.childNodes[3].firstChild.nodeValue && select_service == item.childNodes[0].firstChild.nodeValue)
			{
				return window.location = item.attributes.getNamedItem('link').nodeValue;
			}
			else
			{
				link_open = '#';
			}
		});
	}
	else
	{
    link_open = '#';
	}
	
	window.location = link_open;
}

// rajout du message sur la tool de product selon la config 
function addMessage_tools()
{
	if(typeof(products_message_tools) == 'string'
		&& boxExist('txt_product'))
	{
		$('txt_product').setHTML(products_message_tools);
	}
}

function load_products_int() {
	initConfig(products.config);
	sendRequest(products.lien,"GET",initSelect_products_countries);
	sendRequest(products.lien,"GET",select_keywords);
	$('select_services').addEvent('change',function(){sendRequest(products.lien,"GET",select_services);}) 
	$('select_websites').addEvent('change',function(){sendRequest(products.lien,"GET",select_websites);})
	$('select_keywords').addEvent('change',function(){sendRequest(products.lien,"GET",select_keywords);})
	$('valide_go_products').addEvent('click',function(){sendRequest(products.lien,"GET",valide_link_product);})
	$('valide_go_keywords_products').addEvent('click',function(){sendRequest(products.lien,"GET",valide_link_keywords_product);})
	addMessage_tools();
}



// AJOUT DU KEYWORD
function initSelect_products_keywords_link(responseXML)
{
	var opac_button = new Fx.Styles('valide_go_keywords_products', {duration:750, wait:false});
	opac_button.start({'opacity':0.3});
	$('select_keywords').empty();

	var elem_option_vide = new Element('option',{'value':0});
	elem_option_vide.setHTML(select_keywords_product);
	$('select_keywords').appendChild(elem_option_vide);
		
	var xml_keys = responseXML.getElementsByTagName("keyword");
	xml_keys = $A(xml_keys);
	$('select_keywords').disabled = false;
	
	var verif = $A([]);
	xml_keys.each(function(item,index){
		if((item.firstChild != null) && (item.firstChild.nodeValue != 'undefined')){
			var key = item.firstChild.nodeValue;	
			verif.include(key);
			var elem_option = new Element('option',{'value':key});
			elem_option.setHTML(key);
			$('select_keywords').appendChild(elem_option);
		}
	});
}
function select_keywords(responseXML)
{
	if($('select_keywords').value != 0 )
	{			
		var opac_button = new Fx.Styles('valide_go_keywords_products', {duration:750, wait:false});
		opac_button.start({'opacity':1});	
		$('valide_go_keywords_products').disabled = false;
	} else if($('select_keywords').value == 0) {
		$('valide_go_keywords_products').disabled=true;
		this.sendRequest(products.lien,"GET",initSelect_products_keywords_link);
	}
}


function valide_link_keywords_product(responseXML)
{
	var select_keywords = $('select_keywords').value; 
	var xml_products = responseXML.getElementsByTagName("product");
	xml_products = $A(xml_products);
	
	xml_products.each(function(item,index){
		if(item.childNodes[2].firstChild){
			if(select_keywords == item.childNodes[2].firstChild.nodeValue)
			{
				return window.location = item.attributes.getNamedItem('link').nodeValue;
			}
			else
			{
				link_open = '#';
			}
		}
	});
	/*
	xml_products = $A(xml_products);
	xml_products.each(function(item,index){
		
		if((item.childNodes[1].firstChild != null) && (item.childNodes[1].firstChild.nodeValue != 'undefined')){
			alert(select_keywords +' ,     ' + item.childNodes[2].firstChild.nodeValue)
			if(select_keywords == item.childNodes[2].firstChild.nodeValue)
			{
				link_open = item.attributes.getNamedItem('link').nodeValue;
				return window.location = item.attributes.getNamedItem('link').nodeValue;
			}
			else
			{
				link_open = '#';
			}
		}
	});
	*/


	window.location = link_open;
}
