/**
 *
 * LCMS and its source-code is licensed under the LGPL.
 * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
 * When copying, adapting, or redistributing this document in keeping with the guidelines above,
 * you are required to provide proper attribution to champion open systems.
 * If you reproduce or distribute the document without making any substantive modifications to its content,
 * please use the following attribution line:
 *
 * Copyright 2007 champion open systems (http://www.chaosys.ch) All rights reserved.
 *
 */

/**
* 
* Functions concerning the labels.
*
*/

/** 
*
* Label CRUD - Section.
*
*/
/** 
* Adding a new label to the screen.
*/

function addLabel(newID, labelClosedText, labelOpenedText, percentX, percentY){
		if(newID == '' || labelClosedText == '' || labelOpenedText == ''){
			alert('please fill in the text-fields');
		}else{
		layerObjectElement = document.getElementById('labelLayer');
        navigationScreenElement = document.getElementById('navigationScreen');
        
        /** 
		* Wrapper for Label-Widget.
		*/
        newLabelWidgetWrapper = document.createElement("div");
        newLabelWidgetWrapper.style.position = "absolute";
        newLabelWidgetWrapper.className = "poiLabelWrapper";
        newLabelWidgetWrapper.style.top = percentY + "%";
        newLabelWidgetWrapper.style.left = percentX + "%";
        newLabelWidgetWrapper.id = newID;
        newLabelWidgetWrapper.onmouseover = function(evt){
        	bringtoFront(newID);
        }
 
         /** 
		* Label-Widget displayed on navigation-Screen.
		*/       
        newLabelNavigationScreen = document.createElement("div");
		newLabelNavigationScreen.className = "poiNavigationScreen";
		newLabelNavigationScreen.id = newID + "_overview";
		newLabelNavigationScreen.style.position = "absolute";
		newLabelNavigationScreen.style.visibility = "hidden";
	newLabelNavigationScreen.style.top = percentY + "%";
        newLabelNavigationScreen.style.left = percentX + "%";
        
        /** 
		* Text-Container.
		*/        
        newLabel = document.createElement("span");
        newLabel.id = newID+"_textContainer";
        newLabel.className = "dijitTooltipContainer poiLabel";
        newLabel.style.display = "block";
        
        newLabelWidgetWrapper.appendChild(newLabel);

        /** 
		* Closed-Text.
		*/   
        newLabelClosedText = document.createTextNode('');
        newLabelClosedText.data = labelClosedText;
        newLabel.appendChild(newLabelClosedText);

        /** 
		* Open-Text.
		*/   
		newLabelOpenedText = document.createTextNode('');
		newLabel.appendChild(newLabelOpenedText);
		
        /** 
		* Remover-Button.
		*/   		
		newLabelRemover = document.createElement("div");
		newLabelRemover.className = "poiRemover";
		newLabelRemover.onclick = function(evt){
			removeLabelById(newID);	
        }
        newLabel.appendChild(newLabelRemover);
        
        /** 
		* Mover-Button.
		*/          
        newLabelTurnOnMove = document.createElement("div");
		newLabelTurnOnMove.className = "poiMover";
		newLabelTurnOnMove.id = newID + "_mover";
		newLabelTurnOnMove.onmousedown = function(evt){
				makeLabelMovableById(newID);
        }
        //newLabel.appendChild(newLabelTurnOnMove);

        newLabel.onclick = function(evt){
			newLabelReloaded = document.getElementById(newID+"_textContainer");
			actualClosedData = newLabelReloaded.firstChild.data;
			actualOpenedData = newLabelReloaded.firstChild.nextSibling.data;
			if (actualClosedData == ''){
				newLabelReloaded.firstChild.data = labelClosedText;
				newLabelReloaded.firstChild.nextSibling.data = "";
			}else{
				newLabelReloaded.firstChild.data = "";
				newLabelReloaded.firstChild.nextSibling.data = labelOpenedText;
			}

        }
        
        navigationScreenElement.appendChild(newLabelNavigationScreen);
        layerObjectElement.appendChild(newLabelWidgetWrapper);
        labelRegistry.push(newID);
		}
}

/** 
* Remove all labels.
*/
function removeAllLabels(){
	while (labelRegistry.length > 0){
		elementId = labelRegistry.pop();
		try{
			document.getElementById('labelLayer').removeChild(document.getElementById(elementId));
			document.getElementById('navigationScreen').removeChild(document.getElementById(elementId+"_overview"));
		}catch(err){
		}
	}
}

/** 
* Hide in navigationscreen all labels.
*/
function hideAllLabelsInNavigationscreen(){
	for(i=0; i< labelRegistry.length; i++){
		elementId = labelRegistry[i];
		try{
			document.getElementById(elementId+"_overview").style.visibility="hidden";
		}catch(err){
		}
	}
}

/** 
* Show in navigationscreen all labels.
*/
function showAllLabelsInNavigationscreen(){
	for(i=0; i< labelRegistry.length; i++){
		elementId = labelRegistry[i];
		try{
			document.getElementById(elementId+"_overview").style.visibility="visible";
		}catch(err){
		}
	}
}

/** 
* Remove label by Id.
*/
function removeLabelById( Id ){
	document.getElementById('labelLayer').removeChild(document.getElementById(Id));
	document.getElementById('navigationScreen').removeChild(document.getElementById(Id+"_overview"));
	for(i=0; i< labelRegistry.length; i++){
		if(labelRegistry[i] == Id){
			labelRegistry[i] = null;
		}
	}
}

/** 
* Remove label by Id.
*/
function makeLabelMovableById( Id ){
	element = document.getElementById(Id);
	moverElement = document.getElementById(Id + "_mover");
	navigationScreenElement = document.getElementById(Id + "_overview");
	
	isMovable = false;
	if(isMovable){
		moverElement.style.background = "url('../images/movebg.png')";
		//TODO make unmovable
	}else{
		moverElement.style.background = "url('"+webappHost+"/images/moveactivebg.png')";
		new dojo.dnd.Moveable(element);
		/*element.onmousemove = function(evt){
			navigationScreenElement.style.top = element.style.top;
			navigationScreenElement.style.left = element.style.left;
		}*/
	}
}

/** 
* Load labels.
*/
function loadLabels(){
 	buttonElement = document.getElementById('addLabelLayer');
	showLabelLayer( buttonElement );
	setTimeout("parseViewObjectInfo()", 500);
	setTimeout("parseLabels()",500);
	hideLabelLayer( buttonElement );
}

function parseLabels(){
	xArray = labelsXml.getElementsByTagName("x-percent");
	yArray = labelsXml.getElementsByTagName("y-percent");
	shortArray = labelsXml.getElementsByTagName("label-short-text");
	longArray = labelsXml.getElementsByTagName("label-long-text");
	for (i=0; i< xArray.length; i++){
		try{
	        addLabel("label_"+i, shortArray[i].firstChild.data, longArray[i].firstChild.data, xArray[i].firstChild.data, yArray[i].firstChild.data);
	    }catch(err){
	    }
	        //addLabel("label_"+i, shortArray[i].firstChild.data, longArray[i].firstChild.data, xArray[i].firstChild.data, yArray[i].firstChild.data, "");
	}
}

function parseViewObjectInfo(){
	viewObjectInfoString = labelsXml.getElementsByTagName("subtitle");
	questionId=labelsXml.getElementsByTagName("title-appendix");
	document.getElementById("vObjectInfo").firstChild.nodeValue=viewObjectInfoString[0].firstChild.data;
	currentQuestionId=questionId[0].firstChild.data;
}

function bringtoFront(newID){
}

/** 
* Save labels.
*/
function saveLabels(){}



