locale = "";
			
catT = "";
catID = "";
catF = "";
	
//typeG = "";

cate = false;

catArray = new Array();

currentPage = 0;

typeTitle = "";
typeFilename = "";
typeGalleryURL = "";

strLang = "";
strViewAll = "";
strNext = "";
strPrev = "";
strReadMore = "";
strViewVideo = "";

function init(loc, cateT, cateID, cateF)
{
		
	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
	{
		locale = loc;
		
		catT = cateT;		
		catID =  cateID;
		catF =  cateF;
		
		//typeG =  typeGa;
		
		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)
				{							
					createGallery(data);
				} 
				else
				{
	 				readXML(data);
				}
	    
			}
		}
	});												
	
}
	
function readXML(data)
{			
	types = data.getElementsByTagName('type');					
	//alert("number of types: " + types.length);
	
	noNodes = types.length;
	
	retrieveType();
	fillCategories();
		
	dojo.addOnLoad(init);
					
}
	
function retrieveType()
{	
	foundType = false;
	
	for (var i = 0; i < noNodes; i++) 
	{
		strTypeTitle = types[i].getAttribute("title");
		strTypeFilename = types[i].getAttribute("filename");
		strTypeID = types[i].getAttribute("id");
		strTypeGallery = types[i].getAttribute("galleryURL");
		
		categories = types[i].getElementsByTagName("category");
		//alert("number of categories in this type: " + categories.length);
		
		noCats = categories.length;
		
		if (noCats > 0)
		{
			for (var j = 0; j < noCats; j++) 
			{
			 	strCatTitle = categories[j].getAttribute("title");
				strCatID = categories[j].getAttribute("id");
				
				//alert(strCatTitle + " " + strCatID);
				
				if(strCatID == catID)
				{
					//alert("found category");
					typeTitle = strTypeTitle;
					typeFilename = strTypeFilename;
					typeGalleryURL = strTypeGallery;
					
					foundType = true;
					break;
				}														
			}
		}
		
		if (foundType)
		{
			indexType = i;
			break;
		}
	}
	
	//alert("This category is related to type: " + strTypeTitle);
	
	document.getElementById("TYPE_TITLE").innerHTML = strTypeTitle;
}
	
function fillCategories()
{			
	strHTML = "";
	
	//alert("found type, is in position no: " + indexType);
	
	categories = types[indexType].getElementsByTagName("category");
	//alert("number of categories in this type: " + categories.length);
	
	noCats = categories.length;
	
	if (noCats == 1)
	{
		catIndex = 0;	
	}
	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);
			
			//strURL = "/Content/" + localeVal + "/html/Galleries/Categories/SubCategories/" + strCatFilename + ".htm";		
						
			if(strCatID == catID)
			{									
				strHTML = strHTML + "<option selected=\'selected\' value=\'" + strCatURL + "\'>" + strCatTitle + "</option>";
				catIndex = j;								
			}
			else
			{
				strHTML = strHTML + "<option value=\'" + strCatURL + "\'>" + strCatTitle + "</option>";
			}
			
		}
		
		writeSelect(strHTML, j, noCats);
					
	} //end of if
	
	
	cate = true;
	
	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;
	
	noPages = 0;					
	noPages = noPags / 12;
	
	if((noPags % 12) > 0)
	{
		noPages = noPages + 1;	
	}
	
	if (noPags > 1)
	{
		for (var i = 0; i < noPags; i++) 
		{  
			//alert("value of i = " + 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");
			
			if (strProdContentURL == "")
			{
				strProdContentURL = pages[i].getAttribute("videoURL");
			}
																
			//alert("values recovered from XML file:" + "\n" + strProdID + "\n" + strProdTitle + "\n" + strProdImage + "\n" + strProdImageAlt);	
			
			elem = i;
			elem = elem + 1;
																		
			writeHTML(strProdImage, strProdTitle, strProdSubTitle, elem, strProdImageAlt, strProdContentURL);	
			
			if (i == 11)
			{
				break;	
			}
		} 	
		
		if (noPages > 1)
		{							
			//PRINTING PAGES														
			pagePosition = "FIRST";
			
			writePages(noPages);					
		}
	}					
	
	return;					
}
	
