/*[ Copyright 2006 Sean Colombo and Motive Force LLC.]*/
////
// Author: Sean Colombo
// Date: 6/18/06
//
// Universally applicable javascript functions followed by remindable-specific functions.
////

function el(id) {return document.getElementById(id);}

////
// Takes in a variable number of arguments.  Each should be the id of a focusable element (form element).
// Focuses the first empty item unless all are full in which case it focuses the first element.
//
// If the elements don't exist, this function does nothing (and does not generate JS errors).
////
function conditionalFocus(){
	if(arguments.length > 1){
		var first = el(arguments[0]);
		var found = false;
		for(var cnt=0; ((cnt<arguments.length) && (!found)); cnt++){
			var currEl = el(arguments[cnt]);
			if(currEl && currEl.value == ""){
				currEl.focus();
				found = true;
			}
		}
		if((!found) && (first)){
			first.focus();
		}
	}
}

////
// If the element with the given id is display='none', will set it to inline.  If set to anything else,
// will be changed to showType.  If showType is not set, it will default to inline.
////
function toggleDisplay(id, showType){
	if(!showType){
		showType = 'inline';
	}

	// If object was originally display:none in the CSS, it will show the first time as just display=='', but the width will be 0.
	var obj = el(id);
	if((obj.style.display == 'none') || ((obj.style.display=='') && (obj.offsetWidth==0))){
		obj.style.display = showType;
	} else {
		obj.style.display = 'none';
	}
}
// From quirksmode.org
function findPosX(obj){
	var curleft = 0;
	if (obj.offsetParent){
		while (obj.offsetParent){
			curleft += obj.offsetLeft;
			obj = obj.offsetParent;
		}
	} else if (obj.x){
		curleft += obj.x;
	}
	return curleft;
}
function findPosY(obj){
	var curtop = 0;
	if (obj.offsetParent){
		while (obj.offsetParent){
			curtop += obj.offsetTop;
			obj = obj.offsetParent;
		}
	} else if (obj.y){
		curtop += obj.y;
	}
	return curtop;
}












// Triggered on the click of the button.
function re_startDelete(userId, reId){
	if(confirm('Are you sure you want to delete this reminder?\n\nOK = Delete it!\nCANCEL = Oops, leave it alone')){
		re_sendDelete(userId, reId);
	}
} // end startDelete()

// Requests that the id be deleted.
function re_sendDelete(userId, reId){
	acf_send('obj=remindable__' + userId + '&func=destroy&id=' + reId);
} // end sendDelete()

// When the request is recieved back, this will delete the div of the reminder.
function re_finishDelete(userId, reId){
	var allReminders = el('reminders_'+userId);
	var reminder = el('re_'+userId+'_'+reId);
	if(is_ie){
		new Effect.Fade(reminder);
	} else if(is_opera){
		//new Effect.Fade(reminder);
		allReminders.removeChild(reminder);
	} else {
		new Effect.BlindUp(reminder);
	}
} // end finishDelete()

// Display info about the reminder
function re_info(userId, reId){
	var id = 're_info_'+userId+'_'+reId;

	// Opera needs to position this at run-time.
	if(is_opera){
		var theInfoDiv = el('theInfoDiv');
		if((theInfoDiv.style.left=='') || (theInfoDiv.style.left == '-1000px')){
			var infoLi = el(id);
			var reminder = el('re_'+userId+'_'+reId);
			var offsetLeft = findPosX(reminder);
			var offsetTop = findPosY(reminder) + reminder.offsetHeight;
			theInfoDiv.innerHTML = infoLi.innerHTML;
			theInfoDiv.style.left = offsetLeft + 'px';
			theInfoDiv.style.top = offsetTop + 'px';
		} else {
			theInfoDiv.style.left = '-1000px';
			theInfoDiv.style.top = '-1000px';
		}
	} else {
		toggleDisplay(id, 'block');
	}
} // end re_info()
