/*************************
**       Author         **
**      -======-        **
**     Mike Harris      **
*************************/

var myFx = null;
var divContent = null;

// lets get it started - ha, lets get it started - in here!
function jaxMeUp(iCurrentOffset)
{
	// get the div we're gonna work with ...
	divContent = $('result_holder');

	// create a moostyle reference to my XML fetcher passing the chosen search criteria as a parameter ...
	var strOptions = getCriteria() + "|" + iCurrentOffset;
	var fnCall = getPropertyXML.pass(strOptions);

	// then create a chain to hide the div's current contents, replace them, then reshow them
	myFx = new Fx.Style(divContent, 'opacity', {duration:1000, transition:Fx.Transitions.expoOut});
	myFx.chain(function(){myFx.start(1,0);});
	myFx.chain(fnCall);
	myFx.chain(function(){myFx.start(0,1);});
	
	// fire off the first event in the chain here (NB. the oncompletes of the two myFx calls have call
	// chain calls within them but my replaceContents function does not ... so at the bottom of it we
	// need another callchain call to kick off the final myfx transition)
	myFx.callChain();
}

// set up the variables for the ajax call
function getCriteria()
{
	var arrOptions = new Array(	$('search').value,
								$('propertytype').selectedIndex,
								$('location').selectedIndex,
								$('minimum').selectedIndex,
								$('maximum').selectedIndex,
								$('bedrooms').selectedIndex,
								$('status').value  	);
	
	return arrOptions.join("|");
}

// put in the new HTML
function replaceContents(strNewContent)
{
	divContent.innerHTML = strNewContent;
		
	// kick off the final myfx transition
	myFx.callChain();
}

// create the HTML to render
function buildContent(arrPropData, arrDevNames, arrDevLocs, iPages, iOffset)
{
	var strToReturn =	"<div class=\"pagelist top\">\n" +
						ajaxPaging((arrPropData.length > 0), true, iPages, iOffset) +
						"\t<div class=\"clear\"></div>\n" +
						"</div>\n" +
						"<!--START PROPERTY_ITEMS-->\n";
	
	for(var i = 0; i < arrPropData.length; i++)
	{
		var strImagePath  = "../z_images/photos/test2.jpg";
		var strImageAlt   = arrPropData[i].getMainImage();
		
		if(strImageAlt != "")
		{
			strImagePath = "../z_includes/resizeimage.php?image=../fin/modules/Library/assets/" + strImageAlt + "&amp;width=85&amp;square=1";
		}
		
		strToReturn +=	"<div class=\"property_item" + 
						(arrPropData[i].getAvailable() == 1 ? " available" : "") +
						((i % 2 == 1) ? " odd" : "") + "\">\n" +
						"\t<a href=\"viewaproperty.php?id=" + arrPropData[i].getID() + "&offset=" + iOffset + "\"><img src=\"" + strImagePath + "\" alt=\""+ strImageAlt +"\" border=\"0\"/></a>\n" + 
						"\t<ul>\n" +
						"\t\t<li>" + arrPropData[i].getTitle() + " | <span class=\"green\">" + arrDevNames[arrPropData[i].getDevelopmentID()] + "</span></li>\n" +						
						"\t\t<li class=\"green\">" + arrDevLocs[arrPropData[i].getDevelopmentID()] + "</li>\n" +						
						"\t\t<li>" + arrPropData[i].getBedrooms() + " Bedrooms</li>\n";
						
		if(arrPropData[i].getPrice() > 0 && arrPropData[i].getAvailable() != 2)
		{
			strToReturn += "\t\t<li><strong>" + arrPropData[i].getPriceFormatted() + "</strong></li>\n";
		}
						
		strToReturn +=	"\t\t<li><a href=\"./viewaproperty.php?id=" + arrPropData[i].getID() + "&offset=" + iOffset + "\" class=\"button\">View Details</a></li>\n" +												
						"\t</ul>\n" +
						"\t<div class=\"clear\"></div>\n" +						
						"</div>\n";
	}

	strToReturn 	+=	"<!--END PROPERTY_ITEMS-->\n" +	
						"<div class=\"clear\"></div>\n" +						
						"<div class=\"pagelist bottom\">\n" +
						ajaxPaging((arrPropData.length > 0), false, iPages, iOffset) +
						"\t<div class=\"clear\"></div>\n" +						
						"</div>\n" +
						"<!--CLOSE COLUMN_FIRST-->\n";
						
	replaceContents(strToReturn);
}

