var loftOptions = new Array();

var activeOptions = new Array();

var undoHistory = new Array();
var clickHistory = new Array();

function generateScrollercontent() {
	rstring = '';
	for(i=0;i<loftPlans.length;i++) 
 		rstring += "<a href=\"#\" onclick=\"selectPlan('"+loftPlans[i]['planid']+"'); return false;\"><img src=\"i/plan_"+loftPlans[i]['planid']+"1.gif\" width=\"396\" height=\"252\" alt=\""+loftPlans[i]['planid']+"\" title=\"A1\" border=\"0\" id=\"plana1\"></a>";	
	document.getElementById('scrollercontent').innerHTML = rstring;
}

function generateRctext() {
	rstring = '	<table cellspacing="0" cellpadding="0">';
	for(i=0;i<loftPlans.length;i++) {
		if (loftPlans[i]['status'] == 1) {
			rstring += '<tr valign="top" id="'+loftPlans[i]['planid']+'row" onmouseover="overPlan(\''+loftPlans[i]['planid']+'\');" onmouseout="outPlan(\''+loftPlans[i]['planid']+'\');">';
			rstring += '	<td width="40"><a href="#" onclick="selectPlan(\''+loftPlans[i]['planid']+'\'); return false;"><img src="i/'+loftPlans[i]['planid']+'_0.gif" width="28" height="20" border="0" alt="'+loftPlans[i]['planid']+'" title="'+loftPlans[i]['planid']+'" id="'+loftPlans[i]['planid']+'img"></a></td>';
			rstring += '	<td id="'+loftPlans[i]['planid']+'text">'+loftPlans[i]['planname']+'<br><br></td>';
			rstring += '</tr>';
		}
	}
	rstring += '	</table>';
	document.getElementById('rctext').innerHTML = rstring;
}

var planSelected = 0;

function selectAmenities(x){ 
	document.getElementById('plans'+x).style.color = '#000000';
	document.getElementById('view'+x).style.color = '#000000';
	document.getElementById('amenities'+x).style.color = '#BF1D23';
	document.getElementById('midam').style.visibility = 'visible';
	document.getElementById('rightam').style.visibility = 'visible';
	document.getElementById('rightam2').style.visibility = 'visible';
	document.getElementById('midview').style.visibility = 'hidden';
	document.getElementById('rightview').style.visibility = 'hidden';
   document.getElementById('backbutton').style.visibility = 'hidden';
	document.getElementById('midamtext').style.visibility = 'visible';
	document.getElementById('rightam2').innerHTML = '<img src="i/movehome.gif" width="196" height="55" alt="move home" title="move home">';
}

function showAmenity(x){
    document.getElementById('amstructure').style.color = '#000000';    
    document.getElementById('amsafety').style.color = '#000000';    
    document.getElementById('amfitness').style.color = '#000000';    
    document.getElementById('amresidence').style.color = '#000000';    
    document.getElementById('amkitchen').style.color = '#000000';    
    document.getElementById('ammbath').style.color = '#000000';    
    document.getElementById('amobath').style.color = '#000000';    
    document.getElementById('amlaundry').style.color = '#000000';    
    document.getElementById('am'+x).style.color = '#BF1D23';
	 document.getElementById('midamtext').innerHTML = ammenities[x];
}

var blockHints = 0;

