
/* tools/paint_recycler.js */

 var MMH;
if (typeof MMH === 'undefined') {
	MMH = {};
}
(function () {
	
	MMH.PaintRecycler = {
		init: function(pc_json,name) { 
			
			/* Load the params, if any */
			var params=location.href.toQueryParams();
			if(params.locations) {
				MMH.PaintRecycler.drawRecycleCenters(pc_json,params.locations);
				$("center_list").value = params.locations;
			}else{MMH.PaintRecycler.drawRecycleCenters(pc_json);}
			
		},
		drawSelectBox: function(pc_json,selectBox,addEventListener) {
			var stateList = [];
			var optionList = [];
			var jsonObject = eval(pc_json);
			
			jsonObject.Centers.each(function(item,index){
				stateList.push(item.Area);
			});
			
			stateList=stateList.uniq();
			stateList.each(function(item,index){
				
				newOption = document.createElement('option');
				newOption.setAttribute('value',item);
				newOption.setAttribute('id',"state_"+item)
				newOption.appendChild(document.createTextNode(item));
				$(selectBox).appendChild(newOption);
				
			});
			
			if(addEventListener){this.watchSelect(selectBox,pc_json)};
		},
		watchSelect: function(selectBox,pc_json) {

			$(selectBox).observe('change',function(){
					
					MMH.PaintRecycler.drawRecycleCenters(pc_json,$("center_list").value);
					
				}
			);
			
		},
		drawRecycleCenters: function(pc_json, centerState) {
			MMH.PaintRecycler.lastState ="";
			if(centerState=="null"){
				centerState=null;
			};
			
			var jsonObject = eval(pc_json);
			var cityCol,metroCol,nameCol,addressCol,stateCol,zipCol;
			$("recycle_list").update("");
			
			jsonObject.Centers.each(function(item,index){
				newRow = document.createElement('tr');
				if(centerState==null || centerState == item.Area){
					if(item.Area!=MMH.PaintRecycler.lastState){
						stateRow = document.createElement('tr');
						stateCol = document.createElement('td');
						stateCol.setAttribute('colSpan','6')
						stateHeader = document.createElement('h3');
						stateHeader.appendChild(document.createTextNode(item.Area));
						$(stateHeader).addClassName('flash')
						stateCol.appendChild(stateHeader);
						stateRow.appendChild(stateCol);
						$("recycle_list").appendChild(stateRow);
						$(stateRow).addClassName('state_header');
						$(newRow).addClassName('first');
						
						 
					}
					
					cityCol = document.createElement('td');
					cityCol.setAttribute('class','name');
					cityCol.appendChild(document.createTextNode(item.City));
					$(cityCol).addClassName('city');
					newRow.appendChild(cityCol);
					
					nameCol = document.createElement('td');
					nameCol.appendChild(document.createTextNode(item.Name));
					$(nameCol).addClassName('name');
					newRow.appendChild(nameCol);
					
					addressCol = document.createElement('td');
					addressCol.appendChild(document.createTextNode(item.Address));
					$(addressCol).addClassName('address');
					newRow.appendChild(addressCol);
					
					stateCol = document.createElement('td');
					stateCol.appendChild(document.createTextNode(item.State));
					$(stateCol).addClassName('state');
					newRow.appendChild(stateCol);
					
					zipCol = document.createElement('td');
					zipCol.appendChild(document.createTextNode(item.Zip));
					$(zipCol).addClassName('zip');
					newRow.appendChild(zipCol);
					
					phoneCol = document.createElement('td');
					phoneCol.appendChild(document.createTextNode(item.Phone));
					$(phoneCol).addClassName('phone');
					newRow.appendChild(phoneCol);

					$('recycle_list').appendChild(newRow);
					MMH.PaintRecycler.lastState = item.Area;
					
				}
			});
			sIFR_render_tools();
		},
		lastState: ""
		
	}
	})();
