  	
catT = "";
catF = "";

typT =  "";
typID =  "";

pagT =  "";
pagID =  "";

locale = "";

noSections = "";

cate = false;

gallery = false;

catArray = new Array();

typeFilename = "";
typeGalleryURL = "";
categoryGalleryURL = "";
valCategoryGalleryURL = false;

strLang = "";
strViewAll = "";
strNext = "";
strPrev = "";
strReadMore = "";
strViewVideo = "";

function init(cateT, cateF, typeT, typeID, pageT, pageID, localeP)
{
	//alert("im here! in init 2");
	
	webURL = window.location.href ;
	x = webURL.split("/");
	
	serverName = x[2];
	//alert(x[2]);
	
	if (cate)
	{
		if( serverName.match(/cmsprd/g) || serverName.match(/uslaxcmsqa/g))
		{
			path = "http://" + serverName + "/iw-cc/Herbalife/GHL3.0/Categorization/" + locale + "/Category/" + catF + ".xml";
		}
		else
		{
			path = "http://" + serverName + "/Content/" + localeVal + "/galleryXMLs/Category/" + catF + ".xml";
		}
	}
	else
	{
		catT = cateT;
		catF = cateF;
		
		typT =  typeT;
		typID =  typeID;
		
		pagT =  pageT;
		pagID =  pageID;
		
		locale = localeP;
		
		strLocale = locale.split("_");
		localeVal = strLocale[0] + "-" + strLocale[1];
		strLang = strLocale[0];
		
		openXMLLang();
		
		//alert("values of params: " + catT + " "  + catF);
		
		if( serverName.match(/cmsprd/g) || serverName.match(/uslaxcmsqa/g))
		{
			path = "http://" + serverName + "/iw-cc/Herbalife/GHL3.0/Categorization/" + locale + "/Type/TypeCat.xml";
		}
		else
		{
			path = "http://" + serverName + "/Content/" + localeVal + "/galleryXMLs/Type/TypeCat.xml";
		}
		
	}
	//alert(path);
	
	//RETRIEVING TYPES
	dojo.xhrGet(
	{
    	url: path,
    	handleAs: "xml",
    	handle: function(data,args)
    	{
			if(typeof data == "error")
			{
	    		console.warn("error!");
	    		console.log(args);
			}
			else
			{
		    	// the fade can be plugged in here, too
		   		// dojo.byId("C1").innerHTML = data;
		 		//alert("created " + data);	
		  
				if (cate)
				{		
					//alert("calling createGallery");				
					createGallery(data);
				} 
				else
				{
				 	readXML(data);
				}
	    
			}
    	}
	});		
}

function readXML(data)
{			
	types = data.getElementsByTagName('type');					
	//alert("number of types: " + types.length);
	
	noNodes = types.length;
	
	fillCategories();
					
	dojo.addOnLoad(init);
					
}

function fillCategories()
{			
	strHTML = "";
	
	for (var i = 0; i < noNodes; i++) 
	{  
		//alert("value of i = " + i);
						
		strTypeTitle = types[i].getAttribute("title");
		strTypeID = types[i].getAttribute("id");
		strTypeFilename = types[i].getAttribute("filename");
		strTypeGallery = types[i].getAttribute("galleryURL");
																
		//alert("values recovered from XML file:" + "\n" + strTypeTitle + "\n" + strTypeID );				
		
		if (strTypeID == typID)
		{
			//alert("found type, is in position no: " + i);
			document.getElementById("TYPE_TITLE").innerHTML = "<h2 class=\"greentitle nlpad\">" + 
				strTypeTitle + "</h2>";
				
			typeFilename = strTypeFilename;
			typeGalleryURL = strTypeGallery;
			
			categories = types[i].getElementsByTagName("category");
			//alert("number of categories in this type: " + categories.length);
			
			noCats = categories.length;
			
			if (noCats == 1)
			{
				catIndex = 0;
				
				categoryGalleryURL = categories[0].getAttribute("galleryURL");
				valCategoryGalleryURL = true;	
			}
			else if (noCats > 1)
			{
				//alert("number of categories in this type: " + noCats);
				
				for (var j = 0; j < noCats; j++) 
				{
				 	strCatTitle = categories[j].getAttribute("title");
				 	strCatFilename = categories[j].getAttribute("filename");
				 	strCatID = categories[j].getAttribute("id");
				 	strCatURL = categories[j].getAttribute("galleryURL");
										
					//alert(strCatTitle + " " + strCatOrder);
					
					catArray[j] = strCatID + "|" + strCatTitle + "|" + strCatFilename + "|" + strCatURL;					
					//alert(strValue);
					
				}
								
				retrieveURLCat(noCats);
							
			} //end of if
						
			
		}
		
	}
	
	cate = true;
	
	return;
}

