
function Editable(name, initial, submitFcn) { // [?], [maxlength]
    this.current = initial;
    this.editing = initial;
	this.display_blank = false;
	this.set_up_input = function (x) { }
	this.name = name;
    this.old = null;
	this.div = getObject(name);
	this.maxLength = arguments[4] ? arguments[4] : 64;

	this.display = function() {
	    clearChildren(this.div);
		var a = createElement(this.div, "a");
		a.className = "editabledisplay";
		a.href = "#";
		createText(a, this.current);
		var editable = this;
		a.onclick = function() { editable.edit(); return false; }
    }
	this.edit = function() {
		var editable = this; 
	    clearChildren(this.div);
		var frm = createElement(this.div, "form");
		
		var inp = createInput(frm, "text");
		if (!this.display_blank) {
			inp.value = this.editing;
		}
		inp.className = "editableinput";
		inp.size = 8;
		inp.maxLength = this.maxLength;

		var imgbutton = createElement(frm, "img");
		imgbutton.src = "/images/ok_button.gif";
		imgbutton.style.margin = "2px 0 0 3px";

		this.set_up_input(inp);

		frm.onsubmit = function () {
			editable.editing = inp.value;
			if (inp.value == editable.current) {
				editable.display();
			}
			else {
				inp.disabled = true;
				submitFcn(editable);
			}

			return false;
		}
		inp.onkeyup = function(e) {
		    if (getKeyCode(e) == 27) {
				editable.cancel(); 
			}
			return false;
		}
		inp.onblur = function() {
			frm.onsubmit();
			return true;
		}
		setTimeout(function() { inp.focus();}, 0);
	}

	this.success = function() {
		if (!this.display_blank) {
			this.old = this.current;
			this.current = this.editing;
		}
		this.cancel();
	}

	this.failure = function() {
		this.edit();
	}

	this.cancel = function() {
		this.display();
	}

}


function getKeyCode(e) {
	if (window.event) {
		return window.event.keyCode;
	}
	else if (e) {
		return e.which;
	} else {
		return null;
	}
}

var letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
var numbers = "0123456789";
var email_chars = letters + numbers + "@-+_.&";
var name_chars = letters + numbers + " @-+:'&!*,.;";
var nickname_chars = letters + numbers + "-";
var group_chars = letters + numbers + "-"; 

var isSpecialKeyCode = function(key) {
    return (key==null) || (key==0) || (key==8) || (key==9) || (key==13) || (key==27);
};

var limitTextAreaTo = function(ta, event, limit) {
	var key = getKeyCode(event);
	return isSpecialKeyCode(key) || (ta.value.length< limit);
};

var restrictTo = function(e, chars) {
   var key = getKeyCode(e);
   if (isSpecialKeyCode(key)) {
      return true;
   }
   var keychar = String.fromCharCode(key).toLowerCase();
   return chars.indexOf(keychar) > -1;
};


function LiveEditable(name, maxlength, url, legal_chars, finalfunction, textarea, width) {
	this.gbRE = new RegExp('^Guestbook-[0-9]+$');
	this.current = null;
	this.editing = null;
	this.legal_chars = legal_chars;
	this.old = null;
	this.display_blank = false;
	this.set_up_input = function (x) { }
	this.name = name;
	this.div = getObject('le_' + name);
	this.maxLength = maxlength ? maxlength : 64;
	this.finalfunction = finalfunction;
	this.textarea = (textarea) ? textarea : false;		// should be a boolean
	this.is_editing = false;
	this.width = (width) ? width : 160;
	var editable = this;
	this.connection = new RstColumnConnection(url, name, function(v){
		if(name == 'name' && editable.gbRE.exec(v)){
			editable.current = 'Guestbook';
			editable.editing = 'Guestbook';
		} else {
			editable.current = v;
			editable.editing = v;
		}
		if (! editable.is_editing) {
			editable.display();
		}
	});
};

