//Copyright by Pietro Lusso 2006
//Email: info@pietro-lusso.de
var actualPage = 0;
var letzteseite = 308;
var actualrubric = "MIND";
var actualsymptom = -1;

//Init
function initPage()
{
	checkPage();

        new Draggable('centeredBlock',{revert:false});
	new Draggable('actionwindow',{revert:false});
	new Draggable('remedywindow',{revert:false});
	new Draggable('rubricOverviewWindow',{revert:false});
        new Draggable('repwindow',{revert:false});
	new Draggable('searchwindow',{revert:false});
        new Draggable('xwindow',{revert:false});
        new Draggable('xtopics_window',{revert:false});

        reloadSymptomList();
}

function checkPage()
{
	if (actualPage == 0) browseVisible("prev", "0");
	if (actualPage > 0) browseVisible("prev", "1");

	if (actualPage > letzteseite - 1)
	{
		actualPage = letzteseite;
		browseVisible("next", "0");
		return;
	}
	else
	{
		browseVisible("next", "1");
	}

	if (actualPage < 0)
	{
		actualPage = 0;
		browseVisible("prev", "0");
		return;
	}

	var actualpage_div = document.getElementById("actualpage");
	actualpage_div.innerHTML = actualPage;
}

function checkRubric()
{
	var newRubric = document.getElementById("rubricFeedback");

	if (newRubric)
	{
		setRubricName(newRubric.innerHTML);
		getRubricPages(newRubric.innerHTML);
	}
}

//Nächste Seite wird angezeigt
function nextPage()
{
	actualPage = actualPage + 1;

	browseVisible("prev", "1");

	var nextpage = "repdata/page_" + actualPage + ".html";
	new Ajax.Updater('repertorium', nextpage, {asynchronous:true});
	getRubricPages(actualrubric);

	checkPage();
}

//Vorherige Seite wird angezeigt
function prevPage()
{
	actualPage = actualPage - 1;

	browseVisible("next", "1");

	var prevpage = "repdata/page_" + actualPage + ".html";
	new Ajax.Updater('repertorium', prevpage, {asynchronous:true});
	getRubricPages(actualrubric);

	checkPage();
}

function loadPage(rubric, page)
{
	actualPage = parseInt(page);
	var page = "repdata/page_" + page + ".html";
	new Ajax.Updater('repertorium', page, {asynchronous:true});

	var xwindow = document.getElementById("xwindow");

	if (rubric != "")
	{
		setRubricName(rubric);
		getRubricPages(rubric);
	}
	else
	{
		setRubricName(rubric.id);
		getRubricPages(rubric.id);
	}

	checkPage();
}

function setRubricName(rubric)
{
	var rubricname = document.getElementById("rubricname");
	rubricname.innerHTML = rubric;
}

function initRepertory()
{
	new Ajax.Updater('repertorium', 'repdata/page_0.html', {asynchronous:true});
	getRubricPages("MIND");
}

function showReferences()
{
	var aw_references = document.getElementById("aw-references");

	if (aw_references.style.visibility == "hidden")
	{
		aw_references.style.visibility = "visible";
	}
	else
	{
		aw_references.style.visibility = "hidden";
	}
}

function windowStartDragging(windowId) {

    var windowDiv = document.getElementById(windowId);
    if (windowDiv != null)
    windowDiv.style.visibility = "hidden";
}

function windowEndDragging(windowId) {
        alert(windowId);
    var windowDiv = document.getElementById(windowId);
    if (windowDiv != null)
    windowDiv.style.visibility = "visible";
}

function closeAW()
{
	var aw_references = document.getElementById("aw-references");
	aw_references.style.visibility = "hidden";
	var action_window = document.getElementById("actionwindow");
	action_window.style.visibility = "hidden";
	var aw_add = document.getElementById("aw-add");
	aw_add.style.visibility = "hidden";
}

function closeRO()
{
	var rubric_window = document.getElementById("rubricOverviewWindow");
	rubric_window.style.visibility = "hidden";
}

function closeRW()
{
	var remedy_window = document.getElementById("remedywindow");
	remedy_window.style.visibility = "hidden";
}

function browseVisible(direction, status)
{
	var direction = document.getElementById(direction);

	if (status == "0")
	{
		direction.style.visibility = "hidden";
	}
	else
	{
		direction.style.visibility = "visible";
	}
}

function getRemediesForDetails(id)
{
	actualsymptom = id;
	var url = "getRemedies.php?id=" + id;
        new Ajax.Updater('aw-remedies', url, {asynchronous:true, evalScripts:true});
}

function getReferencesForDetails(id)
{
	var url = "getReferences.php?id=" + id;
	new Ajax.Updater('aw-references', url, {asynchronous:true, evalScripts:true});
}

