var marca=0;
var map;
var marca_foto;
var cargado=0;
var down=0;
var x;
var y;
var modo_prota=0;
var tipo_mapa=0;
var jg=null;
var ancho;
var alto;

window.onload=function(){
	if (GBrowserIsCompatible() && cargado==0) {
		cargado=1;
        map = new GMap2(document.getElementById("map"));
        //map.addControl(new GSmallMapControl());
        //map.addControl(new GMapTypeControl());
        map.addControl(new GSmallZoomControl());
        map.setCenter(new GLatLng(40.262761, -3.22998), 1);
        map.enableScrollWheelZoom();

        /*GEvent.addListener(map, "click", function(marcador, punto) {
  			if(marca==0){
  				crear_punto(punto);
  			}
		});*/
		if(marca==2)
			iniciar();
      }
   }
function crear_punto(point){
	if(marca!=1){
		marca=1;
		map.setCenter(point, map.getZoom());
		//var point =map.getCenter();
		var opciones= {draggable:true};
		marca_foto=new GMarker(point,opciones);
		map.addOverlay(marca_foto);
		marca_foto.enableDragging();
		//marca_foto.bindInfoWindowHtml("<a onclick='borrar_punto();return false;'>Borrar</a>", '10');
		GEvent.addListener(marca_foto,"dragend",function(){salvar_punto();});
		salvar_punto();	
	}
}

function nuevo_punto(){
	if(marca!=1){
		marca=1;
		//map.setCenter(point, map.getZoom());
		var point =map.getCenter();
		var opciones= {draggable:true};
		marca_foto=new GMarker(point,opciones);
		map.addOverlay(marca_foto);
		marca_foto.enableDragging();
		//marca_foto.bindInfoWindowHtml("<a onclick='borrar_punto();return false;'>Borrar</a>", '10');
		GEvent.addListener(marca_foto,"dragend",function(){salvar_punto();});
		salvar_punto();	
	}
}

function cambiar_mapa(){
tipo_mapa++;
if(tipo_mapa>2)
	tipo_mapa=0;
if(tipo_mapa==0)
	map.setMapType(G_NORMAL_MAP);
else if(tipo_mapa==1)
	map.setMapType(G_SATELLITE_MAP);
else if(tipo_mapa==2)
	map.setMapType(G_HYBRID_MAP);
}

function borrar_punto(){
	if(marca==1){
		marca=0;
		map.removeOverlay(marca_foto);
		salvar_punto();
	}
}

function aumentar_zoom(){}

function reducir_zoom(){}

function salvar_punto(){
	if(marca==1){
		map.setCenter(marca_foto.getLatLng());
		punto=marca_foto.getLatLng()
		new Ajax.Request(window.location.pathname,{
 			method: 'post',
 			parameters:'metodo=geoposicionar&latitud='+punto.lat()+'&longitud='+punto.lng()
  		});
	}
	else
		new Ajax.Request(window.location.pathname,{
 			method: 'post',
 			parameters:'metodo=borrar_geoposicion'
 		});
}

function minimizar(id,objeto,tam){
		//cargar_mapa();
		new Effect.toggle(id,'blind');
		//objeto.parent.parent.style.width=tam;
		if (objeto.innerHTML=="-"){
			objeto.innerHTML="+";
		}
		else{
			objeto.innerHTML="-";
		}
 }
 
 function edit(cod){
if (cod=="metodo=borrar_foto")
	var entrar = confirm('Esta seguro de borrar la foto?');
else
	var entrar=true;
	
	if ( entrar ){
	$('edit-respuesta').innerHTML='Procesando...';
	if(cod=='metodo=titulo'){cod=cod+'&texto='+$('input-texto').value;}
	new Ajax.Request(window.location.pathname, {
  		method: 'post',
  		parameters: cod,
  		onSuccess: function(resp){
  			$('edit-respuesta').innerHTML='';
  			if(cod.charAt(7) == 'r'){
  				//alert(cod+' '+resp.responseText);
  				if(resp.responseText.charAt(0)!='<' && resp.responseText.length==32){
  					$('img-foto').src='/image/'+resp.responseText+'/size_2?nuevo='+Math.random();
  					$(resp.responseText).src='/image/'+resp.responseText+'/size_3?nuevo='+Math.random();
  					$('img_foto').width=alto;
					$('img_foto').height=ancho;
					$('foto-foto').style.width=alto+'px';
					$('foto-foto').style.height=ancho+'px';	
  				}
  			}
  			else if(cod.charAt(7) == 't'){
  				elemento=$('lateral-foto-titulo-fecha').childElements()
  				elemento[0].innerHTML=resp.responseText;
  				}
  				else if (cod.charAt(7) == 'b'){
  			window.location.href=window.location.pathname.substring(0,window.location.pathname.indexOf('foto')-1);
  				}
  				
  		}	
	});
	}
return false;
}
function cambia_pri(foto,cam){
	$('edit-respuesta').innerHTML="Procesando...";
	if(cam=='public')
		metodo='publica';
	else
		metodo='privada';
	new Ajax.Request(window.location.pathname, {
  		method: 'post',
  		parameters: 'metodo='+metodo,
  		onSuccess: function(resp){
  			$('edit-respuesta').innerHTML='';
  			if(resp.responseText.charAt(0) == '0'){
  				$('cambio').innerHTML='<img onclick="cambia_pri('+"'"+foto+"','public'"+');" src="/images/lock.png" class="icon_mediano_click">';
  			}
  			else{
  				$('cambio').innerHTML='<img onclick="cambia_pri('+"'"+foto+"','private'"+');" src="/images/lock_open.png" class="icon_mediano_click">';
  				}
  		}	
	});
return false;
}

