/**
* Define the browser loading the page
*
* @var	string	userAgent Useragent string
* @var	boolean	is_opera  Opera
* @var	boolean	is_saf    Safari
* @var	boolean	is_webtv  WebTV
* @var	boolean	is_ie     Internet Explorer
* @var	boolean	is_ie4    Internet Explorer 4
* @var	boolean	is_ie7    Internet Explorer 7
* @var	boolean	is_ps3    Playstation 3
* @var	boolean	is_moz    Mozilla / Firefox / Camino
* @var	boolean	is_kon    Konqueror
* @var	boolean	is_ns     Netscape
* @var	boolean	is_ns4    Netscape 4
* @var	boolean	is_mac    Client is running MacOS
*/
var now = new Date();
var preloadCounter = 0;

var userAgent = navigator.userAgent.toLowerCase();
var is_opera	= ((userAgent.indexOf('opera') != -1) || (typeof(window.opera) != 'undefined'));
var is_saf	= ((userAgent.indexOf('applewebkit') != -1) || (navigator.vendor == 'Apple Computer, Inc.'));
var is_webtv	= (userAgent.indexOf('webtv') != -1);
var is_ie	= ((userAgent.indexOf('msie') != -1) && (!is_opera) && (!is_saf) && (!is_webtv));
var is_ie4	= ((is_ie) && (userAgent.indexOf('msie 4.') != -1));
var is_ie7	= ((is_ie) && (userAgent.indexOf('msie 7.') != -1));
var is_ps3	= (userAgent.indexOf('playstation 3') != -1);
var is_moz	= ((navigator.product == 'Gecko') && (!is_saf));
var is_ff	= (userAgent.indexOf('firefox') != -1);
var is_kon	= (userAgent.indexOf('konqueror') != -1);
var is_ns	= ((userAgent.indexOf('compatible') == -1) && (userAgent.indexOf('mozilla') != -1) && (!is_opera) && (!is_webtv) && (!is_saf));
var is_ns4	= ((is_ns) && (parseInt(navigator.appVersion) == 4));
var is_mac	= (userAgent.indexOf('mac') != -1);

// Fills select element with proper number of days
function showDays(element, month, year, selected_day)
{
	selected_day = parseInt(selected_day);
	month = parseInt(month);

	// If its a month before this month, then the year is next year
	//var year = (month < now.getMonth()) ? now.getFullYear() + 1 : now.getFullYear();

	// Find number of days in the given month
	var days_in_month = new Date(year, month, 0);
	days_in_month = days_in_month.getDate();

	// Fix selected day if needed
	selected_day = (selected_day > days_in_month) ? days_in_month : selected_day;

	// Output options

	var day_options = "";
	for (i = 1; i <= days_in_month; i++)
	{
		var selected = (selected_day > 0 && i == selected_day) ? ' selected="selected"' : '';
		day_options += '<option value="' + i + '"' + selected + '>' + i + '</option>';
	}

	// innerHTML does not work properly on <select> in IE
	if (is_ie)
	{
	    element.outerHTML = '<select name="' + element.name + '" id="' + element.id + '" class="selectOne" tabindex="1" style="width: 60px">' + day_options + "</select>";
	}
	else
	{
		element.innerHTML = day_options;
	}
}

// Google maps
var geocoder;

function newGoogleMap(map, large)
{
	geocoder = new GClientGeocoder();

	if (GBrowserIsCompatible())
	{
		if (large)
		{
			var newMap = new GMap2(map, {mapTypes:new Array(G_NORMAL_MAP, G_SATELLITE_MAP, G_HYBRID_MAP)});
			newMap.addControl(new GMapTypeControl(1));
			var controls = new GLargeMapControl();
		}
		else
		{
			var newMap = new GMap2(map);
			var controls = new GSmallMapControl();
		}
		
		newMap.enableContinuousZoom();
		newMap.addControl(controls);

		showVermont(newMap);
	}

	return newMap;
}

function showLatLng(map, point, label, blurbHTML, skipSetCenter)
{
	if (!skipSetCenter)
	{
		map.setCenter(point, 15);
	}

	var marker = new GMarker(point, {title: label});
	map.addOverlay(marker);

	if (blurbHTML)
	{
		GEvent.addListener(marker, "click", function()
		{
			map.openInfoWindowHtml(point, blurbHTML);
			map.setCenter(point);
		});
	}
	
	return marker;
}

