//data holders
var communityIndex = new Array(); //communityIndex[CODE] = index of communityList
var currentList = new Array(); //community codes in floorPlanlist
var communityList = new Array();
var neighborhoodList = new Array();
var cmuNeighList = new Array();
var floorplanList = new Array();
var sortedCommunityIndex = new Array();
var sortedNeighborhoodIndex = new Array();
var sortedFloorplanIndex = new Array();
var sortedCmuNeighIndex = new Array();

//status variables
var sortOrder = "ASC";
var sortOn = 0;
var lastSortOn = 0;

//list elements
var infoListHeader, infoListHolder, infoList;

function initList(){
	//set up indices
	for (var i=0; i < communityList.length; i++){
		sortedCommunityIndex[i] = i;
		communityIndex[communityList[i][1]] = i;
	}
	
	for (var i=0; i < cmuNeighList.length; i++){
		sortedCmuNeighIndex[i] = i;
	}

	for (var i=0; i < cmuNeighList.length; i++){
		sortedCmuNeighIndex[i] = i;
	}
	reIndexFloorplanList();

	//find elements
	infoListHeader = getById("infolistheader");
	infoListHolder = getById("infolistcontainer");	
	infoList = getById("infolist");	
}

function reIndexFloorplanList(){
	currentList = new Array();
	sortedFloorplanIndex = new Array();
	for (var i=0; i < floorplanList.length; i++){
		sortedFloorplanIndex[i] = i;
		currentList = currentList.concat(floorplanList[i][27]);
	}
}

//communityList structure ===============
// communityList[i][0] = community name
// communityList[i][1] = community code
// communityList[i][2] = city
// communityList[i][3] = state
// communityList[i][4] = product type
// communityList[i][5] = pricehigh
// communityList[i][6] = pricelow
// communityList[i][7] = neighborhoods Array()
// communityList[i][7][j][0] = neighborhood name
// communityList[i][7][j][1] = neighborhood code
// communityList[i][7][j][2] = city
// communityList[i][7][j][3] = state
// communityList[i][7][j][4] = product type
// communityList[i][7][j][5] = pricehigh
// communityList[i][7][j][6] = pricelow
// communityList[i][8] = x coordinate
// communityList[i][9] = y coordinate
// communityList[i][10] = township
// communityList[i][11] = amenities
// communityList[i][12] = schools
// communityList[i][13] = description
// communityList[i][14] = mddid

//cmuNeighList structure ===============
// cmuNeighList[i][0] = community name
// cmuNeighList[i][1] = community code
// cmuNeighList[i][2] = homes available
// cmuNeighList[i][3] = plans available
// cmuNeighList[i][4] = product type
// cmuNeighList[i][5] = pricehigh
// cmuNeighList[i][6] = pricelow

//floorplanList structure ===============
// floorplanList[i][0] = Product Type 0 = Dirt Deal, 1 = Inventory Home
// floorplanList[i][1] = namelink to markethome or dirtdeal links
// floorplanList[i][2] = Plan Market Name
// floorplanList[i][3] = Address
// floorplanList[i][4] = Price Low
// floorplanList[i][5] = Price High
// floorplanList[i][6] = Sqr Feet Low
// floorplanList[i][7] = Sqr Feet High
// floorplanList[i][8] = Bedrooms Std
// floorplanList[i][9] = Bedrooms Opt
// floorplanList[i][10] = Full Baths Std
// floorplanList[i][11] = Full Baths Opt
// floorplanList[i][12] = Half Baths Std
// floorplanList[i][13] = Half Baths Opt
// floorplanList[i][14] = Garage Std
// floorplanList[i][15] = Garage Opt
// floorplanList[i][16] = FFOwners Suite (Y/N)
// floorplanList[i][17] = FFGuest Room (Y/N)
// floorplanList[i][18] = Living Areas
// floorplanList[i][19] = Dining Areas
// floorplanList[i][20] = Stories
// floorplanList[i][21] = Includes
// floorplanList[i][22] = Optional
// floorplanList[i][23] = Avail Date
// floorplanList[i][24] = community Name
// floorplanList[i][25] = neighborhood Name
// floorplanList[i][26] = type (SF,...)
// floorplanList[i][27] = Community Codes Array



