var CSA = { tileInstanceNum: 1, maxTileInstanceNum: 4, numOfTiles: 5, tileImgDir: false, tileImgExt: '.jpg', init: function() { CSA.loadLocationData(); CSA.setTileImgDir(); CSA.replaceTiles(); }, loadLocationData: function() { var locationData = []; $('#locations .vcard').each(function(){ locationData.push({ title: $(this).find('.org').text(), address: $(this).find('.adr').text(), latitude: $(this).find('.latitude').text(), longitude: $(this).find('.longitude').text() }); }); CSA.drawLocationMap(locationData); }, drawLocationMap: function(locationData) { if (locationData.length == 0){ return false; } var points = []; var markers = []; var bounds = new google.maps.LatLngBounds(); var geocoder = new google.maps.Geocoder(); var latLng; for (var a in locationData){ latLng = new google.maps.LatLng(locationData[a].latitude, locationData[a].longitude); //new CSA.geocodeAddress(geocoder, locationData[a].address);// points.push( latLng ); bounds.extend( latLng ); } var myOptions = { zoom: CSA.calcZoomVal(bounds),//15 center: bounds.getCenter(), mapTypeId: google.maps.MapTypeId.ROADMAP }; var map = new google.maps.Map(document.getElementById("map-holder"), myOptions); // Draw markers for each location for (var a in points){ var contentString = '
'+ '
'+ '
'+ '

'+locationData[a].title+'

'+ '
'+ '

'+locationData[a].address+'

'+ '
'+ '
'; var image = new google.maps.MarkerImage('/wp-content/themes/csa/images/markers/'+(parseInt(a)+1)+'.png', // This marker is 20 pixels wide by 32 pixels tall. new google.maps.Size(22, 36), // The origin for this image is 0,0. new google.maps.Point(0,0), // The anchor for this image is the base of the flagpole at 0,32. new google.maps.Point(0, 32)); var shadow = new google.maps.MarkerImage('/wp-content/themes/csa/images/markers/shadow.png', // The shadow image is larger in the horizontal dimension // while the position and offset are the same as for the main image. new google.maps.Size(35, 22), new google.maps.Point(0,0), new google.maps.Point(0, 18)); markers[a] = new google.maps.Marker({ position: points[a], map: map, title:locationData[a].title, icon: image, shadow: shadow }); CSA.createInfoWindow(map,markers[a],contentString); } }, geocodeAddress: function(geocoder,address) { if (geocoder){ geocoder.geocode( {'address': address}, function(results, status){ if (status == google.maps.GeocoderStatus.OK) { alert(address+" coords "+results[0].geometry.location.toString() ); return results[0].geometry.location; } else { alert("Geocode was not successful for the following reason: " + status); return false; } }); } return false; }, createInfoWindow: function(map,marker,contentString) { var infoWindow = new google.maps.InfoWindow({ content: contentString }); google.maps.event.addListener(marker, 'click', function() { infoWindow.open(map,marker); }); }, calcZoomVal: function(bounds) { if (bounds.isEmpty()){ return 1; } var maxZoomLevel = 15; var maxLatLong = bounds.getNorthEast(); var minLatLong = bounds.getSouthWest(); var latDiff = maxLatLong.lat() - minLatLong.lat(); var longDiff = maxLatLong.lng() - minLatLong.lng(); var diff = Math.max(latDiff,longDiff); var maxDiff = (diff == latDiff) ? 180 : 360; var ratio = (diff / maxDiff); var zoomLevel = Math.ceil( Math.abs ( CSA.custLog(ratio,10) * 4) * 0.85); return Math.min(zoomLevel, maxZoomLevel); }, custLog: function(x,base) { // Created 1997 by Brian Risk. http://brianrisk.com return (Math.log(x))/(Math.log(base)); }, setTileImgDir: function() { var src = $('.tiles .tile-1').attr('src'); if (! src){ return false; } CSA.tileImgDir = src.replace( '/1/' + CSA.tileInstanceNum + CSA.tileImgExt, ''); }, replaceTiles: function() { if (! CSA.tileImgDir){ return false; } for (var a = 1; a <= CSA.numOfTiles; a++){ var src = CSA.tileImgDir + "/" + a + "/" + CSA.tileInstanceNum + CSA.tileImgExt; $('') .attr('src', src) .attr('class', 'new tile tile-'+a) .attr('style', 'display:none') .load(function(){ $('div.tiles').append( $(this) ); $(this).fadeIn(); }); } if (CSA.tileInstanceNum > 1){ // to get rid of the white flash $('.tiles :not(.new)').remove(); $('.tiles .tile').removeClass('new'); } CSA.advanceTileInstanceNum(); setTimeout(CSA.replaceTiles, 5000); }, advanceTileInstanceNum: function() { if (CSA.tileInstanceNum == CSA.maxTileInstanceNum){ CSA.tileInstanceNum = 1; } else { CSA.tileInstanceNum++; } } } $(document).ready(function(){ Shadowbox.init(); CSA.init(); });