function defaultHandler() {return false}
function silentHandler()  {return true}
function customHandler(desc,page,line,chr)  {
 return false;
}

window.onerror=customHandler;

function seoStr (sstr) {
	
	myq = sstr.toLowerCase();

    myq = myq.replace(/ /g,"_");
    myq = myq.replace(/\//g,"");
    myq = myq.replace(/\?/g,"");
    myq = myq.replace("#","");
	myq = myq.replace("'","");
	myq = myq.replace("\"","");
	
	myq = myq.replace(/á/gi,"a");
	myq = myq.replace(/é/gi,"e");
	myq = myq.replace(/í/gi,"i");
	myq = myq.replace(/ó/gi,"o");
	myq = myq.replace(/ú/gi,"u");

	myq = myq.replace(/à/gi,"a");
	myq = myq.replace(/è/gi,"e");
	myq = myq.replace(/ì/gi,"i");
	myq = myq.replace(/ò/gi,"o");
	myq = myq.replace(/ù/gi,"u");

	myq = myq.replace(/ã/gi,"a");
	myq = myq.replace(/õ/gi,"o");
	myq = myq.replace(/ç/gi,"c");

	myq = myq.replace(/â/gi,"a");
	myq = myq.replace(/ê/gi,"e");
	myq = myq.replace(/î/gi,"i");
	myq = myq.replace(/ô/gi,"o");
	myq = myq.replace(/û/gi,"u");

	myq = myq.replace(/á/gi,"a");
	myq = myq.replace(/é/gi,"e");
	myq = myq.replace(/í/gi,"i");
	myq = myq.replace(/ó/gi,"o");
	myq = myq.replace(/ú/gi,"u");

	myq = myq.replace(/º/gi,"");
	myq = myq.replace(/,/gi," ");
	myq = myq.replace(/ª/gi,"");
	myq = myq.replace(/\+/gi,"");
	//myq = myq.replace(/\-/gi,"");

	return myq
}

function mysearch() {

    var myq = document.getElementById("q").value;

	myq = seoStr(myq);

	if(typeof mySearchUrl=='undefined') mySearchUrl = 'pesquisa';
	
    window.location.href = "/"+mySearchUrl+"/"+ myq + "/";
	return false;
}

function maximizeMaps() {
	
	if (isMaximized==0){
		
		var myWidth = $(window).width()-4;
		var myHeight = $(window).height()-4;

		$('#ccc').css({'top':0,'left':0, 'width':myWidth, 'height':myHeight, 'position':'absolute','z-index':'100001'});
		$('.mymaps').css({'width':myWidth, 'height':myHeight});

		isMaximized = 1;
		mapBing.Resize(myWidth, myHeight);

	} else {		

		$('#ccc').css({'width':'720px', 'height':'400px', 'position':'relative','z-index':'1'});
		$('.mymaps').css({'width':'720px', 'height':'400px'});

		mapBing.Resize(720, 400);
		isMaximized = 0;
	}

	mapGoogle.checkResize();
	//myPano = new GStreetviewPanorama(document.getElementById("map_street"));

	if (showingMap == 3){
		myPano.checkResize()
	} else {
		myPano = new GStreetviewPanorama(document.getElementById("map_street"));
	}
	
	AddMyControls();
}


var mapGoogle   = null;
var mapBing     = null;
var mapStreet   = null;  
var mapStreetPano = null;
var control     = null;
var showingMap  = 1;
var googleShown = 0;
var bingShown   = 0;
var isMaximized = 0;
var mapLink     = '';

function showBingMap() {
	initializeBingMap();
	showingMap = 1;
	$('.maplink').removeClass('maplink_sel');
	$('#maplinklive').addClass('maplink_sel');
	$('#map_bing').show();
	$('#map_google,#map_street').hide();
	hideMapLink();
}

function showGoogleMap() {
	initializeGoogleMap();
	showingMap = 2;
	//map.SetMapStyle(VEMapStyle.Aerial);

	$('.maplink').removeClass('maplink_sel');
	$('#maplinkgoogle').addClass('maplink_sel');
	$('#map_google').show();
	$('#map_bing,#map_street').hide();

	panGoogle();
	mapGoogle.checkResize();
	hideMapLink();
}

function showStreetMap() {
	initializeGoogleMap();

	$('.maplink').removeClass('maplink_sel');
	$('#maplinkstreet').addClass('maplink_sel');
	$('#map_street').show();
	$('#map_bing,#map_google').hide();

	showingMap = 3;

	GEvent.addListener(myPano, "initialized", function (mlocation) {
		
			if (showingMap==3) {
				mapGoogle.panTo(mlocation.latlng);
			}
		});
	
	var point = mapGoogle.getCenter(); 
	var panoLat = point.lat();
	var panoLon = point.lng();


	var gps = new GLatLng(panoLat,panoLon);
	var pov = {yaw:sYaw,pitch:sPitch,zoom:sZoom};

	myPano.setLocationAndPOV(gps, pov);
	myPano.checkResize();

	
	
	hideMapLink();
}

function panGoogle() {

	if (mapGoogle!=null && showingMap==1){
		var str = ""+bingCoords();

		var virg = str.indexOf(",");
		var lat = (str.substring(0,virg));
		var lng = (str.substring(virg+2));
		
		//alert(lat+'#'+lng);
		var currentGps = new GLatLng(lat.substring(0,6), lng.substring(0,6));
		mapGoogle.panTo(currentGps);

		var pov = {yaw:0,pitch:0,zoom:0};

		myPano.setLocationAndPOV(currentGps, pov);
		myPano.checkResize();
	}
}

function bingCoords()  {

	//Check if in Birdseye or Oblique Map Style
	if (mapBing.GetMapStyle() == VEMapStyle.Birdseye || mapBing.GetMapStyle() == VEMapStyle.BirdseyeHybrid) {

		//IN Birdseye or Oblique Map Style

		//Get the BirdseyeScene being displayed
		var birdseyeScene = mapBing.GetBirdseyeScene();

		//Get approximate center coordinate of the map
		var x = birdseyeScene.GetWidth() / 4;
		var y = birdseyeScene.GetHeight() / 4;

		// Get the Lat/Long 
		var center = birdseyeScene.PixelToLatLong(new VEPixel(x,y), mapBing.GetZoomLevel());

		// Convert the BirdseyeScene LatLong to a normal LatLong we can use
		return (new _xy1).Decode(center);
	
	} else {
		
		// NOT in Birdseye or Oblique Map Style
		return mapBing.GetCenter();
	}
} 

var pinid = 1;

function bingPin() {
  var shape = new VEShape(VEShapeType.Pushpin, mapBing.GetCenter());
  //shape.SetTitle('My pushpin');
  //shape.SetDescription('This is shape number '+pinid);
  pinid++;
  mapBing.AddShape(shape);
}

function AddMyControls() {
	
	$('#mycontrols').css({'right': 0 });
	$('#mycontrols').show();

	$('#myActions').css({'right': -2, 'top':40 });
	$('#myActions').show();
}

function initializeBingMap() { 

	if (bingShown==1) return;
	bingShown=1;

	mapBing = new VEMap('map_bing'); 
	mapBing.LoadMap(new VELatLong(forceLat, forceLon), forceZoom ,'r' ,false);
	bingPin();
	
	mapBing.AttachEvent("onobliqueenter", bingBirdsEye);
	mapBing.AttachEvent("onendpan", panGoogle);

	window.setTimeout("bingBirdsEye();", 1000);
} 


function updateMapLink() {
	var point = mapGoogle.getCenter(); 
	var linkLat = point.lat();
	var linkLon = point.lng();

	if (showingMap == 1) {

		var linkZoom = mapBing.GetZoomLevel();
		var linkType  = 'bing';
		var linkOrientation  = 'North'//mapBing.getBirdseyeOrientation();

		var style = mapBing.GetMapStyle();

        if (style == 'b') {
			
			var be = mapBing.GetBirdseyeScene();
			linkOrientation = be.GetOrientation()
			linkType  = 'bird';
			linkZoom  = 25;
		}

	} else if (showingMap == 2) {

		var linkZoom = mapGoogle.getZoom();
		var linkType  = 'google';
		var linkOrientation  = 'North';

	} else {

		var linkZoom = mapGoogle.getZoom();
		var linkType  = 'street';
		pov = myPano.getPOV();
		var linkOrientation  = pov.yaw + 'x' + pov.pitch + 'x' + pov.zoom;
		//var panoLat = point.lat();
		//var panoLon = point.lng();
	}
	
	$('#mapLinkInput').val(forceUrl + linkLat + '/' + linkLon + '/' + linkZoom + '/' + linkType + '/' + linkOrientation + '/' );

}

function showMapLink(){
	updateMapLink();
	$('#mapLinkLayer').toggle();
}

function hideMapLink(){
	$('#mapLinkLayer').hide();
}

function initializeGoogleMap() {
  
	if (googleShown==1) return;
	googleShown = 1;

	if (GBrowserIsCompatible()) {
		
		mapGoogle = new GMap2(document.getElementById("map_google"));
		mapGoogle.setCenter(new GLatLng(forceLat, forceLon), forceZoom);

		var customUI = mapGoogle.getDefaultUI();
		customUI.controls.maptypecontrol  = false;
        mapGoogle.setUI(customUI);

		mapGoogle.setMapType(G_HYBRID_MAP );
		mapGoogle.addOverlay(new GMarker(new GLatLng(forceLat, forceLon)));

		var mapTypeControl = new GMenuMapTypeControl();
        var topLeft = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(100,8));
        mapGoogle.addControl(mapTypeControl, topLeft);
		
		mapStreet = new GStreetviewClient();
		myPano = new GStreetviewPanorama(document.getElementById("map_street"));
	    GEvent.addListener(myPano, "error", handleNoFlash);  

		GEvent.addListener(mapGoogle, "moveend", function () { 
			if (showingMap==2) {
				point = mapGoogle.getCenter(); 
				mapBing.PanToLatLong(new VELatLong(point.lat(), point.lng()))
	
				var pov = {yaw:sYaw,pitch:sPitch,zoom:sZoom};

				myPano.setLocationAndPOV(point, pov);
				myPano.checkResize();
			}
		});

		var publisher_id = 'pub-7070915893983548';

		var adsManagerOptions = {
		  maxAdsOnMap : 1,
		  style: 'adunit'
		  //,channel: '4912364201'  
		};

		adsManager = new GAdsManager(mapGoogle, publisher_id, adsManagerOptions);
		adsManager.enable();
	}
}

function showStreetData(streetData) {
  if (streetData.code != 200) {
	 
	//GLog.write('showPanoData: Server rejected with code: ' + streetData.code);
	return;
  }
  nextPanoId = streetData.links[0].panoId;
  myPano.setLocationAndPOV(streetData.location.latlng);
}

function handleNoFlash(errorCode) {
  if (errorCode == 603) {
	alert("Error: Flash doesn't appear to be supported by your browser");
	return;
  } else {
		
		
		if (showingMap==3) {
			showGoogleMap();
			alert('StreetView não está disponível para esta localização.');
		}
  }
}  