function setReferencesForDetails(req)
{
	var content = document.getElementById("aw-content");
	content.innerHTML = content.innerHTML + "<br>" + req.responseText;
}

function showRemedy(id)
{
	var remedy_window = document.getElementById("remedywindow");
	var rm_remedy = document.getElementById("rm-remedy-iframe");
        var url = "http://www.homeoint.org/books/boericmm/";
	url = url + id.substr(0,1);
	url = url + "/" + id + "htm";

        //new Ajax.Updater('rm-remedy', url, {asynchronous:true, evalScripts:true});
	rm_remedy.src = url;
	remedy_window.style.left = 100;
	remedy_window.style.top = 100;

	remedy_window.style.visibility = "visible";
}

function checkBrowser()
{
	if (navigator.appName.indexOf("Micro") > -1)
	{

            window.location.replace('index_sorry.php');
	}
}

function getRubricPages(rubric)
{
	var url = "getRubricPages.php?name=" + rubric + "&actualPage=" + actualPage;
	new Ajax.Updater('rubricpages', url, {onComplete:function(){checkRubric()},asynchronous:true, evalScripts:true});
	actualrubric = rubric;
}

function closeXWindow()
{
	showRubrics();
}

function closeXTopics()
{
	showSymptoms();
}

//*********************

function addSymptom()
{
	//reloadSymptomList();

	//actualsymptom
	var rubricname = document.getElementById("rubricname");
	var topic = document.getElementById("aw-content");
	var symptom = "<span class='parentRubric'>" + rubricname.innerHTML + " - </span>";
	symptom = symptom + topic.innerHTML;
	var url = "addSymptomToPool.php?id=" + actualsymptom + "&symptom=" + escape(symptom);
	//new Ajax.Updater('', url, {onComplete:reloadSymptomList()});
	new Ajax.Updater('', url, {onComplete:function(){reloadSymptomList();},asynchronous:true, evalScripts:true});
}

function resetSymptomList()
{
	//reloadSymptomList();
	var url = "resetSymptomList.php";
	//new Ajax.Updater('', url, {onComplete:reloadSymptomList()});
	new Ajax.Updater('', url, {onComplete:function(){reloadSymptomList();},asynchronous:true, evalScripts:true});
}

function reloadSymptomList()
{
	//var symptomlist = document.getElementById("xtopics-iframe");
        new Ajax.Updater('xtopics', 'symptomList.php', {asynchronous:true, evalScripts:true});
}

function showRubricOverview(rubric)
{
	var rubric_window = document.getElementById("rubricOverviewWindow");
	var url = "showRubricOverview.php?rubric=";
	url = url + rubric.innerHTML;
	new Ajax.Updater('ro-rubric', url, {asynchronous:true, evalScripts:true});
	rubric_window.style.left = 100;
	rubric_window.style.top = 100;

	rubric_window.style.visibility = "visible";
}

//Zeigt oder versteckt das Fenster mit der Übersicht der Rubriken.
function showRubrics() {
    var rubric_window = document.getElementById("xwindow");
    var showRubricsButton = document.getElementById("showRubricsButton");
    
    if (rubric_window.style.visibility == 'hidden') {
        rubric_window.style.visibility = "visible";
        showRubricsButton.style.fontStyle = "normal";
    } else {
        rubric_window.style.visibility = "hidden";
        showRubricsButton.style.fontStyle = "italic";
    }
}

function showSymptoms() {
    var xtopics_window = document.getElementById("xtopics_window");
    var showSymptomsButton = document.getElementById("showSymptomsButton");

    if (xtopics_window.style.visibility == 'hidden') {
        xtopics_window.style.visibility = "visible";
        showSymptomsButton.style.fontStyle = "normal";
    } else {
        xtopics_window.style.visibility = "hidden";
        showSymptomsButton.style.fontStyle = "italic";
    }
}

function showSymptomDetails()
{
	alert("function in development...");
}

function search()
{
	var searchwindow = document.getElementById("searchwindow");
	searchwindow.style.visibility = "visible";
}

function analyse()
{
	alert("function in development...");
}

function closeSE()
{
	var searchwindow = document.getElementById("searchwindow");
	searchwindow.style.visibility = "hidden";
}

function searchHandleKeyPress(e){
    var key=e.keyCode || e.which;

    if (key==13){
        searchTopic();
    }
}

function searchHandleKeyPressOpenWindow(e){
    var key=e.keyCode || e.which;

    if (key==13){
        search(); //open the window
        var keywords = document.getElementById("se-textfield");
        var keywords2 = document.getElementById("se-textfield2");
        keywords.value = keywords2.value;
        searchTopic(); // and search
        keywords.focus();
    }
}

function searchTopic()
{
	var keywords = document.getElementById("se-textfield");
	var url = "search.php?keywords=" + escape(keywords.value);
        new Ajax.Updater('se-results', url, {asynchronous:true, evalScripts:true});
}

