/**
 * Collabofit Javascript functions
 *
 * This makes use of the Protoype Javascript library (prototype.js) 
 * http://prototype.conio.net/
 *
 * $Id: collabofit.js 2007 2006-11-14 00:06:18Z gserafini $
 *
 */


/**
 * Append to any existing onload events, used from behaviour.js code
 */
function addLoadEvent(func) {
    var oldonload = window.onload;

    if (typeof window.onload != 'function') {
        window.onload = func;
    }
    else {
        window.onload = function() {
            oldonload();
            func();
        }
    }
}

/**
 * Show success message box
 */
function showSuccessMessage() {
	new Effect.Appear("message");
}

/**
 * Show error message box
 */
function showErrorMessage() {
	new Effect.Shake("message");
}


/**
 * Set focus for signin form.  If the username is already filled in, set focus
 * on the password field.
 *
 */
function setSigninFocus() {

	var element;
	
	if ($("signin-username").value == "") {
		element = $("signin-username");
	}
	else {
		element = $("signin-password");
	}

	if (element) {
		element.focus();
	}
}

/**
 * Set focus for email field
 *
 */
function setEmailFocus() {

	var element;
	
	if ($("input-email").value == "") {
		element = $("input-email");
	}

	if (element) {
		element.focus();
	}
}

/**
 * Display the initial default data entry panel for adding stats functionality
 *
 */
function displayDefaultDataEntryPanel() {
	getPanel("walking");
}

/**
 * make AJAX call to retrieve form input code for an activity
 *
 */
function getPanel(exercise) {

	showPanel(exercise);
	
}

/**
 * make AJAX call to retrieve form input code for an activity
 *
 */
function getAdmin(exercise) {
	$("front-content-panel").innerHTML = "<h2>Loading...</h2>";
	var pars = "exercise=" + exercise;
	var myAjax = new Ajax.Request(
                        "/Ajax/retrieveActivityPointsTemplate",
                        {
                                method: 'get',
                                parameters: pars,
                                onComplete: showAdmin
                        });
	
}


/**
 * Add a new activity note
 *
 * @param activity_entry_id string - database ID of the activity entry to add note to
 */
function addActivityNote(activity_entry_id) {

	var noteContainer = $("activityNoteContainer");

	var noteTitle = noteJSON.activityEntries[0].activityDate + " - " + noteJSON.activityEntries[0].activityTitle;
	var noteContent = noteJSON.activityEntries[1].noteContent;
	

	var templateHTML = '';

	templateHTML += '<div id="noteTitleBar">';
	templateHTML += '<div id="noteTitle">' + noteTitle + '</div>';
	templateHTML += '<div id="noteCloseButton"><a href="#" onclick="closeActivityNote(); return false;" title="Click here to close note"></a></div>';
	templateHTML += '</div>';
	
	templateHTML += '<div id="noteContent"><textarea>' + noteContent + '</textarea></div>';
	templateHTML += '<div id="noteActions"></div>';

	//alert(noteJSON.activityEntries[0].activityTitle);

	noteContainer.innerHTML = templateHTML;

	showHideElement("activityNoteContainer", "show");


	new Draggable('activityNoteContainer', {scroll:window, zindex:1000, handle:'noteTitleBar'});


}

/**
 * Close the popup note
 */
function closeActivityNote() {
	showHideElement("activityNoteContainer", "hide");
}


/**
 * Show or hide an element
 * @param element_id string - DOM ID of the element to show or hide
 * @param action string - Action to perform
 */
function showHideElement(element_id, action) {

	var element=null;

	if (typeof (element_id)=="string" && element_id!=null && typeof (action)=="string" && action!=null) {

		element = document.getElementById(element_id);

		if (element!=null) {

			if (typeof (action)=="string" && action=="show") {

				element.style.visibility="visible";
				element.style.display="block";

			}
			else {

				if (action=="hide") {

					element.style.visibility="hidden";
					element.style.display="none";
				}
			}
		}

	}
	else {
		alert("showHideElement(): bad params passed: element="+element_id+", showHide="+action);
	}

	return;
}


/**
 * Change the first letter of a word to uppercase, leave the rest lower
 * Code from http://javascript.about.com/library/blcase.htm
 */
String.prototype.toCapitalCase = function() {

	var re = /\s/;
	var words = this.split(re);
	re = /(\S)(\S+)/;

	for (i = words.length - 1; i >= 0; i--) {
		re.exec(words[i]);
		words[i] = RegExp.$1.toUpperCase() + RegExp.$2.toLowerCase();
	}

	return words.join(' ');
}



/**
 * show a specific content panel
 * param String content - panel to display
 */
function showAdmin(content) {

	var panel = $("front-content-panel");
	panel.innerHTML = content.responseText;
	
	return;

}


/**
 * Return true if user clicks OK, otherwise return false
 */
function confirmDelete(message) {
	var answer = confirm(message);
	return answer;
}



/**
 * Cancel a form and go back one page
 */
function cancelForm(url) {

	if (typeof(url) != "undefined") {
		window.location = url;
	}
	else {
		history.go(-1);
	}
}


/**
 * Change the classname of an element
 */
function changeClassname(element, classname) {
	$(element).className = classname;
}


