// JavaScript Document



//xmlhttp vars

var http_request = false;



//form vars

var bedrooms = new Array("1bedrooms","2bedrooms","3bedrooms");

var quads = new Array("vnorth","vsouth","veast","vwest");

var bedroomAll = null;

var viewAll = null;



//animation vars

var lf = null;

var a = null;

var b = null;

var c = null;

var d = null;

var step2slider = null;

var step2container = null;

var dropDownA = null;

var dropDownB = null;

var planSlider = null;

var planWidth = null;

var loftomatic = null;

var loftomaticContainer = null;

var loftlist = null;

var loftDetailsHeight = null;

var loftPDF = null;



//loading

var loftomaticLoading = null;

var searchLoading = null;

var unitLoading = null;



//flags

var loftomaticStatus = -1;

var loftomaticDetailStatus = -1;









function init() {

	step2slider = document.getElementById("step2slider");

	step2container = document.getElementById("step2container");

	dropDownA = document.getElementById("pricerange");

	dropDownB = document.getElementById("availability");

	planSlider = document.getElementById("planSlider");

	loftomatic = document.getElementById("loftomatic");

	loftlist = document.getElementById("loftlist");

	loftomaticLoading = document.getElementById("loftScrollerLoading");

	loftomaticContainer = document.getElementById("loftomaticContainer");

	searchLoading = document.getElementById("searchLoading");

	unitLoading = document.getElementById("unitLoading");

	bedroomAll = document.getElementById("anybedroom");

	viewAll = document.getElementById("anyview");

	loftPDF = document.getElementById("loftpdf");



}



function resetSearch() {

	slideLoftomaticUp()

	slideLoftsUp();	

	slideLoftListIn();

	loftomaticStatus = -1;

	loftomaticDetailStatus = -1;

}



/////////////////////////////////

// stupid checkbox controls

function anyRooms() {

	if(bedroomAll.checked) {

		for(room in bedrooms) {

			document.getElementById(bedrooms[room]).checked = false;

		}

	}

}



function anyView() {

	if(viewAll.checked) {

		for(vtype in quads) {

			document.getElementById(quads[vtype]).checked = false;

		}

	}

}



function uncheckAllRooms(e) {

	if(e.checked) {

		bedroomAll.checked = false	;

	}

}



function uncheckAllViews(e) {

	if(e.checked) {

		viewAll.checked = false;

	}

}





/////////////////////////////////////////////////

// Vertical Plan Slider



function slideLoftsDown() {

	if (a != null) {

		a.stop();

		a = null;

	}



	searchLoading.style.visibility = "hidden";

	var dest = 146;



	a = new Accelimation(step2container.style, "height", dest, 300, .5, "px");

	a.onend = onDownEnd;

	a.onframe = planScroller;

	a.start();



	dropDownA.style.visibility = "hidden";

	dropDownB.style.visibility = "hidden";



}



function slideLoftsUp() {

	if (a != null) {

		a.stop();

		a = null;

	}



	var dest = 0;



	a = new Accelimation(step2container.style, "height", dest, 300, .5, "px");

	a.onend = onUpEnd;

	a.onframe = planScroller;

	a.start();



}



function planScroller(x) {

	step2container.style.height = x + "px";

	step2slider.style.top = (x - 146) + "px";

}



function onDownEnd() {

	a.stop();

	a = null;

}



function onUpEnd() {

	a.stop();

	a = null;



	dropDownA.style.visibility = "visible";

	dropDownB.style.visibility = "visible";

}



function viewPlans() {

	slideLoftsDown();	

}





/////////////////////////////////////////////////

// Horizontal Plan Slider



function slideLoftsLeft() {

	if (b != null) {

		return;

	}

	

	planWidth = parseInt(planSlider.style.width);

	planLeft = parseInt(planSlider.style.left);



	if((planLeft - 420) == -planWidth) {

		return;

	}



	b = new Accelimation(planSlider.style, "left", planLeft - 420, 800, .9, "px");

	b.onend = onPlanScrollEnd;

	b.onframe = horizPlans;

	b.start();

}



function slideLoftsRight() {

	if (b != null) {

		return;

	}

	

	planWidth = parseInt(planSlider.style.width);

	planLeft = parseInt(planSlider.style.left);

	

	if((planLeft + 420) > 0) {

		return;

	}



	b = new Accelimation(planSlider.style, "left", planLeft + 420, 800, .9, "px");

	b.onend = onPlanScrollEnd;

	b.onframe = horizPlans;

	b.start();

}



function horizPlans(x) {

	planSlider.style.left = x + "px";

}



