window.onload = function() {
    initPopups();
    loopLinks();
}

function demung(email)
{
	if (email.indexOf("mailto:") == 0)
	{
		email = email.substring("mailto:".length, email.length);
	}
	var lastDot = email.lastIndexOf(".");
	var numb = parseInt(email.substring(lastDot+1, email.length));
	email = email.substring(0, lastDot);
	var email1 = email.substring(0, numb);
	var email2 = email.substring(numb, email.length);
	email = email1 + "@" + email2;
	return "mailto:" + email;
}

function loopLinks()
{
	var l;
	if (document.links) l = document.links;
	else return;
	for (var i=0; i<l.length; i++)
	{
		if (l[i].href.indexOf("mailto:") == 0 && l[i].href.indexOf("@") == -1)
		{
			l[i].href = demung(l[i].href);
		}
	}
}

function showImage(url,width,height,title)
{
	if (!title) title="Picture";
	var html = "<html><head><title>" + title + "</title></head>";
	html += "<body style='margin:0px;'><img src='" + url + "' width='" + width + "' height='" + height + "' title='" + title + "' /></body>";
	html += "</html>";
	var newWin = window.open("about:blank","_newWin","width=" + width + ",height=" + height + ",left=100,top=100");
	newWin.document.open();
	newWin.document.write(html);
	newWin.document.close();
	newWin.focus();
}

function initPopups() {
    var as = document.getElementsByTagName('A');
	for (var i=0; i<as.length; i++) {
        if (as[i].className.match(/\bpopup\b/)) {
            if (!as[i].title) {
                var alt = getLinkImgAlt(as[i]);
                if (alt) {
                    as[i].title = alt;
                }
                else {
                    var tmatch = as[i].href.match(/\/([^\/]+)$/);
                    if (tmatch) { as[i].title = tmatch[1]; }
                }
            }
            as[i].onclick = function(e) {
                var matchArr = this.className.match(/\b(\d+)x(\d+)\b/i);
                var href = this.href;
                var targ = "popup";
                var atts = "width=600,height=400,left=0,top=0,resizable,scrollbars";
                var matchArr = this.className.match(/\b(\d+)x(\d+)\b/i);
                if (matchArr) {
                    var x = parseInt(matchArr[1]);
                    var y = parseInt(matchArr[2]);
                    if (document.body.mergeAttributes) {
                        x+=16;y+=4;
                    }
                    var atts = "width="+x+",height="+y+",left=0,top=0,resizable,scrollbars";
                    var targ = x+"x"+y+"_popup";
                }
                var n = window.open(href,targ,atts);
                if (this.href.match(/(\.gif)|(\.jpg)|(\.jpeg)|(\.png)|(\.bmp)/i)) {
                    try {
                        n.location = "about:blank";
                        n.document.open();
                        n.document.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">"
                            +"<html xmlns=\"http://www.w3.org/1999/xhtml\"><head>"
                            +"<title>"+this.title+"</title></head><body style=\"margin:0px;padding:0px;\">"
                            +"<img src=\""+this.href+"\" alt=\""+this.title+"\"/>"
                            +"</body></html>\n\n");
                        n.document.close();
                    }
                    catch (err) {
                        n.location = this.href;
                    }
                }
                try{n.focus();}catch(err){}
                return false;
            }
        }
    }
}

function getLinkImgAlt(elem) {
    try {
        return elem.childNodes[0].getAttribute('alt');
    }
    catch (err){}
}

