

    //<![CDATA[
        function DGMap() {
			
                    this.map = new GMap2($("map"));
					
                if (GBrowserIsCompatible()) {
                        this.map = new google.maps.Map2($("map"));
                        this.map.setMapType(G_NORMAL_MAP);
                        x = getCookie('dgMapX');
      
						 center = new google.maps.LatLng(-31.946711, 115.853447);
						        this.map.addControl(new GSmallMapControl());
                        // zp = getCookie('dgMapZoom');
						zp = 13;
                        if (zp) {
                                myZp = zp;
                        } else {
                                myZp = 18;
                                document.cookie = "dgMapZoom="+myZp;
                        }
                        this.zoomLevel = myZp;
                        this.map.setCenter(center, myZp);
                  //      this.zoomTo(myZp);
                        var currentPoint = this.map.getCenter().lat();
                 //       this.map.openInfoWindow(this.map.getCenterLatLng(), document.createTextNode("To get started, drag the map around and see what works you can find."));

                        this.currentPos = currentPoint;
				//		alert(currentPoint);
                         
                        var soaphead = '<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns="http://beta.dgmap.concrete.org.au"><SOAP-ENV:Body>';
                        
                        this.soaphead = soaphead;
                        
                        var soaptail = '</SOAP-ENV:Body></SOAP-ENV:Envelope>';
                        this.soaptail = soaptail;
                        
                        var soaphost = "http://www.artrage.com.au/dgsoap";
                        this.soaphost = soaphost;
                        this.updatePos();

                        
                        GEvent.bind(this.map,"moveend",this,this.updatePos);
                        
                        GEvent.bind(this.map,"zoom",this,this.updatePos);
                        
                        GEvent.bind(this.map,"move",this,this.crossHair);
                        

                        // deal with URL state here
                        this.parseRequestState();
                        
                } 
        }
    
        DGMap.prototype.resetView = function () {
                this.map.centerAndZoom(new GPoint(115.84808349609375, -31.982453066235752),7);
        }
    
        DGMap.prototype.parseRequestState = function () {
                        var reqUri = document.location.href;
                        var reqAnchor = reqUri.lastIndexOf('#');
                        if (reqAnchor) {
                                        var reqState = reqUri.substr((reqAnchor + 1),reqUri.length).split(':');
                                for (var i = 0; i < reqState.length; i++) {
                                        if (reqState[i].match("=")) {
                                                var reqString = reqState[i].split("=");
                                                reqArg = reqString[0];
                                                reqVal = reqString[1];
                                        } else {
                                                reqArg = reqState[i];
                                                reqVal = null;
                                        }
                               //         alert("running "+reqArg+" with "+reqVal);
                                        switch(reqArg) {
                                                case "submit" : this.goSubmit(); break;
                                                case "work" : this.loadWork(reqVal);
                                        }
                                }
                        }
        }
    
        DGMap.prototype.updatePos = function() {
        

                this.checkLimits();
                
                coord = $('coords');
                if (coord) {
                       gc = $("withinscope").removeChild(coord); 
                        gc = null;
                }
                var newpos = this.map.getCenter();
                document.cookie = "dgMapX="+newpos.x;
                document.cookie = "dgMapY="+newpos.y;
                this.currentPos = newpos;                
                
                this.buildMarkers();
                
                
        }
 
        DGMap.prototype.checkLimits = function () {
                
                var bounds = this.map.getBounds();
                if ((bounds.minX < 115) ||  (bounds.minY > -32) || (bounds.maxX > 118) || (bounds.maxY < -30)) {
//                        alert("Sorry, this project only exists within the bounds of Metropolitan Perth. Nothing to see out here, move along please.");
  //                      this.map.recenterOrPanToLatLng(this.currentPos);
                }
        }
 
        DGMap.prototype.crossHair = function () {
                if (this.centerMarker) {

                        this.map.removeOverlay(this.centerMarker);
                        this.centerMarker = null;
                        if ($('streetForm')) {
                                this.centerMarker = new GMarker(this.map.getCenter(),crosshair);
                                this.map.addOverlay(this.centerMarker);   
                        
                                this.map.panTo(this.map.getCenter());
                        }
                } 
        }
        
        function getCookie(name) {
          var dc = document.cookie;
          var prefix = name + "=";
          var begin = dc.indexOf("; " + prefix);
          if (begin == -1) {
            begin = dc.indexOf(prefix);
            if (begin != 0) return null;
          } else
            begin += 2;
          var end = document.cookie.indexOf(";", begin);
          if (end == -1)
            end = dc.length;
          return unescape(dc.substring(begin + prefix.length, end));
        } 

        DGMap.prototype.readDOMValue = function (val,docEl) {
                vals = docEl.getElementsByTagName(val);
                
                if (vals.length) {
                        if (vals[0].firstChild) {
                                value = vals[0].firstChild.nodeValue;
                        } else {
                                return false;                  
                        }
                        return value;
                } else {
                        return false;
                }
        }
    
    
        DGMap.prototype.createMarker = function (point,title) {
                 // $log("setting marker at "+point)
                var marker = new GMarker(point,this.icon);
                this.map.addOverlay(marker);
                GEvent.addListener(marker, "click", function() {
                	marker.openInfoWindowHtml(title);
                });
        }

    
        DGMap.prototype.handleMarkerResponse = function (response) {

            if (response.getElementsByTagName('SOAP-ENV:Fault').size) {
            	// $log("Fault");
            } else {
				// $log("Handling marker response");
            this.titles = new Array();
            this.points = new Array();
            var markers = response.documentElement.getElementsByTagName("item");
			// $log("got "+markers.length+" markers");
            for (var i = 0; i < markers.length; i++) {
                // $log('checking result '+i);
            	lng = parseFloat(this.readDOMValue("lng",markers[i]));
            	lat = parseFloat(this.readDOMValue("lat",markers[i]));
            	title = this.readDOMValue("title",markers[i]);
                locationid = this.readDOMValue("locationid",markers[i]);
                point = new GPoint(lat,lng);
				
                if (point) {
                 
 				  // $log("setting marker for "+locationid+" at "+point)
                  // info("title of piece is "+title);
                  this.titles[locationid] = title;
                  this.points[locationid] = point;
                  var marker = createMarker(point,i,title,locationid);
                  this.map.addOverlay(marker);  
                } else {
                        // $log("bad point");
                }
            }
            // info('all markers set from response');
               
            }

        }


