var copyHeight = "" 
var pID;
var effDur;

var moduleScriptCnt = 0;
var moduleStyleCnt = 0;

function updatePageCopy(pageID,moduleParams){
	
	if ( typeof(moduleParams) == "undefined" || moduleParams==null) {
		var moduleParams = ""; 
	}
		
	/*show loader*/
	document.getElementById("loader").style.display='block';
	
	/*remove scripts and styles that might be in there from another module.... right now we don't know how many could  be 
	there, but its probably safe to assume it will be less  than 25, so we'll just loop through and try to remove
	25 module scripts and styles*/
	for(i=0;i<=25;i++){
		
		if ( typeof(document.getElementById("moduleScript" + i)) != "undefined" && document.getElementById("moduleScript" + i) !=null ) {
			buildHtml.removeElement("moduleScript" + i);
		}
		
		if ( typeof(document.getElementById("moduleStyle" + i)) != "undefined" && document.getElementById("moduleStyle" + i) !=null ) {
			buildHtml.removeElement("moduleStyle" + i);
		}
	}
	
	moduleScriptCnt = 0;
	moduleStyleCnt = 0;
		
	/*sprictaculous uses seconds, and setTimeout uses milliseconds, so we * by 1000 for setTimeout*/
	effDur = 0.5;
	
	Effect.Fade("pageCopy",{ duration: effDur,  from: 1, to: 0.001, queue: { position: 'end', scope: 'fadeQue' } });
	Effect.BlindUp("pageCopy",{ duration: effDur, queue: { position: 'end', scope: 'blindQue' } });
		
	/*if we don't finish the effect, sometimes the ajax beats us to it, and it looks weird,
	there is a bug in scriptaculous where afterFinish doesn't always wait until the effect is done*/
	pID = pageID;
	
	/*any parameters we pass to modules must be very unique for that module...
	for example we shouldn't pass one called "productCategoryID", because its possible
	multiple modules might use that name. instead, it would be better to pass, and use,
	a parameter called viewProductProdCatID, or something like that...*/
	
	modParm = moduleParams;
	/*sprictaculous uses seconds, and setTimeout uses milliseconds, so we * by 1000 for setTimeout*/
	setTimeout("pageDataRequest(pID,modParm)",effDur * 1000);
}

function pageDataRequest(pageID,moduleParams){
	
	var pageAjax = ajaxObj();
	
	/*once we hear back from the server, update the div*/
	pageAjax.onreadystatechange=function(){
		if(pageAjax.readyState==4){
			/*0 - title, 1 - name, 2 - code, 3 - metaKeywords, 4 - metaDesc*/
			pageArray = eval(pageAjax.responseText);
			
			try{
				pageTracker._trackPageview(pageArray[0]);
			}
			catch(e){
			}
			
			document.getElementById("pageCopy").innerHTML = pageArray[2];
			document.getElementById("pageMetaKeywords").content = pageArray[3];
			document.getElementById("pageMetaDesc").content = pageArray[4];
			document.title = pageArray[0];
			
			/*setup any modules mapped to this page*/		
			/*we can't just loop through the modules, we need to do a recursive call once the ajax request is made, if we 
			loop through them, we'll be setting up scripts and styles before the html is loaded. we'll increment the counter (the 
			second param), each time we call the function recursively. once there is no more index's in our module array, we'll blind
			down, appea, and show the loader...*/
			
			getPageModules(pageID,0,moduleParams);
			
		}
	}
	
	/*make the request for the page content*/
	var requestParams = "function=getPage&pageID=" + pageID;	
	ajaxPost(pageAjax,"/cmsAPI/pages/pages.php",requestParams);
	
}

/*requests a multi dimensional array, and evaluates it*/
function getPageModules(pageID,moduleIndex,moduleParams){
	var pageModule = ajaxObj();
	
	var requestParams ="function=getPageModules&pageID=" + pageID;
	
	pageModule.onreadystatechange=function(){
		if(pageModule.readyState==4){
			
			try{
			eval(pageModule.responseText);
			}
			catch(e)
			{
				alert(pageModule.responseText);
				alert(e);
			}

			if(ajaxReturn[moduleIndex]){
				/* module itself...*/
				getModuleHTML(ajaxReturn[moduleIndex]["includeFile"],ajaxReturn,moduleIndex,pageID,moduleParams);
			}
			else{
				
				var settingsAjax = ajaxObj();
				
				Effect.Appear("pageCopy",{ duration: effDur, from: 0.001, to: 1, queue: { position: 'end', scope: 'fadeQue' } });
				Effect.BlindDown("pageCopy",{ duration: effDur, queue: { position: 'end', scope: 'blindQue' } });
				
				/*run any script that happens between page loads*/
				settingsAjax.onreadystatechange=function(){
					if(settingsAjax.readyState==4){
						eval(settingsAjax.responseText);
						/*hide loader*/
						document.getElementById("loader").style.display = "none";
						
					}
				}
			
				/*call any script functions that run between page transitions*/
				var settingsParams = "function=getFieldByPriKeyID&fieldName=pageTransition&priKeyID=" + 1;
				ajaxPost(settingsAjax,"/cmsAPI/settings/settings.php",settingsParams);
			}

		}
	}
	
	ajaxPost(pageModule,"/cmsAPI/module/publicModule.php",requestParams);
	
}

function getModuleHTML(modulePath,publicModArray,moduleIndex,pageID,moduleParams){
	var moduleAjax = ajaxObj();
	
	moduleAjax.onreadystatechange=function(){
		if(moduleAjax.readyState==4){
			ajaxResponse = moduleAjax.responseText;
			document.getElementById("pageCopy").innerHTML = document.getElementById("pageCopy").innerHTML + ajaxResponse;
			/*script and style for the module...*/
			insertScriptandStyle(publicModArray[moduleIndex]["jScript"],publicModArray[moduleIndex]["cssLink"],1,publicModArray[moduleIndex]["runScript"]);
			
			moduleIndex++;
			getPageModules(pageID,moduleIndex,moduleParams);
		}
	}

	ajaxPost(moduleAjax,modulePath,moduleParams);

}