function enlazar(foto){
	new Ajax.Request(window.location.pathname, {
                method: 'post',
                parameters: 'metodo=enlazar&foto='+foto,
                onSuccess: function(resp){
                //alert(resp.responseText);
                if(resp.responseText.charAt(0)=='0'){
					$('yo').src='/image/'+foto+'/size_3';
					//alert('/image/'+foto+'/size_3');
				}
				else
					alert(resp.responseText);
				}
	});
}

function comentara(formulario){
	new Ajax.Request(window.location.pathname, {method: 'post', asynchronous:true, parameters:Form.serialize(formulario),onSuccess : function(resp) {
  		if(resp.responseText.charAt(0) == 'E')
  			$('foto-comentarios').innerHTML=$('foto-comentarios').innerHTML +"<br>"+resp.responseText;
  		else
  			$('foto-comentarios').innerHTML=resp.responseText;
  		
  		$('area-coment').value = "";
  		}
 });
return false;
}

function borrarcomen(cod,foto){
var entrar = confirm('Esta seguro de borrar el comentario?');
if ( entrar ){
	new Ajax.Request(window.location.pathname, {
  		method: 'post',
  		parameters: 'metodo=borrar_comentario&borrar='+cod,
  		onSuccess: function(resp){
  			if(resp.responseText.charAt(0) == 'E')
  				$('foto-comentarios').innerHTML=$('foto-comentarios').innerHTML +"<br>"+resp.responseText;
  			else
  				$('foto-comentarios').innerHTML=resp.responseText;
  		}	
	});
}
return false;
}

function nuevo_prota(){
	if(x!=0 && y!=0 && $F('uprota')!='')
	new Ajax.Request(window.location.pathname, {
  		method: 'post',
  		parameters: 'metodo=nuevo_protagonista&usuario='+$F('uprota')+'&x='+x+'&y='+y,
  		onSuccess: function(resp){
  			if(resp.responseText.charAt(0)=='<'){
  				$('usuarios_protagonistas').innerHTML=$('usuarios_protagonistas').innerHTML+resp.responseText;
  			}
  		}
  	});
  	return false;
}
function borrar_prota(texto){
	new Ajax.Request(window.location.pathname, {
  		method: 'post',
  		parameters: 'metodo=borrar_protagonista&usuario='+texto,
  		onSuccess: function(resp){
  			if(resp.responseText.charAt(0)=='0'){
  				$('protagonista_'+texto).parentNode.removeChild($('protagonista_'+texto));
  			}
  			else
  				alert(resp.responseText);
  		}
  	});
}
function nuevo_compartir(texto){
	new Ajax.Request(window.location.pathname, {
  		method: 'post',
  		parameters: 'metodo=nuevo_compartir&usuario='+$F(texto),
  		onSuccess: function(resp){
  			if(resp.responseText.charAt(0)=='<'){
  				$('usuarios_compartidos').innerHTML=$('usuarios_compartidos').innerHTML+resp.responseText;
  			}
  			else
  				alert(resp.responseText);
  				
  		}
  	});
}
function borrar_compartir(texto){
	new Ajax.Request(window.location.pathname, {
  		method: 'post',
  		parameters: 'metodo=borrar_compartir&usuario='+texto,
  		onSuccess: function(resp){
  			if(resp.responseText.charAt(0)=='0'){
  				$('compartido_'+texto).parentNode.removeChild($('compartido_'+texto));
  			}
  			else
  				alert(resp.responseText);
  		}
  	});
}
function actualiza_tag(texto){
	new Ajax.Request(window.location.pathname, {
  		method: 'post',
  		parameters: 'metodo=actualiza_tag&tag='+$F(texto),
  		onSuccess: function(resp){
  			if(resp.responseText.charAt(0)!='<')
  				$('tags_fotoloco').innerHTML=resp.responseText;
  		}
  	});
}



function votar(){
	new Ajax.Request(window.location.pathname, {
  		method: 'post',
  		parameters: 'metodo=votar_foto&voto='+$F('voto'),
  		onSuccess: function(resp){
  			alert(resp.responseText);
  		}
  	});
}

function rellenar(posx,posy){
	$('uprota').disabled=false;
	x=posx;
	y=posy;
}

function flecha(posx,posy){
		var x=(posx);
		var y=(posy);

		jg.drawImage('/images/flecha.png',x-28,y-70,55,70,'onMouseOver="mostrar_persona('+x+','+y+')" onMouseOut="jg.clear()"');
		jg.paint();  
}

function mostrar_persona(posx,posy){
		jg.clear();
		flecha(posx,posy);		
}
function activa_menu_prota(sp){
	if(modo_prota==0){
		modo_prota=1;
		new Effect.toggle('input_prota','blind');
		sp.innerHTML='Cancelar';
		$('img_foto').style.display='';
		$('img-foto').style.display='none';
		
	}
	else{
		modo_prota=0;
		new Effect.toggle('input_prota','blind');
		jg.clear();
		sp.innerHTML='A&ntilde;adir protagonista';
		$('img_foto').style.display='none';
		$('img-foto').style.display='';
		$('uprota').disabled=true;
		$('uprota').value='';
	}
}
