
/********************
   Searching for COMMERCES
/********************/

/* FUNCTION Search for Commerces
     Returns list of coincident commerces and show markers on the map
*/

Site.SearchCommerces = function(json,product,service,ul_id){

		// declaring variables
		Site.qC=json;var puntos=0;var ul=Site.get(ul_id);var html;var marker;var showpopup=false;
		var add_i_to_text='';var iconH;var iconW;var offX=0;var offY=0;
		
		
		Site.clearLayer('CommerceLayer');
		
		// Show ItemList div
		Site.ShowObject('CommerceList',true);
		
		// if no records returned exit
		if (Site.qC.totalCount>0){
			//limpiamos marcadores de comercios
			/*if(typeof Site.markers_global_comm != "undefined")
			{
				Site.markers_global_comm.destroy();
			}*/
			//limpiamos marcadores de direcciones
		
		/*	if(typeof Site.markers_global != "undefined")
			{
				Site.markers_global.destroy();
			}
			if(typeof Site.CommercesPopupMarkers != "undefined")
			{
			  for(cont_i=0;cont_i < Site.CommercesPopupMarkers.length;cont_i++)
			  {
			  	if(typeof Site.CommercesPopupMarkers[cont_i] != "undefined")
			  	{
			  		Site.CommercesPopupMarkers[cont_i].destroy();
			  	}
			  }
			}
			if(typeof Site.AddresPopupMarkers != "undefined")
			{
			  for(cont_i=0;cont_i < Site.AddresPopupMarkers.length;cont_i++)
			  {
			  	if(typeof Site.AddresPopupMarkers[cont_i] != "undefined")
			  	{
			  		Site.AddresPopupMarkers[cont_i].destroy();
			  	}
			  }
			}*/
			Site.limpiarMarcadores();
			Site.markers_global = new OpenLayers.Layer.Markers("points_comm");
			Site.Map.map.olMap.addLayer(Site.markers_global); 
      //Site.Map.map.removeAllFeatures();
			//Site.CommerceMarkers = Array(Site.qC.totalCount); //VM
			Site.CommercesPopupMarkers = Array(Site.qC.totalCount);
			Site.dir_sociales = new Array(Site.qC.totalCount);
			//join product and service in one variable
			var SearchType = product+'/'+service;
			

			switch(SearchType){
		
			case 'geobusiness/searchcommerce':                    // FIND COMMERCE
					// If the addresses are full
				// add head info to the list
								html=Site.Searcher.html_list_start;
								var nam=''; var tel='';
								Site.PutHashonSearchComm(document.getElementById('search_by_commerce_name').value,''); //VM ,document.getElementById('search_by_commerce_item').value
								for (var i=0;i<Site.qC.totalCount;i++){
									if (Site.qC.searchcommerce[i].lon && Site.qC.searchcommerce[i].lat){
				
											if (Site.qC.totalCount==1){
													//Only obtain one address 			
													// set marker style
													marker='/img/markers/commerce.png'; 
													showpopup=true; add_i_to_text='';
													iconH=40;iconW=33;
													offX=-12;offY=-30;
													
													Site.putHTML('CommerceListTitle', Site.Locale.get('tit_commercesatthisarea'));
					
											}else{
													//Get more than one related address							
													// set marker style
													//marker= '/img/estrellaD'+(i+1)+'.gif'; 
													marker='/img/markers/commerce.png'; 
													showpopup=false; add_i_to_text=(i+1)+') ';
													iconH=40;iconW=33;
													offX=-12;offY=-30;
													// Old system... Site.get_lbl ('tit_maybeyouwannasay','ItemListTitle',Site.CurrentLanguage);
													Site.putHTML('CommerceListTitle', Site.Locale.get('tit_commercesatthisarea')+ ' (' +Site.qC.totalCount +')');
					
											}
									
										
										if(Site.qC.searchcommerce[i].name!=null){nam=Site.qC.searchcommerce[i].name+' '}else{nam='';}
										
										
										var tel = trim(Site.qC.searchcommerce[i].originalPhone)
										
										//alert("["+te+"]");
										if(tel!='' && tel!=null){tel='<br>'+Site.Locale.get('tit_phoneabr')+
																												tel+''}else{tel='';}
                     																				
										// add ITEM information to the list
										html+=Site.Searcher.html_list_item(i,'comm'+i,
												''+Site.qC.searchcommerce[i].fantasyName+'<br>'+nam+'<br>'+Site.qC.searchcommerce[i].classStreet+' '+
																						Site.qC.searchcommerce[i].nameStreet+' '+
																						Site.qC.searchcommerce[i].number+', '+
																						Site.qC.searchcommerce[i].district+tel,
												 "Site.Map.CenterandZoomCommerce("+ Site.qC.searchcommerce[i].lon+","+ Site.qC.searchcommerce[i].lat+
																						",13,"+i+");Site.PutHashonSearchComm('"+Site.qC.searchcommerce[i].fantasyName+"','');"); //VM '"+nam+"'
						
									
												// Popup HTML text content
												
									var html_popup_content='<div class="popcommerce" onclick="Site.Map.CenterandZoomCommerce('+ 
																						Site.qC.searchcommerce[i].lon+','+ Site.qC.searchcommerce[i].lat+
																						',12,'+i+');"><div class="popupAddressTit"><b>'+
																						Site.qC.searchcommerce[i].fantasyName+'</div><div class="popupAddressText">';
										if(nam!='')
										{
											html_popup_content +='<br>'+nam+'';								
										}
										html_popup_content +='</b><br>'+ Site.qC.searchcommerce[i].classStreet+' '+
																		Site.qC.searchcommerce[i].nameStreet+' '+
																		Site.qC.searchcommerce[i].number+'<br>'+
																		Site.qC.searchcommerce[i].district+
																		tel+'</div></div>';	
									//AddThis Button BEGIN 
									/* VARIABLES PARA SOCIALES */
									//dominio_sociales=document.domain;	
									
				
									Site.dir_sociales[i]="http://"+Site.dominio_sociales+"/#t=3:c="+Site.qC.searchcommerce[i].fantasyName+"_"+nam+"";
									/**/
									/*
									html_popup_content +='<br><br><div align=left><a href="javascript:open_social(1,'+i+');" ><img src="http://cache.addthis.com/icons/v1/thumbs/facebook.gif" border="0" alt="Facebook" /></a>';
									html_popup_content +='<a href="javascript:open_social(2,'+i+');"><img src="http://cache.addthis.com/icons/v1/thumbs/twitter.gif" border="0" alt="Twitter" /></a>';
									html_popup_content +='<a href="javascript:open_social(3,'+i+');"><img src="http://cache.addthis.com/icons/v1/thumbs/email.gif" border="0" alt="email" /></a>';
									html_popup_content +='<a href="javascript:open_social(4,'+i+');"><img src="http://cache.addthis.com/icons/v1/thumbs/more.gif" border="0" alt="More..." /></a>';
									//html_popup_content +='<a class="addthis_button" href="http://www.addthis.com/bookmark.php?v=250&amp;pubid=ra-4e70f9562fc8b934"><img src="http://s7.addthis.com/static/btn/sm-bookmark-en.gif" width="83" height="16" alt="Bookmark and Share" style="border:0"/></a>';
									html_popup_content +='</div>';		
									*/							
									//AddThis Button END				
									
									//add end info to the list									
												// Add info related the marker on the map
												Site.Map.CreateCommerceMarker(i,
															  Site.qC.searchcommerce[i].lon,
															  Site.qC.searchcommerce[i].lat,
															  marker,
															  iconW,iconH,    // icon size
															  Site.CommerceLayer,
															 html_popup_content,
															  showpopup,
															  offX,offY // offset
															  );
												
												
										}				
									}
									
									html+=Site.Searcher.html_list_end;
								
									//Inject list to ul
									Site.get(ul).dom.innerHTML=html;
									
									
									//Set zoom to the result markers area
									Site.CommercesFitZoom(20);
				
				
				
			
						
			} //End Switch
		} // If there is more than one record...
					
}; // End search address function