function onPlanScrollEnd() {

	b.stop();

	b = null;

}



/////////////////////////////////////////////////

// Big Loft Plan Slider



function slideLoftomaticDown() {

	if ((c != null) || (parseInt(loftomatic.style.top) >= 3)) {

		return;

	}

	

	c = new Accelimation(loftomatic.style, "top", 3, 700, .9, "px");

	c.onend = onLoftomaticDownEnd;

	c.onframe = loftomaticFrame;

	c.start();

	

	loftomaticLoading.style.visibility = "hidden";

	loftPDF = document.getElementById("loftpdf");

	loftPDF.style.visibility = "visible";



}



function slideLoftomaticUp() {

	if ((c != null) || (parseInt(loftomatic.style.top) < 3)) {

		//return;

	}



	loftPDF.style.visibility = "hidden";



	c = new Accelimation(loftomatic.style, "top", -326, 300, .9, "px");

	c.onend = onLoftomaticUpEnd;

	c.onframe = loftomaticFrame;

	c.start();

}



function loftomaticFrame(x) {

	loftomatic.style.top = x + "px";

}



function onLoftomaticDownEnd() {

	c.stop();

	c = null;

	

}



function onLoftomaticUpEnd() {

	c.stop();

	c = null;



	if(loftomaticStatus > 0) {

		loftomaticControl(loftomaticStatus);	

	}

}



function showPlan() {

	slideLoftomaticDown();

	slideLoftListOut();

}



/////////////////////////////////////////////////

// Loft List Slider



function slideLoftListOut() {

	if ((d != null) || (parseInt(loftlist.style.left) == 0)) {

		//return;

	}

	

	d = new Accelimation(loftlist.style, "left", 0, 500, .9, "px");

	d.onend = onLoftlistEnd;

	d.onframe = loftlistFrame;

	d.start();

}



function slideLoftListIn() {

	if ((d != null) || (parseInt(loftomatic.style.left) < 0)) {

		return;

	}

	

	d = new Accelimation(loftlist.style, "left", -220, 200, .5, "px");

	d.onend = onLoftlistEnd;

	d.onframe = loftlistFrame;

	d.start();

}



function loftlistFrame(x) {

	loftlist.style.left = x + "px";

}



function onLoftlistEnd() {

	d.stop();

	d = null;

}





//XMLHTTP Functions

/////////////////////////////////////////////////////

function xmlRequest() {

	http_request = false;



	if (window.XMLHttpRequest) { // Mozilla, Safari,...

		http_request = new XMLHttpRequest();

	} else if (window.ActiveXObject) { // IE

		try {

			http_request = new ActiveXObject("Msxml2.XMLHTTP");

		} catch (e) {

			try {

				http_request = new ActiveXObject("Microsoft.XMLHTTP");

			} catch (e) {}

		}

	}



	if (!http_request) {

		alert('Your browser is incompatible with our web site\n\nPlease visit www.getfirefox.com for a superior browsing experience');

		return false;

	} else {

		return true;	

	}

}



//get horizontal plan scroller from a search

//////////////////////////////////////////////////////////

function getPlans() {

	//check for any forbidden search settings (don't think there are any)



	if(xmlRequest()) {

		http_request.onreadystatechange = gotPlans;

		http_request.open('POST', "planFinder.php", true);

		http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

		

		searchLoading.style.visibility = "visible";

		pInfo = gimmeSearchVars("finderstep=1");		//add this later - send search form data



		http_request.send(pInfo);

	}

}



function gotPlans() {

	if (http_request.readyState == 4) {

		if (http_request.status == 200) {

			//alert(http_request.responseText);

			document.getElementById("step2slider").innerHTML = http_request.responseText;

			planSlider = document.getElementById("planSlider");

			slideLoftsDown();



	} else {

//			alert('There was a problem with the request.');

		}

	}

}



function loftomaticControl(pID) {

	if (c != null || d != null) {

		//return;

	}

	

	loftomaticLoading.style.visibility = "visible";



	if (loftomaticStatus == 0) {

		loftomaticStatus = pID;

		slideLoftomaticUp();

		slideLoftListIn();

	} else {

		loftomaticStatus = 0;

		showPlan(pID);

	}

}



// get big loftomatic plan image and name

function showPlan(pID) {

	if(xmlRequest()) {

		http_request.onreadystatechange = gotPlanDetail;

		http_request.open('POST', "planFinder.php", true);

		http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

		

		var pInfo = "finderstep=2&planid=" + pID;

		lf = pID;

		

		http_request.send(pInfo);

	}

}



