locale = "";
layout = "";
intervalVal = ""; 
ecmID = "";	

catA = new Array();	
pagA = new Array();	

k = 0;
p = 0;

numTotalPags = 0;

strLang = "";
strViewAll = "";
strNext = "";
strPrev = "";
strReadMore = "";
strViewVideo = "";

function getCategories(catID, catT, catF, catSO)
{
	//alert(catID + " " + catT + " " + catF);
				
	pos = catSO - 1;
	catA[pos]= catID + "|" + catT + "|" + catF;
	//alert(pos + ": " + catA[catSO-1]);						
	
}

function retrievePageInfo(loc, tyLy, interv, id)
{
	locale = loc;
	layout = tyLy;
	intervalVal = interv; 
	ecmID = id;
	
	interval = (intervalVal * 60000);
	
	webURL = window.location.href ;
	x = webURL.split("/");
	
	serverName = x[2];
	//alert(x[2]);
	
	strLocale = locale.split("_");
	localeVal = strLocale[0] + "-" + strLocale[1];
	strLang = strLocale[0];
	
	//checkLang();
	openXMLLang();
	
	//alert(catA.length);
				
	for(var i = 0; i < catA.length; i++)
	{					
		temp = 	catA[i];				
		catValues = temp.split("|");
		
		if( serverName.match(/cmsprd/g) || serverName.match(/uslaxcmsqa/g))
		{
			path = "http://" + serverName + "/iw-cc/Herbalife/GHL3.0/Categorization/" + locale + "/Category/" + catValues[2] + ".xml";
		}
		else
		{
			path = "http://" + serverName + "/Content/" + localeVal + "/galleryXMLs/Category/" + catValues[2] + ".xml";			
		}
		//alert(path);	
		
		//RETRIEVING CATEGORIES
		dojo.xhrGet(
		{
	    	url: path,
	    	handleAs: "xml",
	    	handle: function(data,args)
	    	{
				if(typeof data == "error")
				{
			    	console.warn("error!");
			    	console.log(args);
				}
				else
				{					    	
			   		// dojo.byId("C1").innerHTML = data;
			 		//alert("created " + data);	
			 		
			 		readXML(data);				    
				}
		    }
		});	
	}
	
	//createHTML(p);
}

function readXML(data)
{			
	pages = data.getElementsByTagName('page');
	//alert("number of pages in selected category: " + pages.length);
	
	noPags = pages.length;
				
	readPageInfo();
					
}

function readPageInfo()
{
	for (var i = 0; i < noPags; i++) 
	{
		strFeat = pages[i].getAttribute("featured");
		
		if(strFeat == 1)
		{
			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");
			strProdIntro = pages[i].getAttribute("intro");	
			
			strProdContent = pages[i].getAttribute("contentURL");
			strProdVideo = pages[i].getAttribute("videoURL");
			
			pagA[k]= strProdID + "|" + strProdTitle + "|" + strProdSubtitle + "|" + strProdImage + "|" + strProdImageAlt + "|" +
					 strProdIntro + "|" + strProdContent + "|" + strProdVideo;
			//alert(k + ": " + pagA[k]);
			
			/*if (k == 0)
			{
				createHTML();						
			}*/
			k++;
		}
	}	
	
	numTotalPags = pagA.length;
	//alert(numTotalPags);
	
	p = get_random();		
	//alert("random: " + p);
	
	createHTML();
}

function createHTML()
{
	//alert("i am in createHTML");
	//alert(p);
	
	var temp = pagA[p];
	//alert(temp);
	pageContent = temp.split("|");
	
	//strPagID = pageContent[0];
	strPagTitle = pageContent[1];
	strPagSubtitle = pageContent[2];
	strPagImage = pageContent[3];
	strPagImgAlt = pageContent[4];
	strPagIntro = pageContent[5];
	strPagContent = pageContent[6];		
	strPagVideo = pageContent[7];		
	
	//Validations for image
	if (strPagImage == "")
	{
		strImagePath = "/Content/Global/img/layout/pix.gif";
		strImageAlt = "";
	}
	else
	{
		strImagePath = strPagImage;
		strImageAlt = strPagImgAlt;
	}
			
	//IMAGE
	strImgHTML = "<div class=\'module_img_crop\'><img class=\'module_rotating_img\' src=\'" + strImagePath + "\' alt=\'" + strImageAlt + "\' align=\'left\'/></div>";
	//alert(strImgHTML);
	
	document.getElementById("IMG_ROTATING_MODULE_" + ecmID).innerHTML = strImgHTML;
		
	//INTRO
	document.getElementById("INTRO_TEXT_" + ecmID).innerHTML = strPagIntro;
		
	if (layout == '1')
	{
		//TITLE
		document.getElementById("TIT_ROTATING_MODULE_" + ecmID).innerHTML = strPagTitle;
				
		//SUBTITLE
		strSubtitHTML = "<h2 class=\'module_subtitle nopadding\'>" + strPagSubtitle + "</h2>";
		
		document.getElementById("SUBTIT_ROTATING_MODULE_" + ecmID).innerHTML = strSubtitHTML;		
	}
	else
	{
		
		//TITLE AND SUBTITLE
		strTitHTML = "<h2 class=\'module_subtitle nopadding\'>" + strPagTitle + "</h2>" + 
					"<h5 class=\'nopadding\'>" + strPagSubtitle + "</h5>";						
		//alert(strImgHTML);
		
		document.getElementById("TIT_ROTATING_MODULE_" + ecmID).innerHTML = strTitHTML;		
	}
	
	//CONTENT LINK
	if (strPagContent != "")
	{				
		strContHTML = "<a href=\'" + strPagContent + "\' style=\'text-decoration:none\'>" + 
					 strReadMore + " <img src=\'/Content/Global/img/layout/icon_arrow.gif\' /></a>";
	}
	else
	{
		strContHTML = "";
	}
	document.getElementById("CONTENT_LINK_" + ecmID).innerHTML = strContHTML
		
	//VIDEO LINK
	if (strPagVideo != "")
	{				
		strVidHTML = "<a href=\'" + strPagVideo + "\' style=\'text-decoration:none\'>" + 
					  strViewVideo + " <img src=\'/Content/Global/img/layout/icon_video.gif\' /></a>";
	}
	else
	{
		strVidHTML = "";
	}			
	document.getElementById("VIDEO_LINK_" + ecmID).innerHTML = strVidHTML
		
	
	/*if (k == 0)
	{
		t = setTimeout("increaseCounter()",interval);				
	}
	else if (p == (pagA.length - 1))
	{								
		//reseting the counter
		p=0;
		
		setTimeout("createHTML()",interval);
	}
	else
	{
		//alert("reading next page");
		p++;
		
		setTimeout("createHTML()",interval);
	}*/
	
	p = get_random();
	//alert("random: " + p);
	setTimeout("createHTML()",interval);				
	
}

function increaseCounter()
{
	//alert("in increaseCounter");
	p++;
	//clearTimeout(t);
	createHTML();						
}

function get_random()
{	
    var ranNum= Math.floor(Math.random()* numTotalPags);
    return ranNum;
}

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;
		}
		
	}
}