﻿// JScript-Datei

function PhysicalDesc()
{
    	this.physDescURL = "../handler/physicalDescription.ashx";
    	this.raster ={4:{
    	    	     v: {
    	                a:{x1:1584,y1:357,x2:1959,y2:1854},
    	                b:{x1:1146,y1:357,x2:1584,y2:1854},
    	                c:{x1:734,y1:357,x2:1146,y2:1854},
    	                d:{x1:365,y1:357,x2:734,y2:1854},
    	                e:{x1:365,y1:0,x2:1959,y2:357},
    	                f:{x1:0,y1:0,x2:365,y2:1074},
    	                g:{x1:0,y1:1074,x2:365,y2:2318},
    	                h:{x1:365,y1:1854,x2:1959,y2:2318},
    	                i:{x1:1959,y1:1074,x2:2072,y2:2318},
    	                j:{x1:1959,y1:0,x2:2072,y2:1074}},
    	                
    	            r: {
    	                a:{x1:115,y1:355,x2:501,y2:1861},
    	                b:{x1:501,y1:355,x2:924,y2:1861},
    	                c:{x1:924,y1:355,x2:1342,y2:1861},
    	                d:{x1:1342,y1:355,x2:1737,y2:1861},
    	                e:{x1:115,y1:0,x2:1737,y2:355},
    	                f:{x1:1737,y1:0,x2:2066,y2:1120},
    	                g:{x1:1737,y1:1121,x2:2066,y2:2316},
    	                h:{x1:115,y1:1861,x2:1737,y2:2316},
    	                i:{x1:0,y1:1120,x2:115,y2:2316},
    	                j:{x1:0,y1:0,x2:115,y2:1120}}},
    	                
    	         2:{
                    v: {
    	                a:{x1:1158,y1:364,x2:1980,y2:1896},
    	                b:{x1:367,y1:364,x2:1157,y2:1896},
    	                e:{x1:367,y1:0,x2:1980,y2:363},
    	                f:{x1:0,y1:0,x2:366,y2:1132},
    	                g:{x1:0,y1:1133,x2:366,y2:2292},
    	                h:{x1:367,y1:1897,x2:1980,y2:2292},
    	                i:{x1:1981,y1:1133,x2:2066,y2:2292},
    	                j:{x1:1981,y1:0,x2:2066,y2:1132}},  
   	                
    	            r: {
    	                a:{x1:103,y1:369,x2:895,y2:1901},
    	                b:{x1:896,y1:369,x2:1715,y2:1901},
    	                e:{x1:103,y1:0,x2:1715,y2:368},
    	                f:{x1:1716,y1:0,x2:2088,y2:1162},
    	                g:{x1:1716,y1:1163,x2:2088,y2:2298},
    	                h:{x1:103,y1:1902,x2:1715,y2:2298},
    	                i:{x1:0,y1:1163,x2:102,y2:2298},
    	                j:{x1:0,y1:0,x2:102,y2:1162}}},
	             1:{
	                v:  {
	                    a:{x1:0,y1:0,x2:0,y2:0},
	                    e:{x1:0,y1:0,x2:0,y2:0},
	                    f:{x1:0,y1:0,x2:0,y2:0},
	                    g:{x1:0,y1:0,x2:0,y2:0},
	                    h:{x1:0,y1:0,x2:0,y2:0},
	                    i:{x1:0,y1:0,x2:0,y2:0},
	                    j:{x1:0,y1:0,x2:0,y2:0}
	                    },
	                r: {
	                    a: { x1: 0, y1: 0, x2: 0, y2: 0 },
	                    e: { x1: 0, y1: 0, x2: 0, y2: 0 },
	                    f: { x1: 0, y1: 0, x2: 0, y2: 0 },
	                    g: { x1: 0, y1: 0, x2: 0, y2: 0 },
	                    h: { x1: 0, y1: 0, x2: 0, y2: 0 },
	                    i: { x1: 0, y1: 0, x2: 0, y2: 0 },
	                    j: { x1: 0, y1: 0, x2: 0, y2: 0 }
	                 }
    	             }};
    	                
        this.crossReference = {
                                4:{
                                    r:{a:1,b:2,c:3,d:4},
                                    v:{a:4,b:3,c:2,d:1}
                                   },
                               2:{
                                    r:{a:1,b:2},
                                    v:{a:2,b:1}
                                }
                             }; 

        this.rasterShow = false;
        
        this.offsetX1 = 0;
        this.offsetX2 = 0;
        this.offsetY1 = 0;
        this.offsetY2 = 0;
        this.offsetW = 0;
        this.offsetH = 0;
       
        this.loadOffset = false;
        
        this.offsetURL = "../handler/getPhdRasterStart.ashx";
        this.coordUrl = "../handler/getLineCoords.ashx";

        this.lastSelectedParts = "";     
}

PhysicalDesc.prototype.init = function()
{
    this.container = $('physDescText');
    this.openCloseButton = this.container.select('img[class="physDescOpenClose"]');
    this.groups = this.container.getElementsBySelector('ul[class="group"]');
    this.observeEvent();
    this.createRasterLinks();
}

