var textQS;var map;var manager;var customIcon=[];

dojo.addOnLoad(function(){
	
	if (GBrowserIsCompatible()) {/* -34.1933361,-61.2801996 -34.49297540250153, -58.39508056640625 (-34.35704160076073, -58.3154296875*/
		var argentinaLat = -34.35704160076073;var argentinaLog = -58.3154296875;
		var first_time = 25;var init_zoom = 8;var count_marker=200;var data_position=1;
		
		manager = new ManagerMarker();
		map = new GMap2(document.getElementById("Maps"));
		//Maps types
		map.removeMapType(G_HYBRID_MAP);
		map.removeMapType(G_SATELLITE_MAP);//Elimino tipos de mapas
		//Icon
		manager.loadIcons();		
        //Zoom bar
		map.addControl(new GLargeMapControl());
		/* center in argentina*/
		map.setCenter(new GLatLng(argentinaLat, argentinaLog), init_zoom);
		//add markers
		var mgr = new MarkerManager(map);
		mgr.addMarkers(manager.getMarkers(first_time,0),init_zoom);//first time
		
		var count = dataSource.length - first_time;
		while(data_position <= count){
			init_zoom += 2;
			mgr.addMarkers(manager.getMarkers(count_marker,data_position), init_zoom);
			data_position += count_marker;
		}
		
		mgr.refresh();
		
	}
	textQS = dojo.byId('qs').value;
});
dojo.addOnUnload(GUnload);

dojo.byId('qs').onfocus = function(){
	var value = dojo.byId('qs').value;

	if(value == textQS){dojo.byId('qs').value = '';return true;}
}
dojo.byId('qs').onblur = function(){
	var value = dojo.byId('qs').value;

	if(value == ''){dojo.byId('qs').value = textQS;return true;}
}
dojo.byId('sea-form').onsubmit = function(){
	var value = dojo.byId('qs').value;

	if(value == textQS || !value){return false;}
}

var ManagerMarker = function(){}
//Methods
ManagerMarker.prototype.getMarkers = function(count,position,joker){
	var batch = [];
	
	for(var j = 0; j < count;j++){
		if(dataSource[position+j]){
			var letter = 'wifi-'.concat(position+j);
			var webMarker = dataSource[position+j]['add_icon'];
			var latlong =  new GLatLng(dataSource[position+j]['add_latitude'],dataSource[position+j]['add_longitude']) ;
			var marker = new GMarker(latlong,{title:letter,icon:customIcon[webMarker]});
			batch.push(marker);
			GEvent.addListener(marker, "click", function() {
				var arr_position = this.getTitle().split('-');
				this.openInfoWindowHtml(manager.getInfoWindow(arr_position[1]));
  			});
		}
	}
	return batch;
}
ManagerMarker.prototype.getInfoWindow = function(position){
	var html = "";
		
	html = "<div class='div-map'><p><strong>".concat(dataSource[position]['add_name']).concat('</strong>');
	html += "<br />".concat(dataSource[position]['add_address']).concat('<br /> ').concat(dataSource[position]['add_district']);
	html += "<br />Horario: ".concat(dataSource[position]['add_hours']).concat('<br />');
	html += "<a href='zonas-wifi/?id=".concat(dataSource[position]['add_id_pk']).concat("'>Ver m&aacute;s...</a></p>");
	//img
	html += "<img align='right' src='"+dataSource[position]['add_location']+"' onerror=\"this.src='images/image.gif'\"/></div>";
	
	
	return html;
}
ManagerMarker.prototype.loadIcons = function(position){
	//airport
	customIcon['airport'] = new GIcon(G_DEFAULT_ICON);
	customIcon['airport'].iconSize = new GSize(32, 32);
	customIcon['airport'].shadowSize = new GSize(59, 32);
	customIcon['airport'].image = "images/airport.png";
	customIcon['airport'].shadow = "images/shadow.png";
	//bar - restaurnt
	customIcon['bar'] = new GIcon(G_DEFAULT_ICON);
	customIcon['bar'].iconSize = new GSize(32, 32);
	customIcon['bar'].shadowSize = new GSize(59, 32);
	customIcon['bar'].image = "images/restaurant.png";
	customIcon['bar'].shadow = "images/shadow.png";
	//hotel
	customIcon['hotel'] = new GIcon(G_DEFAULT_ICON);
	customIcon['hotel'].iconSize = new GSize(32, 32);
	customIcon['hotel'].shadowSize = new GSize(59, 32);
	customIcon['hotel'].image = "images/hotel.png";
	customIcon['hotel'].shadow = "images/shadow.png";
	//shoping
	customIcon['shop'] = new GIcon(G_DEFAULT_ICON);
	customIcon['shop'].iconSize = new GSize(32, 32);
	customIcon['shop'].shadowSize = new GSize(59, 32);
	customIcon['shop'].image = "images/comercio.png";
	customIcon['shop'].shadow = "images/shadow.png";
	//Station
	customIcon['station'] = new GIcon(G_DEFAULT_ICON);
	customIcon['station'].iconSize = new GSize(32, 32);
	customIcon['station'].shadowSize = new GSize(59, 32);
	customIcon['station'].image = "images/estacion.png";
	customIcon['station'].shadow = "images/shadow.png";
	//Home
	customIcon['home'] = new GIcon(G_DEFAULT_ICON);
	customIcon['home'].iconSize = new GSize(32, 32);
	customIcon['home'].shadowSize = new GSize(59, 32);
	customIcon['home'].image = "images/home.png";
	customIcon['home'].shadow = "images/shadow.png";
	//Other
	customIcon['other'] = new GIcon(G_DEFAULT_ICON);
	customIcon['other'].iconSize = new GSize(32, 32);
	customIcon['other'].shadowSize = new GSize(59, 32);
	customIcon['other'].image = "images/otros.png";
	customIcon['other'].shadow = "images/shadow.png";
	//public
	customIcon['public'] = new GIcon(G_DEFAULT_ICON);
	customIcon['public'].iconSize = new GSize(32, 32);
	customIcon['public'].shadowSize = new GSize(59, 32);
	customIcon['public'].image = "images/public.png";
	customIcon['public'].shadow = "images/shadow.png";
}
