﻿//******************************
//JS for linking transcription with manuscript
//copyright ACS Solutions, Leipzig (Germany) 2007
//******************************

function Linking()
{
	this.XposPixel_1 = 0;
	this.YposPixel_1 = 0;
	this.WidthPixel_1 = 0;
	this.HeightPixel_1 = 0;
	this.XposPixel_2 = 0;
	this.YposPixel_2 = 0;
	this.WidthPixel_2 = 0;
	this.HeightPixel_2 = 0;	
	this.xClick = 0;
	this.yClick = 0;
	this.borderDiv = null;
	this.borderDivPart2 = null;
	this.selectedWord = null;
	this.wordId = null;
	this.transcriptionContainer = null;
	this.splittedWord = false;
	this.linkingUrl = "../handler/linking.ashx";
	this.enabled = true;
}

Linking.prototype.init = function(transCont)
{
 /*
    $(transCont).select('span').each(function(elem)
    {
        if(elem.getAttribute("name")!= null && elem.getAttribute("name")!="undefined")
        {
            Event.observe(elem, "mouseover", function(){
                linking.showLinking(elem.getAttribute("name"))
                });
        
            Event.observe(elem, "mouseout",  linking.deselectWord);            
         }
    });
*/
}

Linking.prototype.setValues = function()
{
	this.YposPixel_1 = Math.round((this.YRel_A1 - zoom.oldYPosAbs) * (Math.pow((1/zoom.zoomFaktor), zoom.zoom)) * zoom.manuscriptHeight);
	this.XposPixel_1 = Math.round((this.XRel_A1 - zoom.oldXPosAbs) *(Math.pow((1/zoom.zoomFaktor), zoom.zoom)) * zoom.manuscriptWidth);
	this.relWidth_1 = Math.round(this.WidthRel_A *(Math.pow((1/zoom.zoomFaktor), zoom.zoom)) * zoom.manuscriptWidth);
	this.relHeight_1 = Math.round(this.HeightRel_A *(Math.pow((1/zoom.zoomFaktor), zoom.zoom)) * zoom.manuscriptHeight);

	if(this.splittedWord)
	{
		this.YposPixel_2 = Math.round((this.YRel_B1 - zoom.oldYPosAbs) * (Math.pow((1/zoom.zoomFaktor), zoom.zoom)) * zoom.manuscriptHeight);
		this.XposPixel_2 = Math.round((this.XRel_B1 - zoom.oldXPosAbs) *(Math.pow((1/zoom.zoomFaktor), zoom.zoom)) * zoom.manuscriptWidth);
		this.relWidth_2 = Math.round(this.WidthRel_B *(Math.pow((1/zoom.zoomFaktor), zoom.zoom)) * zoom.manuscriptWidth);
		this.relHeight_2 = Math.round(this.HeightRel_B *(Math.pow((1/zoom.zoomFaktor), zoom.zoom)) * zoom.manuscriptHeight);
	}
}

Linking.prototype.setMove = function()
{
	if(this.XposPixel_1 < 20 )
	{
		move = Math.abs(this.XposPixel_1)+40;
		move = Math.ceil(move/20)*20;
		this.XposPixel_1 += move;
		this.XposPixel_2 += move;
		zoom.moveHelper("left",move);
	}
	if(this.XposPixel_1 > (zoom.containerWidth-this.relWidth_1-40))
	{
		move = this.XposPixel_1-zoom.containerWidth+this.relWidth_1+40;
		move = Math.ceil(move/20)*20;
		this.XposPixel_1 -= move;
		this.XposPixel_2 -= move;
		zoom.moveHelper("right",move);	
	}
	
	if(this.YposPixel_1 < 20 )
	{
		move = Math.abs(this.YposPixel_1)+40;
		move = Math.ceil(move/20)*20;
		this.YposPixel_1 += move;
		this.YposPixel_2 += move;
		zoom.moveHelper("top",move);
	}
	if(this.YposPixel_1 > (zoom.containerHeight-this.relHeight_1-40))
	{
		move = this.YposPixel_1-zoom.containerHeight+this.relHeight_1+40;
		move = Math.ceil(move/20)*20;
		this.YposPixel_1 -= move;
		this.YposPixel_2 -= move;
		zoom.moveHelper("bottom",move);	
	}	
}