//neighborhoodList structure ============

function createCommunityList(){
	//define list
	
	var cmucolumns = new Array();
	cmucolumns[0] = new Array();
	cmucolumns[0][0] = new Array();
	// cmucolumns[0] = product type
	// fields in cmucolumns[0][0] are used for headers
	// fields in cmucolumns[i][>0] are used for rows with multiple lines
	cmucolumns[0][0][0] = new Object({label:"", 				field:-1, 	sortable:false,	prefix:"type_",		span: 1});
	cmucolumns[0][0][1] = new Object({label:"State", 			field:3, 	sortable:true,	prefix:"state_",	span: 1});
	cmucolumns[0][0][2] = new Object({label:"City", 			field:2, 	sortable:true,	prefix:"city_",		span: 1});
	cmucolumns[0][0][3] = new Object({label:"Community Name",	field:0, 	sortable:true,	prefix:"community_",span: 1});
	cmucolumns[0][0][4] = new Object({label:"", 				field:-1, 	sortable:false,	prefix:"product_",	span: 1});
	cmucolumns[0][0][5] = new Object({label:"Price", 			field:15, 	sortable:true,	prefix:"price_",	span: 1});
	

	currentList = new Array();
	//format fields
        if(communityList.length>0){
	for (var i=0; i < communityList.length; i++){
		communityList[i][15] = (communityList[i][6] == communityList[i][5])?(communityList[i][6]):(communityList[i][6]+"-"+communityList[i][5]);
		for (var j=0; j < communityList[i][7].length; j++){
			communityList[i][7][j][3] = "";
			communityList[i][7][j][15] = (communityList[i][7][j][6] == communityList[i][7][j][5])?(communityList[i][7][j][6]):(communityList[i][7][j][6]+"-"+communityList[i][7][j][5]);
		}
	}
        }
        
	//alert ("createCommunityList: list reformated");

	//create header
	createListHeader(cmucolumns[0][0]);
	//alert ("createCommunityList: header created");

	//remove old list contents
	try{
		infoListHolder.removeChild(getById("infolist"));
	}catch(e){
		
	}
	infoList = document.createElement("div");
	infoList.id = "infolist";
	infoListHolder.appendChild(infoList);
	//alert ("createCommunityList: removed old list");
	//re-populate list
	if (communityList.length > 1){
		for (var k=0; k < communityList.length; k++){
			//create rows in order of the sort
			var index = sortedCommunityIndex[k];
			var type = (communityList[index][7].length > 0)?("cmu"):("cmu"); //let server determine type
			createListRow(index, communityList[index], cmucolumns, 0, "dark", type);
			//do sub rows here
			for (var j=0; j < communityList[index][7].length; j++){
				createListRow(index, communityList[index][7][j], cmucolumns, 0, "light", "neigh");
			}
		}
	}
	else{
		try{
			for (var j=0; j < communityList[0][7].length; j++){
				createListRow(index, communityList[0][7][j], cmucolumns, 0, "dark", "neigh");
			}
		}
		catch(e){}
	}
	
//Fix for the bug Id 23- Community Summary Missing Info. 
     // Defining the schooldistrict var to list all the schoolDistricts parsing the communityList
        var schooldistrict = "";
        for(var i=0; i<communityList.length; i++){
          for(var j=0; j<communityList.length; j++){
            if(j==12 ){
              schooldistrict += communityList[i][12]+",";
           } 
          }
        } 
/* Fix for the bug Id 23 - Community Summary missing Info */
     // Defining the cityTownships var to list all the Cities and Townships parsing the communityList
        var cityTownships = "";
        for(var i=0; i<communityList.length; i++){
           cityTownships += communityList[i][2]+",";
        } 
/* Fix for the bug Id 23 - Community Summary missing Info */

}

