function hmtfwGetOfsLeft(event) {
	if (event.offsetX) {
		return event.offsetX;
	} else {
		return event.layerX;
	}
}

function hmtfwGetOfsTop(event) {
	if (event.offsetY) {
		return event.offsetY;
	} else {
		return event.layerY;
	}
}

function hmtfwGetScrollLeft(obj) {
	return (obj.pageXOffset) ? obj.pageXOffset : obj.scrollLeft;
}

function hmtfwGetScrollTop(obj) {
	return (obj.pageYOffset) ? obj.pageYOffset : obj.scrollTop;
}

function hmtfwSetScrollLeft(obj, left) {
	if (obj.pageXOffset) {
		obj.pageXOffset= left;
	} else {
		obj.scrollLeft= left;
	}
}

function hmtfwSetScrollTop(obj, top) {
	if (obj.pageYOffset) {
		obj.pageYOffset= top;
	} else {
		obj.scrollTop= top;
	}
}

function hmtfwGetScrollWidth(obj) {
	var test1 = obj.scrollWidth;
	var test2 = obj.offsetWidth;
	return Math.max(test1, test2);
}

function hmtfwGetScrollHeight(obj) {
	var test1 = obj.scrollHeight;
	var test2 = obj.offsetHeight
	return Math.max(test1, test2);
}

function hmtfwGetScrollWindowWidth(obj) {
	var test1 = obj.scrollWidth;
	var test2 = obj.offsetWidth;
	return Math.min(test1, test2);
}

function hmtfwGetScrollWindowHeight(obj) {
	var test1 = obj.scrollHeight;
	var test2 = obj.offsetHeight
	return Math.min(test1, test2);
}

function hmtfwGetPageScrollLeft(doc) {
	// courtesy of quirksmode.org
	if (doc.documentElement && doc.documentElement.pageYOffset) { // all except Explorer
		return doc.documentElement.pageXOffset;
	}
	else if (doc.documentElement && doc.documentElement.scrollTop) {
	// Explorer 6 Strict
		return doc.documentElement.scrollLeft;
	} else if (doc.body) { // all other Explorers
		return doc.body.scrollLeft;
	}	
}

function hmtfwGetPageScrollTop(doc) {
	// courtesy of quirksmode.org
	if (doc.documentElement && doc.documentElement.pageYOffset) { // all except Explorer
		return doc.documentElement.pageYOffset;
	}
	else if (doc.documentElement && doc.documentElement.scrollTop) {
	// Explorer 6 Strict
		return doc.documentElement.scrollTop;
	} else if (doc.body) { // all other Explorers
		return doc.body.scrollTop;
	}	
}

function hmtfwLocationWithScrollRatio(href, objId) {
	var obj= gE(objId);
	var h= href;
	if (obj) {
		var path;
		var search;
		if (h.indexOf('?') > 0) {
			path= h.substring(0, h.indexOf('?'));
			search= h.substring(h.indexOf('?'), h.length);
		} else {
			path= h;
			search= '';
		}

		var pairs;
		var width= hmtfwGetScrollWidth(obj);
		var height= hmtfwGetScrollHeight(obj);
		var winwidth= hmtfwGetScrollWindowWidth(obj);
		var winheight= hmtfwGetScrollWindowHeight(obj);
		
		var url;
		if (hmtfwUseCookiesScrollRatio) {
			url= new hmtfwCookie();
		} else {
			url= new hmtfwURLSearch(search);
		}
		url.setValue(
			hmtfwScrollRatioHParamName, 
			((width == 0) ? '' : (hmtfwGetScrollLeft(obj) + winwidth / 2) / width)
		);
		url.setValue(
			hmtfwScrollRatioVParamName, 
			((height == 0) ? '' : (hmtfwGetScrollTop(obj) + winheight / 2) / height)
		);

		if (!hmtfwUseCookiesScrollRatio) {
			h= path + url.getSearch();
		}
	}
	location.href= h;
	return false;
}

function hmtfwRestoreScrollPosition(objId) {
	var obj= gE(objId);
	var h= location.search;
	if (obj) {
		var query;
		if (hmtfwUseCookiesScrollRatio) {
			query= new hmtfwCookie();
		} else {
			query= new hmtfwURLSearch(h);
		}
		var ratio;
		var max;
		var min;
		var ofs;
		
		ratio= query.getValue(hmtfwScrollRatioHParamName);
		max= hmtfwGetScrollWidth(obj);
		min= hmtfwGetScrollWindowWidth(obj);
		if (ratio && ratio.length > 0) {
			ofs= Math.max(0, Math.min(max - min, ratio * max - min / 2));
			hmtfwSetScrollLeft(obj, ofs);
		}
		ratio= query.getValue(hmtfwScrollRatioVParamName);
		max= hmtfwGetScrollHeight(obj);
		min= hmtfwGetScrollWindowHeight(obj);
		if (ratio && ratio.length > 0) {
			ofs= Math.max(0, Math.min(max - min, ratio * max - min / 2));
			hmtfwSetScrollTop(obj, ofs);
		}
	}
}