// use AJAX to request the data
function getPropertyXML(strCriteria)
{
	// create the options for this call
	var objRequest = {arrSearchCriteria:strCriteria};
	
	// dispatch our ajax request, notably bound to the current object
	new Ajax("./buildXML.php", {onComplete:this.ajax_writePropertyDetails.bind(this), postBody:objRequest}).request();
	
	return;
}

// AJAX responder to write the property details
function ajax_writePropertyDetails(objResponseText, objResponseXML)
{
	if(objResponseXML)
	{
		for(var i = 0; i < objResponseXML.childNodes.length; i++)
		{
			if(objResponseXML.childNodes[i].nodeName == "response")
			{
				// grab the properties from the XML
				var properties		 = objResponseXML.childNodes[i].childNodes;
				var arrAllProperties = new Array();
				var arrDevNames		 = new Array();
				var arrDevLocations  = new Array();
				var iOffsetReceived  = -1;
				var iPagesReceived	 = -1;

				for(var j = 0; j < properties.length; j++)
				{
					if(properties[j].nodeName == "property" && properties[j].firstChild.nodeValue.toString() != "")
					{	
						// evaluate the property
						var objJsonProperty = Json.evaluate(properties[j].firstChild.nodeValue.toString());

						// create a "proper" version of the asset
						var objProperty		= new property(	objJsonProperty.ID,
															objJsonProperty.fDevelopmentID,
															objJsonProperty.fTitle,
															objJsonProperty.fPrice,
															objJsonProperty.fPostcode,
															objJsonProperty.fBedrooms,
															objJsonProperty.fDescription,
															objJsonProperty.fActive,
															objJsonProperty.fAvailable,
															objJsonProperty.fCompletion,
															objJsonProperty.fFloorPlans,
															objJsonProperty.fAttachments,
															objJsonProperty.fDimensions,
															objJsonProperty.fPropertyTypeID,															
															objJsonProperty.fMainImage		);

						// write it out
						arrAllProperties.push(objProperty);
					}
					else if(properties[j].nodeName == "development")
					{
						var objDev = properties[j].childNodes;
						var iPos = null;
						
						for(var k = 0; k < objDev.length; k++)
						{
							if(objDev[k].nodeName == "id")
							{
								iPos = objDev[k].firstChild.nodeValue.toString();
							}
							else if(objDev[k].nodeName == "name")
							{
								arrDevNames[iPos] = objDev[k].firstChild.nodeValue.toString();
							}
							else
							{
								arrDevLocations[iPos] = objDev[k].firstChild.nodeValue.toString();							
							}
						}
					}
					else if(properties[j].nodeName == "offset")
					{
						iOffsetReceived = properties[j].firstChild.nodeValue.toString();
					}
					else if(properties[j].nodeName == "pages")
					{
						iPagesReceived = properties[j].firstChild.nodeValue.toString();
					}					
					else
					{
						// must be a debug string ... so alert it's value
						alert(properties[j].toString());
					}
				}

				buildContent(arrAllProperties, arrDevNames, arrDevLocations, iPagesReceived, iOffsetReceived);
			}
		}
	}
	else
	{
		alert("Invalid AJAX response: "+objResponseText);
	}
}

function ajaxPaging(blSomeData, blTop, iPages, iOffset)
{
	var strToReturn = "";
	
	if(blSomeData)
	{
		strToReturn +=	"<h4 class=\"floatleft\"><span class=\"hidden\">Jump to result </span>page:</h4>\n" +
						"<ul>\n";
		
		var strLink = "";
		for(var i = 0; i < iPages; i++)
		{
			strLink = "<a href=\"\" onclick=\"javascript:jaxMeUp(" + i + "); return false;\">";
			strToReturn += "<li>" + (i != iOffset ? strLink : "") + (i + 1);
			if (i < (iPages - 1)){ strToReturn += " | "; }
			strToReturn += (i != iOffset ? "</a>" : "") + "</li>\n";
		}

		strToReturn += "</ul>\n";
	}
	else if(blTop)
	{
		strToReturn += "<p class='green'><strong>We couldn't find any properties that match your criteria</strong></p>\n";
	}

	var iNextPage = parseInt(iOffset) + 1;
	if(iNextPage < iPages)
	{
		strToReturn += "<a href=\"\" onclick=\"javascript:jaxMeUp(" + iNextPage + "); return false;\" class=\"button floatright\">Next page</a>\n";
	}
	else
	{
		strToReturn += "<div class=\"floatright\"></div>\n";
	}
	
	return strToReturn;
}