function createCmuNeighList(){
	//define list
	//alert ("createCmuNeighList:");
	var cmucolumns = new Array();
	cmucolumns[0] = new Array();
	cmucolumns[0][0] = new Array();
	// cmucolumns[0] = product type
	// fields in cmucolumns[0][0] are used for headers
	// fields in cmucolumns[i][>0] are used for rows with multiple lines
	cmucolumns[0][0][0] = new Object({label:"", 				field:-1, 	sortable:false,	prefix:"type_",		span: 1});
	cmucolumns[0][0][1] = new Object({label:"Community Name", 	field:0, 	sortable:true,	prefix:"community_",span: 1});
	cmucolumns[0][0][2] = new Object({label:"", 				field:-1, 	sortable:false,	prefix:"product_",	span: 1});
	cmucolumns[0][0][3] = new Object({label:"Price",			field:7, 	sortable:true,	prefix:"price_",	span: 1});
	cmucolumns[0][0][4] = new Object({label:"Homes Available", 	field:2, 	sortable:true,	prefix:"homes_",	span: 1});
	cmucolumns[0][0][5] = new Object({label:"Plans Available", 	field:3, 	sortable:true,	prefix:"plans_",	span: 1});
	//alert ("createCommunityList: list defined");

	currentList = new Array();
	//format fields
	for (var i=0; i < cmuNeighList.length; i++){
		cmuNeighList[i][7] = (cmuNeighList[i][6] == cmuNeighList[i][5])?(cmuNeighList[i][6]):(cmuNeighList[i][6]+"-"+cmuNeighList[i][5]);
	}
	//alert ("createCommunityList: list reformated");

	//create header
	createListHeader(cmucolumns[0][0]);

	//remove old list contents
	try{
		infoListHolder.removeChild(getById("infolist"));
	}catch(e){
		
	}
	infoList = document.createElement("div");
	infoList.id = "infolist";
	infoListHolder.appendChild(infoList);
	//alert ("createCommunityList: removed old list");
	//re-populate list
	for (var k=0; k < cmuNeighList.length; k++){
		//create rows in order of the sort
		var index = sortedCmuNeighIndex[k];
		var type = "neigh";
		createListRow(index, cmuNeighList[index], cmucolumns, 0, "light", type);
	}
	//alert ("createCommunityList: created new list");
	
}