function showAddress(map, address, label, blurbHTML, skipSetCenter)
{
	geocoder.getLatLng(address,
	function(point) {
		if (!point)
		{
			showVermont(map);
		}
		else
		{
	    	showLatLng(map, point, label, blurbHTML, skipSetCenter);
		}
  	});
}

function showVermont(map)
{
	map.setCenter(new GLatLng(43.871756, -72.451148), 8);
}

// Is QuickTime installed?
// From: http://blog.deconcept.com/2005/01/26/web-standards-compliant-javascript-quicktime-detect-and-embed/
function isQTInstalled()
{
	var qtInstalled = false;
	qtObj = false;

	if (navigator.plugins && navigator.plugins.length)
	{
		for (var i = 0; i < navigator.plugins.length; i++)
		{
			var plugin = navigator.plugins[i];
			if (plugin.name.indexOf("QuickTime") > -1)
			{
				qtInstalled = true;
			}
		}
	}
	else
	{
		execScript('on error resume next: qtObj = IsObject(CreateObject("QuickTimeCheckObject.QuickTimeCheck.1"))', 'VBScript');
		qtInstalled = qtObj;
	}

	return qtInstalled;
}

// Confirm alert
function showConfirm(msg)
{
	var answer = confirm(msg)
	if (answer)
	{
		return true;
	}
	else
	{
		return false;
	}
}

function preload(imgs)
{
	if (!imgs)
	{
		return;
	}
	
	if (imgs instanceof Array)
	{
		for (var i = 0; i < imgs.length; i++)
		{
			this["preloadImg" + preloadCounter] = new Image();
			this["preloadImg" + preloadCounter].src = imgs[i];
			
			preloadCounter++;
		}
	}
	else
	{
		this["preloadImg" + preloadCounter] = new Image();
		this["preloadImg" + preloadCounter].src = imgs;
		
		preloadCounter++;
	}
}

// Popup window
function popUp(url, width, height, name)
{
	title = (typeof(name) != "undefined") ? name : "imgpop";
	window.open(url, name, "width=" + width + ",height=" + height + ",resizable=no,scrollbars=yes,toolbar=no,location=no,directories=no,status=no,menubar=no,copyhistory=no");
}

// Spam resistant mailto link (or so I'm told)
function mailto_link(user, dom, extra)
{
	var email_address = user + '@' + dom;
	extra = (extra) ? extra : '';

	return '<a href="mailto:' + email_address + extra + '">' + email_address + '</a>';
}

function enableFormInteraction()
{
	var objEvt = {
		input   : ["hover", "focus"],
		select  : ["hover", "focus"],
		textarea: ["hover", "focus"]
	};

	// spare use variables
	var temp;
	var tempLen = 0;

	// hover/focus functions
	function hoverFunc()
	{
		this.className += " hover";
	}

	function unHoverFunc()
	{
		this.className = this.className.replace("hover", "");
	}

	function focusFunc()
	{
		this.className += " focus";

		// start URL replacement
		if (navigator.appName.indexOf("Explorer")!=-1)
		{
			if (this.value == this.defaultValue && this.name == "url")
			{
				this.value = "http://";
			}
		}
		// end URL replacement
	}

	function unFocusFunc()
	{
		this.className = this.className.replace("focus", "");
		this.className = this.className.replace("hover", "");
	}

	for (var i in objEvt)
	{
		temp = document.getElementsByTagName(i);
		tempLen = temp.length;

		for (var j = 0; j < tempLen; j++)
		{
			for (var k = 0; k < objEvt[i].length; k++)
			{
				if (objEvt[i][k] == "hover")
				{
					temp[j].onmouseover = hoverFunc;
					temp[j].onmouseout = unHoverFunc;
				}
				else if (objEvt[i][k] == "focus")
				{
					if (!temp[j].onfocus)
					{
						temp[j].onfocus = focusFunc;
					}
					
					if (!temp[j].onblur)
					{
						temp[j].onblur = unFocusFunc;
					}
				}
			}
		}
	}
}

// Trim whitespace
String.prototype.trim = function()
{
	return this.replace(/^\s+|\s+$/g, "");
}