// flashMessanger.js
// Code for messaging flash and changing buildings

// globals
var building = 3;
var lastBuilding = 0;
var floor = 1;
var beds = 0;
var unit = 'nil';
var size = 0;

function unitString() {
	return building.toString() + floor.toString() + unit.toString();
}

function getplans(rooms) {	
	$('rooms_0_info').style.display = 'none';
	$('rooms_1_info').style.display = 'none';
	$('rooms_1s_info').style.display = 'none';
	$('rooms_2_info').style.display = 'none';
	$('rooms_2s_info').style.display = 'none';
	$('rooms_3_info').style.display = 'none';
	
	var study = 0;

	if (rooms.indexOf('s') != -1)
		study = 1;
		
	new Ajax.Request('include/planInfo.php', {
		method: 'post',
		parameters: 'action=list' + '&rooms=' + rooms + '&study=' + study,
		onSuccess: function(response) {
			$('rooms_' + rooms + '_info').innerHTML = '<div style="margin: 2px;">' + response.responseText + '</div>';
			new Effect.SlideDown($('rooms_' + rooms + '_info'));
		}
	});
}

function updateplan(afloor,abuilding,thesize,numbeds,aunit, from) {
/*
	if (!eval('from')) {
		from = 'flash';
	}
	
	if (from != 'flash') {
		$('flashWidget').innerHTML = '';
	}
*/	
	if (!eval('aunit')) {
		var aunit = unit;
	}
	
	if (aunit == 'nil')
		aunit = '01';
	
	aunit = aunit * 1;
	unitSansPadding = aunit;
	if (aunit < 10)
		aunit = "0" + aunit.toString();
	else if (aunit == 10)
		aunit = "10";
	else 
		aunit = aunit;
		
	unit = aunit.toString();

	setfloor(afloor);
	setbuilding(abuilding);
	
	unitImageName ="images/unitplans/" + unitString() + ".gif";
	$('unitImage').innerHTML = '<a href="javascript:openpdf()"><img src="' + unitImageName + '" lowsrc="images/placeholder.gif" alt="Building Image" /></a>'
	$('labelUnit').innerHTML = 'Unit ' + unitString();
	
	if (eval('thesize'))
		$('labelSize').innerHTML = 'approx. ' + thesize + " sq. ft. &#149; ";
	else
		$('labelSize').innerHTML = '&nbsp;'
		
	if (eval('numbeds')) {
		if (numbeds == 0)
			$('labelBeds').innerHTML = 'studio';
		else
			$('labelBeds').innerHTML = numbeds + ' bedroom';
	} else {
		$('labelBeds').innerHTML = '&nbsp;'
	}
	/*
	if (from != 'flash') {
		random = Math.random();
		$('flashWidget').innerHTML = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="180" height="180" title="Couple"><param name="movie" value="swfs/buildings1.swf#'+random+'?thefloor=' + afloor + '&thebuilding=' + abuilding + '&theunit=' + unitSansPadding + '"><param name="quality" value="high"><embed src="swfs/buildings1.swf#'+random+'?thefloor=' + afloor + '&thebuilding=' + abuilding + '&theunit=' + unitSansPadding + '" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="180" height="180"></embed></object>';
		expandBuilding(abuilding, afloor);
	}
	*/
}


// function for SOLD plans only difference is folder images are from and the word SOLD in front of label:
function updateplan_sold(afloor,abuilding,thesize,numbeds,aunit, from) {

	if (!eval('aunit')) {
		var aunit = unit;
	}
	
	if (aunit == 'nil')
		aunit = '01';
	
	aunit = aunit * 1;
	unitSansPadding = aunit;
	if (aunit < 10)
		aunit = "0" + aunit.toString();
	else if (aunit == 10)
		aunit = "10";
	else 
		aunit = aunit;
		
	unit = aunit.toString();

	setfloor(afloor);
	setbuilding(abuilding);
	
	unitImageName ="images/unitplans_sold/" + unitString() + ".gif";
	$('unitImage').innerHTML = '<a href="javascript:openpdf()"><img src="' + unitImageName + '" lowsrc="images/placeholder.gif" alt="[SOLD]" /></a>'
	$('labelUnit').innerHTML = '[SOLD] Unit ' + unitString();
	
	if (eval('thesize'))
		$('labelSize').innerHTML = 'approx. ' + thesize + " sq. ft. &#149; ";
	else
		$('labelSize').innerHTML = '&nbsp;'
		
	if (eval('numbeds')) {
		if (numbeds == 0)
			$('labelBeds').innerHTML = 'studio';
		else
			$('labelBeds').innerHTML = numbeds + ' bedroom';
	} else {
		$('labelBeds').innerHTML = '&nbsp;'
	}
}