function writeHTML(valImage, valTitle, valSubtitle, noElem, altAtt, contURL)
{
	//alert("in function writeHTML");
	
	var htmlImage = "";
	var htmlTitle = "";
	var htmlSubtitle = "";
	var IDval = "";
					
	//alert("values passed to writeHTML function:" + "\n" + valImage + "\n" + valTitle + "\n" + valSubtitle + "\n" + noElem);	
					
	IDval = "C" + noElem;
	//alert("ID generated: " + IDval);
	
	if(valImage == "" && valTitle == "" && valSubtitle == "")
	{		
		//alert("empty");
		//PRINTING HTML
		document.getElementById(IDval).innerHTML = htmlImage + htmlTitle + htmlSubtitle;
		
		strHTMLGallery = "";
		
		//return;
	}			
	else
	{	
		//Validations for image
		if (valImage == "")
		{
			strImagePath = "/Content/Global/img/layout/placeholder.jpg";
			strImageAlt = "";
		}
		else
		{
			strImagePath = valImage;
			strImageAlt = altAtt;
		}
		
		//IMAGE HTML
		htmlImage = "<a href=\'" + contURL + "\' class=\'imgborder\'><img class=\'gallery-img\' src=\'" + strImagePath + "\' alt=\'" + strImageAlt + "\'/></a>";
								
		//TITLE HTML
		htmlTitle = "<a href=\'" + contURL + "\' ><h2 class=\'green\' style=\'padding: 0px;\'>" + valTitle + "</h2></a>";
				
		//SUBTITLE HTML
		htmlSubtitle = "<a href=\'" + contURL + "\' ><span class=\'green\'>" + valSubtitle + "</span></a>";
						
		strHTMLGallery = "<div class=\'storypadding\'><div class=\'gallerymod\'>" + htmlImage + "</div>" + 
		"<div class=\'gallerymodtext\'>" + htmlTitle + htmlSubtitle + "</div></div>";		
		
		//PRINTING HTML
		document.getElementById(IDval).innerHTML = strHTMLGallery;
	}						
						
	return;
}
	
