//Created for Stott Outdoor Advertising
//Copyright 2008 by Erickson Technology Consulting
//Requires that the Google Maps API is loaded before it.
//Future ideas:
//	1.) Add display of current location via getLocations()
var fileFolder="/stottmap/";function loadLocPhotos(num){GDownloadUrl("/locations/imgxml.php?num="+num,function(xmlData){var output="";var xml=GXml.parse(xmlData);var xmlPhotos=xml.documentElement.getElementsByTagName("photo");for(var i=0;i<xmlPhotos.length;i++){output+="<a href='"+xmlPhotos[i].getAttribute("thumb")+"'";output+=" onclick='return GB_showImage(\""+xmlPhotos[i].getAttribute("title")+" Facing \",this.href);'>";output+=xmlPhotos[i].getAttribute("title")+" facing";output+="</a> "}if(xmlPhotos.length==0){output="No Photos Available"}document.getElementById('xml_location_photo').innerHTML=output})}function load(){if(GBrowserIsCompatible()){var map;var mapControl;var prevzoom_control;var prevzoom_center;var prevzoom_zoom;var markerWindow;var existingMarkers=[];var clusterMarkers=[];var lnkHelp=document.getElementById("lnkhelp");lnkHelp.onclick=function(){GB_showCenter('Stott Outdoor Advertising Location Map Help','/stottmap/maphelp.html');return false};map=new GMap2(document.getElementById("map"));map.setCenter(new GLatLng(39.744,-121.823),6);map.addControl(new GOverviewMapControl());map.addControl(new GScaleControl());new GKeyboardHandler(map);var iconBus=new GIcon();iconBus.image=fileFolder+"busicon.png";iconBus.iconSize=new GSize(54,14);iconBus.iconAnchor=new GPoint(26,7);iconBus.infoWindowAnchor=new GPoint(60,20);var iconCluster=new GIcon();iconCluster.image=fileFolder+"Multiple.png";iconCluster.iconSize=new GSize(35,27);iconCluster.iconAnchor=new GPoint(17,27);iconCluster.infoWindowAnchor=new GPoint(17,0);var iconBulletin=new GIcon();iconBulletin.image=fileFolder+"Bulletin-sm.png";iconBulletin.shadow=fileFolder+"Bulletin-sm-shadow.png";iconBulletin.iconSize=new GSize(26,27);iconBulletin.shadowSize=new GSize(38,27);iconBulletin.iconAnchor=new GPoint(13,27);iconBulletin.infoWindowAnchor=new GPoint(17,0);var iconPoster=new GIcon();iconPoster.image=fileFolder+"Poster-sm.png";iconPoster.shadow=fileFolder+"Poster-sm-shadow.png";iconPoster.iconSize=new GSize(18,18);iconPoster.shadowSize=new GSize(26,18);iconPoster.iconAnchor=new GPoint(9,18);iconPoster.infoWindowAnchor=new GPoint(17,0);var iconJuniorPoster=new GIcon();iconJuniorPoster.image=fileFolder+"JuniorPoster-sm.png";iconJuniorPoster.shadow=fileFolder+"JuniorPoster-sm-shadow.png";iconJuniorPoster.iconSize=new GSize(17,12);iconJuniorPoster.shadowSize=new GSize(22,12);iconJuniorPoster.iconAnchor=new GPoint(9,12);iconJuniorPoster.infoWindowAnchor=new GPoint(17,0);var iconTransit=new GIcon();iconTransit.image=fileFolder+"TransitShelter-sm.png";iconTransit.shadow=fileFolder+"TransitShelter-sm-shadow.png";iconTransit.iconSize=new GSize(17,12);iconTransit.shadowSize=new GSize(22,12);iconTransit.iconAnchor=new GPoint(9,12);iconTransit.infoWindowAnchor=new GPoint(17,0);var iconBench=new GIcon();iconBench.image=fileFolder+"BusBench-sm.png";iconBench.shadow=fileFolder+"BusBench-sm-shadow.png";iconBench.iconSize=new GSize(17,12);iconBench.shadowSize=new GSize(22,12);iconBench.iconAnchor=new GPoint(9,12);iconBench.infoWindowAnchor=new GPoint(17,0);var currentType="None";if(typeof(getType)!="undefined")currentType=getType;function toggleLocation(type){if(currentType==type)currentType="None";else currentType=type}function createMarker(markerData){var point=new GLatLng(markerData.lat,markerData.lng);var productType=markerData.xml.getAttribute("type");switch(productType.slice(0,4)){case"Bull":var marker=new GMarker(point,iconBulletin);break;case"Post":var marker=new GMarker(point,iconPoster);break;case"Juni":var marker=new GMarker(point,iconJuniorPoster);break;case"Bus ":var marker=new GMarker(point,iconBench);break;case"Tran":var marker=new GMarker(point,iconTransit);break;case"BusR":var marker=new GMarker(point,iconBus);break;default:var marker=new GMarker(point,iconBulletin);break}GEvent.addListener(marker,"click",function(){markerWindow=marker;this.openInfoWindowHtml("<div class='info_window'><div class='popup_title'>"+markerData.xml.getAttribute("type")+"</div>"+"<strong>Location #:</strong> "+markerData.xml.getAttribute("location_num")+"<br/><strong>City:</strong> "+markerData.xml.getAttribute("city")+"<br/><strong>Facing:</strong> "+markerData.xml.getAttribute("facing")+"<br/><strong>Description: </strong>"+GXml.value(markerData.xml)+"<br/><strong>Photo(s): </strong><span id='xml_location_photo'>Loading...</span>");var location_num=markerData.xml.getAttribute("location_num");if(location_num.slice(0,3)!='BUS'){setTimeout("loadLocPhotos('"+location_num+"')",1000)}});marker.type=productType;return marker}GDownloadUrl(fileFolder+"xml.php",function(xmlData){var xml=GXml.parse(xmlData);var xmlLocations=xml.documentElement.getElementsByTagName("location");var locations=[];for(var i=0;i<xmlLocations.length;i++){var newLocation=new Object();newLocation.location=xmlLocations[i].getAttribute("location_num");newLocation.lat=parseFloat(xmlLocations[i].getAttribute("lat"));newLocation.lng=parseFloat(xmlLocations[i].getAttribute("lng"));newLocation.xml=xmlLocations[i];locations.push(newLocation)}var Rectangles=function(bounds,opt_weight,opt_color,opt_html){this.bounds_=bounds;this.weight_=opt_weight||1;this.html_=opt_html||"";this.color_=opt_color||"#888888"};Rectangles.prototype=new GOverlay();Rectangles.prototype.initialize=function(map){var div=document.createElement("div");div.innerHTML=this.html_;div.style.borderWidth=this.weight_+"px";div.style.borderStyle="dashed";div.style.borderColor=this.color_;div.style.position="absolute";map.getPane(G_MAP_MAP_PANE).appendChild(div);this.map_=map;this.div_=div};Rectangles.prototype.remove=function(){this.div_.parentNode.removeChild(this.div_)};Rectangles.prototype.copy=function(){return new Rectangles(this.bounds_,this.weight_,this.color_,this.backgroundColor_,this.opacity_)};Rectangles.prototype.redraw=function(force){if(!force)return;var c1=this.map_.fromLatLngToDivPixel(this.bounds_.getSouthWest());var c2=this.map_.fromLatLngToDivPixel(this.bounds_.getNorthEast());this.div_.style.width=Math.abs(c2.x-c1.x)+"px";this.div_.style.height=Math.abs(c2.y-c1.y)+"px";this.div_.style.left=(Math.min(c2.x,c1.x)-this.weight_)+"px";this.div_.style.top=(Math.min(c2.y,c1.y)-this.weight_)+"px"};var geocoder=new GClientGeocoder();var frmSearch=document.getElementById("frmsearch");var txtSearch=document.getElementById("txtsearch");frmSearch.onsubmit=function(){var value=txtSearch.value;var found=false;var location;for(i in locations){if(locations[i].location==value.toUpperCase()){found=true;location=locations[i];break}}if(found){var point=new GLatLng(location.lat,location.lng);map.setCenter(point,17)}else{geocoder.getLatLng(value,function(point){if(!point){alert(txtSearch.value+" not found")}else{map.setCenter(point,13)}})}return false};function checkMove(){map.removeControl(prevzoom_control);var windowSize=getWindowSize();var curHeight=windowSize.height-150;var gridSize=Math.floor(curHeight/50);var mapBounds=map.getBounds();var sw=mapBounds.getSouthWest();var ne=mapBounds.getNorthEast();var size=mapBounds.toSpan();var gridCellSizeLat=size.lat()/gridSize;var gridCellSizeLng=size.lng()/gridSize;var gridCells=[];for(k in clusterMarkers){map.removeOverlay(clusterMarkers[k])}clusterMarkers=[];for(k in existingMarkers){if(markerWindow!=existingMarkers[k]){map.removeOverlay(existingMarkers[k]);delete existingMarkers[k]}}visible_locations=[];for(k in locations){var latlng=new GLatLng(locations[k].lat,locations[k].lng);if(mapBounds.contains(latlng)){visible_locations.push(locations[k])}}var numBulletin=0;var numPoster=0;var num8Sheet=0;var numBench=0;var numTransit=0;var numBus=0;var numOther=0;for(k in visible_locations){var latlng=new GLatLng(visible_locations[k].lat,visible_locations[k].lng);var process=false;switch(visible_locations[k].xml.getAttribute("type").slice(0,4)){case"Bull":numBulletin++;if(currentType=='None'||currentType=='Bulletin')process=true;break;case"Post":numPoster++;if(currentType=='None'||currentType=='Poster')process=true;break;case"Juni":num8Sheet++;if(currentType=='None'||currentType=='8Sheet')process=true;break;case"Bus ":numBench++;if(currentType=='None'||currentType=='Bench')process=true;break;case"Tran":numTransit++;if(currentType=='None'||currentType=='Transit')process=true;break;case"BusR":numBus++;if(currentType=='None'||currentType=='Bus')process=true;break;default:numOther++;break}if(process){var testBounds=new GLatLngBounds(sw,latlng);var testSize=testBounds.toSpan();var i=Math.ceil(testSize.lat()/gridCellSizeLat);var j=Math.ceil(testSize.lng()/gridCellSizeLng);var cell=i*10+j;if(typeof gridCells[cell]=='undefined'){var cellSW=new GLatLng(sw.lat()+((i-1)*gridCellSizeLat),sw.lng()+((j-1)*gridCellSizeLng));var cellNE=new GLatLng(cellSW.lat()+gridCellSizeLat,cellSW.lng()+gridCellSizeLng);gridCells[cell]={GLatLngBounds:new GLatLngBounds(cellSW,cellNE),cluster:false,markers:[]}}gridCells[cell].markers.push(visible_locations[k]);if(gridCells[cell].cluster)continue;if(gridCells[cell].markers.length==2&&visible_locations.length>1&&map.getZoom()<16){gridCells[cell].cluster=true}}}for(k in gridCells){if(gridCells[k].cluster==true){var span=gridCells[k].GLatLngBounds.toSpan();var sw=gridCells[k].GLatLngBounds.getSouthWest();var marker=new GMarker(new GLatLng(sw.lat(),sw.lng()+(span.lng()/2)),iconCluster);var rect=new Rectangles(gridCells[k].GLatLngBounds,1,"#8d0f00",'<div style="background:#ebe6dc;text-align:center;font-weight:bold;">'+gridCells[k].markers.length+' Locations</div>');marker.bounds=gridCells[k].GLatLngBounds;GEvent.addListener(marker,"click",function(){markerWindow=marker;prevzoom_center=map.getCenter();prevzoom_zoom=map.getZoom();var zoom=map.getBoundsZoomLevel(this.bounds);var center=this.bounds.getCenter();map.setCenter(center,zoom);prevzoom_control=new PrevZoomControl();map.addControl(prevzoom_control)});marker.rect=rect;GEvent.addListener(marker,"mouseover",function(){this.rectHtml=this.rect.div_.innerHTML;this.rect.div_.innerHTML='<div style="background:#ebe6dc;text-align:center;font-weight:bold;color:red;">Click to Zoom</div>'});GEvent.addListener(marker,"mouseout",function(){this.rect.div_.innerHTML=this.rectHtml});map.addOverlay(marker);clusterMarkers.push(marker);map.addOverlay(rect);clusterMarkers.push(rect)}for(i in gridCells[k].markers){var productType=gridCells[k].markers[i].xml.getAttribute("type");if(productType.slice(0,4)=="BusR"||!gridCells[k].cluster){var marker=createMarker(gridCells[k].markers[i]);map.addOverlay(marker);existingMarkers.push(marker)}}}var totalNotSelected=numBulletin+numPoster+num8Sheet+numBench+numBus+numTransit;var output="";if(numBulletin!=0&&(currentType=='None'||currentType=='Bulletin')){output+="<div class='productitem ' id='productBulletin'>";output+="<img class='producticon' src='"+fileFolder+"Bulletin.png'/><strong>Bulletin</strong>"+"</div>";totalNotSelected-=numBulletin}if(numPoster!=0&&(currentType=='None'||currentType=='Poster')){output+="<div class='productitem ' id='productPoster'>";output+="<img class='producticon' src='"+fileFolder+"Poster.png'/><strong>Poster</strong>"+"</div>";totalNotSelected-=numPoster}if(num8Sheet!=0&&(currentType=='None'||currentType=='8Sheet')){output+="<div class='productitem ' id='product8Sheet'>";output+="<img class='producticon' src='"+fileFolder+"JuniorPoster.png'/><strong>Junior Poster</strong>"+"</div>";totalNotSelected-=num8Sheet}if(numBench!=0&&(currentType=='None'||currentType=='Bench')){output+="<div class='productitem ' id='productBench'>";output+="<img class='producticon' src='"+fileFolder+"BusBench.png'/><strong>Bus Bench</strong>"+"</div>";totalNotSelected-=numBench}if(numBus!=0&&(currentType=='None'||currentType=='Bus')){output+="<div class='productitem ' id='productBus'>";output+="<img class='producticon' src='"+fileFolder+"Bus.png'/><strong>Bus Advertising</strong>"+"</div>";totalNotSelected-=numBus}if(numTransit!=0&&(currentType=='None'||currentType=='Transit')){output+="<div class='productitem ' id='productTransit'>";output+="<img class='producticon' src='"+fileFolder+"TransitShelter.png'/><strong>Transit Shelter</strong>"+"</div>";totalNotSelected-=numTransit}if(currentType!='None'&&totalNotSelected>0){output+="<div class='productitem' id='productNone'>";output+="<em>Click here to view all locations.</em></div>"}if(output=="")output="<div class='products_text'>No products are available in this view. Please select a different view.</div>";document.getElementById("products").innerHTML=output;var title=document.getElementById("products_title");switch(currentType){case"Bulletin":title.innerHTML="Bulletins in Current View";break;case"Poster":title.innerHTML="Posters in Current View";break;case"8Sheet":title.innerHTML="Junior Posters in Current View";break;case"Bench":title.innerHTML="Bus Benches in Current View";break;case"Transit":title.innerHTML="Transit Shelters in Current View";break;case"Bus":title.innerHTML="Bus Ads in Current View";break;default:title.innerHTML="Products in Current View";break}var productNone=document.getElementById("productNone");if(productNone){productNone.onclick=function(){toggleLocation('None');resize()}}var productBulletin=document.getElementById("productBulletin");if(productBulletin){productBulletin.onclick=function(){toggleLocation('Bulletin');resize()}}var productPoster=document.getElementById("productPoster");if(productPoster){productPoster.onclick=function(){toggleLocation('Poster');resize()}}var product8Sheet=document.getElementById("product8Sheet");if(product8Sheet){product8Sheet.onclick=function(){toggleLocation('8Sheet');resize()}}var productBench=document.getElementById("productBench");if(productBench){productBench.onclick=function(){toggleLocation('Bench');resize()}}var productTransit=document.getElementById("productTransit");if(productTransit){productTransit.onclick=function(){toggleLocation('Transit');resize()}}var productBus=document.getElementById("productBus");if(productBus){productBus.onclick=function(){toggleLocation('Bus');resize()}}}GEvent.addListener(map,"moveend",checkMove);function resize(){var windowSize=getWindowSize();var newHeight=windowSize.height-150;map.removeControl(mapControl);if(newHeight<270){mapControl=new GSmallMapControl()}else{mapControl=new GLargeMapControl()}map.addControl(mapControl);var mapDiv=document.getElementById("map");mapDiv.style.height=newHeight+'px';var infoDiv=document.getElementById("leftscroll");infoDiv.style.height=newHeight-60+'px';map.checkResize();checkMove()}if(window.attachEvent){window.attachEvent("onresize",resize)}else{window.addEventListener("resize",resize,false)}resize()});function PrevZoomControl(){}PrevZoomControl.prototype=new GControl();PrevZoomControl.prototype.initialize=function(map){var container=document.createElement("div");var zoomDiv=document.createElement("div");this.setButtonStyle_(zoomDiv);container.appendChild(zoomDiv);zoomDiv.appendChild(document.createTextNode("Previous View"));GEvent.addDomListener(zoomDiv,"click",function(){map.setCenter(prevzoom_center,prevzoom_zoom)});map.getContainer().appendChild(container);return container};PrevZoomControl.prototype.getDefaultPosition=function(){return new GControlPosition(G_ANCHOR_TOP_RIGHT,new GSize(7,7))};PrevZoomControl.prototype.setButtonStyle_=function(button){button.style.color="#000000";button.style.backgroundColor="#ffbc02";button.style.border="1px outset";button.style.padding="5px";button.style.textAlign="center";button.style.width="6em";button.style.cursor="pointer"}}else{alert("I am sorry, but your web browser software is not compatible with Google Maps")}}if(window.attachEvent){window.attachEvent("onload",load);window.attachEvent("onunload",GUnload)}else{window.addEventListener("load",load,false);window.addEventListener("unload",GUnload,false)}function getWindowSize(){var e=new Object();if(window.self&&self.innerWidth){e.width=self.innerWidth;e.height=self.innerHeight}else if(document.documentElement&&document.documentElement.clientHeight){e.width=document.documentElement.clientWidth;e.height=document.documentElement.clientHeight}else{e.width=document.body.clientWidth;e.height=document.body.clientHeight}return e}var even=true;function getCSSclass(){even=!even;if(even==true)return"even";else return"odd"}