function createFloorplanList(){
	//define list
	var fpcolumns = new Array();
	fpcolumns[0] = new Array();
	fpcolumns[0][0] = new Array();
	fpcolumns[0][1] = new Array();
	fpcolumns[1] = new Array();
	fpcolumns[1][0] = new Array();
	fpcolumns[1][1] = new Array();
	fpcolumns[1][2] = new Array();
	fpcolumns[1][3] = new Array();
	
	//type 0 line 1
	fpcolumns[0][0][0] = new Object({label:"", 					field:-1, 	sortable:false,	prefix:"qmi_",		span: 1});
	fpcolumns[0][0][1] = new Object({label:"Floorplan", 			field:2, 	sortable:true,	prefix:"floor_",	span: 1});
	fpcolumns[0][0][2] = new Object({label:"", 					field:-1, 	sortable:false,	prefix:"type2_",	span: 1});
	fpcolumns[0][0][3] = new Object({label:"Base Price", 			field:28, 	sortable:true,	prefix:"fprice_",	span: 1});
	fpcolumns[0][0][4] = new Object({label:"Square Feet",			field:29, 	sortable:true,	prefix:"sqft_",		span: 1});
	fpcolumns[0][0][5] = new Object({label:"Bdrm (Opt)", 				field:30, 	sortable:true,	prefix:"bdrm_",		span: 1});
	fpcolumns[0][0][6] = new Object({label:"Bath Full/Half (Opt)",		field:33, 	sortable:true,	prefix:"bath_",		span: 1});
	fpcolumns[0][0][7] = new Object({label:"Garage (Opt)",				field:34, 	sortable:true,	prefix:"garage_",	span: 1});
	fpcolumns[0][0][8] = new Object({label:"1st Floor Master",	field:16, 	sortable:true,	prefix:"ffoste_",	span: 1});
	fpcolumns[0][0][9] = new Object({label:"1st Floor Guest Rm (Opt)",	field:17, 	sortable:true,	prefix:"ffgrm_",	span: 1});
	fpcolumns[0][0][10] = new Object({label:"Living Areas",		field:18, 	sortable:true,	prefix:"living_",	span: 1});
	fpcolumns[0][0][11] = new Object({label:"Dining Areas",		field:19, 	sortable:true,	prefix:"dining_",	span: 1});
	//type 0 line 2
	fpcolumns[0][1][0] = new Object({label:"", 					field:-1, 	sortable:false,	prefix:"qmib_",		span: 1});
	fpcolumns[0][1][1] = new Object({label:"Stories", 			field:20, 	sortable:false,	prefix:"storiesb_",	span: 2});
	fpcolumns[0][1][2] = new Object({label:"Standard", 			field:21, 	sortable:false,	prefix:"standard_",	span: 4});
	fpcolumns[0][1][3] = new Object({label:"Optional",			field:22, 	sortable:false,	prefix:"optional_",	span: 5});
	
	//type 1 line 1
	fpcolumns[1][0] = fpcolumns[0][0];
	fpcolumns[1][0][0] = new Object({label:"", 					field:37, 	sortable:false,	prefix:"qmi_",		span: 1});
	//type 1 line 2
	fpcolumns[1][1][0] = new Object({label:"", 					field:-1, 	sortable:false,	prefix:"qmi_",		span: 1});
	fpcolumns[1][1][1] = new Object({label:"Stories", 			field:20, 	sortable:false,	prefix:"stories_",	span: 2});
	fpcolumns[1][1][2] = new Object({label:""/*Community*/, 		field:35, 	sortable:false,	prefix:"cmu_",		span: 4});
	fpcolumns[1][1][3] = new Object({label:""/*Neighborhood*/,	field:36, 	sortable:false,	prefix:"neigh_",	span: 5});
	//type 1 line 3
	fpcolumns[1][2][0] = new Object({label:"", 					field:-1, 	sortable:false,	prefix:"qmi_",		span: 1});
	fpcolumns[1][2][1] = new Object({label:"Includes",			field:21, 	sortable:false,	prefix:"includes",	span: 11});
	//type 1 line 4
	fpcolumns[1][3][0] = new Object({label:"", 					field:-1, 	sortable:false,	prefix:"qmib_",		span: 1});
	fpcolumns[1][3][1] = new Object({label:"Address",				field:3, 	sortable:false,	prefix:"address_",	span: 6});
	fpcolumns[1][3][2] = new Object({label:"Est. Ready By",		field:23, 	sortable:false,	prefix:"ready_",	span: 5});

	//format fields
	for (var i=0; i < floorplanList.length; i++){
                // Removing high price from floor plan lists.
		//floorplanList[i][28] = (floorplanList[i][4] == floorplanList[i][5] || floorplanList[i][5] == "")?(floorplanList[i][4]):(floorplanList[i][4]+"-"+floorplanList[i][5]);
		floorplanList[i][28] = (floorplanList[i][4]);
		floorplanList[i][29] = (floorplanList[i][6] == floorplanList[i][7] || floorplanList[i][7] == "")?(floorplanList[i][6]):(floorplanList[i][6]+"-"+floorplanList[i][7]);
		floorplanList[i][30] = (floorplanList[i][9] == "0" || floorplanList[i][9] == "" || floorplanList[i][9] == floorplanList[i][8] )?(floorplanList[i][8]):(floorplanList[i][8]+"("+floorplanList[i][9]+")");
		floorplanList[i][31] = (floorplanList[i][11] == "0" || floorplanList[i][11] == "" || floorplanList[i][11] == floorplanList[i][10]) ?(floorplanList[i][10]):(floorplanList[i][10]+"("+floorplanList[i][11]+")");
		floorplanList[i][32] = (floorplanList[i][13] == "0" || floorplanList[i][13] == "" || floorplanList[i][12] == floorplanList[i][13])?(floorplanList[i][12]):(floorplanList[i][12]+"("+floorplanList[i][13]+")");
		floorplanList[i][33] = floorplanList[i][31] + "/" + floorplanList[i][32];
		floorplanList[i][34] = (floorplanList[i][15] == "0" || floorplanList[i][15] == "" || floorplanList[i][15] == floorplanList[i][14])?(floorplanList[i][14]):(floorplanList[i][14]+"("+floorplanList[i][15]+")");
		if (floorplanList[i][0] == 1){
			floorplanList[i][35] = (floorplanList[i][24] == "")?(""):("Community: "+floorplanList[i][24]);
			floorplanList[i][36] = (floorplanList[i][25] == "" || floorplanList[i][24] == floorplanList[i][25])?(""):("Neighborhood: "+floorplanList[i][25]);
			floorplanList[i][37] = "<img class='qmiicon' alt='Quick Move-In' src='/images/qmiicon.jpg' />";
 		}
		else{
			floorplanList[i][37] = "";
			
		}
	}

	//create header
	createListHeader(fpcolumns[0][0]);
	
	//remove old list contents
	try{
		infoListHolder.removeChild(getById("infolist"));
	}catch(e){
		
	}
	infoList = document.createElement("div");
	infoList.id = "infolist";
	infoListHolder.appendChild(infoList);
	//re-populate list
	for (var i=0; i < floorplanList.length; i++){
		//create rows in order of the sort
		var index = sortedFloorplanIndex[i];
		var cn = (floorplanList[index][0] == 0)?("light"):("dark");
		createListRow(index, floorplanList[index], fpcolumns, floorplanList[index][0], cn, "fpl");
	}
}