function createMarker(point,i,title,locationid) {
  var marker = new GMarker(point,dgmap.icon);

  // Show this marker's index in the info window when it is clicked
  this.title = title;
  
  var html = "<div class='popupInfo'><h1 class='title'>"+title+"</h1><p><a href='"+Papercut.baseHref+"festival/location/"+locationid+"/'>Go</a></p>";
  GEvent.addListener(marker, "click", function() {
	$("mapInfo").update(html);
  });

  return marker;
}
       
    DGMap.prototype.postVenue = function () {
                workEl = $('venueCoords');
                var x = parseFloat($('XCoord').firstChild.nodeValue);
                var y = parseFloat($('YCoord').firstChild.nodeValue);
				// $log("setting coord to "+x+" "+y);
				var myPoint = new GLatLng(y,x);
				this.map.panTo(myPoint);
				this.map.setZoom(18);
    }
    
    
    DGMap.prototype.buildInfoBox = function (title,id) {
        return "<p>"+title+" at "+id+"</p>";
    }
    
    DGMap.prototype.buildMarkers = function () {
	// alert("building markers");
        var icon = new GIcon();
        this.icon = icon;
        this.icon.image = "http://www.artrage.com.au/images/icons/icon.png";
        this.icon.shadow = "http://www.artrage.com.au/images/icons/shadow.png";
        this.icon.transparent = "http://www.artrage.com.au/images/icons/ie.png";

        this.icon.iconSize = new GSize(21, 22);
        this.icon.shadowSize = new GSize(32, 22);
        this.icon.iconAnchor = new GPoint(6, 20);
        this.icon.infoWindowAnchor = new GPoint(5, 1);
                // ajax request to get works within scope
        var request = new ajaxRequest();
        request.open("POST", this.soaphost, true);

        request.onreadystatechange = function() {
                  if (request.readyState == 4) {
                        dgmap.handleMarkerResponse(request.responseXML); 
                  }
        }
        var bounds = this.map.getBounds();
        icon = this.icon;
        
        xmlqry = this.soaphead+'<tns:getWorksInMapScope xmlns:tns="http://www.artrage.com.au/"><scope xsi:type="tns:Scope"><minx xsi:type="xsd:decimal">'+bounds.getSouthWest().lng()+'</minx><maxx xsi:type="xsd:decimal">'+bounds.getNorthEast().lng()+'</maxx><miny xsi:type="xsd:decimal">'+bounds.getSouthWest().lat()+'</miny><maxy xsi:type="xsd:decimal">'+bounds.getNorthEast().lat()+'</maxy><zp xsi:type="xsd:int">'+this.zoomLevel+'</zp></scope></tns:getWorksInMapScope>'+this.soaptail;
		// $log(xmlqry);
        request.setRequestHeader("Content-Type", "text/xml");
        request.setRequestHeader("SOAPAction", "http://www.artrage.com.au//dgsoap/getWorksInMapScope");
 //       alert('sending query'); 
        request.send(xmlqry);      
 //       alert('query sent');
        
    }
    
    DGMap.prototype.panTo = function (compass) {
        currentPoint = this.map.getCenter();
        viewport = this.map.getBounds();
        
        x = currentPoint.x;
        y = currentPoint.y;
        // alert(viewport);
        xtick = x - viewport.minX;
        ytick = y - viewport.minY;
       // window.alert(xtick+' and '+ytick);
        switch (compass) {
                case 'n' : 
                        x = x;
                        y = y + ytick;
                        break;
                case 'nw' :
                        x = x - xtick;
                        y = y + ytick;
                        break;
                case 'ne' :
                        x = x + xtick;
                        y = y + ytick;
                        break;
                case 'nw' :
                        x = x - xtick;
                        y = y + ytick;
                        break;
                case 's' :
                        x = x;
                        y = y - ytick;
                        break;
                case 'sw' :
                        x = x - xtick;
                        y = y - ytick;
                        break;
                case 'se' :
                        x = x + xtick;
                        y = y - ytick;
                        break;
                case 'e' :
                        x = x + xtick;
                        y = y;
                        break;
                case 'w' :
                        x = x - xtick;
                        y = y;
                        break;
        }
        
        dest = new GPoint(x,y);
        this.map.panTo(dest);
    }

	DGMap.prototype.slideTo = function (x,y,submit) {
		newDest = new GPoint(x,y);
		this.map.panTo(newDest);

		return false;
	}
	
	
	DGMap.prototype.zoomTo = function (zoomLevel) {

	       if (zoomLevel == 'in') {
	               zoomLevel = this.zoomLevel - 1;
	       } else if (zoomLevel == 'out') {
	               zoomLevel = this.zoomLevel + 1;
	       }
	       if (zoomLevel > 8) {
	               zoomLevel = 8;
	       } else if (zoomLevel < 1) {
	               zoomLevel = 1;
	       }

	       var zId = zoomLevel;

	       this.zoomLevel = zoomLevel;
	       
	       if (zoomLevel == 1) {
	               zoomLevel = 0;
	       }
	       this.map.zoomTo(zoomLevel);
	       document.cookie = "dgMapZoom="+zId;
	}
	
	DGMap.prototype.showTag = function (tag) {
                // info("running buildstreets");
                var tagResults = document.getElementById('tagResults');
                if (tagResults) {
                        tagResults.style.display = "block";
                        tagResults.innerHTML = "<strong>searching for tag "+tag+"...</strong>";
                    	tagResults.appendChild(logoimg);
                }
                tagReq = new ajaxRequest();
                tagReq.open("GET", "http://www.artrage.com.au//tag/"+tag, true);
                tagReq.onreadystatechange = function() {
                          if (tagReq.readyState == 4) {
                                document.getElementById('tagResults').innerHTML = tagReq.responseText;
                          }
                }
                tagReq.send(null);      
                return false;
	}
	
	/**
   Sends an asynchronous request to a url and alerts on state change (known in the
   buzzworld as AJAX 
*/
function ajaxRequest()
{
        // code for Mozilla, etc.
        if (window.XMLHttpRequest)
          {
          xmlhttp=new XMLHttpRequest()
          }
        // code for IE
        else if (window.ActiveXObject)
          {
          xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
          }

        return xmlhttp;
}


 //]]>