//1/08 added:
// function for UNAVIALBLE plans only difference is folder images are from and the word UNAVIALBLE in front of label:
function updateplan_unavailable(afloor,abuilding,thesize,numbeds,aunit, from) {

	if (!eval('aunit')) {
		var aunit = unit;
	}
	
	if (aunit == 'nil')
		aunit = '01';
	
	aunit = aunit * 1;
	unitSansPadding = aunit;
	if (aunit < 10)
		aunit = "0" + aunit.toString();
	else if (aunit == 10)
		aunit = "10";
	else 
		aunit = aunit;
		
	unit = aunit.toString();

	setfloor(afloor);
	setbuilding(abuilding);
	
	unitImageName ="images/unitplans_unavailable/" + unitString() + ".gif";
	$('unitImage').innerHTML = '<a href="javascript:openpdf()"><img src="' + unitImageName + '" lowsrc="images/placeholder.gif" alt="[UNAVAILABLE]" /></a>'
	$('labelUnit').innerHTML = '[UNAVAILABLE] Unit ' + unitString();
	
	if (eval('thesize'))
		$('labelSize').innerHTML = 'approx. ' + thesize + " sq. ft. &#149; ";
	else
		$('labelSize').innerHTML = '&nbsp;'
		
	if (eval('numbeds')) {
		if (numbeds == 0)
			$('labelBeds').innerHTML = 'studio';
		else
			$('labelBeds').innerHTML = numbeds + ' bedroom';
	} else {
		$('labelBeds').innerHTML = '&nbsp;'
	}
}



function expandBuilding(buildingNumber, floorNumber) {
	// Put all the buildings and floors we are displaying in an array
	var buildings = Array(3, 4, 5);
	var floors = Array(1, 2, 3, 4);
	
	// Set all the floors back to there respective colors for the current building
	for (index = 0; index < floors.length; index++) {
		$('buildings_' + building + '_floor_' + floors[index]).style.color = "#a7c951";
	}
	
	// Set all the buildings to the untouched state
	for (index = 0; index < buildings.length; index++) {
		$('buildings_' + buildings[index] + '_button').style.color = "#a7c951";
	}
	
	// Set the color of the floor
	$('buildings_' + building + '_floor_' + floor).style.color = "white";
	$('buildings_' + building + '_button').style.color = "white";
	
	// Don't do any movement effect if the building hasn't changed
	if (lastBuilding == buildingNumber)
		return;
	
	// Close all the buildings
	for (index = 0; index < buildings.length; index++) {
		$('buildings_' + buildings[index] + '_info').style.display = 'none';
	}
	
	new Effect.SlideDown($('buildings_' + building + '_info'));
}

// switchProxy
function switchProxy(from) {
	if (unit == 'nil')
		unit = '01';
		
	new Ajax.Request('include/planInfo.php', {
		method: 'post',
		parameters: 'action=size&unit=' + unitString(),
		onSuccess: function(response) {
			setsize(response.responseText);
			
			new Ajax.Request('include/planInfo.php', {
				method: 'post',
				parameters: 'action=beds&unit=' + unitString(),
				onSuccess: function(response) {
					setbeds(response.responseText);
					if (from == 'html')
						updateplan(floor, building, size, beds, unit, 'html');
					else
						updateplan(floor, building, size, beds, unit, 'flash');						
				}
			});
		}
	});
}

// -- actions
function setbuilding(newBuilding) {
	lastBuilding = building;
	building = newBuilding;
}

function setfloor(newFloor) {
	floor = newFloor;
}

function setunit(newUnit) {
	newUnit = newUnit * 1;
	if (newUnit < 10)
		unit = "0" + newUnit.toString();
	else if (newUnit == 10)
		unit = "10";
	else 
		unit = newUnit;
		
	unit = unit.toString();
	switchProxy('flash');
}

function setsize(newSize) {
	size = newSize;
}

function setbeds(newBeds) {
	beds = newBeds
}

function openpdf() {
	var unitPDF;
	if (unit == 'nil')
		unitPDF = "pdfs/siteplan_rendering.pdf";
	else
		unitPDF = "pdfs/Unit" + unitString() + ".pdf";
	windowPopup(unitPDF, {scollbars: 'yes', width: '780', height: '580'});
}

function openspecpdf() {
	//assumes we know how many global thebeds there are...
/*	
	thepdflist="specs_1bedoffice,specs_studio-1,specs_2-3beds,specs_2-3beds";
	thepdfs=thepdflist.split(",");
	whichpdf="pdfs/" + thepdfs[thebeds] + ".pdf";
*/
	// for now, use the default "all floors" pdf...
	whichpdf="pdfs/specsheets_all.pdf";
	windowPopup(whichpdf, {scollbars: 'yes', width: '780', height: '580'});
}

// -- Macromedia code
var isInternetExplorer = navigator.appName.indexOf("Microsoft") != -1;
// Handle all the FSCommand messages in a Flash movie.
function buildings1_DoFSCommand (command, args) {
	var buildings1Obj = isInternetExplorer ? document.all.buildings1 : document.buildings1;
	if (command.indexOf("thepdf")) {
		thepdf=args;
		document.images.proxy.src="images/unitplans/" + thepdf + ".gif";
		document.forms.myform.themessage.value=thepdf;
	}

	// Hook for Internet Explorer.
	if (navigator.appName && navigator.appName.indexOf("Microsoft") != -1 && 	navigator.userAgent.indexOf("Windows") != -1 && navigator.userAgent.indexOf("Windows 3.1") == -1) {
		document.write('<script language=\"VBScript\"\>\n');
		document.write('On Error Resume Next\n');
		document.write('Sub buildings1_FSCommand(ByVal command, ByVal args)\n');
		document.write('	Call buildings1_DoFSCommand(command, args)\n');
		document.write('End Sub\n');
		document.write('</script\>\n');
	}
}
	
	
