﻿function iecheck() {
	if (navigator.platform == "Win32" && navigator.appName == "Microsoft Internet Explorer" && window.attachEvent) {
		var rslt = navigator.appVersion.match(/MSIE (\d+\.\d+)/, '');
		var iever = (rslt != null && Number(rslt[1]) >= 5.5 && Number(rslt[1]) <= 7);
	}
	return iever;
}

function truncateLocation(location)
{
	if(!location)
		return;
	if(location.length < 33)
		return location;
	else
	{
		while(location.length > 33)
		{
			location = location.substring(location.indexOf(',') + 1);
		}
		return location;
	}
}

TwiddyWidget = {
	BASE_URL: 'http://www.twiddy.com/',
	STYLESHEET: "css/twiddy_tide_widget.css",
	CONTENT_URL: 'twiddy_tide_data.axd?location=',
	ROOT: 'twiddy_tide_widget',

	requestStylesheet: function(stylesheet_url) {
		stylesheet = document.createElement("link");
		stylesheet.rel = "stylesheet";
		stylesheet.type = "text/css";
		stylesheet.href = stylesheet_url;
		stylesheet.media = "all";
		document.lastChild.firstChild.appendChild(stylesheet);
	},

	requestContent: function(local) {
		var l = document.getElementById('twiddy_tide_link');

		if (l != null && l.href == 'http://www.twiddy.com/' && (l.rel == null || l.rel == '')) {
			var script = document.createElement('script');
			// How you'd pass the current URL into the request
			// script.src = CONTENT_URL + '&url=' + escape(local || location.href);
			script.src = this.BASE_URL + this.CONTENT_URL + l.getAttribute('name');
			document.getElementsByTagName('head')[0].appendChild(script);
		}
	},

	serverResponse: function(data) {
		if (!data) return;
		var div = document.getElementById(this.ROOT);
		var content = '<div id="obxTides"><div class="boxRight"><ul id="tide-info"><li class="title">Current Tide Info</li><li>' + data.currentTime + '</li><li>' + truncateLocation(data.tideLocationName)  + '</li><li><img id="imgTideChange" src="http://www.twiddy.com/siteart/tide-' + data.tideChangeType.toLowerCase() + '.png"></img></li><li class="title">' + data.tideChangeType + '</li><li class="title" id="liNextTide">' + data.nextTide + ' Tide at ' + data.nextTideTime + '</li></ul></div></div>';
		div.innerHTML = content;
		div.style.display = 'block'; // make element visible
		div.style.visibility = 'visible'; // make element visible
		document.getElementById('twiddy_tide_link').style.display = 'none';
	}
	
	

}
TwiddyWidget.requestStylesheet(TwiddyWidget.BASE_URL + TwiddyWidget.STYLESHEET);
document.write("<div id='" + TwiddyWidget.ROOT + "' style='display: none'></div>");
TwiddyWidget.requestContent();
var no_script = document.getElementById('no_script');
if (no_script) { no_script.style.display = 'none'; }
