
function nearBySearch() {
	
	this.latitud = null;
	this.longitud = null;
	this.node = null;

	this.ultimate_zoom = null;
	this.ultimate_center = null;

	this.radio= null;
	this.zoom = 11;
	
	this.chk_restaurants = false;
	this.chk_cinemas = false;
	this.chk_bars = false;
	this.chk_shopping = false;
	this.chk_attactions = false;
	this.chk_travel = false;	

	this.mycoupon = null;

	this.mycoordenadas = new Array();
	this.coordenadas = new Array();

	this.enabledRestaurants = false;
	this.enabledBars = false;
	this.enabledCinemas = false;
	this.enabledShopping = false;
	this.enabledHotels = false;
	this.enabledTransports = false;
	this.enabledAttractions = false;
	
	this.map = function (radio,zoom) {
		this.radio = radio;
		this.zoom = zoom;
		return this;
	};


	this.mycoord = function (mycoord) {
		this.mycoordenadas = eval( mycoord );
		return this;
	}

	this.coupon = function (coupon) {
		this.mycoupon = coupon;
		return this;
	}
	
	this.coord = function (coord) {
		this.coordenadas = eval( ( coord ));
			
		return this;
	}

	this.nid = function (node) {
		this.node = node;
		
		return this;
	}
	
}

function presionSubmit()
{
	var valueRadio = 5;   
	var categoriesEnabled = getNearByEnabledCategories(); 

	if (categoriesEnabled!=""){

		//elemFormRadio = $("input[@name^=radioMilles]:checked")
		//elemFormRadio.each(function () {  
		//     nameRadio = this.name;  
		//     valueRadio = this.value;  
		// });

		elemFormRadio = $("select[@name^=radioMilles] option:selected"); 
		elemFormRadio.each(function () {  
		     nameRadio = this.name;   
		     valueRadio = this.value;  
		});		

		//alert("/marketplace_nearbysearch/"+nbs.node+"?category=" +categoriesEnabled+"&radio="+valueRadio);
	  	$.ajax({
		   async:true,
		   type: "GET",
		   dataType: "JSON",
		   contentType: "application/x-www-form-urlencoded",
		   url:"/marketplace_nearbysearch/"+nbs.node+"?category=" +categoriesEnabled+"&radio="+valueRadio,
		   beforeSend:inicioEnvio,
		   success:llegadaDatos,
		   timeout:300000,
		   error:problemas
		 }); 
	}else{
		gmap();
	}  
	

  return false;
}


function getNearByEnabledCategories() {

	var categories = "";
	
	if (isNearByCategoryEnabled("Restaurants")) {
		if(categories != "") categories = categories + "|" + "Restaurants" ;		
		else categories = categories + "Restaurants" ;
	}
	
	if (isNearByCategoryEnabled("Bars_Pubs_and_Taverns")) {
		if(categories != "") categories = categories + "|" + "Bars_Pubs_and_Taverns" ;
		else categories = categories + "Bars_Pubs_and_Taverns"
			
	}
	
	if (isNearByCategoryEnabled("Cinemas")) {
		
		if(categories != "") categories = categories +"|" + "Cinemas" ;
		else categories = categories  + "Cinemas" ;
	}
	
	if (isNearByCategoryEnabled("Shopping")) {
		
		if(categories != "") categories = categories + "|" +"Shopping" ;
		else	categories = categories +"Shopping" ;
	}
	
	if (isNearByCategoryEnabled("Attactions")) {
		
		if(categories != "") categories = categories + "|" + "Attactions" ;
		else categories = categories  + "Attactions" ;	
	}
	
	if (isNearByCategoryEnabled("Hotels_and_Motels")) {
		
		if(categories != "") categories = categories + "|" + "Hotels_and_Motels" ;
		else categories = categories + "Hotels_and_Motels" ;
	}
	
	if (isNearByCategoryEnabled("Travel_and_Transportation")) {
		
		if(categories != "") categories = categories + "|" + "Travel_and_Transportation";
		else categories = categories + "Travel_and_Transportation";
	}
		
	
	return categories;
}

function inicioEnvio()
{
  var x=$("#resultados");
  x.html('<b style="color:red;">Loading...</B>');
}

function llegadaDatos(datos)
{
   var x=$("#resultados");
   if($("#inputRadio").value==""){		
    	$("[@name^=radioMilles]").removeAttr('disabled');
   }	
   nbs.coord(datos);	

   x.html('');
   gmap();
	
}


