var _resImg = 'res/img/'
var _resImgArrows = 'res/img/arrows/'

var bypixels, addpixels, multpad;
var n = navigator.userAgent;
if(/Opera/.test(n)) {
	bypixels = 2;
	addpixels = 22;
	multpad = 1;
}
else if(/Firefox/.test(n)) {
	bypixels = 3;
	addpixels = 1;
	multpad = 0;
}
else if(/MSIE/.test(n)) {
	bypixels = 2;
	addpixels = 22;
	multpad = 1;
}



// parses a string and replaces %n% with argument nr n
function parseTemplateString (sTemplate) {
	var args = arguments;
	var s = sTemplate;
	
	s = s.replace(/\%\%/g, "%");
	
	for (var i = 1; i < args.length; i++)
		s = s.replace( new RegExp("\%" + i + "\%", "g"), args[i] )
	
	return s;
}

function getElementsByClassName(strClassName, strTagName, oElm){
    var arrElements = (strTagName == "*" && document.all)? document.all : oElm.getElementsByTagName(strTagName);
    var arrReturnElements = new Array();
    strClassName = strClassName.replace(/\-/g, "\\-");
    var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
    var oElement;
    for(var i=0; i<arrElements.length; i++){
        oElement = arrElements[i];      
        if(oRegExp.test(oElement.className)){
            arrReturnElements.push(oElement);
        }   
    }
    return (arrReturnElements)
}

