var toll=toll || {};

//toll.MapInfoWindowFactory
(function(){
	// This class is all about displaying community info on the map (Google Map 'infoWindow' talk bubble thing).
	
	// Mapping of server json params (right) to internal variable names (left).
	// Change values on right to match server output. 
	
	// Append community number to description page ('comm_num') to get community link:
	var communityDescriptionPageUrl="/homesearch/servlet/HomeSearch?app=community_description&comm_num=";
	
	// Community property names (internal names mapped to jsonData feed names)
	var toMap=toll.Model.getCommunityTOMap();
	
	var imgBaseURL="http://www.tollbrothers.com";
	
	// Method to draw single listings on info map window
	//
	//@info Commented and edited by AM on October 21, 2009
	//@param
	//@prarm
	function _drawRecord(community, domEl){
		var subdiv=document.createElement('div');
		subdiv.id="div-left";
		var img=document.createElement('img');
		$(img).css("width","70px");
		$(img).css("height","40px");
		img.src=imgBaseURL+community[toMap.image];
		
		
//		addDebugReportClick(img,community,subdiv,true);

		subdiv.appendChild(img);
		domEl.appendChild(subdiv);
		
		subdiv=document.createElement('div');
		subdiv.id='div-right';
		var a=document.createElement('a');
		//Data
		a.href=communityDescriptionPageUrl+community[toMap.communityNumber];
		a.innerHTML=community[toMap.communityName];
		subdiv.appendChild(a);
		
		var ul=document.createElement('ul');
		var li=document.createElement('li');
		var strong=document.createElement('strong');
		li.appendChild(strong);
		//Data
		strong.innerHTML=community[toMap.city]+', '+community[toMap.state];
		ul.appendChild(li);
		
		for(var i=0; i<community[toMap.home_type].length; i++){
			li=document.createElement('li');
			li.innerHTML=community[toMap.home_type][i];
			ul.appendChild(li);
		}
		
		li=document.createElement('li');
		//Data
		li.innerHTML='From the '+community[toMap.priceRange];

		ul.appendChild(li);
		
		subdiv.appendChild(ul);
		domEl.appendChild(subdiv);
	}
	
	
	function addDebugReportClick(clickDomEl,community,reportDomEl,clickIsImg){
		var toggle=true;
		var dataDumpUL;
		if(clickIsImg){
			var origImgSize={
				width:$(clickDomEl).css("width"),
				height:$(clickDomEl).css("height")
			}
		}
		$(clickDomEl).click(function(){
			if(toggle){
				toggle=false;
				//Set photo to orginal size
				if(clickIsImg){
					$(this).css("width","inherit");
					$(this).css("height","inherit");
				}
				dataDumpUL=document.createElement('ul');
				$(dataDumpUL).css('background-color','white');
				$(dataDumpUL).css('padding','12px');
				$(dataDumpUL).css('width','300px');
				var li;
				for(var p in community){
					li=document.createElement('li');
					li.innerHTML=p+": "+community[p];
					dataDumpUL.appendChild(li);
				}
				reportDomEl.appendChild(dataDumpUL);
				//window.location=this.src;  // 90X53
			}else{
				toggle=true;
				$(dataDumpUL).css("display","none");
				if(clickIsImg){
					$(clickDomEl).css("width",origImgSize.width);
					$(clickDomEl).css("height",origImgSize.height);
				}
			}
		})
	}
	
	
	// Method to draw multi listings on info map window
	//
	//@info Commented and edited by AM on October 21, 2009
	//@param
	//@prarm
	function _drawRecordSmall(community,domEl){
		var subdiv=document.createElement('div');
	
//		addDebugReportClick(subdiv,community,subdiv,false);
		
		
		var a=document.createElement('a');
		//Data
		a.href=communityDescriptionPageUrl+community[toMap.communityNumber]; //  community[toMap.link];
		a.innerHTML=community[toMap.communityName];
		subdiv.appendChild(a);
		
		var ul=document.createElement('ul');
		var li=document.createElement('li');
		var strong=document.createElement('strong');
		li.appendChild(strong);
		//Data
		strong.innerHTML=community[toMap.city]+', '+community[toMap.state];
		ul.appendChild(li);
		
		for(var i=0; i<community[toMap.home_type].length; i++){
			li=document.createElement('li');
			li.innerHTML=community[toMap.home_type][i];
			ul.appendChild(li);
		}
		
		li=document.createElement('li');
		//Data
		if(community[toMap.priceRange] == "N/A")
		{
			li.innerHTML='Price not currently available <br><br>';
		}
		else
		{
			li.innerHTML='From the '+community[toMap.priceRange] + '<br><br>';
		}
		
		ul.appendChild(li);
		
		subdiv.appendChild(ul);
		domEl.appendChild(subdiv);
	};
	
	
	toll.MapInfoWindowFactory={
		getContent:function(communities/*array [{  name:"Norris Canyon Estates",
													id:"113",
													city:"San Ramon",
													state:"CA",
													county:"Contra Costa",
													avgSqFt:"4821",
													priceRange:"Upper $1,000,000s",
													type:"",
													latitude:"37.757222222222225",
													longitude:"-122.00694444444444",
													link:"http://www.tollbrothers.com/homesearch/servlet/HomeSearch?app=community_description&comm_num=113" 
												}]*/
										)/*DOM Element*/{
			
			var div=document.createElement('div');
			div.id="mapInfoWindow";
			if(communities.length==1){
				//No clustering
				_drawRecord(communities[0],div);
			}else{
				//Clustered communities
				for(var i=0; i<communities.length; i++) {
					var com=communities[i];
					_drawRecordSmall(com,div);
				}
			}
			return div;
		}
	}

})();

