// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	isIE: function() {
		return this.browser == "Explorer";
	},
	dataBrowser: [
		{
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Chrome"
		},
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};

 
function determine_view_port() {

  // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
  if (typeof window.innerWidth != 'undefined')
  {
    window.viewport_width = window.innerWidth;
    window.viewport_height = window.innerHeight;
  }
  // IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
  else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0)
  {
    window.viewport_width = document.documentElement.clientWidth,
    window.viewport_height = document.documentElement.clientHeight
  }  
}

function setup_hover_img(img_id, hover_src) {
  img = $(img_id);
  if(!img) { return; }
  img.original_src = img.src;
  img.hover_src = hover_src;
  img.observe('mouseover', function(event) { swap_image(img_id, $(img_id).hover_src) });
  img.observe('mouseout',function(sender) { swap_image(img_id, $(img_id).original_src) });
}

function setup_click(id, action) {
  elem = $(id);
  if(!elem) { return; }
  
  elem.observe('click', action);
}

function setup_popup_launcher(id, popup_section_id) {
  setup_click(id, function(event) { showPopup(popup_section_id) });
}

function link_hover (image, hover_url) {
	image.hover_url = hover_url;
	image.org_url = image.src;
	image.observe('onmouseover', function () { swap_image(image, image.hover_url); });
	image.observe('onmouseout', function () { swap_image(image, image.org_url); });
}

function swap_image(img_id, image_url) {
	$(img_id).src = image_url;	
}

BrowserDetect.init();
determine_view_port();