// Copyright (c) 2008 Anna Putrino and Stanislav Chachkov (http://www.hikr.org), All rights reserved 
var default_point=new GLatLng(46.6795944656402, 8.1298828125)
var default_zoom=7

var map=null
var user_map=null

function r4userMap(user_login){
	  if (GBrowserIsCompatible()) {
	  	r4js(['prototype','bounds','quad2'],function(){

				map = new GMap2(document.getElementById("map"))//,{size:new GSize(500,400)})
				map.addMapType(G_PHYSICAL_MAP)
				map.addControl(new GLargeMapControl3D());
				map.addControl(new GMenuMapTypeControl());
				map.enableScrollWheelZoom()
				map.enableContinuousZoom() 
				
				map.setCenter(default_point,default_zoom,G_NORMAL_MAP);  			
				user_map=new UserMap(user_login)
				user_map.load()
			})
		}
	}


function UserMap(user_login){
	this.user_login=user_login
	this.icons=this.createIcons()
	this.world=null
	this.world_ph=null
	this.opt=null
	
	var me=this
	var fs=$('form_selector')
	if(fs){
		$('form_selector').onsubmit=function(){me.loadPizs(Form.serialize(this));return false;}
		$('cb_pizs').checked=true
		$('cb_photos').checked=false
		
		$('piz_type').selectedIndex=0
		$('rando_act').selectedIndex=0
		
		$('cb_pizs').onclick=function(){me.setVisible('piz',this.checked)}
		$('cb_photos').onclick=function(){me.setVisible('photo',this.checked)}
	}
	
}

UserMap.prototype.setVisible=function(type,status){
	if(type=='piz'){
		if(status&&!this.world){this.loadPizs(this.opt);r4displayBlock('form_selector')}
		if(!status&&this.world){this.world.remove();this.world=null;r4displayNone('form_selector')}
	}
	else if(type=='photo'){
		if(status&&!this.world_ph){this.loadPhotos()}
		if(!status&&this.world_ph){this.world_ph.remove();this.world_ph=null}
	}
}

UserMap.prototype.load=function(){
	this.loadPizs()
}

UserMap.prototype.loadPizs=function(opt){
	if(this.world) this.world.remove()
	var me=this
	this.bounds=new GLatLngBounds()
	
	this.opt=opt
	
	opt=(opt?'&'+opt:'')
	
	this.world=new QuadWorld(r4_root+"load_pizs.php?short_too=1&user_login="+this.user_login+opt,{
			zoom_increment:4,
			createmarker:function(piz){
				var p=new GLatLng(parseFloat(piz.getAttribute("lat")), parseFloat(piz.getAttribute("lon")))
				me.bounds.extend(p)
				var name=piz.getAttribute("name")
				var type=piz.getAttribute("type")
				var id=piz.getAttribute("id")
				var height=piz.getAttribute("height")
			
				var marker=new GMarker(p,{"icon":me.icons[type].yellow,"title":name+" ("+height+" m)"})
				GEvent.addListener(marker, "click", me.onclick(id,opt))
				return marker
			
				},
			onloadend:function(l){
				if(me.bounds.isEmpty()){
					r4openInfoWindow("map", 400, 50, "Info",nopizs_text)
				}else{
					map.setCenter(me.bounds.getCenter(),map.getBoundsZoomLevel(me.bounds))
				}
				if(l>0){
					r4showHTML('selector_count','('+l+')')
				}else{
					r4showHTML('selector_count','(0)')
				}
			}
	})	

	this.world.load()

}

UserMap.prototype.loadPhotos=function(){
	if(this.world_ph) this.world_ph.remove()
	var me=this
	this.bounds=new GLatLngBounds()
	
	this.world_ph=new QuadWorld(r4_root+"load_photos.php?user_login="+this.user_login,{zoom_increment:3,"createmarker":function(piz){
		var p=new GLatLng(parseFloat(piz.getAttribute("lat")), parseFloat(piz.getAttribute("lon")))
		me.bounds.extend(p)
		var id=piz.getAttribute("id")
		var icon=new GIcon(me.icons.photo,r4_root+'files/'+id+'t.jpg')
	
		var marker=new GMarker(p,{"icon":icon})
		GEvent.addListener(marker,"click", function() {
				var im=new Image()
				
				im.onload=function(){
					
					me.openInfoWindow(id,marker)
				}
				im.src=r4_root+'files/'+id+'m.jpg'
		})
		return marker
	
		},"onloadend":function(){
		if(me.bounds.isEmpty()){
			r4openInfoWindow("map", 400, 50, "Info",nophotos_text)
		}else{
			map.setCenter(me.bounds.getCenter(),map.getBoundsZoomLevel(me.bounds))
		}
		
	}})	

	this.world_ph.load()
}


UserMap.prototype.onclick=function(id,opt){
	var me=this
	return function(){
		var url=r4_root+'infowindow.php?nw=1&short_too=1&piz_id='+id+"&user_login="+me.user_login+opt
		//r4displayBlock('loading_map')
		window.setTimeout(function(){r4getInInfoWindow('map',url)},0)
	}
}	

UserMap.prototype.openInfoWindow=function(id,marker){
		var content="<div class='image'>"
		content+="<div id='pcontainer'>"
		content+="	<a href='"+r4_root+"gallery/photo"+id+".html' target='_blank' style='text-decoration:none'><img  id='photos' src='"+r4_root+'files/'+id+'m.jpg'+"' alt='' style='z-index:50;border:0px' onmouseover='user_map.photoMouseover("+id+")' onmouseout='user_map.photoMouseout()'/></a> "
		content+="<div id='caption'><span id='caption_text'>"+"..."+"</span></div></div></div>"
		marker.openInfoWindowHtml(content)	
}


UserMap.prototype.createIcons=function(){
	var icon_names=r4_piz_types
	var icons=new Object()
	for(var i=0;i<icon_names.length;i++){
		var icon = new GIcon();
		icon.image = r4_sroot+"r4icons/ico2_"+icon_names[i]+"_s.png";	    
		//icon.image = r4_sroot+"r4icons/test1.png";	    
		//icon.shadow = r4_sroot+"r4icons/ico_shadow.png";	    
		icon.iconSize = new GSize(16, 16);
		icon.iconAnchor = new GPoint(8,16);
		//icon.shadowSize = new GSize(25.0, 16.0);
		
		icons[icon_names[i]]=new Object
		icons[icon_names[i]].yellow=icon
	}

	var icon=new GIcon()	
	icon.image = r4_sroot+"r4icons/blank32.png";
	icon.shadow = r4_sroot+"r4icons/shadow-blank32.png";	   
	icon.iconSize = new GSize(32, 32);
	icon.iconAnchor = new GPoint(16,32);
	icon.infoWindowAnchor=new GPoint(16,16)
	icon.shadowSize = new GSize(49.0, 32.0);

	icons['photo']=icon
	
	return icons

}

UserMap.prototype.photoMouseover=function(id){
	if($("caption_text").innerHTML=="..."){
		loadXMLDoc(r4_root+"load_photos.php?act=caption&photo_id="+id,function(req){
			if (req.readyState == 4){
				$("caption_text").innerHTML=req.responseText
				
			}			
		})
	}
	new Effect.Opacity("caption",{duration:0.3,from:0, to:0.8})
}

UserMap.prototype.photoMouseout=function(){
	new Effect.Opacity("caption",{duration:0.3,from:0.8, to:0})
}

