/*

Various javascript page functions, including rollover images and popups

*/

var ttpopup = function() {

/*

This has been tested for IE5.5, IE6 IE7, Firefox 2, Firefox 3, Safari 3

*/

	function addLoadEvent(func) {
		var oldonload = window.onload;
		if (typeof window.onload != 'function') {
			window.onload = func;
		} else {
			window.onload = function() {
				if (oldonload) {
					oldonload();
				}
				func();
			}
		}
	}

	function loadPopups() {
		var poplink;
		var ua = navigator.userAgent.toLowerCase();
		var debug = 0;

		// only use tested browsers
		if ( document.all || navigator.userAgent.indexOf("Firefox")!=-1 || ua.indexOf("safari/")!=-1 ) {

			for (var i = document.links.length - 1, poplink = null; (i>=0); i--) {
				poplink = document.links[i];
				if ( poplink.getAttribute("popup") != "" && poplink.getAttribute("popup") != null && poplink.getAttribute("popwidth") > 0 && poplink.getAttribute("popheight") > 0 ) {
					poplink.onclick = function() { return ttpopup.popup(this.getAttribute("popup"), (parseInt(this.getAttribute("popwidth"))), (parseInt(this.getAttribute("popheight"))), this.getAttribute("title")); }
					
					poplink.href = "javascript:void(0);";	// for IE6 and Safari

					//useful for getting the width and height of each popup image
					if ( debug ) {
						poplink.onmouseover = function() { var img = new Image(); img.src=this.getAttribute("popup"); document.title = 'Popup: Width: '+img.width+', Height: '+img.height; }
						poplink.onmouseout = function() { document.title = ''; }
					}
	
				} else if ( poplink.getAttribute("class") == "ttpopup" ) {
					poplink.setAttribute("popup", poplink.href);
					poplink.onclick = function() { return ttpopup.popup(this.getAttribute("popup"), (parseInt(this.getAttribute("popwidth"))), (parseInt(this.getAttribute("popheight"))), this.getAttribute("title")); }
					
					poplink.href = "javascript:void(0);";	// for IE6 and Safari

					//useful for getting the width and height of each popup image
					if ( debug ) {
						poplink.onmouseover = function() { var img = new Image(); img.src=this.getAttribute("popup"); document.title = 'Popup: Width: '+img.width+', Height: '+img.height; }
						poplink.onmouseout = function() { document.title = ''; }
					}
				}
			}
		}
	}

	return {
		// init function
		init: function() {
			addLoadEvent(loadPopups);
		},

		popup: function(popimgsrc, popwidth, popheight, poptitle) {

			var winW;
			var winH;

			if ( parseInt(popwidth) == 0 || parseInt(popheight) == 0 ) {

				// only Firefox and IE6/IE7 have been tested successfully for this
				if ( navigator.userAgent.indexOf("Firefox")!=-1 || document.all ) {

					// preload image to get width and height
					var img = new Image();
					img.src = popimgsrc;
					popwidth = img.width;
					popheight = img.height;

					// open a window of the appropriate size
					var childWindow = window.open(popimgsrc, '_blank', 'width='+popwidth+',height='+popheight);
					if ( childWindow != null && typeof(childWindow) != "undefined" ) {
		
						if ( document.all ) {
							winW = document.body.offsetWidth;
							winH = document.body.offsetHeight;
						} else {
							winW = window.innerWidth;
							winH = window.innerHeight;
						}
		
						// move the window, then re-write the contents
						childWindow.moveTo(Math.round((winW-popwidth)/2),Math.round((winH-popheight)/2));
						childWindow.document.write('<html><head><title>'+poptitle+'</title><style type="text/css">BODY { margin: 0px 0px 0px 0px; }</style></head><body><img src="'+popimgsrc+'"></body></html>');
						if ( childWindow.document.body.innerHTML == "" ) {
							childWindow.close();
							window.location.href=popimgsrc;
						}
					} else {
						document.location.href = popimgsrc;
					}
				} else {

					// this is the best thing when it doesn't work
					document.location.href = popimgsrc;

					/*winW = window.innerWidth;
					winH = window.innerHeight;

					// for non-tested browsers
					var childWindow = window.open(popimgsrc, '_blank', 'width='+(winW-50)+',height='+(winH-50));
					if ( childWindow ) {
		
						childWindow.document.write('<html><head><title>'+poptitle+'</title><style type="text/css">BODY { margin: 0px 0px 0px 0px; }</style></head><body><img id="popimage" name="popimage" src="'+popimgsrc+'"></body></html>');
					}*/
				}
			} else {
				var childWindow = window.open(popimgsrc, '_blank', 'width='+popwidth+',height='+(popheight));
				if ( childWindow != null && typeof(childWindow) != "undefined" ) {
	
					if ( document.all ) {
						winW = document.body.offsetWidth;
						winH = document.body.offsetHeight;
					} else {
						winW = window.innerWidth;
						winH = window.innerHeight;
					}
	
					childWindow.moveTo(Math.round((winW-popwidth)/2),Math.round((winH-popheight)/2));
					childWindow.document.write('<html><head><title>'+poptitle+'</title><style type="text/css">BODY { margin: 0px 0px 0px 0px; background-color: #B9B9B9; }</style>'+
					'</head><body><img name="popimage" id="popimage" src="'+popimgsrc+'"></body></html>');
				} else {
					document.location.href = popimgsrc;
				}
			}
			return false;
		}
	}

}();

ttpopup.init();

var ttroll = function() {

/*

This has been tested for IE5.5, IE6 IE7, Firefox 2, Firefox 3, Safari 3

*/

	function addLoadEvent(func) {
		var oldonload = window.onload;
		if (typeof window.onload != 'function') {
			window.onload = func;
		} else {
			window.onload = function() {
				if (oldonload) {
					oldonload();
				}
				func();
			}
		}
	}

	function loadRolls() {
		var img = null;
		var rollimgs = Array();
		
		for (var i = document.images.length - 1, img = null; (i>=0); i--) {
			img = document.images[i];

			if ( img.getAttribute("roll") != "" && img.getAttribute("roll") != null && img.getAttribute("unroll") != "" && img.getAttribute("unroll") != null ) {
				// queue to pre-load the rollover image
				rollimgs[rollimgs.length] = img.getAttribute("roll");

				// set the events
				if ( document.all ) {
					// for IE, we have to do something different
					img.attachEvent("onmouseover", function() { ttroll.roll(event.srcElement); });
					img.attachEvent("onmouseout", function() { ttroll.unroll(event.srcElement); });
				} else {
					img.setAttribute("onMouseOver", "ttroll.roll(this);");
					img.setAttribute("onMouseOut", "ttroll.unroll(this);");
				}
			}

		}

		// preload queued images
		for (var j=0;j<rollimgs.length;j++) {
			img = new Image;
			img.src = rollimgs[j];
		}
	}

	return {
		// init function
		init: function() {
			addLoadEvent(loadRolls);
		},

		// roll
		roll: function(pic) {
			pic.src = pic.getAttribute("roll");
		},

		unroll: function(pic) {
			pic.src = pic.getAttribute("unroll");
		}
	}

}();

ttroll.init();