function retrieveURLCat(nCat)
{
	//alert(nCat);
	count = 0;
	while(1)
	{
		//alert(catArray[count]);
		
		if (count < nCat)
    	{		
			str = catArray[count].split("|");
			
			if( serverName.match(/cmsprd/g) || serverName.match(/uslaxcmsqa/g))
			{
				pathC = "http://" + serverName + "/iw-cc/Herbalife/GHL3.0/Categorization/" + locale + "/Category/" + str[2] + ".xml";
			}
			else
			{
				pathC = "http://" + serverName + "/Content/" + localeVal + "/galleryXMLs/Category/" + str[2] + ".xml";
			}
			//alert(pathC);
			
			k = 0;
			//RETRIEVING PAGES
			dojo.xhrGet(
			{ 
		    	url: pathC,
		    	handleAs: "xml",
		    	handle: function(data,args)
		    	{			    	
					if(typeof data == "error")
					{
			    		console.warn("error!");
			    		console.log(args);
					}
					else
					{	
						//alert("value of k: " + k);	
						
						pagCat = data.getElementsByTagName('page');
	
						noPagsInCat = pagCat.length;					
						//alert(noPagsInCat);
						
						if (noPagsInCat == 0)
						{
							strURL = "#";
						}
						else
						{
							strURL = pagCat[0].getAttribute("contentURL");
							
							if (strURL == "")
							{
								strURL = pagCat[0].getAttribute("videoURL");
							}
						}
						
						//alert("new value of strURL is: " + strURL);
						
						str = catArray[k].split("|");
						//alert(str[1]);
						
						k++;
						
						if(str[1] == catT)
						{									
							strHTML = strHTML + "<option selected=\'selected\' value=\'" + strURL + "\'>" + str[1] + "</option>";
							catIndex = k;	
							
							if (!valCategoryGalleryURL)
							{
								categoryGalleryURL = str[3];
							}							
						}
						else
						{
							strHTML = strHTML + "<option value=\'" + strURL + "\'>" + str[1] + "</option>";
						}
						//alert(strHTML);
						
						writeSelect(strHTML, k, nCat);				
					}
		    	}
			});	
			count++;					    
		}
		else
		{
    		break;	
    	}
	}
		
	return; 
}

function writeSelect(strHTML, k, nCat)
{
	if (k == nCat)
	{
		//urlAll = "/Content/" + localeVal + "/html/Galleries/Categories/" + typeFilename + ".htm";
		
		//checkLang();
		
		strHTMLViewAll = "<option value=\'" + typeGalleryURL + "\' >" + strViewAll + "</option>";
		
		strHTML = "<select class=\'top_form_styles\' style=\'background: #F4F4F4; border:solid 1px #999; margin:0 0 0 7px; width:150px; _width:160px;\' name=\'CATEGORY_SELECT\' id=\'CATEGORY_SELECT\' onChange=\"openCategory()\">" + 
					strHTMLViewAll + strHTML + "</select>"; 
					//+ "<img src=\'/Content/Global/img/layout/pix.gif\' width=\'8\'/><img src=\'/Content/Global/img/layout/bttn_go_green.gif\' alt=\'Go\' onclick=\"openCategory()\" onmouseover=\"this.style.cursor=\'hand\';\"/>";
				
						document.getElementById("DIV_CATEGORIES").innerHTML = strHTML;	
	}
	
	return;
}