Linking.prototype.showMarker = function()
{
	if(!zoom.move && !zoom.loadNewImgMap)
	{
		this.borderDiv = $("borderElement");
		if(this.borderDiv == null)
		{
			this.borderDiv = document.createElement("div");
			this.borderDiv.id = "borderElement";
			this.borderDiv.style.zIndex = "10";
			this.borderDiv.style.position = "absolute";	
			this.borderDiv.style.border = "2px solid #d75700";
			this.borderDiv.style.fontSize = "3px";
			zoom.imgContainer.appendChild(this.borderDiv);	
		}
				
		this.borderDiv.style.top = (this.YposPixel_1-2) + "px";
		this.borderDiv.style.left = (this.XposPixel_1-2) + "px";
		this.borderDiv.style.width = this.relWidth_1 + "px";
		this.borderDiv.style.height = this.relHeight_1 + "px";
		
		if(this.splittedWord)
		{
			this.borderDivPart2 = $("borderElementPart2");
			if(this.borderDivPart2 == null)
			{
				this.borderDivPart2 = document.createElement("div");
				this.borderDivPart2.id = "borderElementPart2";
				this.borderDivPart2.style.zIndex = "11";
				this.borderDivPart2.style.position = "absolute";	
				this.borderDivPart2.style.border = "2px solid #d75700";
				this.borderDivPart2.style.fontSize = "3px";
				zoom.imgContainer.appendChild(this.borderDivPart2);	
			}
					
			this.borderDivPart2.style.top = (this.YposPixel_2-2) + "px";
			this.borderDivPart2.style.left = (this.XposPixel_2-2) + "px";
			this.borderDivPart2.style.width = this.relWidth_2 + "px";
			this.borderDivPart2.style.height = this.relHeight_2 + "px";			
		}
	}
	else
		window.setTimeout("linking.showMarker()",1000);
}

Linking.prototype.removeMarker = function()
{
	if(this.borderDiv != null)
	{
		zoom.imgContainer.removeChild(this.borderDiv)
		this.borderDiv = null
	}

	if(this.borderDivPart2 != null)
	{
		zoom.imgContainer.removeChild(this.borderDivPart2)
		this.borderDivPart2 = null
	}	
}

Linking.prototype.getCoordinates = function()
{
	params = "wId="+this.wordId+"&t=tc&q="+transcription.quire+"&f="+transcription.folio+"&s="+transcription.side
	var myAjax = new Ajax.Request(
		this.linkingUrl, 
		{
			method: 'get',
			asynchronous: false,
			parameters: params,
			onComplete: linking.setCoordinates
	});
}

Linking.prototype.getWordID = function()
{
	params = "x="+this.xClick+"&y="+this.yClick+"&q="+transcription.quire+"&f="+transcription.folio+"&s="+transcription.side+"&t=mc";
	var myAjax = new Ajax.Request(
		this.linkingUrl, 
		{
			method: 'get',
			asynchronous: false,
			parameters: params,
			onComplete: linking.setWordID
	});
}

Linking.prototype.setCoordinates = function(request)
{    
	var reqObj = eval('(' + request.responseText + ')');

	linking.splittedWord = false;
	linking.XRel_A1 = reqObj.coord[0].x1;
	linking.YRel_A1 = reqObj.coord[0].y1;
	linking.XRel_A2 = reqObj.coord[0].x2;
	linking.YRel_A2 = reqObj.coord[0].y2;	
	linking.WidthRel_A = linking.XRel_A2 - linking.XRel_A1;
	linking.HeightRel_A = linking.YRel_A2- linking.YRel_A1;

	if(reqObj.count == 2)
	{
		linking.splittedWord = true;
		linking.XRel_B1 = reqObj.coord[1].x1;
		linking.YRel_B1 = reqObj.coord[1].y1;
		linking.XRel_B2 = reqObj.coord[1].x2;
		linking.YRel_B2 = reqObj.coord[1].y2;	
		linking.WidthRel_B = linking.XRel_B2 - linking.XRel_B1;
		linking.HeightRel_B = linking.YRel_B2- linking.YRel_B1;	
	}
	
	linking.setValues();
	linking.setMove();
	linking.showMarker();	
}

