var allimages = new Array();

function init() {
    doReplacements();
}
    

function doReplacements() {

    var elems = document.getElementsByTagName("A");
    var replace = document.createElement("img");
    var lastimg;

    for (var i=0; i<elems.length; i++) {
	if (elems[i].className.substr(0,3) == "nav") {
	    var img = new Image();
	    if (elems[i].className == "nav bold")
		if (elems[i].href)
		    img.src = "/img/textbold/"+elems[i].firstChild.nodeValue.replace(/\s+/g,"%20")+".png";
		else
		    img.src = "/img/textbl/"+elems[i].firstChild.nodeValue.replace(/\s+/g,"%20")+".png";
	    else if (elems[i].href)
	        img.src = "/img/text/"+elems[i].firstChild.nodeValue.replace(/\s+/g,"%20")+".png";
	    else
	        img.src = "/img/textlight/"+elems[i].firstChild.nodeValue.replace(/\s+/g,"%20")+".png";
		
	    img.alt = elems[i].firstChild.nodeValue;
	    img.border = 0;
	    img.align = "top";
	    img.where = elems[i];
	    // if (allimages.length) allimages[allimages.length-1].onload = null;
	    // img.onload = function() { alert(this.fsd); }
	    elems[i].replaceChild(img, elems[i].firstChild);
	    allimages[allimages.length++] = img;
	    lastimg = img;
	}
    }
    // lastimg.onload = finalize;
}

var done = false;
function finalize() {
    if (done) return false;
    for (var i=0; i<allimages.length; i++)
	allimages[i].where.replaceChild(allimages[i], allimages[i].where.firstChild);

    done = true;
    return false;
}


var globalid = 0;

function pop(obj) {
    var width=(arguments.length>1)?arguments[1]:336;
    var height=(arguments.length>2)?arguments[2]:500;
    var rval=(arguments.length>3)?arguments[3]:false;
    var href = (typeof obj == "object")?obj.href:obj;
    var winref = window.open(href, 'image', 'width='+width+',height='+height+',toolbar=no,location=no,directories=no,status=no,resizeable=no,scrollbars=no');
    winref.focus();
    if (rval !== null) return rval;
}
function pop2(obj) {
    var width=(arguments.length>1)?arguments[1]:336;
    var height=(arguments.length>2)?arguments[2]:500;
    var rval=(arguments.length>3)?arguments[3]:false;
    var href = (typeof obj == "object")?obj.href:obj
    var winref = window.open(href, 'image2', 'width='+width+',height='+height+',toolbar=no,location=no,directories=no,status=no,resizeable=no,scrollbars=no');
    winref.focus();
    if (rval !== null) return rval;
}

function contentpopup(id) {
    var width=(arguments.length>1)?arguments[1]:500;
    var height=(arguments.length>2)?arguments[2]:600;
    var href = '/contentpopup.php?'+id;
    var winref = window.open(href, 'contentpopup', 'width='+width+',height='+height+',toolbar=no,location=no,directories=no,status=no,resizeable=no,scrollbars=1');
}


function popup_control(return_url, safe_classes) {
    document.onclick = function(e) {
	if (!e) var e = window.event;
	if (e.target) targ = e.target;
	else if (e.srcElement) targ = e.srcElement;
	if (targ.nodeType == 3) // defeat Safari bug
	    targ = targ.parentNode;
	
	while (targ && (!targ.tagName || targ.tagName.toLowerCase()!="body")) {
	    if (targ.id) {
		for (var i in safe_classes) if (targ.id == safe_classes[i]) return;
	    }
	    targ = targ.parentNode;
	}

	if (!document.location.href.match(new RegExp(return_url+"$"))) {
	    document.location.href = return_url;
	}
    }
}


function delimg(id) {
  if (!confirm('Are you sure you want to delete this image?'+id)) return;

  var x = document.getElementById('img_'+id);
  if (!x) return;

  x.parentNode.removeChild(x);
}

var lastid;

function addItem(caption) {
    var l = document.getElementById("editform");
    if (!caption) caption = "item";
    l.getElementsByTagName("H1")[0].innerHTML = "Add "+caption;
    l.style.display = "block";
    document.getElementById("editform_id").value = "";
}

function editItem(id, values, caption) {
    if (lastid) {
        var i = document.getElementById("item_"+lastid);
        if (i) i.style.backgroundColor = "";
    }
    lastid = id;
    var i = document.getElementById("item_"+id);
    if (i) i.style.backgroundColor = "#666666";

    var l = document.getElementById("editform");
    if (!caption) caption = "item";
    l.getElementsByTagName("H1")[0].innerHTML = "Edit "+caption;
    l.style.display = "block";
    document.getElementById("editform_id").value = id;

    if (typeof values == "object") {
        for(var k in values) {
            var e = document.getElementById("editform_"+k);
	    try {
            if (e) e.value = values[k];
	    } catch (e)  {}
        }
    }
}

function cancelItem() {
    document.getElementById('editform').style.display='none';
    if (lastid) {
        var i = document.getElementById("item_"+lastid);
        if (i) i.style.backgroundColor = "";
        lastid = false;
    }
}