function writePages(noPags)
{
	//alert(noPags);
	noTotalPags = 0;
	
	if(noPags >= 2)
	{
		strPags = "";
		for (i=1; i<=noPags; i++)
		{	
			if ((pagePosition == "FIRST" && i == 1) || (pagePosition == "LAST" && i == noPags) )
			{				
				strPags = strPags + "<span class=\'pagination-links on\'>" + i + "</span>" + 
				"<img src=\'/Content/Global/img/layout/pix.gif\' width=\'3\'/>";		
			}
			else if (i == currentPage)
			{
				strPags = strPags + "<span class=\'pagination-links on\'>" + i + "</span>" + 
				"<img src=\'/Content/Global/img/layout/pix.gif\' width=\'3\'/>";		
			}			
			else
			{
				strPags = strPags + "<a href=\'#\' class=\'pagination-links\' onClick=\"printAnotherPage(" + i + ")\"><span>" + i + "</span></a>" + 
						"<img src=\'/Content/Global/img/layout/pix.gif\' width=\'3\'/>";		
			}
			
			noTotalPags = noTotalPags + 1;												
		}
								
		if (pagePosition == "FIRST")
		{
			//alert(pagePosition);
			strHTMLP = "<img src=\'/Content/Global/img/layout/previousW_off.jpg\' width=\'22\' height=\'22\' border=\'0\' alt=\'" + strPrev + "\' />" + 
			"<img src=\'/Content/Global/img/layout/pix.gif\' width=\'3\'/>" + 
			"<span class=\'pagination-links on\'>" + strPrev + "</span><img src=\'/Content/Global/img/layout/pix.gif\' width=\'3\'/><img src=\'/Content/Global/img/layout/pix.gif\' width=\'3\'/>";						
			
			strHTMLN = "<img src=\'/Content/Global/img/layout/pix.gif\' width=\'10\'/ ><a href=\'#\' class=\'pagination-links\' onclick=\"printAnotherPage(\'2\')\">" + strNext + "</a>" +
			"<img src=\'/Content/Global/img/layout/pix.gif\' width=\'3\'/>" + 
			"<a href=\'#\'><img src=\'/Content/Global/img/layout/nextW.jpg\' width=\'22\' height=\'22\' border=\'0\' alt=\'" + strNext + "\' onclick=\"printAnotherPage(\'2\')\"/></a>";
		}
		else if (pagePosition == "LAST")
		{
			//alert(pagePosition);
			strHTMLP = "<a href=\'#\'><img src=\'/Content/Global/img/layout/previousW.jpg\' width=\'22\' height=\'22\' border=\'0\' alt=\'" + strPrev + "\' onclick=\"printAnotherPage(\'" + (noTotalPags - 1) + "\')\"/></a>" + 
			"<img src=\'/Content/Global/img/layout/pix.gif\' width=\'3\'/>" + 
			"<span><a href=\'#\' class=\'pagination-links\' onclick=\"printAnotherPage(\'" + (noTotalPags - 1) + "\')\">" + strPrev + "</a></span><img src=\'/Content/Global/img/layout/pix.gif\' width=\'3\'/><img src=\'/Content/Global/img/layout/pix.gif\' width=\'3\'/>";						
			
			strHTMLN = "<img src=\'/Content/Global/img/layout/pix.gif\' width=\'10\'/><span class=\'pagination-links on\'>" + strNext + "</span>" +
			"<img src=\'/Content/Global/img/layout/pix.gif\' width=\'3\'/>" + 
			"<img src=\'/Content/Global/img/layout/nextW_off.jpg\' width=\'22\' height=\'22\' border=\'0\' alt=\'" + strNext + "\' />";
		}
		else
		{
			//alert(pagePosition);
			
			prevP = 0;
			nextP = 0;
			
			prevP = currentPage - 1;
			nextP = prevP + 2;
			
			//alert(prevP + " " + nextP);
			
			strHTMLP = "<a href=\'#\'><img src=\'/Content/Global/img/layout/previousW.jpg\' width=\'22\' height=\'22\' border=\'0\' alt=\'" + strPrev + "\' onclick=\"printAnotherPage(\'" + prevP + "\')\"/></a>" + 
			"<img src=\'/Content/Global/img/layout/pix.gif\' width=\'3\'/>" + 
			"<span><a href=\'#\' class=\'pagination-links\' onclick=\"printAnotherPage(\'" + prevP + "\')\">" + strPrev + "</a></span><img src=\'/Content/Global/img/layout/pix.gif\' width=\'3\'/><img src=\'/Content/Global/img/layout/pix.gif\' width=\'3\'/>";						
			
			strHTMLN = "<img src=\'/Content/Global/img/layout/pix.gif\' width=\'10\'/><a href=\'#\' class=\'pagination-links\' onclick=\"printAnotherPage(\'" + nextP + "\')\">" + strNext + "</a>" +
			"<img src=\'/Content/Global/img/layout/pix.gif\' width=\'3\'/>" + 
			"<a href=\'#\'><img src=\'/Content/Global/img/layout/nextW.jpg\' width=\'22\' height=\'22\' border=\'0\' alt=\'" + strNext + "\' onclick=\"printAnotherPage(\'" + nextP + "\')\"/></a>";
		}
								
		document.getElementById("PAGINATION").innerHTML = strHTMLP + strPags + strHTMLN;
								
	}
	
	return;					
}
	
function printAnotherPage(noPage)
{	
	//alert(noPage);
	
	currentPage = noPage;
	
	nodeMax = 12 * noPage;
	init = nodeMax - 12;
	
	//alert("start in: " + init);
	//alert("end in: " + nodeMax);
	
	elem = 1;
	
	for (var i = init; i < nodeMax; i++) 
	{  
		//alert("value of i = " + i);
		
		//cleaning variables
		var story = null;
		var strProdImage = "";
		var strProdImageAlt = "";
		var strProdTitle = "";
		var strProdSubTitle = "";
		
		if (pages[i] != null)
		{						
			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");
			
			if (strProdContentURL == "")				
			{
				strProdContentURL = pages[i].getAttribute("videoURL");
			}
		}						
		//alert("values recovered from XML file:" + "\n" + strProdID + "\n" + strProdTitle + "\n" + strProdImage + "\n" + strProdImageAlt);	
																							
		writeHTML(strProdImage, strProdTitle, strProdSubTitle, elem, strProdImageAlt, strProdContentURL);
		
		elem = elem + 1;
	}
	
	if (noPage == 1)
	{
		pagePosition = "FIRST";						
	}
	else if(noPage == noTotalPags)
	{
		pagePosition = "LAST";						
	}
	else
	{
		pagePosition = "";
	}
	//alert(pagePosition);
	writePages(noTotalPags);
}


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;
		}
		
	}
}
