var loc_grpDescr_base = 'http://registration.yogajournal.com/ka/group_handler';

//
// Handle inserting an element into the DOM
//
function insertAfter(newElement,targetElement) {
	var parent = targetElement.parentNode;
	if (parent.lastchild == targetElement) {
		parent.appendChild(newElement);
	} else {
		parent.insertBefore(newElement, targetElement.nextSibling);
	}
}

function insertBefore(newElement,targetElement) {
	var parent = targetElement.parentNode;
	parent.insertBefore(newElement, targetElement);
}

// 
// Handle Group Description operations
//
function grpResponse(response, message) {
	if (response != 'GOOD') {
		alert(message);
	}
	else {
		alert("Group description was saved.");
	}
}

// 
// Handle the group description
//
var flgShowDescription = false;

function saveGrpDescription() {
	var response;
	
	// Handle the update
	request = loc_grpDescr_base + '_save.php?groupid=' + Ka.Info.CLUBID + '&description=' + encodeURIComponent(document.getElementById("group_description").value) + '&callback=grpResponse';

	aObj = new JSONscriptRequest(request);
	aObj.buildScriptTag();
	aObj.addScriptTag();
}

function getGrpDescription() {
	var response;
	var groupid;
	var grpURL;

	// setup
	grpURL = location.protocol+'//'+location.hostname+'/kickapps/service/getHttpUrl.kickAction?urlLink=' + loc_grpDescr_base + '_get.php?groupid=' + Ka.Info.CLUBID;
	
	// Handle getting the group description
	var response = $j.ajax({
		type: "GET",
		url: grpURL,
		async: false
	}).responseText;
	
	if (response.indexOf('Error') != -1) {
		alert(response);
		return "";
	}
	else {
		return response;		
	}
}

function showDescription() {
	if (Ka.Info.PAGE == 'pages/clubHome.jsp' && Ka.Info.PAGETYPE == 'Group') {

		// Add the group description and text
		if (!(flgShowDescription)) {
			if (document.getElementById('ka_groupProfile')){
				var theHTML;
				var Description = 'This is a test of the description';
				
				// Get the description
				Description = getGrpDescription();
				
				// Build out the HTML
				if (Ka.Info.ROLENAME == 'WEBMASTER' || Ka.Info.ROLENAME == 'EDITOR') {
					theHTML = '<div><form onsubmit="javascript:saveGrpDescription();" action="javascript:requestClubs();"><h3 class="ka_nullTitle ka_contentTitle"><span>Group Description</span></h3><textarea id="group_description" name="group_description" cols=93 rows=6>' + Description + '</textarea><input type="button" value="save" onclick="javascript:saveGrpDescription()" class="ka_button ka_inputTiny"/></form></div>';
				}
				else {
					theHTML = '<div>' + Description + '</div>';
				}

				// Write the description into the page
				var ka_groupdesc = document.createElement("div");
				ka_groupdesc.id = "ka_groupdescription";
				ka_groupdesc.className = "clearfix";
				ka_groupdesc.innerHTML = theHTML;

				var groupCategory = document.getElementById('ka_groupCategory');
				if (groupCategory != null) {
					insertBefore(ka_groupdesc, groupCategory)
				}
				
				// Set flag
				flgShowDescription = true;	
			}
		}
	}
}

// Show the description
Ka.addDOMLoadEvent(showDescription);