Linking.prototype.setWordID = function(request)
{
    var reqObj = eval('(' + request.responseText + ')');
    
	linking.splittedWord = false;
	linking.XRel_A1 = reqObj.coord[0].x1;
	linking.YRel_A1 = reqObj.coord[0].y1;
	linking.XRel_A2 = reqObj.coord[0].x2;
	linking.YRel_A2 = reqObj.coord[0].y2;	
	linking.WidthRel_A = linking.XRel_A2 - linking.XRel_A1;
	linking.HeightRel_A = linking.YRel_A2- linking.YRel_A1;
	
	if(reqObj.count == 2)
	{
		linking.splittedWord = true;
		linking.XRel_B1 = reqObj.coord[1].x1;
		linking.YRel_B1 = reqObj.coord[1].y1;
		linking.XRel_B2 = reqObj.coord[1].x2;
		linking.YRel_B2 = reqObj.coord[1].y2;	
		linking.WidthRel_B = linking.XRel_B2 - linking.XRel_B1;
		linking.HeightRel_B = linking.YRel_B2- linking.YRel_B1;	
	}
	
	linking.wordId = reqObj.coord[0].wID;
	linking.selectWord(reqObj.coord[0].wID);
	linking.selectTranslation();
	
	window.location.href = window.location.href.split("#")[0]+"#"+linking.wordId;

	linking.setValues();
	linking.setMove();
	linking.showMarker();	    
}

Linking.prototype.deselectWord = function()
{
	if(linking.selectedWord!=null)
	{	
		for(i = 0; i < linking.selectedWord.length; i++)
		{
			linking.selectedWord[i].style.backgroundColor = "";
		}
		linking.selectedWord = null;
		linking.removeMarker();
	}
}

Linking.prototype.showLinking = function(id)
{
        linking.deselectWord();
        linking.deselectTranslation();
   	    linking.removeMarker();
   	    
	    if(id.match(/\d+-\d+-\d+[a-z]*-\d+/))
	    {
	        linking.wordId = id;
            linking.selectWord(linking.wordId);
		    linking.getCoordinates();
	    } 
}

Linking.prototype.selectWord = function(id)
{
	wordParts = getDescendantElementsByAttribute(transcription.transcriptionContainer, "name", id);
	for(i = 0; i < wordParts.length; i++)
	{
		wordParts[i].style.backgroundColor = "#fffada";
	}
	linking.selectedWord = wordParts;
}

Linking.prototype.selectTranslation = function()
{
    var idParts = linking.wordId.split("-");
    var myId = idParts[0]+"-"+idParts[1]+"-"+idParts[2];
    if($(myId)!=null) $(myId).style.backgroundColor = "#fffada";
}

Linking.prototype.deselectTranslation = function()
{
	if(linking.wordId!=null)
	{
        var idParts = linking.wordId.split("-");
        var myId = idParts[0]+"-"+idParts[1]+"-"+idParts[2];
        if($(myId)!=null) $(myId).style.backgroundColor = "";
    }
}

Linking.prototype.transcriptionClick = function(event,transcriptionContainer)
{
    if(this.enabled)
    {
        this.deselectWord();
   	    this.removeMarker();
   	    this.deselectTranslation();
        	
	    this.transcriptionContainer = transcriptionContainer;
	    event = event || window.event;
	    if (! event.target)
		    event.target = event.srcElement;
    		
	    srcElem = Event.element(event);
	    if(srcElem.getAttribute("name") != null && srcElem.getAttribute("name").match(/\d+-\d+-\d+[a-z]*-\d+/))
	    {
		    this.wordId = srcElem.getAttribute("name");
            this.selectWord(this.wordId);
            this.selectTranslation(this.wordId);
		    this.getCoordinates();
	    }  
	}    
}

Linking.prototype.manuscriptClick = function(x,y)
{
    if(this.enabled)
    {
	    //relative x,y - coordinates in the whole image -> multiply relative coordinates by zoomfactor to the power of zoom
	    //e.g.: zoomFactor = 0.8, width = 600, xPos = 60, zoom=2
	    //		xPosRel = 0.8² * 60/600 = 0.064
	    xPosRel = (Math.pow(zoom.zoomFaktor, zoom.zoom)*(x/zoom.manuscriptWidth));
	    yPosRel = (Math.pow(zoom.zoomFaktor, zoom.zoom)*(y/zoom.manuscriptHeight));


	    // relative, absolute coordinates in the whole image
	    //e.g.: oldXPosAbs=0.2 xPosRel = 0.064
	    //		xPosAbs = 0.2 + 0.064 = 0.264
	    this.xClick = parseFloat(zoom.oldXPosAbs) + xPosRel;
	    this.yClick = parseFloat(zoom.oldYPosAbs) + yPosRel;
    	
	    this.deselectWord();
	    this.deselectTranslation();
	    this.removeMarker();
    	
	    this.getWordID();
    }
}

linking = new Linking();