function createGallery(dataC)
{	
	strHTML = "";

	pages = dataC.getElementsByTagName("page");
	//alert("number of pages in selected category: " + pages.length);
	
	noPags = pages.length;
	
	//if (noPags > 1)
	if (noPags > 0)
	{
		for (var i = 0; i < noPags; i++) 
		{
			strProdID = pages[i].getAttribute("id");
			strProdTitle = pages[i].getAttribute("title");
			strProdSubtitle = pages[i].getAttribute("subtitle");
			strProdImage = pages[i].getAttribute("image");
			strProdImageAlt = pages[i].getAttribute("alt");
			strProdContentURL = pages[i].getAttribute("contentURL");
			strProdVideoURL = pages[i].getAttribute("videoURL");
			
			if (strProdID == pagID)
			{
				pageIndex = i;
				//alert(pageIndex);
				strHTMLClass = "class=\'scrollrepeat seleccionado\'";
				strAStart = "";
				strAEnd = "";	
											
			}
			else
			{
				if (strProdContentURL == "")
				{
					strHREF = "href=\'" + strProdVideoURL + "\'";
				}
				else
				{
					strHREF = "href=\'" + strProdContentURL + "\'";
				}
				
				strHTMLClass = "class=\'scrollrepeat\'";
				strAStart = "<a " + strHREF + " onclick=\"openPage(\'" + strProdContentURL + "\')\">";
				strAEnd = "</a>";									
			}
									
			/*strHTML = strHTML + strAStart + "<div " + strHTMLClass + ">" + 
			"<img src=\'" + strProdImage + "\' class=\'scrollimage\' alt=\'" + strProdImageAlt + "\' title=\'" + strProdImageAlt + "\' align=\'left\' />" + 
			"<div style=\'padding-top:5px\'><span style=\'font-size:18px\'>" + strProdTitle + "</span><br />" + strProdSubtitle +
			"</div></div>" + strAEnd;*/
			
			//Validations for image
			if (strProdImage == "")
			{
				strImagePath = "/Content/Global/img/layout/pix.gif";
				strImageAlt = "";
				
				/*strHTML = strHTML + strAStart + "<div " + strHTMLClass + ">" + 				
				"<div class=\'scrollrepeattext\'><span style=\'font-size:18px\'>" + strProdTitle + "</span><br />" + strProdSubtitle +
				"</div></div>" + strAEnd;*/
				
				strHTML = strHTML + strAStart + "<div " + strHTMLClass + ">" + 				
				"<div class=\'scrollrepeattext\'><span>" + strProdTitle + "</span>" +
				"</div></div>" + strAEnd;
			}
			else
			{
				strImagePath = strProdImage;
				strImageAlt = strProdImageAlt;
				
				/*strHTML = strHTML + strAStart + "<div " + strHTMLClass + ">" + 
				"<div class=\'scrollrepeatimage\'>" + 
				"<img src=\'" + strImagePath + "\' class=\'scrollimage\' alt=\'" + strImageAlt + "\' title=\'" + strImageAlt + "\' align=\'left\' />" + 
				"</div>" + 
				"<div class=\'scrollrepeattext\'><span style=\'font-size:18px\'>" + strProdTitle + "</span><br />" + strProdSubtitle +
				"</div></div>" + strAEnd;*/
				
				strHTML = strHTML + strAStart + "<div " + strHTMLClass + ">" + 
				"<div class=\'scrollrepeatimage\'>" + 
				"<img src=\'" + strImagePath + "\' class=\'scrollimage\' alt=\'" + strImageAlt + "\' title=\'" + strImageAlt + "\' align=\'left\' />" + 
				"</div>" + 
				"<div class=\'scrollrepeattext\'><span>" + strProdTitle + "</span>" +
				"</div></div>" + strAEnd;
			}
			
			/*strHTML = strHTML + strAStart + "<div " + strHTMLClass + ">" + 
			"<div class=\'scrollrepeatimage\'>" + 
			"<img src=\'" + strImagePath + "\' class=\'scrollimage\' alt=\'" + strImageAlt + "\' title=\'" + strImageAlt + "\' align=\'left\' />" + 
			"</div>" + 
			"<div class=\'scrollrepeattext\'><span style=\'font-size:18px\'>" + strProdTitle + "</span><br />" + strProdSubtitle +
			"</div></div>" + strAEnd;	*/					
		}
		
		document.getElementById("DIV_PROD_GALLERY").innerHTML = strHTML;
				
		assignPrevNext();
		//fixPNG();
	}
	else if(noPags == 1)
	{
		pageIndex = 0;
	}
	else	//there are no pages
	{
		pageIndex = -1;
	}
	
	if (pageIndex == -1)
	{
		strHREF = "href=\'#\'";
	}
	else
	{
		strProdContentURL = pages[pageIndex].getAttribute("contentURL");
		//alert(strProdContentURL);
		if (strProdContentURL == "")
		{
			strHREF = "href=\'#\'";
			//alert(strHREF);
		}
		else
		{
			strHREF = "href=\'" + strProdContentURL + "\'";
			//alert("2nd " + strHREF);
		}
	}		
	
	strLinkHTML = "<a " + strHREF + " title=\'" + strReadMore + "\'>" + strReadMore + "\&nbsp;\&nbsp;<img src=\'/Content/Global/img/layout/link_arrow.gif\' /></a>";

	document.getElementById("READ_MORE_LINK").innerHTML = strLinkHTML;
	
	return;
}

