//<![CDATA[

//creo il container che conterrątutta l'applicazione

a=document.createElement('DIV')
a.setAttribute('id','container')
document.body.appendChild(a)
$('container').style.width=(myX-40)+'px';
$('container').style.height=(myY-50)+'px';

//mappa
a=document.createElement('DIV')
a.setAttribute('id','map')
$('container').appendChild(a)

//calcolodimensionialDIV'map'
$('map').style.width=(Element.getDimensions('container').width-155)+'px';
$('map').style.height=(Element.getDimensions('container').height-100)+'px';
//dimensionifisse,soloperdebugsulineelente
//$('map').style.width=(300)+'px';
//$('map').style.height=(300)+'px';


//generazionegooglemap
var map=new GMap2(document.getElementById("map"));
setHome()
map.addControl(new GScaleControl());
map.addControl(new GLargeMapControl());
map.addControl(new GMapTypeControl());
map.addMapType(G_SATELLITE_3D_MAP) 
//generazione logo in basso sx
a=document.createElement('DIV')
a.setAttribute('id','logo')
document.body.appendChild(a)

//generazione finestra per indicare i caricamenti
a=document.createElement('DIV')
a.setAttribute('id','loadingWindow')
$('container').appendChild(a)
$('loadingWindow').className='loadingWindow'
$('loadingWindow').innerHTML='loading...' 
$('loadingWindow').style.left=(myX/2-150)+'px'
$('loadingWindow').style.top=myY/2+'px'
//segna i circoli
//drawTc();
//disegna i tools
drawTools();
//chiudo la finestra di caricamento

function drawTc(){
	buildASC()
}

function buildASC(){for (x=0;x<19;x++) showAddress2(x)}
function buildComunali(){for (x=19;x<44;x++) showAddress2(x)}




function showAddress(address) {//questa usa il geocoding dell'indirizzo
	geocoder = new GClientGeocoder();
  	geocoder.getLatLng(address,function(point) {
		if (!point) {
        		alert(address + " non riesco a trovarlo!");
      		} else {
              		//map.setCenter(point, 13);
              		var marker = new GMarker(point);
			map.addOverlay(marker);
			GEvent.addListener(marker, "mouseover", function() {
				toolTip(1,x,marker);
		  	});
			GEvent.addListener(marker, "mouseout", function() {
				removeToolTip(x);
		  	});
            }
        }
  );
}

function showAddress2(x) {//questa usa lat e long; usata per i centri ASC
              		point=new GLatLng(tc[x]['y'],tc[x]['x']);
			var icon = new GIcon();
			if(x<19)
			icon.image =nowPlaying(x);
			else icon.image = "img/verde.png";
			
			//icon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
			icon.iconSize = new GSize(16, 28);
			icon.shadowSize = new GSize(16, 28);
			icon.iconAnchor = new GPoint(6, 20);
			icon.infoWindowAnchor = new GPoint(5, 1);
			var marker = new GMarker(point,icon);
			map.addOverlay(marker);
			GEvent.addListener(marker, "mouseover", function() {
				toolTip(1,x,marker);
		  	});
			GEvent.addListener(marker, "mouseout", function() {
				removeToolTip(x);
		  	});
			GEvent.addListener(marker, "click", function() {
				permaToolTip(marker,x);
		  	});
//bounds.extend(point)
}


function toolTip(mode,x,marker){

   a=document.createElement('DIV')
      //a.setAttribute('class','pop')
      a.setAttribute('id','pop'+x)

         //document.getElementById('map').appendChild(a)
	 map.getPane(G_MAP_FLOAT_PANE).appendChild(a)
	 iH='<p>'+tc[x]['name']+'</p>';
	 imgIcon=nowPlaying(x);
	 if(imgIcon=='img/arancio_C.png') iH+='<p>'+nextEvent.getDate()+'/'+(nextEvent.getMonth()+1)+ ': maschile C</p>';
	 if(imgIcon=='img/arancio_BA.png') iH+='<p>'+nextEvent.getDate()+'/'+(nextEvent.getMonth()+1)+ ': masch. B femm. A</p>';
	 if(imgIcon=='img/arancio_AB.png') iH+='<p>'+nextEvent.getDate()+'/'+(nextEvent.getMonth()+1)+ ': masch. A femm. B</p>';
	 if(imgIcon=='img/arancio_M.png') iH+='<p>'+nextEvent.getDate()+'/'+(nextEvent.getMonth()+1)+ ': misto A e B</p>';
	 if(imgIcon=='img/arancio_J.png') iH+='<p>'+nextEvent.getDate()+'/'+(nextEvent.getMonth()+1)+ ': juniores A e B</p>';
	 $('pop'+x).className='pop'
	 document.getElementById('pop'+x).style.position='absolute'
	 document.getElementById('pop'+x).style.left=(map.fromLatLngToDivPixel(marker.getPoint()).x+10)+'px'
	 document.getElementById('pop'+x).style.top=(map.fromLatLngToDivPixel(marker.getPoint()).y-10)+'px'
	 document.getElementById('pop'+x).innerHTML=iH;
}
	       