PhysicalDesc.prototype.setPhysicalDescriptionContent = function()
{

			params = {q:workspace.quire, f:workspace.folio, s: workspace.side, lid: workspace.language};
			var myAjax = new Ajax.Updater(
				{success:  $('physDescText')}, 
				this.physDescURL, 
				{
					method: 'get', 
					parameters: params,
					evalScripts: true
			});
}

PhysicalDesc.prototype.generateRaster = function()
{ 
    if(!this.rasterShow && !this.loadOffset)
    {
	    params = "q="+workspace.quire+"&f="+workspace.folio+"&s="+workspace.side;
	    var myAjax = new Ajax.Request(
		    this.offsetURL, 
		    {
			    method: 'get',
			    asynchronous: false,
			    parameters: params,
			    onComplete: ph.setOffset
	    });
	}
    this.printRaster();	
}

PhysicalDesc.prototype.getCoordinates = function(column1, line1, column2, line2)
{
    params = { q: workspace.quire, f: workspace.folio, s: workspace.side, c1: column1, l1: line1, c2: column2, l2: line2 };
	var myAjax = new Ajax.Request(
		this.coordUrl, 
		{
			method: 'get',
			asynchronous: false,
			parameters: params,
			onComplete: linking.setCoordinates
	});
}

PhysicalDesc.prototype.observeEvent = function()
{
    this.openCloseButton.each(function(elem)
    {
        Event.observe(elem, 'click', ph.showHidegroup)
    })
}

PhysicalDesc.prototype.showHidegroup = function(event)
{
    
    icon = $(Event.element(event));
    list = document.getElementById(Event.element(event).readAttribute('name').split("_")[0]);


    if(list.getStyle('display')=="block")
    {
       list.setStyle({display:'none'});
       icon.writeAttribute('src', '../media/images/plus.gif')
    }
    else
    {
        list.setStyle({display:'block'});
        icon.writeAttribute('src', '../media/images/minus.gif')
    }  
}

PhysicalDesc.prototype.resetRaster = function() {

    for (var item in this.raster[transcription.cols][workspace.side]) {


        if ((rasterItem = $(item)) == null) rasterItem = document.createElement("div");

        rasterItem.id = item;

        if(this.offsetW != 0 && this.offsetH != 0)
        {

            w = (parseInt(this.raster[transcription.cols][workspace.side][item].x2) - parseInt(this.raster[transcription.cols][workspace.side][item].x1)) * zoom.manuscriptZoomWidth / parseInt(this.offsetW);
            h = (parseInt(this.raster[transcription.cols][workspace.side][item].y2) - parseInt(this.raster[transcription.cols][workspace.side][item].y1)) * zoom.manuscriptZoomHeight / parseInt(this.offsetH);
            x = (((parseInt(this.offsetX1) + parseInt(this.raster[transcription.cols][workspace.side][item].x1)) / parseInt(this.offsetW)) - zoom.oldXPosAbs) * zoom.manuscriptZoomWidth;
            y = (((parseInt(this.offsetY1) + parseInt(this.raster[transcription.cols][workspace.side][item].y1)) / parseInt(this.offsetH)) - zoom.oldYPosAbs) * zoom.manuscriptZoomHeight;

            rasterItem.style.display = "block";
            rasterItem.style.border = "0px solid #bb0404";
            rasterItem.style.zIndex = 3;
            rasterItem.style.position = "absolute";
            rasterItem.style.width = w + "px";
            rasterItem.style.height = h + "px";
            rasterItem.style.top = y + "px";
            rasterItem.style.left = x + "px";

            //rasterItem.setStyle({display:'block',border:'1px solid #FFCCCC',zIndex:3,position:'absolute',width:w+'px',:h+'px',top:y+'px',left:x+'px'});
            if (!this.rasterShow)
                zoom.imgContainer.appendChild(rasterItem);
          }

    }

    linking.removeMarker();
}

PhysicalDesc.prototype.printRaster = function() {
    if (ph.offsetX1 != 0 && ph.offsetY1 != 0 && ph.offsetH != 0 && ph.offsetW != 0) {

        this.resetRaster();
        this.showRaster(this.lastSelectedParts);
        this.rasterShow = true;
    }
}

PhysicalDesc.prototype.setOffset = function(request)
{
    ph.loadOffset = true;
    var offset = eval('(' + request.responseText + ')');

    if(offset.x1 != "" && offset.y1 != "" && offset.x2 != "" && offset.y2 != "")
    {
        ph.offsetX1 = offset.x1;
        ph.offsetX2 = offset.x2;
        ph.offsetY1 = offset.y1;
        ph.offsetY2 = offset.y2;
        ph.offsetH = offset.h;
        ph.offsetW = offset.w;
        ph.printRaster();
    }
}

PhysicalDesc.prototype.hideRaster = function()
{

    for (var item in this.raster[transcription.cols][workspace.side])
    {
        if((rasterItem = $(item))!=null)
            rasterItem.style.display = "none";
    }
    this.rasterShow = false;
}


