/*******************************
 ***** ROTATING PROPERTIES *****
 *******************************/
window.onload = init;

var slideshow_width = 100;
var slideshow_height = 75;
var imageCSSClass = "fp_image";
var fadeImages = new Array();
var textArray = new Array();
var isDataReady = false;
var intvl = null;

var fpReq = getXMLHttpRequestObject();
var pdReq = getXMLHttpRequestObject();

if(fpReq && pdReq) {
	//attempt to load FeaturedProperties.xml
	fpReq.open("GET", "/xml/FeaturedProperties.xml", true);
	fpReq.onreadystatechange = loadProjectData;
	fpReq.send(null);
} //end if

function loadProjectData() {
	if(fpReq.readyState != 4 || fpReq.status != 200) return;

	//attempt to load projectdata.xml
	pdReq.open("GET", "/xml/projectdata.xml", true);
	pdReq.onreadystatechange = fillDataArrays;
	pdReq.send(null);
} //end function loadProjectData

function fillDataArrays() {
	if(pdReq.readyState != 4 || pdReq.status != 200) return;

	//fill fadeImages/textArray arrays
	var fpDoc = fpReq.responseXML.documentElement;
	var pdDoc = pdReq.responseXML.documentElement;
	var fpList = fpDoc.getElementsByTagName("FeaturedProperty");
	var projList = pdDoc.getElementsByTagName("Project");

	var imgElem = null;
	var copyElem = null;
	var fProjElem = null;
	var enabledElm = null;
	
	for(var i = 0; i < fpList.length; i++) {
		imgElem = fpList[i].getElementsByTagName("Image"); //contains image path
		fProjElem = fpList[i].getElementsByTagName("ProjectGUID"); //contains project guid
		enabledElm = fpList[i].getElementsByTagName("isEnabled"); //if the featured project is enabled

		if (enabledElm.length == 0)
		    continue;

		if (enabledElm[0].firstChild.nodeValue != "true")
		    continue;
		
		
		var guid = fProjElem[0].firstChild.nodeValue;
		linkDest = "javascript:gotoProjectPage(\'" + guid + "\');";

		//search through projList for name/location of featured product
		copyElem = fpList[i].getElementsByTagName("Copy"); //contains copy for current featured property
		var pdProjElem = null;
		var state = "";
		var altText = "";
		for(var j = 0; j < projList.length; j++) {
		    pdProjElem = projList[j];

		          
			        if(guid == pdProjElem.getAttribute("Project_GUID")) {
				        state = pdProjElem.getAttribute("State") == "FL" ? "Florida" : pdProjElem.getAttribute("State");
				        textArray.push(
					        {
						        "guid" : guid,
						        "title" : pdProjElem.getAttribute("ProjectName"),
						        "location" : pdProjElem.getAttribute("City") + ", " + state,
						        "copy" : copyElem[0].firstChild.nodeValue
					        }
				        );

				        altText = pdProjElem.getAttribute("ProjectName") != "" ? pdProjElem.getAttribute("ProjectName") : "";
				        fadeImages.push(new Array("/" + imgElem[0].firstChild.nodeValue, linkDest, "", altText));

				        break;
			        } //end if
		        } //end for
	} //end for

	//randomize arrays
	try { shuffle(fadeImages, textArray); }
	catch(err) {
		alert(err.message);
	} //end catch

	isDataReady = true;
} //end function fillDataArrays

function init() {
	isAuto = true;
	pause = 10000;
	if(isDataReady) start(displayPropertyData);
	else intvl = setInterval(startFading, 1000);
} //end function init

function startFading() {
	if(isDataReady) {
		clearInterval(intvl);
		start(displayPropertyData);
	} //end if

	return;
} //end function startFading

function displayPropertyData() {
	//write out data to data fields
	var title = document.getElementById("lblFeatureTitle");
	var location = document.getElementById("lblFeatureLocation");
	var copy = document.getElementById("lblFeatureCopy");
	var link = document.getElementById("hlFeatureLink2");
	if(textArray[curimageindex]) {
		title.innerHTML = textArray[curimageindex].title;
		location.innerHTML = textArray[curimageindex].location;
		copy.innerHTML = textArray[curimageindex].copy;
		link.href = "javascript:gotoProjectPage('" + textArray[curimageindex].guid + "');";
	} //end if
	else {
		title.innerHTML = "";
		location.innerHTML = "";
		copy.innerHTML = "";
		link.href = "#";
	} //end else

	//resize IFrame window to prevent scrollbars
	if(parent.adjustIFrameSize) parent.adjustIFrameSize(window);
} //end function displayPropertyData

function gotoProjectPage(guid) {
	if(parent) parent.window.location = "Projects.aspx?prjID=" + guid;
} //end function gotoPage

function getXMLHttpRequestObject() {
	if(window.XMLHttpRequest) return new XMLHttpRequest();
	else if(window.ActiveXObject) {
		try { return new ActiveXObject("Msxml2.XMLHTTP.6.0"); } catch(e) {}
		try { return new ActiveXObject("Msxml2.XMLHTTP.3.0"); } catch(e) {}
		try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) {}
		try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {}
	} //end else

	return null;

	
} //end function getXMLHttpRequestObject