function problemas()
{
  $("#resultados").text('Server Error.');
}



function showRadio(){

//	nbs.radio = $("[@name^=radioMilles]:checked").val();	

	elemFormRadio = $("select[@name^=radioMilles] option:selected"); 
	elemFormRadio.each(function () {  
	     nameRadio = this.name;   
	     nbs.radio = this.value;  
	});	

        presionSubmit();

	return;
}
		
		

function enableDisable(type) {
	
	$("#hid-"+type).val(isNearByCategoryEnabled(type) ? 0 : 1);
	
	if ($("#button-"+type+" a").hasClass("enabled")) {
		$("#button-"+type+" a").removeClass("enabled");
		
	} else {
		$("#button-"+type+" a").addClass("enabled");
		
	}

	presionSubmit();
}

function isNearByCategoryEnabled(type) {

  return ($("#hid-"+type).val() == 1);
}

function gmap(){ 

	map = mymarker();
	verify_chk();
		
	if(nbs.coordenadas != null){		
	  makers(map);			
	}	 
}        

function mymarker(){
	
	var latitud = nbs.mycoordenadas[0].latitude;	
	var longitud = nbs.mycoordenadas[0].longitude;

	var zoom = nbs.zoom;
 	var map = new GMap2(document.getElementById("gmap-view_gmap-gmap0"));
		
	map.addControl(new GSmallMapControl());
	map.addControl(new GMapTypeControl());

	// event mouse roller	
	map.enableScrollWheelZoom(); 	

	//Taken Zoom
	if( nbs.ultimate_zoom	== null || nbs.ultimate_zoom == 'undefined'){	
		 nbs.ultimate_zoom = zoom;
	}else{	
		 zoom=nbs.ultimate_zoom;
	}
	var new_zoom=map.getZoom();
	GEvent.addListener(map,"zoomend", function(zoom, new_zoom) {     	   
		nbs.ultimate_zoom = map.getZoom();	  
	});

	//Taken Move
	GEvent.addListener(map, "moveend", function() {		
		nbs.ultimate_center = map.getCenter();
	});

	//Taken Center
	var latlng1 = new GLatLng(latitud, longitud);

	var center=latlng1;	
	if( nbs.ultimate_center == null || nbs.ultimate_center == 'undefined'){	
		 nbs.ultimate_center = center;
	}else{	
		 center = nbs.ultimate_center;
	}
	map.setCenter(center, zoom);		

	// Create our "tiny" marker icon
	var blueIcon = new GIcon(G_DEFAULT_ICON);
	var redIcon = new GIcon(G_DEFAULT_ICON);

	if (nbs.mycoupon){
		blueIcon.iconSize = new GSize(34, 36);
		blueIcon.shadowSize = new GSize(37, 34);
		blueIcon.iconAnchor = new GPoint(9, 34);
		blueIcon.infoWindowAnchor = new GPoint(9, 2);	
		blueIcon.image = Drupal.gmap.iconpath+ '/coupons/coupons.png';
		blueIcon.shadow = Drupal.gmap.iconpath+ '/coupons/shadow.png';
		mymarkerOptions = { icon:blueIcon };

	}else{
		redIcon.iconSize = new GSize(34, 36);
		redIcon.shadowSize = new GSize(37, 34);
		redIcon.iconAnchor = new GPoint(9, 34);
		redIcon.infoWindowAnchor = new GPoint(9, 2);	
		redIcon.image = Drupal.gmap.iconpath+ '/marketplace/marketplace.png';
		redIcon.shadow = Drupal.gmap.iconpath+ '/marketplace/shadow.png';
		mymarkerOptions = { icon:redIcon };
	}

	function createMyMarker(point,content,mymarkerOptions) {
	   var mymarker = new GMarker(point,mymarkerOptions);
	   GEvent.addListener(mymarker, "click", function() {
	     mymarker.openInfoWindowHtml("" +  content);
	   });

		 return mymarker;
	}
	var mycoords = null;
	for (mycoords in nbs.mycoordenadas){
		if(isNumeric(mycoords)){

			var latlng = new GLatLng(nbs.mycoordenadas[mycoords].latitude, nbs.mycoordenadas[mycoords].longitude);	
			map.addOverlay(createMyMarker( latlng ,nbs.mycoordenadas[mycoords].desc, mymarkerOptions));
		}
	}				

	return map;	
}