function selectPlan(x) {
	createUndo('planid', x);
	
	plan_i = getPlan_i(x);
	hintShow('<a href="/lofts/">Start over</a> or <a href="" onclick="doUndo(); return false;">undo your last selection</a>');
	blockHints = 1;
			
	planSelected = 1;
		

	document.getElementById('midam').style.visibility = 'hidden';
	document.getElementById('midamtext').style.visibility = 'hidden';
	document.getElementById('rightam').style.visibility = 'hidden';
	document.getElementById('rightam2').style.visibility = 'hidden';
	document.getElementById('midview').style.visibility = 'hidden';
	document.getElementById('rightview').style.visibility = 'hidden';
   document.getElementById('midtext').style.visibility = 'visible';
   document.getElementById('rctext').style.visibility = 'hidden';
   document.getElementById('rcmap').style.visibility = 'visible';
   document.getElementById('leftslider').style.visibility = 'hidden';
   document.getElementById('rightslider').style.visibility = 'hidden';
	document.getElementById('bigletter').src = 'i/'+x+'_2.gif';
	document.getElementById('mapimg').src = 'i/map'+x+'.gif';
	document.getElementById('rcmaptext').innerHTML = loftPlans[plan_i]['plantext'];
	
	pN = loftPlans[plan_i]['planname'];
	pN = pN.split('<br>');
	pN = '<b>'+pN[0]+'</b><br>'+pN[1];
	document.getElementById('col1').innerHTML = pN;

	document.getElementById('col2').innerHTML = '<a href="#" onclick="selectPlan(\''+x+'\'); return false;" id="plans'+x+'" style="color: #BF1D23">See Floor plans</a><br><a href="downloads/'+x +'.pdf" target="_blank">Download</a>';
	document.getElementById('col3').innerHTML = '<a href="#" onclick="selectView(\''+x+'\'); return false;" id="view'+x+'">See View</a><br><a href="#" onclick="selectAmenities(\''+x+'\'); return false;" id="amenities'+x+'">See Amenities</a>';
	document.getElementById('plans'+x).style.color = '#BF1D23';
	document.getElementById('view'+x).style.color = '#000000';
	document.getElementById('amenities'+x).style.color = '#000000';	
}

function getPlan_i(x) {
	plan_i = -1;
	
	for(i=0;i<loftPlans.length;i++) 
		if (loftPlans[i]['planid'] == x)
			plan_i = i;
	
	if (plan_i == -1)
		return false;
	else
		return plan_i;
}

var viewNames = {};
viewNames['e'] = 'east';
viewNames['w'] = 'west';
viewNames['ne'] = 'northeast';
viewNames['nw'] = 'northwest';
viewNames['se'] = 'southeast';
viewNames['sw'] = 'southwest';
 
function selectView(x) {
	plan_i = getPlan_i(x);
	
	viewName = viewNames[loftPlans[plan_i]['view']];
	
	document.getElementById('midam').style.visibility = 'hidden';
	document.getElementById('rightam').style.visibility = 'hidden';
	document.getElementById('rightam2').style.visibility = 'hidden';
	document.getElementById('midview').style.visibility = 'visible';
	document.getElementById('rightview').style.visibility = 'visible';
   document.getElementById('backbutton').style.visibility = 'hidden';
	document.getElementById('midview').innerHTML = '<img src="i/'+viewName+'1.jpg" width="396" height="297" alt="'+viewName+' view" title="'+viewName+' view">';
	document.getElementById('rightview').innerHTML = '<img src="i/'+viewName+'2.jpg" width="196" height="352" alt="'+viewName+' view" title="'+viewName+' view">';
	document.getElementById('plans'+x).style.color = '#000000';
	document.getElementById('view'+x).style.color = '#BF1D23';
	document.getElementById('amenities'+x).style.color = '#000000';
}



function overPlan(x) {
	plan_i = getPlan_i(x);
	document.getElementById(x+'text').innerHTML = '<span style="color: #BF1D23;">'+loftPlans[plan_i]['planname']+'</span><br><br>';
	document.getElementById(x+'img').src = 'i/'+loftPlans[plan_i]['planid']+'_1.gif';	
	xSlideTo(xGetElementById('scrollercontent'),-(plan_i*396),0,250);	
}

function outPlan(x) {
	plan_i = getPlan_i(x);
	document.getElementById(x+'text').innerHTML = loftPlans[plan_i]['planname']+'<br><br>';
	document.getElementById(x+'img').src = 'i/'+loftPlans[plan_i]['planid']+'_0.gif';
}

function hintShow(x){
   if (blockHints != 1)
		document.getElementById('hint').innerHTML = x;
}

function overOption(t, x) {
	if ((loftOptions[t][x] == 1) && (activeOptions[t] != x)) {
		document.getElementById(t+x+'link').style.cursor = 'hand';
	}
	else
		document.getElementById(t+x+'link').style.cursor = 'default';
}

function outOption(t, x) {
	
}

