var nativeSplit = nativeSplit || String.prototype.split;

String.prototype.split = function (s /* separator */, limit) {
	// If separator is not a regex, use the native split method
	if (!(s instanceof RegExp))
		return nativeSplit.apply(this, arguments);

	/* Behavior for limit: If it's...
	 - Undefined: No limit
	 - NaN or zero: Return an empty array
	 - A positive number: Use limit after dropping any decimal
	 - A negative number: No limit
	 - Other: Type-convert, then use the above rules */
	if (limit === undefined || +limit < 0) {
		limit = false;
	} else {
		limit = Math.floor(+limit);
		if (!limit)
			return [];
	}

	var	flags = (s.global ? "g" : "") + (s.ignoreCase ? "i" : "") + (s.multiline ? "m" : ""),
		s2 = new RegExp("^" + s.source + "$", flags),
		output = [],
		lastLastIndex = 0,
		i = 0,
		match;

	if (!s.global)
		s = new RegExp(s.source, "g" + flags);

	while ((!limit || i++ <= limit) && (match = s.exec(this))) {
		var zeroLengthMatch = !match[0].length;

		// Fix IE's infinite-loop-resistant but incorrect lastIndex
		if (zeroLengthMatch && s.lastIndex > match.index)
			s.lastIndex = match.index; // The same as s.lastIndex--

		if (s.lastIndex > lastLastIndex) {
			// Fix browsers whose exec methods don't consistently return undefined for non-participating capturing groups
			if (match.length > 1) {
				match[0].replace(s2, function () {
					for (var j = 1; j < arguments.length - 2; j++) {
						if (arguments[j] === undefined)
							match[j] = undefined;
					}
				});
			}

			output = output.concat(this.slice(lastLastIndex, match.index), (match.index === this.length ? [] : match.slice(1)));
			lastLastIndex = s.lastIndex;
		}

		if (zeroLengthMatch)
			s.lastIndex++;
	}

	return (lastLastIndex === this.length) ?
		(s.test("") ? output : output.concat("")) :
		(limit      ? output : output.concat(this.slice(lastLastIndex)));
};

function load_map(){
	if (GBrowserIsCompatible() ){

		map = new GMap2(document.getElementById("map2"));
		map.setCenter(new GLatLng(40.17887331434696, 3.3837890625), 6);

		map.addControl(new GMapTypeControl());
		map.addMapType(G_PHYSICAL_MAP);
		map.removeMapType(G_HYBRID_MAP);
		map.setMapType(G_PHYSICAL_MAP);

		map.addControl(new GLargeMapControl());
		map.enableScrollWheelZoom();
		var keyboard = new GKeyboardHandler(map);
		
		//GEvent.addListener(map, 'zoomend', showEscoltar);
		//GEvent.addListener(map, 'moveend', showEscoltar);
		
		//progressBar = new ProgressbarControl(map, {width:150});

	} else {
		alert("Lo sentimos, la API de Google Maps no es compatible con su navegador.");
	}

}

function MarkerMap(punt, color, dragg){

	var tinyIcon = new GIcon();

	switch(color){

		case "pdi":
			tinyIcon.image = "http://www.onsortir.cat/ub/files/img/pdi.gif";
			tinyIcon.iconSize = new GSize(10, 17);
			tinyIcon.iconAnchor = new GPoint(8, 17);
			tinyIcon.infoWindowAnchor = new GPoint(7, 7);
			markerOptions = {icon:tinyIcon,draggable:dragg};
			break;
		case "so10":
			tinyIcon.image = "http://www.onsortir.cat/ub/files/img/marker_10.png";
			tinyIcon.iconSize = new GSize(8, 21);
			tinyIcon.iconAnchor = new GPoint(6, 21);
			tinyIcon.infoWindowAnchor = new GPoint(4, 10);
			markerOptions = {icon:tinyIcon,draggable:dragg};
			break;
		case "so11":
			tinyIcon.image = "http://www.onsortir.cat/ub/files/img/marker_11.png";
			tinyIcon.iconSize = new GSize(10, 19);
			tinyIcon.iconAnchor = new GPoint(5, 19);
			tinyIcon.infoWindowAnchor = new GPoint(5, 9);
			markerOptions = {icon:tinyIcon,draggable:dragg};
			break;
		case "so12":
			tinyIcon.image = "http://www.onsortir.cat/ub/files/img/marker_12.png";
			tinyIcon.iconSize = new GSize(6, 22);
			tinyIcon.iconAnchor = new GPoint(5, 22);
			tinyIcon.infoWindowAnchor = new GPoint(3, 11);
			markerOptions = {icon:tinyIcon,draggable:dragg};
			break;
		case "so13":
			tinyIcon.image = "http://www.onsortir.cat/ub/files/img/marker_13.png";
			tinyIcon.iconSize = new GSize(5, 21);
			tinyIcon.iconAnchor = new GPoint(3, 21);
			tinyIcon.infoWindowAnchor = new GPoint(3, 10);
			markerOptions = {icon:tinyIcon,draggable:dragg};
			break;
		case "so14":
			tinyIcon.image = "http://www.onsortir.cat/ub/files/img/marker_14.png";
			tinyIcon.iconSize = new GSize(7, 21);
			tinyIcon.iconAnchor = new GPoint(5, 21);
			tinyIcon.infoWindowAnchor = new GPoint(3, 10);
			markerOptions = {icon:tinyIcon,draggable:dragg};
			break;
		case "so15":
			tinyIcon.image = "http://www.onsortir.cat/ub/files/img/marker_15.png";
			tinyIcon.iconSize = new GSize(10, 22);
			tinyIcon.iconAnchor = new GPoint(5, 22);
			tinyIcon.infoWindowAnchor = new GPoint(5, 11);
			markerOptions = {icon:tinyIcon,draggable:dragg};
			break;
		case "so16":
			tinyIcon.image = "http://www.onsortir.cat/ub/files/img/marker_16.png";
			tinyIcon.iconSize = new GSize(8, 22);
			tinyIcon.iconAnchor = new GPoint(5, 22);
			tinyIcon.infoWindowAnchor = new GPoint(4, 11);
			markerOptions = {icon:tinyIcon,draggable:dragg};
			break;
		case "so17":
			tinyIcon.image = "http://www.onsortir.cat/ub/files/img/marker_17.png";
			tinyIcon.iconSize = new GSize(9, 21);
			tinyIcon.iconAnchor = new GPoint(5, 21);
			tinyIcon.infoWindowAnchor = new GPoint(5, 11);
			markerOptions = {icon:tinyIcon,draggable:dragg};
			break;
		case "so18":
			tinyIcon.image = "http://www.onsortir.cat/ub/files/img/marker_18.png";
			tinyIcon.iconSize = new GSize(8, 21);
			tinyIcon.iconAnchor = new GPoint(4, 21);
			tinyIcon.infoWindowAnchor = new GPoint(4, 10);
			markerOptions = {icon:tinyIcon,draggable:dragg};
			break;
		case "so19":
			tinyIcon.image = "http://www.onsortir.cat/ub/files/img/marker_19.png";
			tinyIcon.iconSize = new GSize(5, 23);
			tinyIcon.iconAnchor = new GPoint(5, 23);
			tinyIcon.infoWindowAnchor = new GPoint(3, 11);
			markerOptions = {icon:tinyIcon,draggable:dragg};
			break;
		default:
			tinyIcon.image = "http://www.onsortir.cat/ub/files/img/marker_11.png";
			tinyIcon.iconSize = new GSize(10, 19);
			tinyIcon.iconAnchor = new GPoint(5, 19);
			tinyIcon.infoWindowAnchor = new GPoint(5, 9);
			markerOptions = {icon:tinyIcon,draggable:dragg};
			break;
	}

	var mark = new GMarker(punt, markerOptions);
	return mark;
}
//****************************************************************************