function createListHeader(h){
	//remove previous header row
	try{
		infoListHeader.removeChild(getById("list_header_table_body"));
	}catch(e){
		
	}
	//create new header row
	var bdy = document.createElement("tbody");
	bdy.id = "list_header_table_body";
	var row = document.createElement("tr");
	row.id = "header_row";
	row.vAlign = "top";
	
	//create header columns
	var header;
	
	for (var i=0; i < h.length; i++){
		header = document.createElement("td");
		header.id = h[i].prefix+"header";
		var tn = document.createTextNode(h[i].label);
		var img;
		if (h[i].sortable){
			var sl = document.createElement("a");
			sl.href = (sortOn != h[i].field)?("javascript:doSortOn("+h[i].field+");"):("javascript:switchSort();");
			sl.appendChild(tn);
			if (sortOn == h[i].field){
				//add arrow
				img = document.createElement("img");
				img.src = (sortOrder == "ASC")?('/images/arrow_up.gif'):('/images/arrow_down.gif');
				img.alt = "";
				img.className = 'sort_arrow';
				sl.appendChild(img)
			}
			header.appendChild(sl);
		}
		else{
			header.appendChild(tn);
		}
		//add column to row
		row.appendChild(header);
	}
	//add row to header table
	if (infoListHolder.className == "scroll"){
		header = document.createElement("td");
		header.id = "scroll_spacer";
		header.width = 4;
		row.appendChild(header);
	}
	bdy.appendChild(row);
	infoListHeader.appendChild(bdy);
}	
	