function clickOption(t, x, f) {
		if (loftOptions[t][x] == 1) {
		if (f == null) 
			createUndo(t, x);
		if (activeOptions[t] == -1) {
			activeOptions[t] = x;
			for(i=0;i<loftPlans.length;i++) {
				if (loftPlans[i][t] != x) {
					loftPlans[i]['status'] = 0;
				}
			}
			initOptions();
			updateButtons();
			generateRctext();
		
			document.getElementById('midam').style.visibility = 'hidden';
			document.getElementById('leftslider').style.visibility = 'visible';
			document.getElementById('rightslider').style.visibility = 'visible';
			document.getElementById('rightcol').style.background = '#F2EFEE';
			document.getElementById('rctext').style.visibility = 'visible';
			document.getElementById('scroller').style.visibility = 'visible';
			document.getElementById('midcol').innerHTML = '';
			leftSlide();
			leftSlide();
			leftSlide();
			leftSlide();
		}
	}
}

function updateButtons() {	
	for (testKey in loftOptions['squarefeet']) {
		document.getElementById('sq'+testKey).src = 'i/'+testKey+'_'+loftOptions['squarefeet'][testKey]+'.gif';
	}
	
	for (testKey in loftOptions['floor']) {
		document.getElementById('fl'+testKey+'a').src = 'i/'+testKey+'a'+'_'+loftOptions['floor'][testKey]+'.gif';
		document.getElementById('fl'+testKey+'b').src = 'i/'+testKey+'b'+'_'+loftOptions['floor'][testKey]+'.gif';
	}

	for (testKey in loftOptions['bedrooms']) {
		document.getElementById('big'+testKey).src = 'i/big'+testKey+'_'+loftOptions['bedrooms'][testKey]+'.gif';
	}
	
	for (testKey in loftOptions['view']) {
		document.getElementById('v'+testKey).src = 'i/v'+testKey+'_'+loftOptions['view'][testKey]+'.gif';
	}

	if (activeOptions['squarefeet'] != -1)
		document.getElementById('sq'+activeOptions['squarefeet']).src = 'i/'+activeOptions['squarefeet']+'_2.gif';

	if (activeOptions['floor'] != -1) {
		document.getElementById('fl'+activeOptions['floor']+'a').src = 'i/'+activeOptions['floor']+'a_2.gif';
		document.getElementById('fl'+activeOptions['floor']+'b').src = 'i/'+activeOptions['floor']+'b_2.gif';
	}

	if (activeOptions['bedrooms'] != -1)
		document.getElementById('big'+activeOptions['bedrooms']).src = 'i/big'+activeOptions['bedrooms']+'_2.gif';

	if (activeOptions['view'] != -1)
		document.getElementById('v'+activeOptions['view']).src = 'i/v'+activeOptions['view']+'_2.gif';
}

function initActives() {
	activeOptions['squarefeet'] = -1;
	activeOptions['floor'] = -1;
	activeOptions['view'] = -1;
	activeOptions['bedrooms'] = -1;
	activeOptions['planid'] = -1;
}

function doUndo() {
	undo = clickHistory.pop();

	initAmmenities();
	initLoftPlans();
	initActives();
	initOptions();

	//alert(clickHistory);
	
	if (clickHistory.length > 0)
		for(u=0;u<clickHistory.length;u++) {
//			alert('u='+u+' history='+clickHistory[u]+' length='+clickHistory.length);
			clickOption(clickHistory[u][0], clickHistory[u][1], 1);
	}
	updateButtons();
	generateRctext();
	
	if (planSelected == 1) {
		document.getElementById('midam').style.visibility = 'hidden';
		document.getElementById('rightam').style.visibility = 'hidden';
		document.getElementById('rightam2').style.visibility = 'hidden';
		document.getElementById('midview').style.visibility = 'hidden';
		document.getElementById('rightview').style.visibility = 'hidden';
	   document.getElementById('midtext').style.visibility = 'hidden';
	   document.getElementById('rctext').style.visibility = 'visible';
	   document.getElementById('rcmap').style.visibility = 'hidden';
	   document.getElementById('leftslider').style.visibility = 'visible';
	   document.getElementById('rightslider').style.visibility = 'visible';
		document.getElementById('rcmaptext').innerHTML = '';
		document.getElementById('col1').innerHTML = '';
		document.getElementById('col2').innerHTML = '';
		document.getElementById('col3').innerHTML = '';

		planSelected = 0;
	}

	if (clickHistory.length == 0) {
		document.getElementById('midcol').innerHTML = '<img src="../i/momentum.gif" width="396" height="322" alt="" border="0" id="momentum" border="0" title="" class="hugger">';
		document.getElementById('leftslider').style.visibility = 'hidden';
		document.getElementById('rightslider').style.visibility = 'hidden';
		document.getElementById('rightcol').style.background = '#B9B0AD';
		document.getElementById('rctext').style.visibility = 'hidden';
		document.getElementById('scroller').style.visibility = 'hidden';
	}

	
	hintHide();
}