function gotPlanDetail() {

	if (http_request.readyState == 4) {

		if (http_request.status == 200) {

			//alert(http_request.responseText);

			document.getElementById("loftomaticwaxecstatic").innerHTML = http_request.responseText;

			getLoftList(lf);

			lf = null;



	} else {

			alert('There was a problem with the request.');

		}

	}

}



function getLoftList(pID) {

	//check for any forbidden search settings (don't think there are any)



	if(xmlRequest()) {

		http_request.onreadystatechange = gotLoftList;

		http_request.open('POST', "planFinder.php", true);

		http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

		

		//if we want search info to carry over we add that here

		var pInfo = "finderstep=3&planid=" + pID;

		

		http_request.send(pInfo);

	}

}



function gotLoftList() {

	if (http_request.readyState == 4) {

		if (http_request.status == 200) {

			//alert(http_request.responseText);

			document.getElementById("listolofts").innerHTML = http_request.responseText;

//			slideLoftomaticDown();

			slideLoftListOut();



	} else {

//			alert('There was a problem with the request.');

		}

	}

}



function gimmeSearchVars(pInfo) {

	if(!bedroomAll.checked) {

		cuartos = "";

		for(room in bedrooms) {

			cuarto = document.getElementById(bedrooms[room]);

			if(cuarto.checked) {

				cuartos += (cuartos == "") ? cuarto.value : "," + cuarto.value;

			}

			pInfo += (cuartos == "") ? "" : "&roomy=" + cuartos;

		}

	}



	if(!viewAll.checked) {

		views = "";

		for(vtype in quads) {

			vw = document.getElementById(quads[vtype]);

			if(vw.checked) {

				views += (views == "") ? vw.value : "," + vw.value;

			}

			pInfo += (views == "") ? "" : "&view=" + views;

		}

	}

	

	price = document.getElementById("pricerange");

	if(price.selectedIndex > 0) {

		pInfo += "&price=" + price.options[price.selectedIndex].value;

	}



	avail = document.getElementById("availability");

	if(avail.selectedIndex > 0) {

		pInfo += "&avail=" + avail.options[avail.selectedIndex].value;

	}



	return pInfo;

}



//get loft unit details

//////////////////////////////////////////////////////////

function getUnitDetails(uid) {

	if(xmlRequest()) {

		http_request.onreadystatechange = gotUnit;

		http_request.open('POST', "loftFinder.php", true);

		http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');



		unitLoading.style.visibility = "visible";

		pInfo = "uid=" + uid;



		http_request.send(pInfo);

	}

}



function gotUnit() {

	if (http_request.readyState == 4) {

		if (http_request.status == 200) {

			//alert(http_request.responseText);

			document.getElementById("loftomaticforever").innerHTML = http_request.responseText;

			slideLoftomaticDownFarther();

	} else {

//			alert('There was a problem with the request.');

		}

	}

}



/////////////////////////////////////////////////

// Big Loft Plan Slider



function slideLoftomaticDownFarther() {

	if ((c != null) || (parseInt(loftomatic.style.top) >= 653)) {

		return;

	}



	unitLoading.style.visibility = "hidden";



	c = new Accelimation(loftomatic.style, "top", 705, 700, .9, "px");

	c.onend = onLoftomaticDetailsDownEnd;

	c.onframe = loftomaticDetailsFrame;

	c.start();

	

}



function slideLoftomaticDetailsUp() {

	if ((c != null) || (parseInt(loftomatic.style.top) < 653)) {

		return;

	}

	

	c = new Accelimation(loftomatic.style, "top", 3, 300, .9, "px");

	c.onend = onLoftomaticDetailsUpEnd;

	c.onframe = loftomaticDetailsFrame;

	c.start();

}



function loftomaticDetailsFrame(x) {

	

	loftomaticContainer.style.height = (x >= 0) ? x + 360 + "px" : (-1 * x) + "px";

	loftomatic.style.top = x + "px";

}



function onLoftomaticDetailsDownEnd() {

	c.stop();

	c = null;

}



function onLoftomaticDetailsUpEnd() {

	c.stop();

	c = null;



	if(loftomaticDetailStatus > 0) {

		loftomaticDetailControl(loftomaticDetailStatus);	

	}



}



function loftomaticDetailControl(pID) {

	if (c != null || d != null) {

		return;

	}

	

//	unitLoading.style.visibility = "visible";



	if (loftomaticDetailStatus == 0) {

		loftomaticDetailStatus = pID;

		slideLoftomaticDetailsUp();

	} else {

		loftomaticDetailStatus = 0;

		getUnitDetails(pID);

	}

}


