/*****************************************************************
 * CakeMix Library - Copyright 2005 The Creative Cake
 * This code is owned and licenced by The Creative Cake
 * No unauthorised copying or distribution is allowed
 *
 * cakemix/js/cakemix_inputs.js
 * Input related JS functions
 *
 * Created By: Jonathan Lozinski, 2006-01-19
 * Last Edit: Jonathan Lozinski, 2006-01-19
 ******************************************************************/

function CakemixSwapTableRows(table, row1, row2) {
	rows = table.rows;
	var r1 = rows[row1].cloneNode(true);
	var r2 = rows[row2].cloneNode(true);
	swapDOM(rows[row1], r2);
	swapDOM(rows[row2], r1);
}

function CakemixTableRestyleRows(table) {
	trows = table.rows;
	styles = Array("odd", "even");
	curstyle = 0;
	for(i=0; i < trows.length; i++) {
		trows[i].className = styles[curstyle];
		curstyle = 1-curstyle;
	}
}

/* dialog */
var CakeMixDialogHeight;

function CakeMixDialogSlideOn(start) {
	//slide on to the screen a-la mac os/x
	dlog = getElement("CakeMixDialog");
	//alert(CCMSDialogHeight);
	if(start) {
		//createLoggingPane(true);
		dlog.style.overflow = "hidden";
		dlog.style.height = "1px";
		dlog.style.visibility = "visible";
		//alert(dlog.scrollHeight);
		//if(isIE()) {
		//} else {
			dlog.style.opacity = 0;
		//}
		setTimeout("CakeMixDialogSlideOn()", 1);
	} else {
		var height = Number(dlog.style.height.replace("px", ""));
		height += 10 ;
		opac = height/CakeMixDialogHeight;
		//if(isIE()) {
		//} else {
			dlog.style.opacity = opac;
		//}
		//log(height + ", vs, " + CCMSDialogHeight);
		if(height < CakeMixDialogHeight) {
			dlog.style.height = height.toString() + "px";
			setTimeout("CakeMixDialogSlideOn()", 1);
		} else {
			dlog.style.height = CakeMixDialogHeight.toString() + "px";
		}
	}
}

function CakeMixDialogShow(message, icon, buttons) {
	dlog = getElement("CakeMixDialog");
	dlogicon = getElement("CakeMixDialog_icon");
	dlogmessage = getElement("CakeMixDialog_message");
	dlogbuttons = getElement("CakeMixDialog_buttons");
	if(icon) {
		replaceChildNodes(dlogicon,
			IMG({"src":icon}, null)
			);
	}
	if(buttons) {
		replaceChildNodes(dlogbuttons, buttons);
	}
	if(message) {
		replaceChildNodes(dlogmessage, message);
	}
	dlog.style.visibility = "hidden";
	dlog.style.display = "block";
	CakeMixDialogHeight = dlog.clientHeight-40;
	CakeMixDialogSlideOn(true);
}

function CakeMixYesNoDialog(message, yesfunc, nofunc) {
	yes = "javascript:CakeMixDialogCallback(" + yesfunc + ")";
	no = "javascript:CakeMixDialogCallback(" + nofunc + ")";
	var buttons = UL( null,
		LI(null, A({'href':yes}, "Yes")),
		LI(null, A({'href':no}, "No"))
	);
	CakeMixDialogShow(message, "/lib/cakemix/template/images/icon-dialog-question.gif", buttons);
}

function CakeMixDialogClear() {
	dlog = getElement("CakeMixDialog");
	dlog.style.display = "none";
	dlog.style.height = "";
}

function CakeMixDialogCallback(callback) {
	// call back and clear message
	if(callback) callback;
	CakeMixDialogClear();
}

function CakeMixSwapElements(elem1, elem2) {
	if(typeof(elem1) == "string") {
		elem1 = getElement(elem1);
	}
	
	if(typeof(elem2) == "string") {
		elem2 = getElement(elem2);
	}

	placer1 = DIV();
	placer2 = DIV();
	parent1 = elem1.parentNode;
	parent2 = elem2.parentNode;
	e_old = parent1.replaceChild(placer1, elem1);
	e_new = parent2.replaceChild(placer2, elem2);
	swapDOM(placer1, e_new);
	swapDOM(placer2, e_old);
	//replacedNode = parentNode.replaceChild(newChild, oldChild);
}

/* Inputs for inclusion */
function CakeMixSelectInput(name, values, selected, attributes) {
	if(isNull(attributes)) {
		attributes = {};
	}
	attributes.name = name;

	sel = SELECT(attributes);

	for(i in values) {
		if(i == selected) {
			opt = OPTION({value:i, 'selected':"selected"}, values[i]);
		} else {
			opt = OPTION({value:i}, values[i]);
		}
		appendChildNodes(sel, opt);
	}
	return sel;
}