function makers(map){

	var count_restaurants=0;
	var count_bars=0;
	var count_cinemas=0;
	var count_shopping=0;	
	var count_attactions=0;
	var count_hotels=0;
	var count_travel=0;

	var baseIcon = new GIcon();
	baseIcon.image= Drupal.gmap.iconpath+ '/marketplace/marketplace.png';
	//baseIcon.shadow = Drupal.gmap.iconpath+ '/marketplace/shadow.png';
	baseIcon.iconSize = new GSize(22, 23);
	baseIcon.shadowSize = new GSize(37, 34);
	baseIcon.iconAnchor = new GPoint(9, 34);
	baseIcon.infoWindowAnchor = new GPoint(9, 2);	

	function createMarker(point, index,html,category,milles) {
		
		var letteredIcon = new GIcon(baseIcon);	
		var flag='no';
		if(category){

//			console.log(category);

			if(nbs.chk_restaurants == true && (category.match(/Restaurants/gi))) {
				letteredIcon.image = Drupal.gmap.iconpath+ '/search_by_nearby/gm-restaurants-on.png';
				//letteredIcon.shadow = Drupal.gmap.iconpath+ '/marketplace/shadow.png';
				count_restaurants ++; 
				flag='ok';
				$("#counterRestaurants").html("("+description_cant_chk(count_restaurants)+")");												
			} //else if (nbs.chk_restaurants == true ) {  
				//nbs.chk_restaurants = true; $("#counterRestaurants").html("("+description_cant_chk(0)+")"); flag='ok'; }

			if (nbs.chk_bars == true && (category.match(/Bars_Pubs_and_Taverns/gi))) {
			  	letteredIcon.image = Drupal.gmap.iconpath+ '/search_by_nearby/markerB.png';
				//letteredIcon.shadow = Drupal.gmap.iconpath+ '/marketplace/shadow.png';
				count_bars ++;
				flag='ok';
				$("#counterBars_Pubs_and_Taverns").html("("+description_cant_chk(count_bars)+")");					
			} //else if (nbs.chk_bars == true ) {  
				//$("#counterBars_Pubs_and_Taverns").html("("+description_cant_chk(0)+")"); flag='ok'; }

			if( nbs.chk_cinemas == true && (category.match(/Cinemas/gi))){								
	  			letteredIcon.image = Drupal.gmap.iconpath+ '/search_by_nearby/markerC.png';
				//letteredIcon.shadow = Drupal.gmap.iconpath+ '/marketplace/shadow.png';
				count_cinemas ++;
				flag='ok';	
				$("#counterCinemas").html("("+description_cant_chk(count_cinemas)+")");					
			} //else if (nbs.chk_cinemas == true ) {  
				//$("#counterCinemas").html("("+description_cant_chk(0)+")"); flag='ok'; }

			if( nbs.chk_shopping == true && (category.match(/Shopping/gi))){
		  		letteredIcon.image = Drupal.gmap.iconpath+ '/search_by_nearby/gm-shopping-on.png';
				//letteredIcon.shadow = Drupal.gmap.iconpath+ '/marketplace/shadow.png';	
				count_shopping ++;
				flag='ok';
				$("#counterShopping").html("("+description_cant_chk(count_shopping)+")");						
			} //else if (nbs.chk_shopping == true ) {  
				//$("#counterShopping").html("("+description_cant_chk(0)+")"); flag='ok'; }

			if( nbs.chk_attactions == true && (category.match(/Attactions/gi))){
		  		letteredIcon.image = Drupal.gmap.iconpath+ '/search_by_nearby/gm-attractions-on.png';
				//letteredIcon.shadow = Drupal.gmap.iconpath+ '/marketplace/shadow.png';
				count_attactions ++;
				flag='ok';
				$("#counterAttactions").html("("+description_cant_chk(count_attactions)+")");					
			} //else if (nbs.chk_attactions == true ) {  
				//$("#counterAttactions").html("("+description_cant_chk(0)+")"); flag='ok'; }

			if( nbs.chk_hotels == true &&(category.match(/Hotels_and_Motels/gi))){
		  		letteredIcon.image = Drupal.gmap.iconpath+ '/search_by_nearby/markerL.png';
				//letteredIcon.shadow = Drupal.gmap.iconpath+ '/marketplace/shadow.png';
				count_hotels ++;
				flag='ok';
				$("#counterHotels_and_Motels").html("("+description_cant_chk(count_hotels)+")");					
			} //else if (nbs.chk_hotels == true ) {  
				//$("#counterHotels_and_Motels").html("("+description_cant_chk(0)+")"); flag='ok'; }

			if( nbs.chk_travel == true && (category.match(/Travel_and_Transportation/gi))){
	  			letteredIcon.image = Drupal.gmap.iconpath+ '/search_by_nearby/gm-transportation-on.png';
				//letteredIcon.shadow = Drupal.gmap.iconpath+ '/marketplace/shadow.png';
				count_travel ++;
				flag='ok';
				$("#counterTravel_and_Transportation").html("("+description_cant_chk(count_travel)+")");
			} //else if (nbs.chk_travel  == true ) {  
				// $("#counterTravel_and_Transportation").html("("+description_cant_chk(0)+")"); flag='ok'; }

		}

		if(flag=='ok'){
			  markerOptions = { icon:letteredIcon };
			  var marker = new GMarker(point, markerOptions);

			  GEvent.addListener(marker, "click", function() {
			    marker.openInfoWindowHtml(html);
			  });	
		}
		return marker; 
	}
	for (coords in nbs.coordenadas){
		//console.log(nbs.coordenadas[coords]);			
		if(!isArray(nbs.coordenadas[coords].latitude) && (nbs.coordenadas[coords].latitude) != null ){
			
			//console.log(nbs.coordenadas[coords].latitude);
			var latlng = new GLatLng(nbs.coordenadas[coords].latitude, nbs.coordenadas[coords].longitude);
			var mark = createMarker(latlng,i,nbs.coordenadas[coords].html,nbs.coordenadas[coords].type,nbs.coordenadas[coords].milles);		
			if(mark) map.addOverlay(mark);
		}
		
	}
	return;
}

