/* vim: ts=4 sw=4 tw=0
*/

Event.observe(window, 'load', init, false);
function init(){

	Event.observe('buoy_imagemap','click', imagemap_clicked, false);
	Event.observe('chkLatLon','click', swap_map, true);

	/* preload */
	if (document.images) {
		var dummy = new Image;
		dummy.src = "/images/buoy_data/buoy_map_grid.png";
		dummy.src = "/images/buoy_data/checkbox_on.gif";
	}

}

var popWinOpts = "width=600+height=800,resizable=yes,locationbar=no,menubar=no,statusbar=no,toolbar=no,personalbar=no,scrollbars=yes";

function imagemap_clicked(e){
	var target = String(Event.element(e));

	if (target.indexOf('http') == -1 ){ // safari, konqueror
		target = String(e.target.href);
	}

	if (target.indexOf('http') == 0){

		if (target.indexOf('tidesonline') == -1) {

			target = target.replace('recent.html','recent_data.html');
			data_fetch(target);

		} else {
			pop_win(target);
		}

		Event.stop(e);

	}
}

function pop_win(url){
	var newWin = window.open(target,'nos',popWinOpts);
	newWin.focus();
}

function data_fetch(url){
	$('feedbacker').removeClassName('feedback_disabled');
	// remove http and domain from url for reporting if it's there
	var google_analytic_url = url ;
	if (url.indexOf('http://') == 0){
		google_analytic_url = stripDomain(url) ;
	}
	var updater = new Ajax.Updater(
			'data_output',
			url,
			
			{  
				method: 'get',  
				onSuccess: function(transport) {
					// Legacy Jason code (did tracker was null!)
					//var tracker = transport.getResponseHeader('X-Ajax-URL');
					//_uacct = "UA-775587-1";
					//urchinTracker(tracker);
					// Ian's first attempt and unknown if it would have been successful
					// _uacct = "UA-775587-1";
					// urchinTracker(url);
					// Ian putting in Googles new tracking code.
					var pageTracker = _gat._getTracker("UA-775587-1");
					pageTracker._trackPageview(google_analytic_url);
				},
				onComplete: function(){
					$('feedbacker').addClassName('feedback_disabled');
				},
				onFailure: function(){
					$('feedbacker').addClassName('feedback_disabled');
				}
			}
	);
}

var LatLonState = false;
function swap_map(e){
	if (document.images){
		switch(LatLonState){
			case false:
				$('buoy_map').src =  "/images/buoy_data/buoy_map_grid.png";
				$('chkLatLon').src = "/images/buoy_data/checkbox_on.gif";
				LatLonState = true;
				break;
			case true:
				$('buoy_map').src = "/images/buoy_data/buoy_map.png";
				$('chkLatLon').src = "/images/buoy_data/checkbox_off.gif";
				LatLonState = false;
				break;
			default:
				alert('Missing variable LatLonState');
				LatLonState = false;
				break;
		}
	}
}

// attempt to strip the domain from the url. If unsuccessful
// return the original url. No harm no foul.
// str example: "http://mapple.gomoos.org:8083/data/recent_data.html?platform=F01"
//
function stripDomain(str)
{
	var ret_str = str ;
	if (str == null || str.length == 0)
		return ret_str;
	str = str.toLowerCase();
	var i = str.indexOf("//");
	// just walk this thing and hope.
	// if we found http://
	if (i > -1)  {
		str = str.substring(i+2);
		// mapple.gomoos.org:8083/data/recent_data.html?platform=f01"
		// look for the first / 
		var j = str.indexOf("/");
		if ( j > -1 ) {
			// /data/recent_data.html?platform=f01
			var ret_str = str.substring(j) ;
		}
	}	
	return ret_str;
}

