/*
Script Name: Pyrmont Village Scripting
Description: An automatic aggregate of all Pyrmont Village scripts to reduce the number of queries.
Version: 1
Author: Ashley Kyd
Author URI: http://kyd.com.au/
*/

/* Google API Key */
key = 'ABQIAAAA_iM-ueRKgr8P9ci9zKEE1xTa9CNh8X7PWYXYzKiAtkci2k9elhSjJfD-mv9OF052mtJqxutgGRlyIw';
/*
Script Name: vCard Map
Description: Adds Google Maps to static hCard addresses.
Version: 1.5.1
Author: Ashley Kyd
Author URI: http://kyd.com.au/
*/

var xmlDoc;
var map;
var geocoder;
var mapLoaded = false;

/*
 * API Loading function.
 * The variable "key" needs to be defined already.
 */
function loadApi(){
	// Load the Google Maps API, now that we need it.
	
	// Here's our url.
	googleApiSrc = "http://www.google.com/jsapi?callback=apiLoadedEvent&key="+key;

	// Now we can create a script element to insert into the <head>
	var script = document.createElement("script");
	script.src = googleApiSrc;
	script.type = "text/javascript";
	document.getElementsByTagName("head")[0].appendChild(script);
}

/*
 * API Loaded.
 * This is a callback function to let us know the API has been loaded.
 */
function apiLoadedEvent(){
	// Load the Maps API now.
	google.load("maps", "2", {"callback" : mapsLoadedEvent});
}

/*
 * Maps Loaded.
 * This is a callback function to let us know the maps API has been loaded.
 */
function mapsLoadedEvent(){
	// Mark the API as completely loaded. Now everything will be clickable.
	mapLoaded = true;

	// We've found an address. Make a map. :-)
	map = new GMap2(document.getElementById("map_canvas"));
	
	// Initialise our map defaults
	map.setCenter(new GLatLng(34, 0), 16);
	geocoder = new GClientGeocoder();
	geocoder.getLocations(address, addAddressToMap);
	document.getElementById('map_canvas_wrap').style.display='block';
	
}

function findAdr(vcard){
	
	var spans = vcard.getElementsByTagName('span');
	
	var address = new Array();
	var addressValid = false;
	
	for(i=0;i<spans.length;i++){
		className = spans[i].className;
		switch(className){
			case 'street-address':
				address[0] = spans[i].childNodes[0].nodeValue;
				addressValid = true;
				break;
			case 'locality':
				address[1] = spans[i].childNodes[0].nodeValue;
				break;
			case 'region':
				address[2] = spans[i].childNodes[0].nodeValue;
				break;
			case 'postal-code':
				address[3] = spans[i].childNodes[0].nodeValue;
				break;
			
		}
		
	}
	
	address = address.join(', ');
	
	return address;
	
	
}

function makeMaps(){
	
	var vcard = document.getElementById('content-column-left-sub-right');
	if(vcard){
		address = findAdr(vcard);
	
		if(address){
			
			loadApi();	
			
		}else{
			
			// No hcard/address address found
			
		}
	}
	
}

// addAddressToMap() is called when the geocoder returns an
// answer.  It adds a marker to the map with an open info window
// showing the nicely formatted version of the address and the country code.
function addAddressToMap(response) {
	map.clearOverlays();
	if (!response || response.Status.code != 200) {
		// Address couldn't be geocoded. :-(
		return false;
	} else {
		place = response.Placemark[0];

		point = new GLatLng(place.Point.coordinates[1],	place.Point.coordinates[0]);
		point1 = new GLatLng(place.Point.coordinates[1]+.0025,	place.Point.coordinates[0]-.0025);
						
		map.setCenter(point1);	
		marker = new GMarker(point);
		map.addOverlay(marker);
	}
}

window.onload = makeMaps;
/*
Script Name: Wp-Adserve Dynamic Update Script
Description: Queries and refreshes WP Adserve content through the XML backend.
Version: 0.4-proprietary
Author: Ashley Kyd
Author URI: http://kyd.com.au/
*/

// The ID prefix used by all of our ads.
adPrefix = 'iriAd';

// The frequency of the rotations (seconds)
adRotationFrequency = 10;

// The maximum number of rotations (the more impressions, the less likely anyone
// is paying attention.
adRotationLimit = 100;

adRotationFrequency = adRotationFrequency*1000;
// The banner class.
function banner(){
	this._bannum = '';
	this._numrows = '';
	this._number = '';
	this._href = '';
	this._src = '';
	this._alt = '';
}
banner.prototype._bannum;
banner.prototype._numrows;
banner.prototype._number;
banner.prototype._href;
banner.prototype._src;
banner.prototype._alt;

var i = 1;
var element;
var xmlDoc;


function switchAd(){
	// Create a new banner class and populate it with values from the XML.
	ad = new banner();
	try{
		ad.bannum = xmlDoc.getElementsByTagName("bannum")[0].childNodes[0].nodeValue;
		ad.numrows  = xmlDoc.getElementsByTagName("numrows")[0].childNodes[0].nodeValue;
		ad.number = xmlDoc.getElementsByTagName("number")[0].childNodes[0].nodeValue;
		ad.href = xmlDoc.getElementsByTagName("href")[0].childNodes[0].nodeValue;
		ad.src = xmlDoc.getElementsByTagName("src")[0].childNodes[0].nodeValue;
		ad.alt = xmlDoc.getElementsByTagName("alt")[0].childNodes[0].nodeValue;
		
		// If this is a valid ad, let's go.
		if(ad.number){
			element.href = ad.href;
			image = element.childNodes[0];
			image.src = ad.src;
			image.alt = ad.src;
		}
	} catch(e){
		//invalid xml.
		adRotationLimit++;
	}
	
	adRotationLimit--;
	
	// We want to repeat ad infinitum, or until adRotationLimit.
	// Set to a negative value and this will never be triggered.
	if(adRotationLimit!=0){
		newTimer = setTimeout("rotateAds()", adRotationFrequency);
	}
}

// Function to find and replace the existing ads.
function rotateAds(){

	elementName = adPrefix+i;
	element = document.getElementById(elementName);
	if(element){

		// Work out the zone this ad belongs to (class names are usually
		// "advert-zone_name", so we can chop off the first 7 characters.
		zone = element.className.substring(7);
	
		// Create an empty XML object
		try { // The Internet Explorer way
			xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
			xmlDoc.onreadystatechange = function () {
				if (xmlDoc[index].readyState == 4) switchAd;
			};

		} catch(e) {
			try { // The more standard way.
				xmlDoc = document.implementation.createDocument("","",null);
				xmlDoc.onload = switchAd;
			} catch(e) {
				//Fail silently.
			}
		}
		
		// We've found an ad element to update. Pull a new ad from the server.
		xmlDoc.load('?zone='+zone);
	}else{
		newTimer = setTimeout("rotateAds()", adRotationFrequency);
		i=0;
	}
	i++;
}

// This will set the ball rolling.
newTimer = setTimeout("rotateAds()", adRotationFrequency);
