// 
//  documents.js
//  colinbate
//  
//  Created by Colin Bate on 2007-08-29.
//  Copyright 2007 rhuvium Information Services. All rights reserved.
// 

var headblock = null;
var thisblock = null;

function Block(t, i, s, p, n) {
	this.title = t;
	this.id = i;
	this.sort = s;
	this.prev = p;
	this.next = n;
	this.div = null;
}

Block.prototype.down=function() {
	if (this.next == null) return false;
	if (this.prev != null) this.prev.next = this.next;
	this.next.prev = this.prev;
	this.prev = this.next;
	if (this.next.next != null) this.next.next.prev = this;
	this.next = this.next.next;
	this.prev.next = this;
	if (this.prev.prev == null) return this.prev;
	return headblock;
}

Block.prototype.up=function() {
	if (this.prev == null) return false;
	if (this.next != null) this.next.prev = this.prev;
	this.prev.next = this.next;
	this.next = this.prev;
	if (this.prev.prev != null) this.prev.prev.next = this;
	this.prev = this.prev.prev;
	this.next.prev = this;
	if (this.prev == null) return this;
	return headblock;
}

Block.prototype.display=function(e, f) {
	if (this.div == null) {
		//alert("Creating div " + this.id);
		newDiv = document.createElement("div");
		newDiv.id = "block" + this.id;
		newDiv.className = "item";
		var msg = "<a href=\"javascript:void();\" class=\"sortbtn\" onclick=\"moveup(" + this.id + ");\">&#x2191;</a> ";
		msg = msg + "<a href=\"javascript:void();\" class=\"sortbtn\" onclick=\"movedown(" + this.id + ");\">&#x2193;</a> ";
		msg = msg + "<a href=\"category?c=" + this.id + "\">" + this.title + "</a>";
		//document.write(msg);
		newDiv.innerHTML = msg;
		newDiv.myself = this;
		this.div = newDiv;
	}
	e.appendChild(this.div);
	if (f.value != "") f.value = f.value + ":";
	f.value = f.value + this.id;
}

function Document (title) {
	this.title = title;
	this.editing = 0;
}

function clearElement(idname) {
	var element = document.getElementById(idname);
	while (element.firstChild) {
		element.removeChild(element.firstChild);
	}
}

function showall() {
	thisblock = headblock;
	elem = document.getElementById("blocks");
	tf = document.ro.neworder;
	tf.value = "";
	while (thisblock != null) {
		thisblock.display(elem, tf);
		thisblock = thisblock.next;
	}
}

function moveup(idnum) {
	idname = "block" + idnum;
	var e = document.getElementById(idname);
	node = e.myself;
	res = node.up();
	if (res != false) {
		headblock = res;
		btn = document.ro.subbtn;
		//btn.enabled = true;
		btn.value = "Save Sort Order";
	}
	clearElement("blocks");
	showall();
}

function movedown(idnum) {
	idname = "block" + idnum;
	var e = document.getElementById(idname);
	node = e.myself;
	res = node.down();
	if (res != false) {
		headblock = res;
		btn = document.ro.subbtn;
		//btn.enabled = true;
		btn.value = "Save Sort Order";
	}
	clearElement("blocks");
	showall();
}

function getID (id) {
	if (typeof id == "string") {
		obj = document.getElementById(id);
		str = id;
	} else {
		obj = id;
		str = obj.id;
	}
	return {'obj': obj, 'str': str};
}

function docEditBlock (id) {
	logmsg('Edit '+id);
	var b = getID(id);
	b.obj.old = b.obj.innerHTML;
	b.obj.innerHTML = '<textarea name="edit_'+b.str+'" style="width: 99%;" rows="10">'+b.obj.innerHTML+'</textarea><br><input type="button" value="Cancel" onclick="docCancelEdit(\''+b.str+'\');">';
}

function docEditDocTitle (id) {
	var title = getID(id);
	title.obj.old = title.obj.innerHTML;
	title.obj.innerHTML = '<input name="edit_'+title.str+'" style="width: 99%;" value="'+title.obj.innerHTML+'"><br><input type="button" value="Cancel" onclick="docCancelEdit(\''+title.str+'\');">';
}

function docCancelEdit (id) {
	var e = getID(id);
	e.obj.innerHTML = e.obj.old;
}

function logmsg (msg) {
	var logfile = document.getElementById('logfile');
	if (logfile) {
		logfile.innerHTML = logfile.innerHTML+'<br>'+msg;
	}
}