function help()
{
	alert("function in development...");
}

function repertorisation()
{
	var repwindow = document.getElementById("repwindow");
        var url = "repertorisation.php";
        new Ajax.Updater('re-repertorisation', url, {asynchronous:true, evalScripts:true});
	repwindow.style.visibility = "visible";
}

function closeRE()
{
	var repwindow = document.getElementById("repwindow");
	repwindow.style.visibility = "hidden";
}

function deleteSymptom(id)
{
	check = confirm("Do you realy want to remove symptom no. " + id + " ?");

	if (check == true)
	{
		var url = "deleteSymptom.php?id=" + id;
		new Ajax.Updater('', url, {onComplete:function(){reloadSymptomList();},asynchronous:true, evalScripts:true});
	}
}

function setPrio(id, prio)
{
	var url = "setPrio.php?id=" + id + "&prio=" + prio;

	new Ajax.Updater('', url, {onComplete:function(){reloadSymptomList();},asynchronous:true, evalScripts:true});
}

var xCoord = 0;
var yCoord = 0;

function checkwhere(e) {
        if (document.layers){
	        xCoord = e.x;
	        yCoord = e.y;
		}
        else if (document.all){
	        xCoord = event.clientX;
	        yCoord = event.clientY;
		}
        else if (document.getElementById){
	        xCoord = e.clientX;
	        yCoord = e.clientY;
		}
        //self.status = "X= "+ xCoord + "  Y= " + yCoord;
}


document.onmousemove = checkwhere;
//if(document.captureEvents) {document.captureEvents(Event.MOUSEMOVE);}

//Auswahlfenster mit möglichen Aktionen wird angezeigt
function topicSelected(id)
{
	var topic = document.getElementById(id);
	var level = topic.className.substr(6);

	var topic_text = topic.innerHTML;

	if (level == "0")
	{
		topic_text = "<span class='parentTopic'>" + topic_text + "</span>";
	}
	else
	{
		topic_text = "<span class='childTopic'>" + topic_text + "</span>";
	}

	showTopicActions(topic_text, id);
}

function showTopicActions(topic, id)
{
	var action_window = parent.document.getElementById("actionwindow");
	var aw_content = parent.document.getElementById("aw-content");
	var aw_add = parent.document.getElementById("aw-add");
	topic = getWholeTopic(id, topic);
	aw_content.innerHTML = topic;

	if (action_window)
	{
            aw_add.style.visibility = "visible";
            action_window.style.visibility = "visible";
	}

	parent.getReferencesForDetails(id);
	parent.getRemediesForDetails(id);
}

function showTopicActions2(topic, id)
{
	var action_window = parent.document.getElementById("actionwindow");
	var aw_content = parent.document.getElementById("aw-content");
	var aw_remedies = parent.document.getElementById("aw-remedies");
	var aw_add = parent.document.getElementById("aw-add");
	aw_content.innerHTML = topic;

	if (action_window)
	{
            aw_add.style.visibility = "hidden";
            action_window.style.visibility = "visible";
	}

	parent.getReferencesForDetails(id);
	parent.getRemediesForDetails(id);
}

//Extrahiert den gesamten Text aus den 'Parents'-Knoten, je indiv. Ebene
function getWholeTopic(childID, subtext)
{
	var wholeTopic = subtext;
	var childTopic = document.getElementById(childID);
	var level = childTopic.className;

	if (level == null) return "FEHLER: level = null";

	level = level.substr(6);
	var tempLevel = level;
	var actualLevel	= level;

	try
	{
		//Extrahiere solange bis Level 0 erreicht wurde
		while(level > 0)
		{
			childID = childID - 1;
			if (childID < 0) break;

			var topTopic = document.getElementById(childID);

			level = topTopic.className.substr(6);

			if (level > tempLevel)
			{
				continue;
			}

			if (level == tempLevel)
			{
				continue;
			}
			else
			{
				tempLevel = level;
			}
			if (level == "0")
			{
				wholeTopic = "<span class='parentTopic'>" + topTopic.innerHTML + "</span>, " + wholeTopic;
			}
			else
			{
				wholeTopic = "<span class='childTopic'>" + topTopic.innerHTML + "</span>, " + wholeTopic;
			}

		}
	}catch(e)
	{
		var parent_text	= "";

		//Lese Text aus Parent-Topics
		for(var x=0; x < level; x++)
		{
			var parent_topic = document.getElementById("parent_" + x);

			if (x == "0")
			{
				parent_text = "<span class='parentTopic'>" + parent_topic.innerHTML + "</span>";
			}
			else
			{
				parent_text = parent_text + ", <span class='childTopic'>" + parent_topic.innerHTML + "</span>";
			}
		}

		wholeTopic = parent_text + ", " + wholeTopic;
	}



	return wholeTopic;
}