function hintHide() {
	if (clickHistory.length>0)
		hintShow('Choose from the options above or <a href="#" onclick="doUndo();">undo your last selection</a>');
	else
		hintShow('Choose from the options above');
}

function resetOptions() {
	for(i=0;i<loftPlans.length;i++) {
		loftPlans[i]['status'] = 1;
	}	

	for (testKey in loftOptions['squarefeet']) 
		loftOptions['squarefeet'][testKey] = 1;

	for (testKey in loftOptions['floor']) 
		loftOptions['floor'][testKey] = 1;

	for (testKey in loftOptions['view']) 
		loftOptions['view'][testKey] = 1;

	for (testKey in loftOptions['planid']) 
		loftOptions['planid'][testKey] = 1;
		
	for (testKey in loftOptions['bedrooms']) 
		loftOptions['bedrooms'][testKey] = 1;
}

var currentPlan = -1;

function nextRight() {
	for(i=(currentPlan+1);i<loftPlans.length;i++) {
		if (loftPlans[i]['status'] == 1)
			return i;
	}
	return -1;
}

function nextLeft() {
	for(i=(currentPlan-1);i>-1;i--) {
		if (loftPlans[i]['status'] == 1)
			return i;
	}
	return -1;
}

function scanOptions(opt) {
	loftOptions[opt] = {};
	tempOptions = {};

	for(i=0;i<loftPlans.length;i++) {
		if (tempOptions[loftPlans[i][opt]] != 1)
			loftOptions[opt][loftPlans[i][opt]] = loftPlans[i]['status']; 
		if (loftPlans[i]['status'] == 1)
			tempOptions[loftPlans[i][opt]] = 1;
	}

	tempCount = 0;
	tempKey = '';

	for (testKey in loftOptions[opt]) {
		if (loftOptions[opt][testKey] ==1) {
			tempCount += 1;
			tempKey = testKey;
		}
	}
	
	if (tempCount == 1) {
		activeOptions[opt] = tempKey;
	}

}

function momouse(x, y){
    if (y=='on')  document.getElementById(x).style.background = '#3C3737';
    if (y=='off') document.getElementById(x).style.background = '#7B7070';
}

function mocontact(x, y){
    if (y=='on')  document.getElementById(x).style.background = '#BF1D23';
    if (y=='off') document.getElementById(x).style.background = '#000000';
}

function createUndo(t, x) {
	clickHistory[clickHistory.length] = new Array(t,x);
}

function initOptions() {
	loftOptions = new Array();
	scanOptions('planid');
	scanOptions('squarefeet');
	scanOptions('floor');
	scanOptions('view');
	scanOptions('bedrooms');
}

function leftSlide(){
	nL = nextLeft();
	if (nL != -1) {
		xSlideTo(xGetElementById('scrollercontent'),-(nL*396),0,250);
		currentPlan = nL;
	}
}

function rightSlide(){
	nR = nextRight();
	if (nR != -1) {
		xSlideTo(xGetElementById('scrollercontent'),-(nR*396),0,500);
		currentPlan = nR;
	}
}

window.onload = function() {
  	document.getElementById('midcol').style.visibility = 'visible';
  	document.getElementById('midcol2').style.visibility = 'visible';
  	document.getElementById('midloader').style.visibility = 'hidden';
   initAmmenities();
   initLoftPlans();
	initActives();
   initOptions();
   generateRctext();
	generateScrollercontent();
	xMoveTo(xGetElementById('scrollercontent'),0,0);
}