/*
 * initWatermarks
 * Initializes all form elements with the "watermark" CSS class.
 */
var initWatermarks = function() {
	var aw=function(e){if(e.value==''){e.value=e.title;YAHOO.util.Dom.addClass(e,'watermark');}};
	var cw=function(e){if(e.value==e.title){e.value='';YAHOO.util.Dom.removeClass(e,'watermark');}};
	var es=YAHOO.util.Dom.getElementsByClassName('watermark');
	if(es!=null){
		YAHOO.util.Dom.batch(es,function(e){e.value=e.title;});
		YAHOO.util.Event.addListener(es,"click",function(e){cw(this);});
		YAHOO.util.Event.addListener(es,"focus",function(e){cw(this);});
		YAHOO.util.Event.addListener(es,"blur",function(e){aw(this);});
	}
};
YAHOO.util.Event.onDOMReady(initWatermarks);
/*
 * targetBlank
 * Opens the specified URL in a new window.
 * Taken from "Is there an equivalent to the target link attribute for XHTML?"
 * http://www.ozoneasylum.com/5332
 */
function targetBlank (url) {
  blankWin = window.open(url,'_blank','menubar=yes,toolbar=yes,location=yes,directories=yes,fullscreen=no,titlebar=yes,hotkeys=yes,status=yes,scrollbars=yes,resizable=yes');
}

$(document).ready(function() {
	$('a[rel="external"]').click(function(e) { e.preventDefault(); targetBlank(this.href); return false; });
	if ($('div#globe-wrapper div#globe').length == 1) {
		var globe = document.getElementById('globe');

		$('div#globe-wrapper div#globe map#globemap area').hover(
			function() { globe.className = this.id; },
			function() { globe.className = 'default'; }
		);

		$('div#sidebar ul#continents li a').hover(
			function() { globe.className = this.pathname.substring(this.pathname.lastIndexOf('/') + 1, this.pathname.lastIndexOf('.')); },
			function() { globe.className = 'default'; }
		);
	}

	if ($('div#sidebar ul#continents').length == 1) {
		var match = new RegExp('/retailers/([\\w|-]*)').exec(location.href);
		if (match != null) {
			$('div#sidebar ul#continents li a').each(function() {
				if (this.pathname.substring(0, this.pathname.lastIndexOf('.')) == match[0]) $(this).parent('li').addClass('selected');
			});
		}
	}
});

var loadMap = function() {
	if (typeof mapConfig != 'undefined') {
		if (GBrowserIsCompatible()) {
			var map = new GMap2(document.getElementById("map"));
			map.setCenter(new GLatLng(0, 0), 0);

			var bounds = new GLatLngBounds();
			if (mapConfig.bounds != undefined) {
				bounds = new GLatLngBounds(
					new GLatLng(mapConfig.bounds.south, mapConfig.bounds.west),
					new GLatLng(mapConfig.bounds.north, mapConfig.bounds.east)
				);
			}

			if (mapConfig.markers != undefined) {
				for (var index = 0; index < mapConfig.markers.length; index++) {
					var config = mapConfig.markers[index];
					if (config.position != null) {
						var point = new GLatLng(config.position.latitude, config.position.longitude);
						map.addOverlay(createMarker(point, config.icon, config.html));
						bounds.extend(point);
					}
				}
			}

			var zoom = mapConfig.zoom;
			if (zoom == 0) {
				zoom = map.getBoundsZoomLevel(bounds)-1;
			}
			map.setZoom(zoom);
			map.setCenter(bounds.getCenter());
		}
	}
};

YAHOO.util.Event.addListener(window, 'load', loadMap);

function createMarker(point, iconUrl, html) {
	var icon = new GIcon(G_DEFAULT_ICON);
	if (iconUrl != undefined && iconUrl != null & iconUrl.length > 0) {
		icon.image = iconUrl;
		icon.iconSize = new GSize(32, 32);
	}
	
	var marker = new GMarker(point, { icon: icon });
	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml(html);
	});
	return marker;
}