LiveEditable.prototype.display = function(newText) {
	this.is_editing = false;
	clearChildren(this.div);
	var a = createElement(this.div, "a");
	a.className = (this.textarea) ? "editableareadisplay" : "editabledisplay";
	a.style.width = this.width + "px";
	a.href = "#";
	createText(a, newText || this.current || "");
	var editable = this;
	a.onclick = function() { editable.edit(); return false; }
};

LiveEditable.prototype.edit = function() {
	this.is_editing = true;
	clearChildren(this.div);
	var frm = createElement(this.div, "form");
	
	var inp = createInput(frm, (this.textarea) ? "textarea" : "text");
	if (!this.display_blank) {
		inp.value = this.editing || "";
	}
	inp.className = (this.textarea) ? "editabletextarea" : "editableinput";
	inp.style.width = this.width + "px";
	inp.maxLength = this.maxLength;

	var imgbutton = createElement(frm, "img");
	imgbutton.src = "/images/ok_button.gif"
	imgbutton.style.cssFloat = "left";
	imgbutton.style.styleFloat = "left";
	imgbutton.style.margin = "2px 0 0 3px";

	imgbutton.onclick = function() { frm.onsubmit(); }

	this.set_up_input(inp);
	var editable = this;
	frm.onsubmit = function () {
		editable.editing = inp.value;
		if (inp.value == editable.current) {
			editable.display();
			editable.finalfunction && editable.finalfunction(false);
		}
		else {
			editable.display(inp.value);
			editable.connection.submit(inp.value,
		 	editable.finalfunction ? function() { editable.finalfunction(true); } : null);
		}
		return false;
	}
	inp.onkeypress = function(e) {
		return !editable.legal_chars || restrictTo(e, editable.legal_chars);
	};

	inp.onkeyup = function(e) {
		if (getKeyCode(e) == 27) {
			editable.cancel(); 
		}
		return false;
	}
	
//	inp.onblur = function() {
//		frm.onsubmit();
//		return true;
//	}

	setTimeout(function() { inp.focus();}, 0);
};

LiveEditable.prototype.success = function() {
	if (!this.display_blank) {
		this.old = this.current;
		this.current = this.editing;
	}
	this.cancel();
};

LiveEditable.prototype.failure = function() {
	this.edit();
};

LiveEditable.prototype.cancel = function() {
	this.display();
};


function editableField(name, fieldname, maxlength, url, legal_chars, finalfunction, textarea, width, linkText, renderFunction, initialValue) {

	this.gbRE = new RegExp('^Guestbook-[0-9]+$');
    this.current = null;
    this.editing = null;
    this.legal_chars = legal_chars;
    this.old = null;
	this.display_blank = false;
	this.set_up_input = function (x) { }
	this.name = name;
	this.linkText = linkText ? linkText : "edit";
	this.div = getObject('ef_' + name);
	this.maxLength = maxlength ? maxlength : 64;
	this.finalfunction = finalfunction;
	this.textarea = (textarea) ? textarea : false;		// should be a boolean
	this.is_editing = false;
	this.width = (width) ? width : 160;
	this.renderFunction = renderFunction || function(v) { return v || ""; };
	var editable = this;
	this.connection = new RstColumnConnection(url, fieldname, function(v){
		editable.current = v;
		editable.editing = v;
		if (! editable.is_editing) {
			editable.display();
		}
	}, initialValue);
};

editableField.prototype.display = function(newText) {
	this.is_editing = false;
	clearChildren(this.div)
	if(this.gbRE.exec(this.current)){
		this.current = 'Guestbook';
	}
	createText(this.div, this.renderFunction(newText || this.current));
	var editLink = createElement(this.div, "span");
	editLink.className = "editableFieldLink";
	createText(editLink, " (");
	var a = createElement(editLink, "a");
	createText(a, this.linkText);
	a.href = "#";
	createText(editLink, ")");
	var editable = this;
	a.onclick = function() { editable.edit(); return false; }
};