function removeToolTip(x){
	a=document.getElementById('pop'+x);
	map.getPane(G_MAP_FLOAT_PANE).removeChild(a)
}

function lookAdr(){
	//addScript('getAdr.js')
	showAddress3($('adr').value)
}

function addScript(url){
        var script = document.createElement('script');
        script.type = 'text/javascript';
        script.id = 'lnk_aprs';
        script.src = url;
        document.getElementsByTagName('head')[0].appendChild(script);
}

function addPoint(){
alert('E\' stato aggiunto un punto al centro della mappa; serve come segnaposto per ricordare un luogo.\nSi puo\' spostare con il mouse')
              		point=map.getCenter();
			var icon = new GIcon();
			icon.image = "img/home.gif";
			icon.shadow = "img/homeshadow.png";
			icon.iconSize = new GSize(29, 25);
			icon.shadowSize = new GSize(41,36);
			icon.iconAnchor = new GPoint(6, 20);
			icon.infoWindowAnchor = new GPoint(5, 1);
			id_marker = new GMarker(point,{icon:icon,draggable:true});
			map.addOverlay(id_marker);
			GEvent.addListener(id_marker, "click", function() {
				permaToolTip('home');
		  	});
			GEvent.addListener(id_marker, "dragend", function() {
			//alert(id_marker.getPoint())	
		  	
			});
}




function getCenter(){
	alert(map.getCenter())
}

function print_r(theObj){
  if(theObj.constructor == Array ||
       theObj.constructor == Object){
           document.write("<ul>")
       for(var p in theObj){
             if(theObj[p].constructor == Array||
              theObj[p].constructor == Object){
      document.write("<li>["+p+"] => "+typeof(theObj)+"</li>");
              document.write("<ul>")
              print_r(theObj[p]);
              document.write("</ul>")
            } else {
    document.write("<li>["+p+"] => "+theObj[p]+"</li>");
          }
      }
          document.write("</ul>")
    }
    }


function drawTools(){
	for(x=0;x<tools.length;x++){
		//var newDiv=document.createElement('div')
		//newDiv.setAttribute('id','tool_'+x)
		//$('container').appendChild(newDiv)
		//$('tool_'+x).style.left=(Element.getDimensions('container').width-105)+'px'
		//$('tool_'+x).className='toolx'
		//$('tool_'+x).style.top=(20+x*50)+'px'
		//$('tool_'+x).innerHTML=tools[x]
	}

	var newDiv=document.createElement('div')
	newDiv.setAttribute('id','check')
	$('container').appendChild(newDiv)
	$('check').style.left=(Element.getDimensions('container').width-105)+'px'
	$('check').style.width='100px'
	$('check').className='toolcheck'
	$('check').style.top=(50)+'px'
	//$('check').innerHTML='<input type=checkbox checked id=checkASC onClick=setOfData()>ASC'
	//$('check').innerHTML+='<br><input type=checkbox onClick=setOfData() id=checkComunali>com. TO'
	$('check').innerHTML+='<br><input type=button onClick=getAPRS() id=checkWeather value=\'get APRS\'>'
	$('check').innerHTML+='<br><input type=checkbox  id=onlymeteo checked=on>meteo only'
	$('check').innerHTML+='<br><input type=input size=3 id=lastxhours value=24> hours'
}

function setHome(){
map.setCenter(new GLatLng(45.069,7.742),10);
}

function setOfData(){
map.clearOverlays()
if($('checkASC').checked==true) buildASC();
if($('checkComunali').checked==true) buildComunali();
}


function loading(how,message){
if(how=='on'){
$('loadingWindow').style.display='block'
if(message) $('loadingWindow').innerHTML=message
}
if(how=='off') $('loadingWindow').style.display='none'
}

function weather(){

getAPRS();
showAPRS();
}

//]]>