PhysicalDesc.prototype.createRasterLinks = function()
{
    var lis = $$('li.propValue', 'td.propValue');

    lis.each(function(item)
    {
        
        if(item.innerHTML.match(/###/))
        {
            //var regex = /(([a-j][\d*(\s|\+|\-)]*\s*)+[a-j][\+\d,\-]*)/;
            var regex = /(###)((.|\n)*?)(###)/;
            
            var found = null;
            
            while(found = regex.exec(item.innerHTML))
            {            
                var link = "";

                if(found != null)
                { 

                   $A(found[2].split("+")).each(function(item2){
                            link += '<a href="#" onClick="ph.resetRaster();ph.showRaster(\''+ item2.replace(/%0D%0A/gi," ") +'\')" >' + item2 + '</a> + '; 
                   });
                    var test = item.innerHTML.replace(/(###)((.|\n)*?)(###)/,link.slice(0,link.length-2));
                    item.update(test);
                }
            }
        }
    });
}

PhysicalDesc.prototype.showRaster = function(s) {

    if (s != "") {

        //remove space on the left and right and remove additional information in brackets
        var part = s.replace(/\([^\)]*\)/g, "").basicTrim();

        if (part.match(/\-/)) {
            var s2 = part.split("-");
            var l1 = -1;
            var c1 = -1;
            var l2 = -1;
            var c2 = -1;

            for (var j = 0; j < s2.length; j++) {

                var tempC = null;
                var tempL = null;

                if (s2[j].match(/[a-j]\d+/) && this.crossReference[transcription.cols][workspace.side][s2[j].match(/[a-j]/)[0]] != null) {
                    tempC = this.crossReference[transcription.cols][workspace.side][s2[j].match(/[a-j]/)[0]];
                    tempL = parseInt(s2[j].match(/\d+/)[0]);
                }
                else if (s2[j].match(/\d+/)) {
                    tempL = parseInt(s2[j]);
                }
                else if (s2[j].match(/[a-j]/) && part.match(/\d+/) && this.crossReference[transcription.cols][workspace.side][s2[j].match(/[a-j]/)[0]] != null) {
                    tempC = this.crossReference[transcription.cols][workspace.side][s2[j].match(/[a-j]/)[0]];
                }
                else {
                    $(s2[j]).setStyle({ border: '1px solid #bb0404', zIndex: 4 });
                }
                if (tempC != null && (c1 == -1 || tempC < c1))
                    c1 = tempC;
                if (tempL != null && (l1 == -1 || tempL < l1))
                    l1 = tempL;
                if (tempC != null && (c2 == -1 || tempC > c2))
                    c2 = tempC;
                if (tempL != null && (l2 == -1 || tempL > l2))
                    l2 = tempL;
            }

            this.getCoordinates(c1, l1, c2, l2);
        }
        else if (part.match(/[a-j]\d+/)) {
            if (this.crossReference[transcription.cols][workspace.side][part.match(/[a-j]/)[0]] != null)
                this.getCoordinates(this.crossReference[transcription.cols][workspace.side][part.match(/[a-j]/)[0]], part.match(/\d+/)[0], -1, -1);

        }
        else if (!part.match(/\d+/)) {
            $(part).setStyle({ border: '1px solid #bb0404', zIndex: 4 });
        }

        this.lastSelectedParts = s;
    }

    /*  part = s.basicTrim();
    
    linking.removeMarker();
    
    if(part.match(/^[a-j]\d+\-\d+$/))
    {
    var s1=part.split("-");
    var lines = "";
    for(var j=s1[0].replace(/[a-j]/,'');j<=s1[1];j++)
    {
    lines += j;
    if(j!=s1[1]) lines += ",";   
    }
    this.getCoordinates(this.crossReference[transcription.cols][workspace.side][part.match(/[a-j]/)[0]],lines);
    }
    else if(part.match(/\-/))
    {
    var s2=part.split("-");
    var lastPart = "";
    for(var j=0;j<s2.length;j++)
    {
    if(j!=0)
    {
    if(s2[j].match(/^[a-d]*\d+$/))
    lastPart += "-" + s2[j];
    else
    {
    ph.showRaster(lastPart);
    lastPart = s2[j];
    }
    }
    else
    lastPart = s2[j];
    }
    ph.showRaster(lastPart);
    }
    else if(part.match(/[a-j]\d+/))
    {
    //$(part.match(/[a-j]/)[0]).setStyle({border:'1px solid red',zIndex:4});
    if(this.crossReference[transcription.cols][workspace.side][part.match(/[a-j]/)[0]]!=null)
    this.getCoordinates(this.crossReference[transcription.cols][workspace.side][part.match(/[a-j]/)[0]],part.match(/\d+/)[0]);
        
    }
    else if(!part.match(/\d+/))
    {
    $(part).setStyle({border:'1px solid red',zIndex:4});
    }
    */
}

var ph = new PhysicalDesc();