function description_cant_chk(valor) {
	var description_cant = valor;
	if (valor == 0) {  description_cant = 'None in the search area.'; }
	return description_cant;
}



function verify_chk(){

	if($("#hid-Restaurants").val() == 1){  nbs.chk_restaurants = true; $("#counterRestaurants").html("("+description_cant_chk(0)+")");}			
	else{ nbs.chk_restaurants = false; $("#counterRestaurants").html("");}

	if($("#hid-Cinemas").val() == 1){ nbs.chk_cinemas = true; $("#counterCinemas").html("("+description_cant_chk(0)+")");}	
	else{ nbs.chk_cinemas = false; $("#counterCinemas").html("");}

	if($("#hid-Bars_Pubs_and_Taverns").val() == 1){ nbs.chk_bars = true; $("#counterBars_Pubs_and_Taverns").html("("+description_cant_chk(0)+")");}	
	else{ nbs.chk_bars = false; $("#counterBars_Pubs_and_Taverns").html("");}
	
	if($("#hid-Shopping").val() == 1){ nbs.chk_shopping = true; $("#counterShopping").html("("+description_cant_chk(0)+")");}	
	else{ nbs.chk_shopping = false; $("#counterShopping").html("");}
	
	if($("#hid-Attactions").val() == 1){ nbs.chk_attactions = true; $("#counterAttactions").html("("+description_cant_chk(0)+")");}	
	else{ nbs.chk_attactions = false; $("#counterAttactions").html("");}
	
	if($("#hid-Hotels_and_Motels").val() == 1){ nbs.chk_hotels = true; $("#counterHotels_and_Motels").html("("+description_cant_chk(0)+")");}	
	else{ nbs.chk_hotels = false; $("#counterHotels_and_Motels").html("");}

	if($("#hid-Travel_and_Transportation").val() == 1){ nbs.chk_travel = true; $("#counterTravel_and_Transportation").html("("+description_cant_chk(0)+")");}	
	else{ nbs.chk_travel = false; $("#counterTravel_and_Transportation").html("");}
	
	return;

}

function isNumeric(form_value){ 

	if (form_value.match(/^\d+$/) == null){ 
		return false; 
	}else{
		return true;
	}	 
} 

function isArray(value) {
	if (value instanceof Array) {
		return true;
	} else {
		return false;
	}
}


