function setupA() {
    if (document.getElementsByTagName) {
	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors.length; i++) {
	    var anchor = anchors[i];
	    c = anchor.className.split(" ").pop();
	    if (anchor.getAttribute("href") && c == "blankWindow") {
		anchor.target = "_blank";
	    } else if (anchor.getAttribute("href") && c == "toPageTop" && window.pageYOffset != null) {
		anchor.onclick = function() {
		    pageTopId = setInterval("toPageTop()", 15);
		    return false;
		}
	    }
	}
    }
}

function setupButton() {
    if (document.getElementsByTagName) {
	var inputs = document.getElementsByTagName("input");
	for (var i=0; i<inputs.length; i++) {
	    var input = inputs[i];
	    if (input.type == "button" && input.onclick == undefined) {
		input.onclick = function() {
		    buttonFunc(this);
		}
		//input.onclick = buttonFunc;
		//input.onclick = "buttonFunc(this);";
	    }
	}
    }
}

function buttonFunc(obj) {
    func = new Function("arg", "func_"+obj.name+"(arg);");
    func(obj);
}

function func_preview(obj) {
    form = getParentForm(obj);
    action = form.action;
    target = form.target;
    if (action.indexOf("admin")) {
	var re = new RegExp("admin/(.*?)/(.*?)/");
	newaction = action.replace(re, "admin/$1/preview/$2/");
	form.target = "preview";
	form.action = newaction;
	form.submit();
    }
    form.target = target;
    form.action = action;
}

function getParentForm(obj) {
    if (obj.nodeName == "FORM") {
	return obj;
    } else {
	return getParentForm(obj.parentNode);
    }
}

var pageTopId;
function toPageTop() {
    var y = window.pageYOffset/2;
    if (y < 1) {
	y = 0;
	clearInterval(pageTopId);
    }
    window.scrollTo(window.pageXOffset, y);
}

function init() {
    setupA();
    setupButton();
    startList();
    relTags();
    if(document.getElementById && document.createTextNode) {
	if (window.ul2finder) {
	    ul2finder();
	}
    }
}

window.onload = function() {
    init();
}


// JavaScript Document
function startList() {
    if (document.all&&document.getElementById) {
	navRoot = document.getElementById("navi");
	if (navRoot) {
	    for (i=0; i<navRoot.childNodes.length; i++) {
		node = navRoot.childNodes[i];
		if (node.nodeName=="LI") {
		    node.onmouseover=function() {
			this.className+=" over";
		    }
		    node.onmouseout=function() {
			this.className=this.className.replace(" over", "");
		    }
		}
	    }
	}
    }
}

function setUPFilename(id, filename) {
    var div = document.getElementById(id);
    if (div) {
	div.value = filename;
    }
}

function addLink(id, title, href, flag) {
    var value = title+"__"+href;
    var label = title+" -> "+href;
    var limit = 70;
    if (label.length > limit) {
	var last = limit - title.length-6;
	label = title+" -> "+href.substr(0, last)+"...";
    }
    var select = document.getElementById("s"+id);
    if (select) {
	o = document.createElement("option");
	o.value = value;
	o.text = label;
	select.appendChild(o);
	if (flag) {
	    select.selectedIndex = select.length-1;
	}
    }
    return true;
}

function relTags() {
	if (document.getElementsByTagName) {
		var anchors = document.getElementsByTagName("a");
		for (var loop = 0; loop < anchors.length; loop++) {
			var anchor = anchors[loop];
			if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") {
				anchor.target = "_blank";
			} else if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "popup") {
				anchor.onclick = function () {
					openPopUpWindow(this);
					return false;
				};
			}
		}
	}
}