function createListRow(_i, _d, _c, _t, _class, _type){
	// i = item index
	// d = data
	// c = columns array
	// t = product type display column / 0 means not display typed
	// class = table class
	// type = row type for mouse down events
	
	//create row / table
	var table = document.createElement("table");
	var tbody = document.createElement("tbody");
	var tn;
	table.className = _class;
	table.cellPadding = 0;
	table.cellSpacing = 0;
	table.border = 0;
	table.hitType = _type;
	table.index = _i;
	table.code = _d[1];
	//alert("0: "+_d[0]+"\n1:"+_d[1]+"\n2:"+_d[2]+"\n3:"+_d[3]+"\n4:"+_d[4]+"\n5:"+_d[5])
	switch (_type){
		case "fpl":
			if (_t == 0)
				table.title = "click for interactive floorplan or floorplan brochure";
			else
				table.title = "click for detailed market home information";
			break;
		case "cmu":
				table.title = "click for community page";
			break;
		case "neigh":
				table.title = "click for neighborhood page";
			break;
	}
	 // add event handlers here
	table.onmouseover = function(){
		handleListMouseOver(this, this.index);
        }
	table.onmouseout = function(){
		handleListMouseOut(this, this.index);
	}
	table.onmousedown = function(){
		handleListMouseDown(this, this.index);
	}
	//check for row type index
	for (var r=0; r < _c[_t].length; r++){
		//for each line
		var row = document.createElement("tr");
	
		for (var l=0; l < _c[_t][r].length; l++){
			//for each column
			var TEXT = "";
			if (_c[_t][r][l].field >= 0){
				TEXT +=  (r>0 && _c[_t][r][l].label != "")?(_c[_t][r][l].label + ": " +_d[_c[_t][r][l].field]):(_d[_c[_t][r][l].field]);
			}
			var col = htmlTag("td",TEXT);
			col.className = _c[_t][r][l].prefix+"column";
			col.colSpan = _c[_t][r][l].span;
			row.appendChild(col);
		}
		tbody.appendChild(row);
	}
	table.appendChild(tbody);
	infoList.appendChild(table);
	var spc = document.createElement("div");
	spc.title = "";
	spc.className = "list_row_spacer";
	infoList.appendChild(spc);

}

function doSortOn(f){
	lastSortOn = sortOn;
	sortOn = f;
	if (floorplanList.length > 0){
		sortedFloorplanIndex.sort(doCompare);
		if (sortOrder != "ASC")
			sortedFloorplanIndex.reverse();
		createFloorplanList();
	}
	else if (neighborhoodList.length > 0){
		sortedNeighborhoodIndex.sort(doCompare);
		if (sortOrder != "ASC")
			sortedNeighborhoodIndex.reverse();
			
		createNeighborhoodList();
	}
	else if (cmuNeighList.length > 0){
		sortedCmuNeighIndex.sort(doCompare);
		if (sortOrder != "ASC")
			sortedCmuNeighIndex.reverse();
			
		createCmuNeighList();
	}
	else{
		sortedCommunityIndex.sort(doCompare);
		if (sortOrder != "ASC")
			sortedCommunityIndex.reverse();
			
		createCommunityList();
	}
}

function switchSort(){
	sortOrder = (sortOrder == "ASC")?"DESC":"ASC";
	doSortOn(sortOn);
}

function doCompare(a,b){
	var arr;
	if (floorplanList.length > 0){
		arr = floorplanList;
	}
	else if (neighborhoodList.length > 0){
		arr = neighborhoodList;
	}
	else if (cmuNeighList.length > 0){
		arr = cmuNeighList;
	}
	else{
		arr = communityList;
	}

	if (arr[a][sortOn] > arr[b][sortOn])
		return 1;
	if (arr[a][sortOn] < arr[b][sortOn])
		return -1;
	if (arr[a][sortOn] == arr[b][sortOn]){
		var x = (sortOrder == "ASC")?(1):(-1);
		if (arr[a][lastSortOn] > arr[b][lastSortOn])
			return x;
		if (arr[a][lastSortOn] < arr[b][lastSortOn])
			return -x;
	}
	return 0;
}
