
var winProps = 'location=0,status=0,scrollbars=0,width=700,height=500';
var tiscoverProps = 'location=1, status=1, scrollbars=1,width=800, height=600,resizable=1';

/****************** Guide Page ******************/
function createNode(type, value){
	node = document.createElement(type);
	node.innerHTML = value;
	return node;
}

function createInfoNode(title, description){
	container = document.createElement("div");
	container.appendChild(createNode("h4", title));

	var desc = createNode("div", description);
	container.appendChild(desc);
	return container;
}

function objPOI(key, title, description, level, idealLevel, icon, icon_width, icon_height, longitude, latitude,url,newwin,popupBalloon){
	var me = this;
	var title = title;
	var description = description;
	var icon = icon;
	var longitude = longitude;
	var latitude = latitude;

	var point = new GLatLng(latitude, longitude);

	me.key = key;
	me.level = level;
	me.idealLevel = idealLevel;

	if ( icon != null && icon != '') {
		var iconMarker = new GIcon(baseIcon);
	  	iconMarker.image = icon;
	  	iconMarker.title = title;
	  	if ( icon_width != '' && icon_height != '' ) {
	  	  iconMarker.iconSize = new GSize(icon_width, icon_height);
	  	}
		me.marker = new GMarker(point, {icon: iconMarker, title: title});
	} else {
		me.marker = new GMarker(point, {title: title});
	}


	var infoNode;
	if ( popupBalloon != null && popupBalloon != '' ) {
	  infoNode = createInfoNode(title, description);
	}

	var props = winProps;
	if ( icon.indexOf('hotel') > -1 ) { props = tiscoverProps; }

	GEvent.addListener(
	  me.marker, "click", function () {
	    if ( popupBalloon != null && popupBalloon != '' ) {
	      me.marker.openInfoWindowHtml(infoNode, {maxWidth : '250'});
	    } else {
	      if ( newwin ) {
	        var w=window.open(url,'newwin',props); w.focus();
	      } else {
	        window.location=url;
	      }
	    }
	  })

	if ( popupBalloon != null && popupBalloon != '' ) {
	    me.showInfo = function(){
	      me.marker.openInfoWindowHtml(infoNode);
	    }
	}
	
        map.addOverlay(me.marker);

	me.show = function(){
//		map.setZoom(me.idealLevel);
		map.setCenter(point);
	if ( popupBalloon != null && popupBalloon != '' ) {
		me.marker.openInfoWindowHtml(infoNode);
          }
	}
}

function showMarkers(oldLvl, newLvl){
	map.clearOverlays();

	pois.iterate( function(poi){
		if (poi.level <= map.getZoom()){
			map.addOverlay(poi.marker);
		}
	});
}

/****************** Morello InSitu Editing ******************/
function createDraggableMarker(point, showInfo) {
	var args = { 
		title: "Test",
		draggable: true
	};

	var marker = new GMarker(point, args);

	GEvent.addListener(marker, "dragend", function() {
		var point = marker.getPoint();
		map.setCenter(point);
		updateMSLatLng(point);
	});

	if (showInfo){
		GEvent.addListener(marker, "click", function() {
			info.style.display = "block";
			marker.openInfoWindowHtml(info);
		});
	}

	return marker;
}

function geocodePostcode(postcode){
    var s = document.createElement('script');
    s.src = 'http://geo.localsearchmaps.com/?cb=setPostcodePoint&use=google2&country=uk&zip=' + postcode;
    s.type = 'text/javascript';
    document.getElementsByTagName('head')[0].appendChild(s);
}

function setPostcodePoint(lon, lat){
	point = new GLatLng(lon, lat)
    map.setCenter(point);
	marker.setPoint(point);
	updateMSLatLng(point);
}

/****************** Common ******************/
function setupMap(map) {
	if (GBrowserIsCompatible()) { 
		if (isSet(longitude) && isSet(latitude)){
			map.setCenter(new GLatLng(latitude, longitude), zoom);
		}else{
			map.setCenter(new GLatLng(stLat, stLong), zoom);		
		}
		ORIGINAL_PZ_CONTROL = new GSmallMapControl();
		map.addControl(ORIGINAL_PZ_CONTROL);
		ORIGINAL_TYPE_CONTROL = new GMapTypeControl();
		map.addControl(ORIGINAL_TYPE_CONTROL);
	}
	var currZoom = map.getZoom();
	if ( currZoom < zoom ) {
	  for ( var i=currZoom; i<zoom; i++ ) { map.zoomIn(); }
	} else {
	  for ( var i=currZoom; i>zoom; i-- ) { map.zoomOut(); }
	}
}

var ORIGINAL_PZ_CONTROL;
var ORIGINAL_TYPE_CONTROL;

var baseIcon = new GIcon();
baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
baseIcon.iconSize = new GSize(14, 30);
baseIcon.shadowSize = new GSize(25, 30);
baseIcon.iconAnchor = new GPoint(7, 30);
baseIcon.infoWindowAnchor = new GPoint(9, 2);
baseIcon.infoShadowAnchor = new GPoint(11, 23);