function assignPrevNext()
{
	//if prev = '0' means to move next
	
	//alert("in assignPrevNext");
	var totalPags = noPags;
	var indexP = pageIndex;
				
	if (noPags == 1)
	{
		//alert("Just one page in this category");
		
		prev = indexP;
		next = indexP;	
		
		strElement = "ONLY ONE";				
	}
	else
	{
		//alert("More than 1 page in this category");
		
		if (indexP == 0) //means that i am already in the 1st element
		{
			//alert("This page is the 1st element in the list");
			
			prev = indexP;
			next = indexP + 1;
			
			strElement = "FIRST";
			
		}
		else if(indexP == (totalPags - 1))  //means that i am in the last element
		{
			//alert("This page is the last element in the list");
			
			next = indexP;
			prev = indexP - 1;
			
			strElement = "LAST";
		}
		else
		{
			//alert("This page is the position no.: " +  indexP);
			
			prev = pageIndex - 1;
			next = indexP + 1;
			
			strElement = "MIDDLE";				
		}
	}
	
	//prevPageID = pages[prev].getAttribute("id");
	//prevPageTitle = pages[prev].getAttribute("title");
	prevPageContentURL = pages[prev].getAttribute("contentURL");
	if (prevPageContentURL == "")
	{
		prevPageContentURL = pages[prev].getAttribute("videoURL");
	}
	
	//nextPageID = pages[next].getAttribute("id");
	//nextPageTitle = pages[next].getAttribute("title");
	nextPageContentURL = pages[next].getAttribute("contentURL");
	if (nextPageContentURL == "")
	{
		nextPageContentURL = pages[next].getAttribute("videoURL");
	}
	
	if (strElement == "FIRST")
	{					
		//IMAGE
		strHTMLImgP = "<img src=\'/Content/Global/img/layout/previousW_off.jpg\' width=\'22\' height=\'22\' border=\'0\' alt=\'" + strPrev + "\' onclick=\"openPage(\'" + prevPageContentURL + "\')\" />";
		//alert(strHTMLImgP);
		
		strHTMLImgN = "<a href=\'" + nextPageContentURL + "\'>" + 
					"<img src=\'/Content/Global/img/layout/nextW.jpg\' width=\'22\' height=\'22\' border=\'0\' alt=\'" + strNext + "\' onclick=\"openPage(\'" + nextPageContentURL + "\')\" />" + 
					"</a>";
		//alert(strHTMLImgN);
		
		//TEXT
		strHTMLLinkP = "<span class=\'pagination-links on\'>" + strPrev + "</span>";
		//alert(strHTMLLinkP);
		
		strHTMLLinkN = "<a href=\'" + nextPageContentURL + "\' class=\'pagination-links\' onclick=\"openPage(\'" + nextPageContentURL + "\')\">" + strNext + "</a>";
		//alert(strHTMLLinkN);
	}
	else if (strElement == "LAST")
	{
		//IMAGE
		strHTMLImgP="<a href=\'" + prevPageContentURL + "\'>" + 
					"<img src=\'/Content/Global/img/layout/previousW.jpg\' width=\'22\' height=\'22\' border=\'0\' alt=\'" + strPrev + "\' onclick=\"openPage(\'" + prevPageContentURL + "\')\" />" + 
					"</a>";
		strHTMLImgN="<img src=\'/Content/Global/img/layout/nextW_off.jpg\' width=\'22\' height=\'22\' border=\'0\' alt=\'" + strNext + "\' onclick=\"openPage(\'" + nextPageContentURL + "\')\" />";	
		
		//TEXT
		strHTMLLinkP="<a href=\'" + prevPageContentURL + "\' class=\'pagination-links\' onclick=\"openPage(\'" + prevPageContentURL + "\')\" >" + strPrev + "</a>";
		strHTMLLinkN="<span class=\'pagination-links on\'>" + strNext + "</span>";
	}
	else
	{				
		//IMAGE
		strHTMLImgP="<a href=\'" + prevPageContentURL + "\'>" + 
					"<img src=\'/Content/Global/img/layout/previousW.jpg\' width=\'22\' height=\'22\' border=\'0\' alt=\'" + strPrev + "\' onclick=\"openPage(\'" + prevPageContentURL + "\')\" />" + 
					"</a>";
		strHTMLImgN="<a href=\'" + nextPageContentURL + "\'>" + 
					"<img src=\'/Content/Global/img/layout/nextW.jpg\' width=\'22\' height=\'22\' border=\'0\' alt=\'" + strNext + "\' onclick=\"openPage(\'" + nextPageContentURL + "\')\" />" + 
					"</a>";	
		
		//TEXT
		strHTMLLinkP="<a href=\'" + prevPageContentURL + "\' class=\'pagination-links\' onclick=\"openPage(\'" + prevPageContentURL + "\')\" >" + strPrev + "</a>";
		strHTMLLinkN="<a href=\'" + nextPageContentURL + "\' class=\'pagination-links\' onclick=\"openPage(\'" + nextPageContentURL + "\')\" >" + strNext + "</a>";
	}
	
	//strHREF = "/Content/" + localeVal + "/html/Galleries/" + catF + ".htm";	
	//alert("Cat Titleee:"+ catT);
	
	/*catTitle = catT.replace(/\W/g, "");
	catTitle = catT.replace(/\s/g, "");
	catTitle = catTitle.toLowerCase();
	
	strHREF = "/" + catTitle;*/
	
	//strHREF = "/Content/" + localeVal + "/html/Galleries/Categories/SubCategories/" + catF + ".htm";
	strHREF = categoryGalleryURL;
	//alert(strHREF);

	if (strElement != "ONLY ONE")
	{
		strHTML = strHTMLImgP + "<img src=\'/Content/Global/img/layout/pix.gif\' width=\'3\'/>" + 
				strHTMLLinkP +  "<img src=\'/Content/Global/img/layout/pix.gif\' width=\'3\'/>" + 
				"<a href=\'" + strHREF + "\' class=\'pagination-links\'>" + strViewAll + "</a>" + 
				"<img src=\'/Content/Global/img/layout/pix.gif\' width=\'3\'/>" + 
				strHTMLLinkN + "<img src=\'/Content/Global/img/layout/pix.gif\' width=\'3\'/>" + 
				strHTMLImgN;	
		//alert(strHTML);
			
		document.getElementById("PAGINATION").innerHTML = strHTML;
	}
	
	gallery = true;
	
	return;
					
}

