
// Panel buffers
ludi_display.buffers = [];


ludi_display.messageInput_after_html = function () {
	ludi_display.hideHTMLId("#spanMsgTo");
	ludi_root.addComboValue ("cmbMsgLan", "EN");
	ludi_root.addComboValue ("cmbMsgLan", "ES");
	ludi_root.addComboValue ("cmbMsgLan", "EO");
}

ludi_display.showHTMLId = function (id) {
	return $(id).show();	
}

ludi_display.hideHTMLId = function (id) {
	return $(id).hide();	
}


ludi_display.getHTMLId = function (id) {
	return $(id).html();	
}

ludi_display.setHTMLId = function (id, txt) {
	$(id).html(txt);	
}

ludi_display.showMappedPanel = function  (idPanelIn, txt) {

	var screenType = (ludi_root.screenType == -1)? 0: ludi_root.screenType;

	var idPanelIn_Redirection = idPanelIn;
	
	if (idPanelIn != ludi_root.enumPanels.Messages) {
		if (!ludi_root.visiblePanels) {
			if (idPanelIn_Redirection == ludi_root.enumPanels.Panel1) {
				idPanelIn_Redirection = ludi_root.enumPanels.Main;
			}
		}
	}

	var idPanelOut = ludi_root.panelMapping[idPanelIn_Redirection][screenType];
	
	ludi_display.setHTMLId ("#"+ idPanelOut, txt);
	
	return idPanelOut;

}


ludi_display.goTopInMainPanel = function  () {
	var idPanel = ludi_root.panelMapping[ludi_root.enumPanels.Main][ludi_root.screenType];
	
	var elem = document.getElementById(idPanel);
	if (elem != null) 
		elem.scrollTop = 0;	
	
	
}

ludi_display.putTextInMainPanel_AppendLast = function  (idBuffer, txt, refresh, append) {

	append = append || false;

	if (append) ludi_display.buffers [idBuffer] += txt;
	else ludi_display.buffers [idBuffer] = txt; 
	
	if (idBuffer == ludi_root.bufferEnum.AllActions) 
		ludi_display.buffers [ludi_root.bufferEnum.LastAction] = txt;
	
	if (refresh) ludi_display.refreshMainPanel  (idBuffer);

}

ludi_display.putTextInMainPanel_AppendFirst = function  (idBuffer, txt, refresh, append) {

	append = append || false;

	if (ludi_display.buffers [idBuffer] == undefined) ludi_display.buffers [idBuffer] = "";
	
	if (append) ludi_display.buffers [idBuffer] = txt + ludi_display.buffers [idBuffer];
	else ludi_display.buffers [idBuffer] = txt; 
	
	if (idBuffer == ludi_root.bufferEnum.AllActions) 
		ludi_display.buffers [ludi_root.bufferEnum.LastAction] = txt;
	
	if (refresh) ludi_display.refreshMainPanel  (idBuffer);

}

ludi_display.refreshMainPanel = function  (idBuffer) {
	
	if (ludi_display.buffers [idBuffer] == undefined) ludi_display.buffers [idBuffer] = "";
	var txt = ludi_display.buffers [idBuffer];
	
	var idPanel;

	if (idBuffer == ludi_root.bufferEnum.allMessages)
		idPanel = ludi_display.showMappedPanel (ludi_root.enumPanels.Messages, txt);
	else 
		idPanel = ludi_display.showMappedPanel (ludi_root.enumPanels.Main, txt);
	
	// automatically at the end of scrolled div
	var elem = document.getElementById(idPanel);
	
	if (elem != null) 
		elem.scrollTop = elem.scrollHeight;	
	

}