// --------------------------------------------------------------------------------
// cookies
// --------------------------------------------------------------------------------
function getCookie(name) {
    if(document.cookie.length > 0) {
        var c_start = document.cookie.indexOf(name + '=');
        if(c_start != -1) {
            c_start = c_start + name.length + 1;
            var c_end = document.cookie.indexOf(';', c_start);
            if(c_end == -1)
                c_end = document.cookie.length;
            return unescape(document.cookie.substring(c_start, c_end));
        }
    }
    return null;
}
function setCookie(name, value, expires, path, domain, secure) {
	var today = new Date();
	today.setTime(today.getTime());
	
	/*
	if the expires variable is set, make the correct 
	expires time, the current script below will set 
	it for x number of days, to make it for hours, 
	delete * 24, for minutes, delete * 60 * 24
	*/
	if (expires) {
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date(today.getTime() + (expires));
	
    // var exdate = new Date();
    // exdate.setDate(exdate.getDate() + expiredays);
    // ((expiredays == null) ? '' : ';expires=' + exdate) + 
    
    document.cookie = name + '=' + escape(value) + 
    	((expires) ? ";expires=" + expires_date.toGMTString() : '') +
		((path) ? ';path=' + path : '') + 
		((domain) ? ';domain=' + domain : '') +
		((secure) ? ';secure' : '');
}
function deleteCookie(name, path, domain) {
	if (getCookie(name))
		document.cookie = name + '=' +
			((path) ? ';path=' + path : '') +
			((domain) ? ';domain=' + domain : '') +
			';expires=Thu, 01-Jan-1970 00:00:01 GMT';
}

// --------------------------------------------------------------------------------
// event listener
// --------------------------------------------------------------------------------
function addEvent(obj, evType, fn)
{
	if (obj.addEventListener) {
		obj.addEventListener(evType, fn, false);
		return true;
	} else if (obj.attachEvent) {
		var r = obj.attachEvent('on'+evType, fn);
		return r;
	} else {
		return false;
	}
}

// event listeners eventually to replace the one above
function addEventToObject(obj, evt, func)
{
	var oldhandler = obj[evt];
	if (typeof obj[evt] != 'function') {
		obj[evt] = func;
	} else {
		obj[evt] = function()
			{
			oldhandler();
			func();
			}
	}
}

// --------------------------------------------------------------------------------
// highlight comments using url anchors
// --------------------------------------------------------------------------------
// find appropriate text in the URL anchor string
var thisURL = '' + document.location;
var hashPos = thisURL.indexOf('#');
if (hashPos >= 0)
	{
	var anchorTxt = thisURL.substring(hashPos+1, thisURL.length);
	// set regex to find comment anchor
	var cmntTxt = /cmnt([0-9]+)/.test(anchorTxt)
	var runFade = (anchorTxt == 'cmnt-holder' || cmntTxt == true) ? 1 : 0;
	}


// --------------------------------------------------------------------------------
// utility functions
// --------------------------------------------------------------------------------
function printit(){window.print()}

function px(w, h){document.write('<img src="' + _resImg + 'px.gif" width="' + w  + '" height="' + h + '"/>')}

function addbookmark() {
	var url = location.toString();
	var title = document.title;
	if (document.all) window.external.addFavorite(url, title);
}

function urlAddParam(url, param, value) {
	if (param != "" && value != "") {	
		if (url.substring(url.length - 1) != "?") {
			if (url.indexOf("?") > 0)
				url += "&"
			else	url += "?";
		}
		url += param + "=" + escape(value);
	}
	return url;
}

function pickRandom(range) {
	if(Math.random) {
		return Math.round(Math.random() * (range - 1));
	} else {
		var now = new Date();
		return (now.getTime() / 1000) % range;
	}
}

function newWindow(url, name, w, h, scroll) {
	var win = null;
	if (url == '') return;
	leftPos = (screen.width) ? (screen.width - w) / 2 : 0;
	topPos = (screen.height) ? (screen.height - h) / 2 : 0;
	settings = 'height=' + h + ',width=' + w + ',top=' + topPos + ',left=' + leftPos + ',scrollbars='+scroll+',resizable';
	win = window.open(url, name, settings);
}

function openWindow(url, w, h, ws, dialog) {
	var windowStyle;
	var windowParent = {};
	if (w == null) w = 800;
	if (h == null) h = 600;
	
	if (url == '') return
			
	if (dialog == true) {
		windowStyle = 'dialogWidth:' + w + 'px;dialogHeight:' + h + 'px;help:no;status:yes;'; //scroll:no;resizable:yes;
		if (ws) {windowStyle += ',' + ws}
		window.showModalDialog(url, windowParent, windowStyle);
	} else {
		windowStyle = 'width='+ w +',height='+ h +',screenX=50,left=50,screenY=50,top=50';
		if (ws) {windowStyle += ',' + ws}
		var newWin = window.open(url, "popup", windowStyle);
		newWin.opener = window;
		newWin.focus()
	}
	
	// window.open(URL, windowName , "width=" + width + ",height=" + height + ",scrollbars,location=yes,menubar=yes,status=yes,toolbar=yes,left=" + left +",top=" + top);
    // window.open(sUrl, "_blank", "top=" + y + ",left=" + x + ",width=" + w + ",height=" + h + ",location=no,menubar=no,directories=no,toolbar=no,status=no,titlebar=no,resizable=no,scrollbars=no,dependent=no,hotkeys=0")
}

// return s.replace(/\'/g, "\\'").replace(/\\/g, '\\\\').replace(/\r/g, '').replace(/\n/g, '\\n').replace(/\./, '\\.');

// auto maximize
/*
top.window.moveTo(0,0);
if (document.all) {
	top.window.resizeTo(screen.availWidth,screen.availHeight);
}
else if (document.layers||document.getElementById) {
	if (top.window.outerHeight<screen.availHeight||top.window.outerWidth<screen.availWidth) {
		top.window.outerHeight = screen.availHeight;
		top.window.outerWidth = screen.availWidth;
	}
}
*/

// escapes regular expression characters
RegExp.escape = function(text) { // http://simon.incutio.com/archive/2006/01/20/escape
  if (!arguments.callee.sRE) {
    var specials = ['/', '.', '*', '+', '?', '|','(', ')', '[', ']', '{', '}', '\\'];
    arguments.callee.sRE = new RegExp('(\\' + specials.join('|\\') + ')', 'g');
  }
  return text.replace(arguments.callee.sRE, '\\$1');
}

/**
 * COMMON DHTML FUNCTIONS
 * These are handy functions I use all the time.
 *
 * By Seth Banks (webmaster at subimage dot com)
 * http://www.subimage.com/
 *
 * Up to date code can be found at http://www.subimage.com/dhtml/
 *
 * This code is free for you to use anywhere, just keep this comment block.
 */

/**
 * X-browser event handler attachment and detachment
 * TH: Switched first true to false per http://www.onlinetools.org/articles/unobtrusivejavascript/chapter4.html
 *
 * @argument obj - the object to attach event to
 * @argument evType - name of the event - DONT ADD "on", pass only "mouseover", etc
 * @argument fn - function to call
 */
function addEvent(obj, evType, fn){
 if (obj.addEventListener){
    obj.addEventListener(evType, fn, false);
    return true;
 } else if (obj.attachEvent){
    var r = obj.attachEvent("on"+evType, fn);
    return r;
 } else {
    return false;
 }
}
function removeEvent(obj, evType, fn, useCapture){
  if (obj.removeEventListener){
    obj.removeEventListener(evType, fn, useCapture);
    return true;
  } else if (obj.detachEvent){
    var r = obj.detachEvent("on"+evType, fn);
    return r;
  } else {
    alert("Handler could not be removed");
  }
}