editableField.prototype.edit = function() {
	this.is_editing = true;
	clearChildren(this.div);
	var frm = createElement(this.div, "form");
	
	var inp = createInput(frm, (this.textarea) ? "textarea" : "text");
	if (!this.display_blank) {
		inp.value = this.editing;
	}
	inp.className = (this.textarea) ? "editabletextarea" : "editableinput";
	inp.style.width = this.width + "px";
	inp.maxLength = this.maxLength;

	var imgbutton = createElement(frm, "img");
	imgbutton.src = "/images/ok_button.gif"
	imgbutton.style.cssFloat = "left";
	imgbutton.style.styleFloat = "left";
	imgbutton.style.margin = "2px 10px 0 3px";

	imgbutton.onclick = function() { frm.onsubmit(); }

	this.set_up_input(inp);
	var editable = this;
	frm.onsubmit = function () {
		editable.editing = inp.value;
		if (inp.value == editable.current) {
			editable.display();
			editable.finalfunction && editable.finalfunction(false);
		}
		else {
			editable.display(inp.value);
			editable.connection.submit(inp.value,
			  editable.finalfunction ? function() { editable.finalfunction(true); } : null);
		}
		return false;
	}
	inp.onkeypress = function(e) {
		return !editable.legal_chars || restrictTo(e, editable.legal_chars);
	};

	inp.onkeyup = function(e) {
		if (getKeyCode(e) == 27) {
			editable.cancel(); 
		}
		return false;
	}
	
	setTimeout(function() { inp.focus();}, 0);
};

editableField.prototype.success = function() {
	if (!this.display_blank) {
		this.old = this.current;
		this.current = this.editing;
	}
	this.cancel();
};

editableField.prototype.failure = function() {
	this.edit();
};

editableField.prototype.cancel = function() {
	this.display();
};


var liveSetText = function(divname) {
	var div = getObject(divname);

	return function(v) {
		clearChildren(div);
		createText(div, v);
	};
}

function Transform(f, listener) {
	return function (v) {
		listener(f(v));
	}
}

var limitOptions = function(parentvalue, parentcheck, fieldid, fieldvalue, disabled) {

	limitField = getObject(fieldid);

	if (parentvalue == parentcheck) {
		limitField.value = fieldvalue;
		if (disabled) limitField.disabled = true;
	} else {
		limitField.disabled = false;
	}
}

// start ratings stuff //
var hideRatingText = function(cid, ciid){
	var _m = "_my_rating_" + cid + "_" + ciid;
	if (_m != 0){
		return;
	}
	title_div = $('ratings_title_' + cid + '_' + ciid)
	title_div.innerHTML = eval('_original_rating_title_' + cid + '_' + ciid);
}

var showRatingText = function(rating, cid, ciid){
	var _m = "_my_rating_" + cid + "_" + ciid;
	if (_m != 0){
		return;
	}
	title_div = $('ratings_title_' + cid + '_' + ciid)
	if (rating == 20){
		title_div.innerHTML = 'Poor';
	} else if (rating == 40) {
		title_div.innerHTML = 'Nothing Special';
	} else if (rating == 60) {
		title_div.innerHTML = 'Worth watching';
	} else if (rating == 80) {
		title_div.innerHTML = 'Pretty cool';
	} else if (rating == 100) {
		title_div.innerHTML = 'Awesome!';
	}
}

var submitRating = function(rating, cid, ciid){

	var _m = "_my_rating_" + cid + "_" + ciid;
	eval(_m = rating);

	asyncAction('channelajax',
				{
				'action'    : 'submit_rating', 
				'rating'    : rating, 
				 'cid'       : cid,
				 'ciid'      : ciid
				 } ,
			function(theResponse) {
				var tokens = theResponse.split(':');
				if (tokens[0] == 'PASS') {
					// thank the user //
					var title_div = $('ratings_title_' + cid + '_' + ciid);
					title_div.innerHTML = cl.Ratings.Thanks_for_rating;

					// update the stars //
					for (i=20; i<=100; i=i+20){
						if (rating >= i){
							var star = $('star_' + i + '_' + cid + '_' + ciid);
							star.src = "/images/star_on.gif";
						}
					}

					// update the count //
					var count_div = $('ratings_count_num_' + cid + '_' + ciid);
					count_div.innerHTML = (count_div.innerHTML * 1 + 1);
				} else {
					alert(theResponse);
				}
			});

}
// end ratings stuff //
