var debug = false;

function initProfile( profileId, updateDB, callback ) {
	new Dragger( profileId, updateDB, 'drag', 'div', debug );
//	new Dragger( udpateDB );
}

function removeProfileSection( event, elementId, profileId ) {
	stopEvent( event );
	if ( confirm( "Click OK to remove this section from your profile.  You can add it back by editing your Display Options." )) {
		document.getElementById( elementId ).style.display = "none";
		data = elementId.split( "_" );
		ajax = new Ajax();
		ajax.post( 
			SYSTEM_URL + "/include/update_profile_sections.php", updateCallback, [
				[ "function", "hide" ],
				[ "pageName", encodeURI( data[0] )],
				[ "sectionName", encodeURI( data[1] )],
				[ "profileId", encodeURI( profileId )],
				[ "isHidden", "1" ]
			]
		);
		var displayOption = document.getElementById( "show" + data[1] );
		if ( typeof( displayOption != "undefined" )) {
			displayOption.checked = false;
		}
	}
}

function hideProfileSection( event, elementId ) {
	stopEvent( event );
	document.getElementById( elementId ).style.display = "none";
}

function toggleProfileSection( event, elementId, button ) {
	stopEvent( event );
	section = document.getElementById( elementId );
	var isMinimized = 0;
	if ( section.className.match( /( minimized)\b/ )) {
		section.className = section.className.replace( /( minimized)\b/, "" );
		button.title = button.title.replace( /(Show)\b/, "Hide" );
	} else {
		section.className+= " minimized";
		isMinimized = 1;
		button.title = button.title.replace( /(Hide)\b/, "Show" );
	}
	return isMinimized;
}

function openCloseProfileSection( event, elementId, button, profileId ) {
	var isMinimized = toggleProfileSection( event, elementId, button );
	data = elementId.split( "_" );
	ajax = new Ajax();
	ajax.post( 
		SYSTEM_URL + "/include/update_profile_sections.php", updateCallback, [
			[ "function", "min" ],
			[ "pageName", encodeURI( data[0] )],
			[ "sectionName", encodeURI( data[1] )],
			[ "profileId", encodeURI( profileId )],
			[ "isMinimized", encodeURI( isMinimized )]
		]
	);
}

function editProfileSection( event, elementId, button, sessionId, profileId ) {
	stopEvent( event );
	section = document.getElementById( elementId );

	if ( section.className.match( /( editing)\b/ )) {

		// Process database updates.
		var update_function = "update_" + elementId;
		if ( eval( "typeof(" + update_function + ")") == "function" ) {
			if ( !eval( update_function + "( elementId, sessionId, profileId )" )) return false;
		} else {
			if ( debug ) alert( "Debug: For update processing, create a function called: " + update_function + "(elementId, sessionId, profileId)" );
		}

		section.className = section.className.replace( /( editing)\b/, "" );
		button.innerHTML = "Edit";
	} else {

		// Process editing.
		var update_function = "edit_" + elementId;
		if ( eval( "typeof(" + update_function + ")") == "function" ) {
			if ( !eval( update_function + "( elementId, profileId )" )) return false;
		} else {
			if ( debug ) alert( "Debug: To do custom edit processing, create a function called: " + update_function + "(elementId, profileId)" );
		}
		section.className+= " editing";
		button.innerHTML = "Done";

	}
}

function updateCallback( msg ) {
	if ( debug ) alert( msg );
}
