addEventToObject = function(obj,evt,func) {
	var oldhandler = obj[evt];
	obj[evt] = (typeof obj[evt] != 'function') ? func : function(ev){oldhandler(ev);func(ev);};
}

cancelVideo = function()
{
	var frame = document.getElementById('videoFrame');
	frame.src = "blank.html";
	
	closeModalWindow('videoGallery');
}

showVideo = function()
{
	var frame = document.getElementById('videoFrame');
	frame.src = "video.html";
	
	openModalWindow('videoGallery');
}

// Code to work with page sizing and the presentation of modal windows
// ----------------------------------------------
Position.getPageSize = function()
{
	var xScroll, yScroll, scrollOffsetY;
	if (window.innerHeight && window.scrollMaxY)
	{  
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	}
	else if (document.body.scrollHeight > document.body.offsetHeight)
	{	// all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	}
	else
	{	// Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	if (self.pageYOffset)
	{
		scrollOffsetY = self.pageYOffset;
	}
	else if (document.documentElement && document.documentElement.scrollTop)
	{	 // Explorer 6 Strict
		scrollOffsetY = document.documentElement.scrollTop;
	}
	else if (document.body)
	{	// all other Explorers
		scrollOffsetY = document.body.scrollTop;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight)
	{	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight)
	{	// Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	}
	else if (document.body)
	{	// other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	} 
	
	// for small pages with total height less then height of the viewport
	pageHeight = Math.max(windowHeight,yScroll);
	
	// for small pages with total width less then width of the viewport
	pageWidth = Math.max(windowWidth,xScroll);
	
	return {
		page:
		{
			width: pageWidth,height: pageHeight
		}, 
		window:
		{
			width: windowWidth,height: windowHeight
		},
		scroll:
		{
			top: scrollOffsetY
		}
	};
}

// Modal windows
// ----------------------------------------------

createOverlay = function()
{
	var objBody = document.getElementsByTagName("body").item(0);
	var objOverlay = document.createElement("div");
	
	objOverlay.setAttribute('id','overlay');
	objOverlay.style.display = 'none';
	objBody.appendChild(objOverlay);
}

var multiWindow = false;
var CurrentModalWindow = "none";

// string selectorId-->id of content to be shown in modal window
// string [objA]-->anchor object which contains id of first slideshow image to be shown in rel attribute
// should be id of object shown first when modal window contents contain a collection

openModalWindow = function(selectorId, strStartId) {
	var overlayID = 'overlay';
	var v = Position.getPageSize();

	// Turn off scrollbars in the viewport as they render above the overlay //
	
	var problemTags = document.getElementsByTagName("textarea");
	for (var i = 0;i<problemTags.length;i++)
	{
		if (problemTags[i].className != "scrollOK")
		{
			problemTags[i].style.overflow = "hidden";
		}
	}
	
	// Hide flash in the viewport as it interfers with the overlay //
	
	var flashObjects = (document.getElementsByTagName("object") || document.getElementsByTagName("embed"));
	for (var i = 0;i<flashObjects.length;i++)
	{
		if (flashObjects[i].className != "viewOK")
		{
			flashObjects[i].style.visibility = "hidden";
		}
	}
	
	// Hide select tags in IE as they render through the overlay //
	
	if (BrowserDetect.browser=="Explorer")
		{
			var problemTagsForIE = document.getElementsByTagName("select");
			for (var i = 0;i<problemTagsForIE.length;s++)
			{
				problemTagsForIE[i].style.visibility = "hidden";
			}		
		}
					
	if (CurrentModalWindow != "none")
	{
		document.getElementById(overlayID).style.height = v.page.height + "px";
		document.getElementById(selectorId).style.top = (v.scroll.top+((v.window.height/100)*5)) + "px";
		multiWindow = true;
	}
	else
	{
		document.getElementById(overlayID).style.height = v.page.height + "px";
		document.getElementById(selectorId).style.top = (v.scroll.top+((v.window.height/100)*5)) + "px";
	}

	Effect.Appear(overlayID,
	{
		duration: 0.2,
		fps: 80,
		from: 0,
		to: 0.90,
		afterFinish:function()
		{
			Effect.BlindDown(selectorId,
			{
				delay: 0.1,
				duration: 2.0,
				afterFinish:function()
				{
					if (strStartId) {
						var arrA = $('previewImages').getElementsByTagName('a');
						showImage(arrA[strStartId]);
					} else {
						var objTemp = $('previewImages').firstChild.firstChild;
						showImage(objTemp);
					}
				}
			});
		}
	});
		
	CurrentModalWindow = selectorId;
}

foo = function(objImg)
{
	
	showImage(objImg);
}

closeModalWindow = function(selectorId)
{
	if (multiWindow!=true)
	{
		CurrentModalWindow = "none";
		var overlayID = 'overlay';
		
		Effect.BlindUp(selectorId,
		{
			duration: 0.7,
			fps: 80,
			afterFinish:function()
			{
				Effect.Fade(overlayID,
				{
					delay: 0.1,
					duration: 0.3,
					from: 0.90,
					to: 0.0,
					afterFinish:function()
					{
						if (BrowserDetect.browser=="Explorer")
						{
							var problemTagsForIE = document.getElementsByTagName("select");
							for (var i = 0;i<problemTagsForIE.length;s++)
							{
								problemTagsForIE[i].style.visibility = "visible";
							}		
						}
					}
				});
			}
		});
	}
	else
	{
		multiWindow = false;
	}
	
	// Turn scrollbars in the viewport back on //
	
	var problemTags = document.getElementsByTagName("textarea");
	for (var i = 0;i<problemTags.length;i++)
	{
		if (problemTags[i].className != "scrollOK")
		{
			problemTags[i].style.overflow = "auto";
		}	
	}
	
	// Restore flash in the viewport //
	
	var flashObjects = (document.getElementsByTagName("object") || document.getElementsByTagName("embed"));
	for (var i = 0;i<flashObjects.length;i++)
	{
		flashObjects[i].style.visibility = "visible";
	}
}

// Browser detection
// ----------------------------------------------
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));
	},
	dataBrowser: [
		{ 	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"
		}
	]

};
BrowserDetect.init();

// Attach functions to window onload
// ----------------------------------------------
Event.observe(window,'load',createOverlay);