YAHOO.namespace("keep.selection");

function handleEntry(currentUrl, pageId, userId, objectType, objectId, mode) {
	/* Destroy open panels from user online plugin */
	if (window.panel) {
		panel.destroy();
	}
	
	/* Destroy open detail views */
	if (YAHOO.keep.selection.window) {
		YAHOO.keep.selection.window.destroy();
	}

	YAHOO.keep.selection.window = new YAHOO.widget.Panel("message-window",
		{
			effect:{effect:YAHOO.widget.ContainerEffect.FADE,duration:0.45},
			// underlay:"shadow",
			underlay:"none",
			constraintoviewport: true,
			close:true,
			visible:true,
			draggable:true,
			modal:false,
			width: "350px",
			xy: ["250", "300"],
			iframe: false
		}
	);

	var header = "<div id=\"form-head\" style=\"background: url(/typo3conf/ext/mw_es_portrait_view/res/gfx_contact_edge_l.gif) 0 0 no-repeat #797979; height: 20px;\"><div id=\"form-head-inner\" style=\"background: url(/typo3conf/ext/mw_es_portrait_view/res/gfx_contact_edge_r.gif) 100% 0 no-repeat; color: #fff; height: 20px; text-align: right; padding: 3px 23px 0px 0px;\"><a href=\"#\" onclick=\"YAHOO.keep.selection.window.destroy(); return false;\" style=\"color: #fff;\">schliessen</a></div></div>";
	var body = "<div id=\"message-window-content\">body</div>";

	YAHOO.keep.selection.window.setHeader(header);
	YAHOO.keep.selection.window.setBody(body);
	YAHOO.keep.selection.window.render("frame");

	divPublicList = document.getElementById("message-window-content");
	
	getPublicListData("message-window-content", pageId, currentUrl, userId, objectType, objectId);
}


/*** Connection ***/
/* Default URL for extension communication */
var UrlPublicList = "";
var g_portraitId = "";
var g_pageId = "";
var g_userId = "";
var g_objectType = "";
var g_objectId = "";
var g_displayAll = "";
var g_numberOfPortraitsShown = "";
var g_showRow = "";
var g_sort = "";
var g_searchText = "";
var g_searchType = "";
var g_start = "";

var keepSelectionGetUrl = "";

/* Define the div to replace */
var divPublicList = '';

var handleSuccessPublicList = function(u){
	if(u.responseText !== undefined){

		// Create new div object
		var innerDivSuccess = document.createElement("div");

		innerDivSuccess.innerHTML = u.responseText;

		// Delete old content
		divPublicList.removeChild(divPublicList.firstChild);
		// Load new content
		divPublicList.appendChild(innerDivSuccess);
	}
}

var handleFailurePublicList = function(u){
	if(u.responseText !== undefined){

		// Create new div object
		var innerDivFailure = document.createElement("div");

		innerDivFailure.innerHTML = '<p class="notice">Wegen eines technischen Problems kann dein Eintrag leider nicht gespeichert werden.</p>';

		// Delete old content
		divPublicList.removeChild(divPublicList.firstChild);
		// Load new content
		divPublicList.appendChild(innerDivFailure);
	}
}

var callbackPublicList =
{
  success:handleSuccessPublicList,
  failure:handleFailurePublicList,
  argument: { fooPublicList:"fooPublicList", fooPublicList:"barPublicList" }
};

YAHOO.keep.selection.makePublicListRequest = function() {

	// Create new div object
	var innerDivWait = document.createElement("div");

	if (UrlPublicList.indexOf("?") > -1) {
		keepSelectionGetUrl = UrlPublicList + "&eID=tx_mwesmesseplaner_pi_suche&pageUid=" +g_pageId;
	} else {
		keepSelectionGetUrl = UrlPublicList + "?eID=tx_mwesmesseplaner_pi_suche&pageUid=" +g_pageId;
	}
	
	keepSelectionGetUrl += '&tx_mwesmesseplaner_pi_suche[mp_user]=' + g_userId;
	keepSelectionGetUrl += '&tx_mwesmesseplaner_pi_suche[mp_action]=' + g_mode;
	keepSelectionGetUrl += '&tx_mwesmesseplaner_pi_suche[mp_' +g_objectType+ ']=' + g_objectId;
	/*
	&mp_user=" + userId + "&mp_action=merken&mp_" + objectType + "=" + objectId
	
	keepSelectionGetUrl += '&tx_mwesmesseplaner_pi_suche[displayAll]=' + g_displayAll;
	keepSelectionGetUrl += '&tx_mwesmesseplaner_pi_suche[numberOfPortraitsShown]=' + g_numberOfPortraitsShown;
	keepSelectionGetUrl += '&tx_mwesmesseplaner_pi_suche[showRow]=' + g_showRow;
	keepSelectionGetUrl += '&tx_mwesmesseplaner_pi_suche[sort]=' + g_sort;
	keepSelectionGetUrl += '&tx_mwesmesseplaner_pi_suche[search][text]=' + g_searchText;
	keepSelectionGetUrl += '&tx_mwesmesseplaner_pi_suche[search][type]=' + g_searchType;
	keepSelectionGetUrl += '&tx_mwesmesseplaner_pi_suche[start]=' + g_start;
	*/
	
	// Fill new div with content
	innerDivWait.innerHTML = '<div style="width: 420px; text-align: center; padding-top: 100px;"><p class="loading"><img src="../typo3conf/ext/mw_es_community/res/loading_indicator_profil.gif" style="margin: 0px 0px 0px 10px;" alt="Laden..." width="16" height="16" />&nbsp;Laden...</p></div>';
	
	// Delete old content
	divPublicList.removeChild(divPublicList.firstChild);
	// Load new content
	divPublicList.appendChild(innerDivWait);

	window.setTimeout('getPublicListURL()', 0);
}

function getPublicListURL() {
	var requestPublicList = YAHOO.util.Connect.asyncRequest('GET', keepSelectionGetUrl, callbackPublicList);
}

function getPublicListData(element, pageId, currentUrl, userId, objectType, objectId, mode) {
	UrlPublicList = currentUrl;
	
	// Set global variables
	g_pageId = pageId;
	g_userId = userId;
	g_objectType = objectType;
	g_objectId = objectId;
	g_mode = mode;
	/*
	g_numberOfPortraitsShown = numberOfPortraitsShown;
	g_showRow = showRow;
	g_sort = sort;
	g_searchText = searchText;
	g_searchType = searchType;
	g_start = start;
	*/
	YAHOO.util.Event.onContentReady("content-frame", YAHOO.keep.selection.makePublicListRequest);
}
