/**
* YUI Shortcuts if YUI called
*/
if( typeof YAHOO != 'undefined' && YAHOO.util ) {
	var $D = YAHOO.util.Dom;
	var $E = YAHOO.util.Event;
	var $ = $D.get;
}

/**
* Email de-obscurificator. Converts someone [at] domain [dot] come style text to clickable mailto: links for JS enabled users
* Simply wrap <span class="obs"></span> around an obscured email address and the JS will convert back to working links.
* by Mike Healy (www.mikehealy.com.au)
* this code may be freely used and adapted
* v2 now REPLACES the original <span class='obs'> with link (previously link was child of <span>). Test in more browsers!
*/
//Obscure email replacement
var obsReplace = function() {
	
	var spans = document.getElementsByTagName('span');
	if(!spans) return;
	for(i=0; i<spans.length; i++) {
		if(spans[i].className.search(/obs/) != -1) {
			addy = spans[i].innerHTML.replace(/\[ ?at ?\]/g, '@');
			addy = addy.replace(/ /g, '');
			addy = addy.replace(/\[ ?dot ?\]/g, '.');
			var link = document.createElement('a');
			if(spans[i].title && spans[i].title!='') {
				link.title = spans[i].title;
				link.title = link.title.replace(/\[email\]/g, addy);
			}
			link.href = 'mailto:' + addy;
			link.innerHTML = addy;
			
			spans[i].parentNode.insertBefore(link, spans[i]);
			spans[i].parentNode.removeChild(spans[i]);
		}
	}
}();


/**
* Highlight current link in sub-nav list
* @todo auto-run on ul.highlight-current
* @todo use JS Library to addClass better than set class
*/
var current_page = function(parent){
	
	var highlight = function(list) {
		var links = list.getElementsByTagName('a');
		if( !links ) {
			return;
		}
		var l = links.length;
		for(i=0; i<l; i++) {
			if(links[i].href == document.location) {
				links[i].className = 'current';
			}
		}
	};
	
	var ul = document.getElementsByTagName('ul');
	var l = ul.length;
	for(i=0; i<l; i++) {
		if(ul[i].className.search(/highlight\-current/) != -1) {
			highlight(ul[i]);
		}
	}
}();


/**
* Offices - click map icon for Google map
* YUI2 version
*/
var offices = {
	
	init : function() {
		var map_icons = $D.getElementsByClassName('map-icon', 'div', 'content');
		
		$E.on(map_icons, 'click', function(ev) {
			
			var tgt = $E.getTarget(ev);
			
			$D.removeClass(map_icons, 'active');
			$D.addClass( tgt, 'active' );
			
			offices.render_map(tgt.id);
			
			//Scroll Down
			var xy = $D.getXY(tgt);
			window.scroll(xy[0], xy[1]);
		});
	},
	
	/**
	* Initiate Google map
	* @todo if map rendered, just change center and add marker
	*/
	render_map : function(map_name) {
		
		var coords = {
			'proserpine_map' : [-20.402955, 148.582921, 16],
			'mackay_map'	 : [-21.144460, 149.1881280, 16]
		};
		
		//Coords/data for *this* office
		coords = coords[map_name];
		
		var map_el = $('map');
		map_el.className = 'active';
		
		var LatLng = new google.maps.LatLng(coords[0], coords[1]);
		var cfg = {
			zoom : coords[2],
			center : LatLng,
			mapTypeId : google.maps.MapTypeId.ROADMAP
		};
		
		var map = new google.maps.Map(map_el, cfg);
		
		//Overlay
		var marker = new google.maps.Marker({
			position: LatLng,
			title: 'WS Group'});
		marker.setMap(map);
	}
};
