var homesList = new Array();
var cmuList = new Array();
var divId;
var image, loading;

var filter = "";
var filteredIndex;
var new_r,detail_r;

var dheight = 1;
var floorplans,detail,detail_l,detail_r,content;

//homesList Structure===================

//floorplanList[i][0] = Marketing Name
//floorplanList[i][1] = Floorplan Code
//floorplanList[i][2] = Thumb ID
//floorplanList[i][3] = Built In Codes
//floorplanList[i][3] = Models In Codes
//floorplanList[i][5] = [[ID,title],[ID,title],[ID,title]]



//cmuList Structure===================

//cmuList[CODE] = Marketing Name


function init(){
	filter = (queryString["code"])?(queryString["code"]):("");
	floorplans = getById("floorplans");
	detail = getById("floorplaninfo");
	content = getById("content");
	loading = new Image();
	loading.src = "/images/loading.gif";
	renderHomes();	
}

function renderHomes(){
	var newfp = document.createElement("div");
	newfp.id = "floorplans";
	var showdiv= htmlTag("p","Show: ");
	var show= document.createElement("select");
	show.id="show_select";
	var count = 0;
	show.options[count] = new Option("All","",false,false);
	count++;
	for (var index in cmuList){
		show.options[count] = new Option(cmuList[index],index,false,false);
		if (index == filter){
			show.selectedIndex = count;
		}
		count++;
	}
	show.onchange = function(){
		filter = this.value;
		renderHomes();
		detail.style.display = "none";
	}
	showdiv.appendChild(show);
	newfp.appendChild(showdiv);
	
	var fp,cl;
	filteredIndex = new Array();
	for (var i=0; i<homesList.length; i++){
		if (filteredPlan(i)){
			filteredIndex.push(i);
			fp = htmlTag("div","<div class='title'>"+homesList[i][0]+"</div><img alt='"+homesList[i][0]+"' src='/jsp/cachedImageView.do?key="+homesList[i][2]+"' width='100' />");
			fp.className = "fp_holder";
			fp.index = filteredIndex.length -1;
			fp.onmousedown = function(){
				showPlan(this.index);
			}
			newfp.appendChild(fp);
			
			if (filteredIndex.length % 5 == 0){
				cl=document.createElement("div");
				cl.className="clearer";
				newfp.appendChild(cl);
			}
			
		}
	}
	content.replaceChild(newfp,floorplans);
	floorplans = getById("floorplans");
}

function filteredPlan(index){
	if (filter == ""){
		return true;
	}
	for (var k=0; k<homesList[index][3].length; k++){
		if (homesList[index][3][k] == filter)
			return true;
	}
	return false;
}

function showPlan(index){
	if (index >= filteredIndex.length)
		index = 0;
	else if (index < 0)
		index = filteredIndex.length - 1;
	
	index = filteredIndex[index];
	
	detail.style.display = "block";
	var new_l = document.createElement("div");
	new_l.id = "fpinfoleft";
	
	try{
		detail_l = getById("fpinfoleft");
	}
	catch(e){
		detail_l = document.createElement("div");
		detail.appendChild(detail_l);
	}
	//left content
	var nfp,pfp,fpt,fpb;
	nfp = htmlTag("a","");
	nfp.className = "right_arrow";
	nfp.href = "javascript:showPlan("+(index+1)+")";

	pfp = htmlTag("a","");
	pfp.className = "left_arrow";
	pfp.href = "javascript:showPlan("+(index-1)+")";

	fpt = htmlTag("h2", homesList[index][0]);
	fpt.className = "centered";
	if (filter == "")
	{
		fpb = htmlTag("div","<a class='fpbutton' href='/jsp/floorPlans.do?planCode="+homesList[index][1]+"&divId="+homesList[index][8]+"&nbCode=&nbName=&siteName=zaring'>Click Here For Interactive Floorplan or PDF</a>");
	}
	else
	{
		fpb = htmlTag("div","<a class='fpbutton' href='/jsp/floorPlans.do?planCode="+homesList[index][1]+"&divId="+homesList[index][8]+"&nbCode="+filter+"&nbName="+cmuList[filter]+"&siteName=zaring'>Click Here For Interactive Floorplan or PDF</a>");
	}
	fpb.className = "clearer";
	new_l.appendChild(nfp);
	new_l.appendChild(pfp);
	new_l.appendChild(fpt);
	new_l.appendChild(fpb);
	
	var fpcmubuiltin = document.createElement("div");
	var fpmoddelsin = document.createElement("div");
	fpcmubuiltin.id = "fpcmubuiltin";
	fpmoddelsin.id = "fpmoddelsin";
	
	var cmuinlbl = htmlTag("strong","Built In:");
	var mdinlbl = htmlTag("strong","Models: ");
	var cmulist = document.createElement("ul");
	var mdlist = document.createElement("ul");
	var listitem;
	
	for (var i=0; i<homesList[index][3].length; i++){
		listitem = htmlTag("li","<a href='neighborhood.do?nelNbCode="+homesList[index][3][i]+"'>"+cmuList[homesList[index][3][i]]+"</a>");
		cmulist.appendChild(listitem);
	}
	
	for (var i=0; i<homesList[index][4].length; i++){
		listitem = htmlTag("li","<a href='neighborhood.do?nelNbCode="+homesList[index][4][i]+"'>"+cmuList[homesList[index][4][i]]+"</a>");
		mdlist.appendChild(listitem);
	}
	
	//right content
	setFpImage(index,0);

	fpcmubuiltin.appendChild(cmuinlbl);
	fpcmubuiltin.appendChild(cmulist);
	fpmoddelsin.appendChild(mdinlbl);
	fpmoddelsin.appendChild(mdlist);
	
	new_l.appendChild(fpcmubuiltin);
	new_l.appendChild(fpmoddelsin);
	
	detail.replaceChild(new_l,detail_l);
}

function hideDetail(){
	detail.style.display = "none";
}


function setFpImage(index, num){
	if (num >= homesList[index][5].length)
		num = 0;
	else if (num < 0)
		num = homesList[index][5].length - 1;
	
	new_r = document.createElement("div");
	new_r.id = "fpinforight";
	
	try{
		detail_r = getById("fpinforight");
	}
	catch(e){
		detail_r = document.createElement("div");
		detail.appendChild(detail_r);
	}
	//right
	var nfpi,pfpi,fpc,fpi,fpt;
	nfpi = htmlTag("a","");
	nfpi.className = "right_arrow";
	nfpi.href = "javascript:setFpImage("+index+","+(num+1)+")";

	pfpi = htmlTag("a","");
	pfpi.className = "left_arrow";
	pfpi.href = "javascript:setFpImage("+index+","+(num-1)+")";


	fpc = htmlTag("div","");
	fpc.className="clearer";
	
	
	try
	{
		fpi = htmlTag("div","<img id='fp_image' width='425' src='/jsp/cachedImageView.do?key="+homesList[index][5][num][0]+"' />");
		fpi.id = "image_holder";
		fpt = htmlTag("h3",homesList[index][5][num][1]);
		fpt.className = "centered";
	}
	catch(e)
	{
		fpi = htmlTag("div","");
		fpt = htmlTag("h3","");
		fpt.className = "centered";
	}
	
	
	new_r.appendChild(fpc);
	new_r.appendChild(fpi);
	new_r.appendChild(nfpi);
	new_r.appendChild(pfpi);
	new_r.appendChild(fpt);
	
	//setTimeout("chkImageLoaded("+index+","+num+")",100);
	detail.replaceChild(new_r,detail_r);
}