function openPage(pagURL)
{
	//redirect to page
	//alert("in function, page that needs to be loaded is:" + pagURL);
	
	window.open(pagURL, "_self");
}	

function openCategory()
{
					
	sel = document.getElementById("CATEGORY_SELECT");
	selectedVal = sel.options[sel.selectedIndex].value;
	//alert(selectedVal);	
		
	window.open(selectedVal, "_self");
}

function openXMLLang()
{	
	path = "http://" + serverName + "/Content/Global/xml/LangTerms.xml";
	//alert(path);
	
	//RETRIEVING LANGUAGES
	dojo.xhrGet(
	{
    	url: path,
    	handleAs: "xml",
    	handle: function(data,args)
    	{
			if(typeof data == "error")
			{
	    		console.warn("error!");
	    		console.log(args);
			}
			else
			{		    			  
				retrieveTerms(data);	    
			}
    	}
	});	
		
}

function retrieveTerms(data)
{
	//alert("i am in retrieveTerms");
	
	nodeLang = data.getElementsByTagName('language');
	numNodes = nodeLang.length;	
	//alert("number of nodes: " + numNodes);
	
	for (var i = 0; i < numNodes; i++) 
	{  
		//alert("value of i = " + i);
						
		lang = nodeLang[i].getAttribute("id");
		
		if (strLang == lang)
		{
			var va = nodeLang[i].getElementsByTagName("view_all");			
			strViewAll = va[0].childNodes[0].nodeValue;
			//alert("Value of view all: " + strViewAll);
			
			var va = nodeLang[i].getElementsByTagName("next");						
			strNext = va[0].childNodes[0].nodeValue;
			//alert("Value of next: " + strNext);
			
			var va = nodeLang[i].getElementsByTagName("previous");
			strPrev = va[0].childNodes[0].nodeValue;
			//alert("Value of prev: " + strPrev);	
			
			var va = nodeLang[i].getElementsByTagName("read_more");
			strReadMore = va[0].childNodes[0].nodeValue;
			//alert("Value of strReadMore: " + strReadMore);
			
			var va = nodeLang[i].getElementsByTagName("view_video");
			strViewVideo = va[0].childNodes[0].nodeValue;
			//alert("Value of strViewVideo: " + strViewVideo);			

			break;
		}
		
	}
}