function hmtfwURLSearch(q, sep) {
	// parts courtesy of http://www.eggheadcafe.com/articles/20020107.asp
	if (q.length > 1) this.q = q.substring(1, q.length);
	else this.q = null;
	this.keyValuePairs = new Array();
	this.sep= sep || '&';
	if(this.q) {
		for(var i=0; i < this.q.split(this.sep).length; i++) {
			this.keyValuePairs[i] = hmtfwStringTrim(this.q.split(this.sep)[i]);
		}
	}

	this.getKeyValuePairs = function() { 
		return this.keyValuePairs; 
	}
	
	this.getValue = function(s) {
		for(var j=0; j < this.keyValuePairs.length; j++) {
			if(this.keyValuePairs[j].split("=")[0] == s) {
				return decodeURIComponent(this.keyValuePairs[j].split("=")[1]);
			}
		}
		return null;
	}

	this.setValue = function(param, value) {
		for(var j=0; j < this.keyValuePairs.length; j++) {
			if(this.keyValuePairs[j].split("=")[0] == param) {
				this.keyValuePairs[j]= param + '=' + encodeURIComponent(value);
				return false;
			}
		}
		this.keyValuePairs[this.keyValuePairs.length]= param + '=' + encodeURIComponent(value);
		return true;
	}
	
	this.getParameters = function() {
		var a = new Array(this.getLength());
		for(var j=0; j < this.keyValuePairs.length; j++) {
			a[j] = this.keyValuePairs[j].split("=")[0];
		}
		return a;
	}

	this.getLength = function() { 
		return this.keyValuePairs.length; 
	} 
	
	this.getSearch = function() {
		var s= '';
		var sep= '?';
		for(var j=0; j < this.keyValuePairs.length; j++) {
			s = s + sep + this.keyValuePairs[j];
			sep= this.sep;
		}
		return s;
	}
	
}

function hmtfwCookie() {
	// parts courtesy of http://www.eggheadcafe.com/articles/20020107.asp
	this.q= document.cookie || '';
	this.keyValuePairs = new Array();
	this.sep= ';';
	if(this.q) {
		for(var i=0; i < this.q.split(this.sep).length; i++) {
			this.keyValuePairs[i] = hmtfwStringTrim(this.q.split(this.sep)[i]);
		}
	}

	this.getKeyValuePairs = function() { 
		return this.keyValuePairs; 
	}
	
	this.getValue = function(s) {
		for(var j=0; j < this.keyValuePairs.length; j++) {
			if(this.keyValuePairs[j].split("=")[0] == s) {
				return decodeURIComponent(this.keyValuePairs[j].split("=")[1]);
			}
		}
		return null;
	}

	this.setValue = function(param, value) {
		this.setCookie(param, value);

		for(var j=0; j < this.keyValuePairs.length; j++) {
			if(this.keyValuePairs[j].split("=")[0] == param) {
				this.keyValuePairs[j]= param + '=' + encodeURIComponent(value);
				return false;
			}
		}
		this.keyValuePairs[this.keyValuePairs.length]= 
			param 
			+ '=' 
			+ encodeURIComponent(value);
		return true;
	}
	
	this.getLength = function() { 
		return this.keyValuePairs.length; 
	} 
	
	this.setCookie = function(param, value, path, domain, expiresGMT) {
		if (!path) path= '';
		if (!domain) domain= '';
		if (!expiresGMT) expiresGMT= '';
		document.cookie= 
			param + '=' + encodeURIComponent(value)
			+ condtext('; expires=', '', expiresGMT)
			+ condtext('; path=', '', path)
			+ condtext('; domain=', '', domain)
			;
	}
	
}

function hmtfwGetCookie(param) {
	var url= new hmtfwCookie();
	return url.getValue(param);
}

function hmtfwStringTrim(str) {
	if (!str) return str;
	return str.replace(/^\s+/, '').replace(/\s+$/, '');
}

var hmtfwScrollRatioHParamName= 'scrollRatioH';
var hmtfwScrollRatioVParamName= 'scrollRatioV';
var hmtfwUseCookiesScrollRatio= navigator.cookieEnabled == true;