ludi_display.resize = function  () { // show / hide panels

	/*
	if (ludi_root.visibleMessages) $('#messages_panel').show();
	else $('#messages_panel').hide();
	
	if (ludi_root.screenType == ludi_root.screenOrientation.Horiz) { // panel1 + panel2 = horiz_left
		$('#main_vert').hide();
		$('#subpanel_vert').hide();
		$('#main_horiz').show();
		if (ludi_root.visiblePanels) $('#subpanel_horiz').show();
		else  $('#subpanel_horiz').hide();
	} else {
		$('#main_horiz').hide();
		$('#subpanel_horiz').hide();
		$('#main_vert').show();
		if (ludi_root.visiblePanels) $('#subpanel_vert').show();
		else  $('#subpanel_vert').hide();

	}
	
	return; 
	
	*/
	
	
	var remainingHeight = 100;

	if (ludi_root.visibleMessages) {
		$('#messages_panel').show();
		remainingHeight -= 12;
		$('#messages_panel').css('height', '12%');
	} else {
		$('#messages_panel').hide();
	}
	
	// if (ludi_renderer.generalContext == "loadingGame") return;
	
	
	if (ludi_root.screenType == ludi_root.screenOrientation.Horiz) { // panel1 + panel2 = horiz_left
		$('#main_vert').hide();
		$('#subpanel_vert').hide();

		if (ludi_root.visiblePanels) {
			ludi_display.showHTMLId('#subpanel_horiz');
			$('#subpanel_horiz').css('width','25%');
			$('#main_horiz').css('width','75%');
		} else {
			ludi_display.hideHTMLId('#subpanel_horiz');
			$('#subpanel_horiz').css('width','0%');
			$('#main_horiz').css('width','100%');
		}
		
		$('#subpanel_horiz').css('height', remainingHeight + '%');
		$('#main_horiz').css('height', remainingHeight + '%');
		
	} else if (ludi_root.screenType == ludi_root.screenOrientation.Vert) { // panel1 + panel2 = vert_up_4
	
		$('#main_horiz').hide();
		$('#subpanel_horiz').hide();

		if (ludi_root.visiblePanels) {
			ludi_display.showHTMLId('#subpanel_vert');
			$('#subpanel_vert').css('height','30%');
			remainingHeight -= 30;
			$('#main_vert').css('height', remainingHeight + '%');
			
		} else {
			ludi_display.hideHTMLId('#subpanel_vert');
			$('#subpanel_vert').css('height','0%');
			$('#main_vert').css('height', remainingHeight + '%');
		
		}

	}
	
}

ludi_display.replaceMenuBySelectedOption = function   (msg, option) {
	// var txt = ludi_display.getHTMLId('#dialogMenu');
	var txt = "";
	// var id = ludi_display.getHTMLId('#dialogMenu');
	var $htmlDoc = ludi_display.getHTMLId ("#menu" + option);
	// var alt= $($htmlDoc).text();
	// var alt2= alt.split("\n");
	
	txt += "<p id = \"result\">" ;
	//txt = "<p>" + msg + ": " + alt2[option] + "</p>";
	txt = "<p>" + msg + ": '" + $htmlDoc + "'</p>";
	
	
	$('#dialogMenu').remove();
	
	// from htlm to cache (horrible!!!)
	txt = ludi_display.getHTMLId ("#"+ludi_root.panelMapping[ludi_root.enumPanels.Main][ludi_root.screenType]) + txt;
	
	//ludi_display.putTextInMainPanel_AppendLast (ludi_root.bufferEnum.AllActions,  ludi_display.buffers[ludi_root.bufferEnum.AllActions]  + txt, true);
	
	// 
	ludi_display.putTextInMainPanel_AppendLast (ludi_root.bufferEnum.AllActions, txt, true);
	
}

ludi_display.showChat = function( txt) {

	ludi_display.setHTMLId('#lblChat', txt);

	$('#cmbMsgLan')[0].value = ludi_display.chatParams.lan; 
	if (ludi_display.chatParams.scope == "private")
		ludi_display.showHTMLId("#spanMsgTo");
	else
		ludi_display.hideHTMLId("#spanMsgTo");
	ludi_display.setHTMLId('#lblMsgTo', ludi_display.chatParams.to);
	 
	ludi_display.setHTMLId('#lblMsgText', ludi_display.chatParams.text);

}



ludi_display.putTextInMainPanel_AppendLast_before = function  (idBuffer, txt, refresh, append) {

	append = append || false;

	if (append) ludi_display.buffers [idBuffer] = txt + ludi_display.buffers [idBuffer];
	else ludi_display.buffers [idBuffer] = txt; 
	
	if (idBuffer == ludi_root.bufferEnum.AllActions) 	ludi_display.buffers [ludi_root.bufferEnum.LastAction] = txt;
	
	if (refresh) ludi_display.refreshMainPanel  (idBuffer);

}
