var map = null;
var mgr = null;
var icon = null; 
var icon2 = null; 
var icon_add = null; 
var geocoder = null;
var curpoint = null; 
var iniLat = 21.375720318880305;
var iniLng = -157.91404724121094;
var iniZoom = 10;

function showTap(target) {
	var total = 4; 
	document.getElementById('tab'+target).className = 'tabShow';
//	document.getElementById('tab'+target).class = 'tabShow';
	document.getElementById('tabContent'+target).style.display = "block";
	for (var i = 1; i <= total; i++) {
		if (i!=target) {
			document.getElementById('tab'+i).className = 'tabHide';
//			document.getElementById('tab'+i).class = 'tabHide';
			document.getElementById('tabContent'+i).style.display = 'none';
		}
	}

}

function ShowMeTheMap() {
	Resize();
	pointIni = new GLatLng(iniLat,iniLng);
	newGMap2(pointIni,iniZoom,G_HYBRID_MAP);
}

function Resize() {
    nav = navigator.userAgent;
    if (document.all) {
      	document.getElementById('map').style.height = document.documentElement.clientHeight;
      	document.getElementById('sliderContent').style.height = document.documentElement.clientHeight;
    }
    else {
      	document.getElementById('map').style.height = '100%';
      	document.getElementById('sliderContent').style.height = '100%';
    }
    if (window.opera) {
      	document.getElementById('map').style.height = '100%';
      	document.getElementById('sliderContent').style.height = '100%';
    }
}

function newGMap2 (point,zoom,mapType) {
	document.getElementById('map').innerHTML = '';
	map = null;
	map = new GMap2(document.getElementById("map"));
	map.setCenter(point,zoom);
	map.addControl(new GLargeMapControl());
	map.addControl(new GMapTypeControl());
	map.addControl(new GOverviewMapControl())
	map.addControl(new GScaleControl());
	map.enableScrollWheelZoom();
	
	icon = new GIcon();
	icon.image = "images/marker.png";
	icon.shadow = "images/shade.png";
	icon.iconSize = new GSize(29, 29);
	icon.shadowSize = new GSize(37, 29);
	icon.iconAnchor = new GPoint(15, 29);
	icon.infoWindowAnchor = new GPoint(14, 32);
	
	icon2 = new GIcon();
	icon2.image = "images/marker_new.gif";
	icon2.shadow = "images/shade.png";
	icon2.iconSize = new GSize(29, 29);
	icon2.shadowSize = new GSize(37, 29);
	icon2.iconAnchor = new GPoint(15, 29);
	icon2.infoWindowAnchor = new GPoint(14, 32);
	
	geocoder = new GClientGeocoder();
	mgr = new GMarkerManager(map);

	/*
	infoPanel = new YAHOO.widget.Panel("infoPanel", { width:"200px", visible:true, constraintoviewport:true } );
	infoPanel.render();
	YAHOO.util.Event.addListener("btnShowPanel", "click", infoPanel.show, infoPanel, true);

	//loaddata();
	//temp
	GEvent.addListener(map, "zoomend", function() {
		var zoomlevel = map.getZoom();
  		document.getElementById("msg").innerHTML = zoomlevel.toString();
	});
	
	document.getElementById("msg").innerHTML = document.documentElement.clientHeight;
*/
}

function addGMap2 () {
	icon_add = new GIcon();
	icon_add.image = "images/marker_add.png";
	icon_add.shadow = "images/shade.png";
	icon_add.iconSize = new GSize(33, 29);
	icon_add.shadowSize = new GSize(37, 29);
	icon_add.iconAnchor = new GPoint(15, 29);
	icon_add.infoWindowAnchor = new GPoint(14, 32);

	var contextmenu = document.createElement("div");
	contextmenu.style.visibility="hidden";
	contextmenu.style.background="#ffffff";
	contextmenu.style.border="1px solid #8888FF";
	
	contextmenu.innerHTML = '<a href="javascript:addMarkerHere()"><div class="context">&nbsp;&nbsp;Add marker here&nbsp;&nbsp;</div></a>';
	
	map.getContainer().appendChild(contextmenu);
	
	// === listen for singlerightclick ===
	GEvent.addListener(map,"singlerightclick",function(pixel,tile) {
		// store the "pixel" info in case we need it later
		// adjust the context menu location if near an egde
		// create a GControlPosition
		// apply it to the context menu, and make the context menu visible
		clickedPixel = pixel;
		var x=pixel.x;
		var y=pixel.y;
		if (x > map.getSize().width - 120) { x = map.getSize().width - 120 }
		if (y > map.getSize().height - 100) { y = map.getSize().height - 100 }
		var pos = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(x,y));  
		pos.apply(contextmenu);
		contextmenu.style.visibility = "visible";
	});
	
	function zoomInHere() {
		var point = map.fromContainerPixelToLatLng(clickedPixel)
		map.zoomIn(point,true);
		contextmenu.style.visibility="hidden";
	}      
	
	GEvent.addListener(map, "click", function() {
		contextmenu.style.visibility="hidden";
	});
	
}

function addMarker() {
	var point = map.fromContainerPixelToLatLng(new GPoint(130,  65));
	//var point = map.getCenter();
	//alert(point);
}

function addMarkerInfo(point) {
	var info = "hi hi";
    //var marker = new GMarker(point, icon);
    var marker = new GMarker(point, {icon: icon_add, draggable: true});
	createEditableMarker(marker, info);
	mgr.addMarker(marker, 1);
	//marker.openInfoWindowHtml(info);
}

function createMarker(point, info, title, type) {
	  if (type==2) {
      	var marker = new GMarker(point, {icon: icon2, title: title});
	  } else {
      	var marker = new GMarker(point, {icon: icon, title: title});
	  }
      GEvent.addListener(marker, "click", function() {
        marker.openInfoWindowHtml(info);
      });
    return marker;
}

function createEditableMarker(marker, info) {
	  
 	GEvent.addListener(marker, "click", function() {
	  	marker.openInfoWindowHtml(info);
  	});
	  
	GEvent.addListener(marker, "dragstart", function() {
		map.closeInfoWindow();
		curpoint = marker.getPoint(); 
	});
		
	GEvent.addListener(marker, "dragend", function() {
	  	marker.openInfoWindowHtml(info);
		//var answer = confirm ("Are you sure to move the marker?"); 
		marker.setPoint(curpoint);
		marker.openInfoWindowHtml(info);
	});
	  
}

function showAddress(address, level) {
	if (level==0) {
		level = 14; 
	}
    if (geocoder) {
        geocoder.getLatLng(
          address,
          function(point) {
            if (!point) {
              alert(address + " not found");
            } else {
				//document.getElementById("msg").innerHTML = point.toString();
              map.setCenter(point, level);
              //var marker = new GMarker(point, icon);
              //map.addOverlay(marker);
              //marker.openInfoWindowHtml(address);
            }
          }
        );
    }
}

function ValidateNumber(strString){
	var strValidChars = "-.0123456789"; 
	var strChar;
	var blnResult = true;
	
	for (i = 0; i < strString.length && blnResult == true; i++) {
		strChar = strString.charAt(i);
		if (strValidChars.indexOf(strChar) == -1) {
			blnResult = false;
		}
	}
	
	return blnResult;
}