//###############################//
//***** FUNCIONS D'ESCOLTAR *****//
//###############################//
function showEscoltar(){
	//bounds=map.getBounds();
	//latlang_max=bounds.getSouthWest();
	//latlang_min=bounds.getNorthEast();
	//MaxLat=latlang_max.lat();
	//MaxLong=latlang_max.lng();
	//MinLat=latlang_min.lat();
	//MinLong=latlang_min.lng();

	map.clearOverlays();
	
	//var TotalPdis=file("kernel/escoltar/escoltar_INFO.php?funcio=3&categoria="+Categoria+"&comarca="+Comarca+"&paraula="+Paraula+"&maxlat="+MaxLat+"&maxlong="+MaxLong+"&minlat="+MinLat+"&minlong="+MinLong);
	//progressBar.start(TotalPdis);
	
	//var StrPdis = file("kernel/escoltar/escoltar_INFO.php?funcio=1&categoria="+Categoria+"&comarca="+Comarca+"&paraula="+Paraula+"&maxlat="+MaxLat+"&maxlong="+MaxLong+"&minlat="+MinLat+"&minlong="+MinLong);
	var StrPdis = file("kernel/escoltar/escoltar_INFO.php?funcio=1&categoria="+Categoria+"&comarca="+Comarca+"&paraula="+Paraula);
	if(StrPdis!=""){
		var ids = StrPdis.split('||');
		for (i=0; i<ids.length; i++){
			var StrInfo = file("kernel/escoltar/escoltar_INFO.php?funcio=2&id="+ids[i]);
			if(StrInfo!=""){
				var Dades = StrInfo.split('||');
				showEscoltarPDI(Dades[0], Dades[1], Dades[2], Dades[3], ids[i], Dades[4], Dades[5], Dades[6]);
				//progressBar.updateLoader(1);
			}
		}
	}

}

function showEscoltarPDI(Lat, Lng, Titol, Descripcio, idSo, idUsr, Imatge, Cat){

	var mark = MarkerMap(new GLatLng(Lat, Lng), 'so'+Cat, false);
	map.addOverlay(mark);

	GEvent.addListener(mark, "click", function(){
		mark.closeInfoWindow();
		mark.openInfoWindowHtml("<table border='0' height='140px'><tr><td><div id='player' style='margin-bottom:5px; position;absolute; border:0px solid red; z-index:80;'></div></td></tr><tr valign='top'><td><a href='/"+Imatge+"' rel='lightbox[escoltar]' title=\"<a class='link_lightbox' href='/"+Imatge+"' target='_blank'>Versi&oacute; per guardar</a>\"><img src='extensions/phpThumb/phpThumb.php?src=/"+Imatge+"&w=120&h=105' border='0'/></a></td><td valign='top'><b>"+Titol+"</b><br>"+Descripcio+"<br></td></tr></table></div>", {maxWidth:275});
		var fo = new FlashObject("files/swf/musicplayer.swf?autoplay=true&song_url=/contents/"+idUsr+"/so/"+idSo+".mp3", "player_x", "17", "17", "6", "#FFFFFF");
		fo.write('player');
		initLightbox();
	});

}

// -------------------- AJAX -------------------------
function file(fichier){
	if(window.XMLHttpRequest) // FIREFOX
		xhr_object = new XMLHttpRequest();
	else if(window.ActiveXObject) // IE
		xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
	else return(false);

	xhr_object.open("GET", fichier, false);
	xhr_object.send(null);

	if(xhr_object.readyState == 4)
		return(xhr_object.responseText);
	else return(false);
}
// -------------------- AJAX -------------------------