/*
	Uses Ajax to read in the content data of the website from an xml page for each of the 
	wineries. It uses two xml files: 
	** An initial one contains the main page information which is loaded upon startup and 
	after each language change. This contains the location of the second xml file for each 
	language. The location of this file is determined in the main html page via a meta field.
	** The second xml file contains the information that needs to be presented in the information 
	fields. It contains such information only for one language. This information is presented 
	inside a multidiv in the main website, with tabs.	
*/

/* Global variables*/


//flag to know when the second XML is loaded
var XML2_ready = 0;

var current_language;
var info_file; //second XML file

//date of modification gotten from the html page
var date_modified = null;

//we are in dopriorat
var products_producers = null;

//holds a tab to be shown as startpoint. Main one if null
var initial_tab = null;
var initial_id = null;


/*############## Functions to remove elements from the DOM ####################*/

/*Removes all children from a given element*/
function remove_children(dad)
{
	if(dad.childNodes.length != 0)
	{
		while(dad.firstChild)
		{
			dad.removeChild(dad.firstChild);
		}
	}
}

/*removes the element passed on as parameter. It recursively revoves all of its children*/
function remove_elem(element)
{
	element.parentNode.removeChild(element);
}

/*############### Functions to change the focus of items in the page #####################*/


/* Changes focus to a given multidiv, prior check that the data is there*/
function showMultiDiv(multidiv)
{
	if(XML2_ready == 0)
	{
		/*not ready yet, show the progress_FFF as background in a centered DIV in the info area*/
		
		//show the progress clock
		//document.getElementById("progress").style.display="block";
	}
	else
	{	
		//hide the progress clock
		//document.getElementById("progress").style.display="none";
	
		/*mark the selected item as selected in the main menu (and unmark any others)*/
		var main_menu_items = document.getElementById("main_menu").getElementsByTagName("a");
		for(var i=0; i<main_menu_items.length; i++)
		{
			if(main_menu_items.item(i).attributes.getNamedItem("id").nodeValue == multidiv)
			{
				main_menu_items.item(i).parentNode.attributes.getNamedItem("class").nodeValue = "selected_main_item";
			}
			else
			{
				main_menu_items.item(i).parentNode.attributes.getNamedItem("class").nodeValue = "unselected_main_item";
			}
		}
	
		//eliminate the text and make the bg image transparent
		//of course, in case it exists
		if(document.getElementById("center_bg_text")){document.getElementById("center_bg_text").style.display="none"};
		var bg_image = document.getElementById("center_bg_image");

		if(bg_image)
		{
 			if ((navigator.appName.indexOf("Netscape")!=-1 && parseInt(navigator.appVersion)>=5) || (window.opera && window.getSelection))
			{
   				bg_image.style.opacity=0.4;
 			}
 			else
 			{ 
 				if (navigator.appName.indexOf("Microsoft")!= -1 && parseInt(navigator.appVersion)>=4)
			    {
			    	bg_image.style.filter="progid:DXImageTransform.Microsoft.Alpha(opacity=40)";
			    }
			    else
			    {
					bg_image.style.display="none";
				}
			}
		}
		//find the div we want to enable
		var center_div = document.getElementById("main_center");
		var div_list = center_div.childNodes;
		for(var i=0; i<div_list.length; i++)
		{
			var div_tmp = div_list.item(i).attributes.getNamedItem("id");
			if(div_tmp){div_id = div_tmp.nodeValue;}else{div_id = null;}
			var div_tmp = div_list.item(i).attributes.getNamedItem("class");
			if(div_tmp){div_class = div_tmp.nodeValue;}else{div_class = null;}
			if(div_class == "multidiv")
			{
				if( div_id == multidiv)
				{
					//show the multidiv
					div_list.item(i).style.display="block";
				}
				else
				{
					div_list.item(i).style.display="none";
				}
			}
		}		
	}
}

/*
	switches the focus on screen given a DIV ID
	It searches for the div within the multidivs and the inside divs
*/
function switchById(div_id)
{
	var found = 0;
	//look in the multidivs
	for(var i=0; i<tablist_multidiv.length; i++)
	{
		if(tablist_multidiv[i] == div_id)
		{
			found = 1;
			showMultiDiv(tablist_multidiv[i]);
		}
	}
	
	//look in the inside divs
	if(!found)
	{
		for(var i=0; i<tablist_id.length; i++)
		{
			if(tablist_id[i] == div_id)
			{
				showMultiDiv(tablist_multidiv[i]);
				switchMultidiv(tablist_multidiv[i], tablist_id[i])
			}
		}
	}
	return false;
}

/*
	switches focus to a multidiv given the title of the multidivdiv
	It finds the relationship of title vs ID in the tables
*/
function switchbyname(div_title)
{
	div_title = eliminate_accents(div_title);
	//find the div that has a title similar to the desired one
	var regexp = new RegExp(div_title, "i");
	for(var i=0; i<tablist_title.length; i++)
	{
		if(tablist_title[i])
		{
			var text = eliminate_accents(tablist_title[i]);
			if(text.match(regexp))
			{
				showMultiDiv(tablist_multidiv[i]);
				switchMultidiv(tablist_multidiv[i], tablist_id[i])
			}
		}
	}

	return false;
}

