// Google Maps for XGear 

var baseIcon;
var baseRegX = 50;
var baseRegY = 60;
var baseReg2X = 60;
var baseReg2Y = 60;
var baseWideRegX = 80;
var baseWideRegY = 42;
var baseWideReg2X = 86;
var baseWideReg2Y = 31;
var point1 = new GLatLng();
var point2 = new GLatLng();
var point3 = new GLatLng();
var point4 = new GLatLng();
var pcountArray = new Array(0,0,0,0);
var dirArray = new Array("_top", "_bot", "_right", "_left");
var equalpt;



function hideMap()
{
  var elem, vis;
  if( document.getElementById ) 
    elem = document.getElementById( "map" );
  else if( document.all )
      elem = document.all["map"];
  else if( document.layers )
    elem = document.layers["map"];
  vis = elem.style;
  vis.display = "none";
}


function showMap()
{
  var elem, vis;
  if( document.getElementById )
    elem = document.getElementById( "map" );
  else if( document.all ) 
      elem = document.all["map"];
  else if( document.layers ) 
    elem = document.layers["map"];
  vis = elem.style;
  vis.display = "block";
}


function genMap(storeName1, storeName2, storeName3, storeName4,
								lat1, lat2, lat3, lat4, long1, long2, long3, long4,
								address1, address2, address3, address4,
								ico1, ico2, ico3, ico4 ,
								icotype1, icotype2, icotype3, icotype4 ) {
	if (GBrowserIsCompatible()) {
		var bounds = new GLatLngBounds();
		var storeName;
		var abbrev;
		var latitude;
		var longitude;
		var point;
		var count = 0;
		pcountArray = new Array(0,0,0,0);
		point1 = new GLatLng();
		point2 = new GLatLng();
		point3 = new GLatLng();
		point4 = new GLatLng();
		
		showMap();
	
		var map = new GMap2(document.getElementById("map"));
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
		map.setCenter(new GLatLng(0, 0), 0);

		// Create a base icon all markers -- changes depending on store type
		baseIcon = new GIcon();
		baseIcon.iconAnchor = new GPoint(9, 34);
		baseIcon.infoWindowAnchor = new GPoint(9, 2);
		
		// generate + display the 4 store markers
		if(storeName1 != ""){
			genMarker(map, bounds, 1, storeName1, lat1, long1, address1, ico1, icotype1);
			point1 = new GLatLng(lat1, long1); 
		}
		if(storeName2 != ""){
			genMarker(map, bounds, 2, storeName2, lat2, long2, address2, ico2, icotype2);
			point2 = new GLatLng(lat2, long2); 
		}
		if(storeName3 != ""){
			genMarker(map, bounds, 3, storeName3, lat3, long3, address3, ico3, icotype3);
			point3 = new GLatLng(lat3, long3); 
		}
		if(storeName4 != ""){
			genMarker(map, bounds, 4, storeName4, lat4, long4, address4, ico4, icotype4);
			point4 = new GLatLng(lat4, long4); 
		}
		
		map.setZoom(map.getBoundsZoomLevel(bounds)-1);
		map.setCenter(bounds.getCenter());
	}
}


function genMarker(map, bounds, count, storeName, latitude, longitude, address, ico, icotype){
	point = new GLatLng(latitude, longitude);
	bounds.extend(point);
	map.addOverlay(displayMarker(point, count, ico, storeName + "<br>" + address, icotype));	
}


function displayMarker(point, index, letter, address, icotype) {
	var icox;
	var icoy;
	var anchx;
	var anchy;
	var dir;
	
	if(icotype == "wide"){
		icox = baseWideRegX;
		icoy = baseWideRegY;
		anchx = baseWideRegX/2;
		anchy = baseWideRegY;
	} else {
		icox = baseRegX;
		icoy = baseRegY;
		anchx = baseRegX/2;
		anchy = baseRegY;
	}
	
	dir = "_top";

	if(checkPointsWith(point)){
		dir = dirArray[ pcountArray[equalpt-1] ];

		if(icotype == "wide"){
			
			if(dir=="_bot"){
				anchy -= icoy;
			} else if(dir=="_right"){
				icox = baseWideReg2X;
				icoy = baseWideReg2Y;
				anchx = baseWideRegX/2;
				anchy = baseWideRegY;
				anchx -= icox/2; 
				anchy -= icoy/2;
			} else {
				icox = baseWideReg2X;
				icoy = baseWideReg2Y;
				anchx = baseWideRegX/2;
				anchy = baseWideRegY;
				anchx += icox/2; 
				anchy -= icoy/2;
			}
			
		} else {
			
			if(dir=="_bot"){
				anchy -= icoy;
			} else if(dir=="_right"){
				icox = baseReg2X;
				icoy = baseReg2Y;
				anchx = baseRegX/2;
				anchy = baseRegY;
				anchx -= icox/2; 
				anchy -= icoy/2;
			} else {
				icox = baseReg2X;
				icoy = baseReg2Y;
				anchx = baseRegX/2;
				anchy = baseRegY;
				anchx += icox/2; 
				anchy -= icoy/2;
			}
			
		}
		
	}

	baseIcon.iconSize = new GSize(icox, icoy);
	baseIcon.iconAnchor = new GPoint(anchx, anchy);
  baseIcon.infoWindowAnchor = new GPoint(anchx, 2);
	
	var icon = new GIcon(baseIcon);  
	icon.image = "images/icons/" + letter + dir + ".png";  
	
	var marker = new GMarker(point, icon);  
	GEvent.addListener(marker, "click", function() { marker.openInfoWindowHtml("<b>" + address + "</b>"); });  
	return marker;
}

function arePtEq(pn1, pn2){
	return ((pn1.x==pn2.x) && (pn1.y==pn2.y));
}

function checkPointsWith(p){
	if( arePtEq(p, point1) ){
		equalpt = 1;
		pcountArray[0]++;
		return true;
	} else if( arePtEq(p, point2) ){
		equalpt = 2;
		pcountArray[1]++;
		return true;
	} else if( arePtEq(p, point3) ){
		equalpt = 3;
		pcountArray[2]++;
		return true;
	} else if( arePtEq(p, point4) ){
		equalpt = 4;
		pcountArray[3]++;
		return true;
	} else {
		return false;
	}
}
