var map = null;
var geocoder = null;

function teste()
{
	alert("asdasdsa");
}

function load(id) 
{
	if (typeof id == "undefined") {
      id = 0;
    }

	if (GBrowserIsCompatible()) 
	{
    	map = new GMap2(document.getElementById("map"), {draggableCursor: 'crosshair', draggingCursor: 'move'});
        map.setCenter(new GLatLng( 40.20877009281995, -8.426599502563477), 13);
        map.addControl(new GLargeMapControl(), new GControlPosition(G_ANCHOR_TOP_LEFT));
        map.addControl(new GMapTypeControl(), new GControlPosition(G_ANCHOR_TOP_RIGHT));
        map.setMapType(G_NORMAL_MAP);
        map.enableDoubleClickZoom();
        keyboardhandler = new GKeyboardHandler(map);
        geocoder = new GClientGeocoder();
		
		if (id>0) {
			showUser(id,'1');
		}
    }
}

function register_listener(marker,id)
{

	GEvent.addListener(marker, "click", function() { showUser(id, '1');	});
}
	
	

function showAddress(address, info, id, name, imagens, idbalao) 
{
	var my_tabs = "";
	overflow_type = "auto";
	map.clearOverlays(); 
	var turismocoimbra = (' \" ' + name + ' \" ');
	var o_click = ("<br/><a href='favoritos.php?ord=create&id=" + idbalao + "' target='favoritos'><img src='images/addfavs.png' width='50' height='21' alt='' border='0' /></a>");

	if (imagens.length != 1)
	{
		var content = "<div id=\"balao\" style=\"font-size:10px;width:220px;height:120px;overflow-x:hidden;overflow: "+ overflow_type +";\" >" + info + "</div>";
		var content_images = "<div style='width:220px;height:120px;overflow-x:hidden;overflow:" + overflow_type + ";' >" + imagens + "</div>";		
		my_tabs = [new GInfoWindowTab("INFO",content + o_click), new GInfoWindowTab("Imagens",content_images)];
		
	}
	else
	{
		var content = "<div id=\"balao\" style=\"font-size:10px;width:220px;height:120px;overflow-x:hidden;overflow: "+ overflow_type +";\" >" + info + "</div>";
		my_tabs = [new GInfoWindowTab("INFO",content + o_click)];
		<!--my_tabs = [new GInfoWindowTab("INFO",content)];-->
	}
	
	
	var opts = { maxWidth : 300 };
    if (geocoder) 
	{
		geocoder.getLatLng(address,
        	function(point) 
			{
            	if (!point) 
				{
					//alert("Morada Inexistente");
            	} 
				else 
				{
				
              		map.setCenter(new GLatLng(point.lat()+0.020000, point.lng()), 13);
              		var marker = new GMarker(point);

					register_listener(marker,idbalao);					
					
              		map.addOverlay(marker);					
//					marker.openInfoWindowTabsHtml(my_tabs);  
					map.openInfoWindowTabsHtml(point,my_tabs);

            	}
          	}
        );
	}
}

function createCookie(name,value,days) 
{
	if (days) 
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
	var fav = document.getElementById('favoritos');
	if (fav != null)
	{
		fav.contentWindow.location.reload(true);
	}

}

function eraseCookie(name) 
{
	createCookie(name,"",-1);
	var fav = document.getElementById('favoritos');
	fav.contentWindow.location.reload(true);
}

var xmlHttp;

function showUser(str, mode)
{ 

	xmlHttp = GetXmlHttpObject();
	if ( xmlHttp == null )
	{
 		alert ( "Browser does not support HTTP Request" );
 		return null;
 	}
	var url = "getponto.php";
	url = url + "?q=" + str;
	url = url + "&sid=" + Math.random();
	url = url + "&mode=" + mode;
	xmlHttp.onreadystatechange = stateChanged;
	xmlHttp.open ("GET", url, true);
	xmlHttp.send (null);
}


function stateChanged() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
 	{ 
 		//document.getElementById("txtHint").innerHTML=xmlHttp.responseText 
 		arr = xmlHttp.responseText.split("|");
 		showAddress(arr[0], arr[1], arr[2], arr[3], arr[4], arr[5]);
 		parent.frames["calculo"].location.href='calculo.php?q='+arr[2];
 
 	} 
}

function GetXmlHttpObject()
{
	var xmlHttp=null;
	try
 	{
 		// Firefox, Opera 8.0+, Safari
		xmlHttp = new XMLHttpRequest();
 	}
	catch (e)
 	{
 		//Internet Explorer
 		try
  		{
  			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
  		}
 		catch (e)
  		{
  			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
  		}
 	}
	return xmlHttp;
}

function MM_swapImgRestore() 
{
	var i,x,a=document.MM_sr; 
	for(i=0; a&&i<a.length&&(x=a[i])&&x.oSrc; i++)
	{
		x.src=x.oSrc;
	}
}

function MM_preloadImages() 
{
	var d = document; 
	if(d.images)
	{ 
		if(!d.MM_p) d.MM_p = new Array();
    	var i,j = d.MM_p.length, a = MM_preloadImages.arguments; 
		for(i=0; i<a.length; i++)
		{
    		if (a[i].indexOf("#") != 0)
			{ 
				d.MM_p[j] = new Image; 
				d.MM_p[j++].src = a[i];
			}
		}
	}
}

function MM_findObj(n, d) 
{
  	var p, i, x;
  	if ( !d )
  	{
  		d = document;
  	}
	if ( (p = n.indexOf("?")) > 0 && parent.frames.length ) 
  	{
  		d = parent.frames[n.substring(p+1)].document; 
		n = n.substring(0,p);
	}
  	if ( !(x=d[n]) && d.all )
	{
		x = d.all[n];
		for ( i=0; !x&&i < d.forms.length ; i++ )
		{
			x = d.forms[i][n];
		}
  		for ( i=0; !x&&d.layers && i < d.layers.length; i++ ) 
		{
			x = MM_findObj(n,d.layers[i].document);
		}
		if ( !x && d.getElementById ) 
		{
			x = d.getElementById(n); 
			return x;
		}
	}
}

function MM_swapImage() 
{
	var i, j = 0, x, a = MM_swapImage.arguments; 
	document.MM_sr = new Array; 
	for ( i=0; i < (a.length - 2); i += 3 )
	{
   		if ( (x = MM_findObj(a[i])) != null)
		{ 
			document.MM_sr[j++] = x; 
			if ( !x.oSrc )
			{
				x.oSrc = x.src; 
				x.src = a[i+2];
			}
		}
	}
}

function checkEmail(strEmail) 
{
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if ( !filter.test(strEmail) ) 
	{
		return false;
	}
	else
	{
		return true;
	}
}