/*Support function to eliminate accents from strings
Used when comparing titles with queries, as www queries 
cannot contain native accents
*/
function eliminate_accents(text)
{
	var reg = /á/;
	text = text.replace(reg, "a");
	var reg = /é/;
	text = text.replace(reg, "e");
	var reg = /í/;
	text = text.replace(reg, "i");
	var reg = /ó/;
	text = text.replace(reg, "o");
	var reg = /ú/;
	text = text.replace(reg, "u");
	var reg = /à/;
	text = text.replace(reg, "a");
	var reg = /è/;
	text = text.replace(reg, "e");
	var reg = /ò/;
	text = text.replace(reg, "o");
	return text;
}

/*
Function that switches to a particular tab in the CURRENT multidiv
Version for main multidivs
*/

function switchMultidiv(multidiv_id, tab_id)
{
	//find the current multidiv
	var center_div = document.getElementById("main_center");
	//we get all children from this element, so that we narrow the search space
	var div_list = center_div.childNodes;
	
	for(var i=0; i<div_list.length; i++)
	{
		var div_id = null;
		var div_tmp = div_list.item(i).attributes.getNamedItem("id");
		if(div_tmp){div_id = div_tmp.nodeValue;}
		
		var div_class = null;
		var div_tmp = div_list.item(i).attributes.getNamedItem("class");
		if(div_tmp){div_class = div_tmp.nodeValue;}

		if(div_class == "multidiv")
		{
			if(div_id == multidiv_id)
			{
				//go through all divs "info_div" inside and show the desired one
				//on the tabs div also change the properties of the selected tab
				
				var info_div_list = div_list.item(i).childNodes;				
				switchTab(info_div_list, tab_id);				
			}
			else
			{
				div_list.item(i).style.display="none";
			}
		}			
	}		
}

/*
Function that shows a particular tab in the current multidiv
version for multidivs inside SUBS
*/
function switchSub(multidiv_id, tab_id)
{
	//get the parent node for the nodes I want to change
	var wanted_div = document.getElementById(multidiv_id).parentNode;
	var div_list = wanted_div.childNodes;
	
	for(var i=0; i<div_list.length; i++)
	{
		var div_id = null;
		var div_tmp = div_list.item(i).attributes.getNamedItem("id");
		if(div_tmp != null){div_id = div_tmp.nodeValue;}
		
		var div_class = null;
		var div_tmp = div_list.item(i).attributes.getNamedItem("class");
		if(div_tmp != null){div_class = div_tmp.nodeValue;}

		//we use a regexp to find the value
		var regexp = /multisub/;
		if(div_class.match(regexp))
		{
			if(div_id == multidiv_id)
			{
				//go through all divs "info_div" inside and show the desired one
				//on the tabs div also change the properties of the selected tab
				var info_div_list = div_list.item(i).childNodes;				
				switchTab(info_div_list, tab_id);				
			}
			else
			{
				div_list.item(i).style.display="none";
			}
		}			
	}		
}


/*
Switches the focus inside a multidiv/multisub list of tabs
*/
function switchTab(div_list, tab_id)
{
	for(var j=0; j<div_list.length; j++)
	{
		var info_div_class = div_list.item(j).attributes.getNamedItem("class").nodeValue;
		
		var info_div_id = null;
		var tmp_div_id = div_list.item(j).attributes.getNamedItem("id");		
		if(tmp_div_id != null){info_div_id = tmp_div_id.nodeValue;}
		
		if(info_div_class == "info_div" || info_div_class == "info_sub")
		{
			if(info_div_id == tab_id)
			{
				div_list.item(j).style.display="block";
			}
			else
			{
				div_list.item(j).style.display="none";
			}
		}
		if(info_div_class == "nav_div")
		{
			var tab_li_list = div_list.item(j).firstChild.getElementsByTagName("li");
			//the tab name with "_tab"
			var new_tab_id = tab_id+"_tab";
			for(var k=0; k<tab_li_list.length; k++)
			{
				var tab_li_id = tab_li_list.item(k).attributes.getNamedItem("id").nodeValue;
				if(tab_li_id == new_tab_id)
				{
					tab_li_list.item(k).attributes.getNamedItem("class").nodeValue = "selected_tab";
				}
				else
				{
					tab_li_list.item(k).attributes.getNamedItem("class").nodeValue = "unselected_tab";
				}
			}
		}
	}
}


/*
function to do the import
*/

function _importNode(node, importChildren) 
{
	if(node)
	{
 		switch (node.nodeType) 
  			{
				case 1: 
					//document.ELEMENT_NODE:
	    			var newNode = document.createElement(node.nodeName);
	    			
	    			/* does the node have any attributes to add? */
	    			if (node.attributes)
	    			{
	        			for (var i = 0; i < node.attributes.length; i++)
	        			{
	        				newNode.setAttribute(node.attributes[i].nodeName, node.getAttribute(node.attributes[i].nodeName));
						}
					}
					
	    			/* are we going after children too, and does the node have any? */
	    			if (importChildren && node.childNodes)
	    			{
						for (var i = 0; i < node.childNodes.length; i++)
	    				{
	    					var tmp_child = _importNode(node.childNodes[i], importChildren);
	    					if(tmp_child)
	    					{
		        				newNode.appendChild(tmp_child);
		        			}
	        			}
	    			}
	    			return newNode;
	    			break;
	
    			case 3:
    			case 4:
    				//document.CDATA_SECTION_NODE:
    				//case document.COMMENT_NODE:
    				//document.TEXT_NODE:
    				return document.createTextNode(node.nodeValue);
    				break;
  			}
	}
}

