
function uom(el, str)
{
    var newstr = '';
    if (!str.length) return false;
    for (var i = str.length - 1; i >= 0; i--) {
        newstr += str[i];
    }
    el.href = 'mailto:' + newstr;
    return true;
}

function create_modal()
{
    var pageSize = getPageSize();
    var c = document.createElement('DIV');
    c.id = 'modal';
    var dimw = pageSize[2];
    var dimh = pageSize[3];
    c.style.width = document.body.offsetWidth + 'px';
    c.style.height = pageSize[1] + 'px';
    document.body.appendChild(c);
}

function open_popup(url)
{
    var pageSize = getPageSize();
	create_modal();
    var i = document.createElement('IMG');
    i.className = 'bigimg';
    i.onclick = function() { closebig(this); };
    i.src = url;
    i.onload = function() {
		center_absolute(this);
        this.style.visibility = 'visible';
    };
    document.body.appendChild(i);
}
function center_absolute(el)
{
	var pageScroll = getPageScroll();
	var pageSize = getPageSize();
	var mv = (pageSize[3] - el.offsetHeight) / 2 + pageScroll[1];
	var mh = (pageSize[2] - el.offsetWidth) / 2 + pageScroll[0];
	el.style.top = mv + 'px';
	el.style.left = mh + 'px';
}

function closebig(el)
{
    var p = el.parentNode;
    p.removeChild(el);
    var m = document.getElementById('modal');
    document.body.removeChild(m);
}

function getPageScroll()
{
    var scrOfX = 0, scrOfY = 0;
    if (typeof(window.pageYOffset) == 'number') {
        //Netscape compliant
        scrOfY = window.pageYOffset;
        scrOfX = window.pageXOffset;
    } else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
        //DOM compliant
        scrOfY = document.body.scrollTop;
        scrOfX = document.body.scrollLeft;
    } else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
        //IE6 standards compliant mode
        scrOfY = document.documentElement.scrollTop;
        scrOfX = document.documentElement.scrollLeft;
    }
    return [scrOfX, scrOfY];
}

function getPageSize()
{
    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;
    }

    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
    if(yScroll < windowHeight){
        pageHeight = windowHeight;
    } else {
        pageHeight = yScroll;
    }

    // for small pages with total width less then width of the viewport
    if(xScroll < windowWidth){
        pageWidth = windowWidth;
    } else {
        pageWidth = xScroll;
    }
    return [pageWidth, pageHeight, windowWidth, windowHeight];
}

function generate_comment(id, name, comment, email, time, url)
{
	var d = document.createElement('DIV');
	d.innerHTML = comment;
	var p = document.createElement('P');
	p.appendChild(document.createTextNode('kakelde '));
	var n = null;
	if (email || url) {
		n = document.createElement('A');
		if (email) {
			n.href = 'mailto:' + email;
			n.onclick = function() { var s = new String(this); return uom(this, s.replace(/mailto:/, '')); };
		} else {
			n.href = url;
		}
		n.appendChild(document.createTextNode(name ? name : (email ? email : url)));
	} else {
		n = document.createTextNode(name);
	}
	p.appendChild(n);
	p.appendChild(document.createTextNode(' om ' + time));
	if (url) {
		p.appendChild(document.createTextNode(' | '));
		var u = document.createElement('A');
		u.href = url;
		u.appendChild(document.createTextNode('Website'));
		p.appendChild(u);
	}
	var li = document.createElement('LI');
	li.appendChild(d);
	li.appendChild(p);
	return '<li>' + li.innerHTML + '<\/li>';
}

function init()
{
	// do some initing
	var as = document.getElementsByTagName('A');
	var re = new RegExp("^http:\/\/" + document.domain.replace('/', '\/'));
	for (var i = 0; i < as.length; i++) {
		if (!as[i].href.match(re)) {
			as[i].onclick = function() {
				this.href = '/page/linkout.php?url=' + encodeURIComponent(this.href);
				return true;
			};
		}
	}
}

