var fen1;
var infowindowContent = new Array();
var map;

function attachInfoWindow(marker, number){
	GEvent.addListener(marker, 'click', function() {
		map.openInfoWindow(marker.getLatLng(),infowindowContent[number]);
	});
}

function setMarkers(map, locations) {      
	for (var i = 0; i < locations.length; i++) {
		var note = locations[i];
		var myLatLng = new GLatLng(note[1], note[2]);
		var markerOptions;
		var marker;
      
		if (note[4] != '') {
			var picIcon = new GIcon(G_DEFAULT_ICON);
    		picIcon.image = "images/picture.png";
    		picIcon.iconSize = new GSize(16,16); 
    		picIcon.iconAnchor = new GPoint(0,16);
    		picIcon.shadowSize = new GSize(0,0);
    		picIcon.infoWindowAnchor = new GPoint(16,0);
    		marker = new GMarker(myLatLng, {icon: picIcon});
		} else {
			var favIcon = new GIcon(G_DEFAULT_ICON);
    		favIcon.image = "images/favicon.png";
    		favIcon.iconSize = new GSize(16,16);
    		favIcon.iconAnchor = new GPoint(0,16);  
    		favIcon.shadowSize = new GSize(0,0);
    		favIcon.infoWindowAnchor = new GPoint(16,0);
    		marker = new GMarker(myLatLng, {icon: favIcon});
		}

		var content = "<div style='width:180;'>"+ note[0] + "<div/>";
		if (note[4] != '')
			content += ("<div style='height:160px;margin-top:5px;'><img src='" + note[4] + "' style='height:160px;'/><div/>");     
      
		infowindowContent.push(content);      
		attachInfoWindow(marker, i);
		map.addOverlay(marker);
    }
}

function initialize() {
    var centerLatLng = new GLatLng(0,-5);
    var canvasDiv = document.getElementById("map_canvas");
    map = new GMap2(canvasDiv);
    map.setCenter(centerLatLng, 2, G_NORMAL_MAP);
    map.setUIToDefault();
    setMarkers(map, notes);
    setupDiv();   
}

function xFenster(eleId, iniX, iniY, barId, resBtnId, rssBtnId) {
    // Private Properties
    var me = this;
    var ele = xGetElementById(eleId);
    var rBtn = xGetElementById(resBtnId);
    var rssBtn = xGetElementById(rssBtnId);

    // Public Methods
    this.onunload = function() {
    	if (xIE4Up) { // clear cir refs
    		xDisableDrag(barId);
        	xDisableDrag(rBtn);
        	rssBtn.onclick = ele.onmousedown = null;
        	me = ele = rBtn = zBtn = null;
    	}
    }

    this.paint = function() {
    	xMoveTo(rBtn, xWidth(ele) - xWidth(rBtn) - 2, xHeight(ele) - xHeight(rBtn) - 2);
    	xMoveTo(rssBtn, 0, xHeight(ele) - xHeight(rssBtn) - 2);
    }

    // Private Event Listeners
    function barOnDrag(e, mdx, mdy) {
    	xMoveTo(ele, xLeft(ele) + mdx, xTop(ele) + mdy);
    }

    function resOnDrag(e, mdx, mdy) {
    	// the 4 subtraction is to account for the border width.  xWidth counts that as part of the item width   
    	xResizeTo(ele, xWidth(ele) + mdx - 4, xHeight(ele) + mdy - 4);
    	me.paint();
    }

    function fenOnMousedown() {
    	xZIndex(ele, xFenster.z++);
    }

    // ******************************* //
    // added function to handle zoom on bounds
    // with code taken from Mike Williams tutorial: http://www.econym.demon.co.uk/googlemaps/basic14.htm
    // ******************************* //
    function RssOnClick() {
		var upperRightPt = new GPoint(xLeft(ele)+xWidth(ele),xTop(ele)-133);
		var lowerLeftPt = new GPoint(xLeft(ele),xTop(ele)-133+xHeight(ele));
		var NElatLng = map.fromContainerPixelToLatLng(upperRightPt);
		var SWlatLng = map.fromContainerPixelToLatLng(lowerLeftPt);

        window.location ="RssFeed.php?N=" + NElatLng.lat() + "&S=" + SWlatLng.lat() +
        	"&E=" + NElatLng.lng() + "&W=" + SWlatLng.lng();
    }

    // Constructor Code
    xFenster.z++;
    this.paint();
	xLeft(ele, iniX);
	xTop(ele, iniY);
    xEnableDrag(barId, null, barOnDrag, null);
    xEnableDrag(rBtn, null, resOnDrag, null);

    rssBtn.onclick = RssOnClick;
    ele.onmousedown = fenOnMousedown;
    xShow(ele);
    ele.style.display = 'none';
	ele.style.visibility = 'hidden';
    
} // end xFenster object prototype

xFenster.z = 1000; // xFenster static property
      
function setupDiv() {
	fen1 = new xFenster('zoomWindow', 400, 400, 'zoomWindow', 'resizeBtn', 'rssBtn');
}
  
function ToggleDisplay(id) {
  	var elem = document.getElementById(id);
  	if (elem) {
  		if (elem.style.display != 'block') {
  			elem.style.display = 'block';
  			elem.style.visibility = 'visible';
  		} else {
  			elem.style.display = 'none';
  			elem.style.visibility = 'hidden';
  		}
  	}
}