﻿//******************************
//JS to order divisions and there content in the manuscript workspace 
//copyright ACS Solutions, Leipzig (Germany) 2007
//******************************

function Workspace()
{

	this.language = "";
	this.quire = 0;
	this.folio = 0;
	this.side = "";
	this.translationTextURL = "../handler/translation.ashx";
	this.getFolioListUrl = "../handler/getFolioList.ashx";
	this.translationTextContainer = null;
	this.translationTextHead = null;
	this.transcriptionHead = null;
	this.transcriptionContainer = null;
	this.manuscriptHead = null;
	this.manuscriptContainer = null;
	this.physDescHead = null;	
	this.physDescContainer = null;	
	this.toolbar = null;
	
	this.fullscreen = false;
	this.fullscreenCSS = null;
	
	this.manuscriptShow = true;
	this.transcriptionShow = true;
	this.translationShow = true;
	this.physDescShow = false;
	
    this.loadCSS();
}

Workspace.prototype.init = function()
{

	this.translationTextContainer = $("translationText");
	this.translationTextHead = $("translationHead");
	this.transcriptionHead = $("transcriptionHead");
	this.transcriptionContainer = $("transcriptionText");
	this.physDescHead = $("physDescHead");
	this.physDescContainer = $("physDescText");	
	this.manuscriptHead = $("manuscriptHead");
	this.manuscriptContainer = $("manuscript");
	this.workspaceElem = $("manuscriptWorkspace");
	this.language = $F("lid");
	this.toolbar = $("toolbar")
	
	this.quire = $F("quireNo");
	this.folio = $F("folioNo");
	this.side = $F("side");
	
	this.showToolbar();

	if(Prototype.Browser.IE)
		this.transcriptionContainer.style.width = this.workspaceElem.offsetWidth - zoom.containerWidth - 53 + "px";
	
    Event.observe(window,"resize",workspace.setWorkspace);
	this.loadCookie();
	this.setPopupLayer();

	this.setWorkspace();	
	ph.setPhysicalDescriptionContent();

}

Workspace.prototype.showToolbar = function()
{
	this.toolbar.style.visibility = "visible";
	this.toolbar.style.height = "123px";
	
	img = document.createElement("img");
	img.id = "fullscreen";
	img.onmouseover = function(){this.style.cursor = 'pointer';}
	img.onclick = new Function("workspace.clickToolbar('fullscreen',this)");
	img.src = "../media/images/manuscript/fullscreen_inact.gif"
	img.alt = localize["toolbarFullscreenText"];
	img.title = localize["toolbarFullscreenText"]
	this.toolbar.appendChild(img);
	
	img = document.createElement("img");
	img.id = "zoomIcon";
	img.onmouseover = function(){this.style.cursor = 'pointer';}
	img.onclick = new Function("workspace.clickToolbar('zoom',this)");
	img.src = "../media/images/manuscript/zoom_act.gif"
	img.alt = localize["toolbarZoomText"];
	img.title = localize["toolbarZoomText"];
	this.toolbar.appendChild(img);	

/*
	img = document.createElement("img");
	img.id = "move";
	img.onmouseover = function(){this.style.cursor = 'pointer';}	
	img.onclick = new Function("workspace.clickToolbar('move',this)");
	img.src = "../media/images/manuscript/move_inact.gif"
	img.alt = localize["toolbarMoveText"];
	img.title = localize["toolbarMoveText"];
	this.toolbar.appendChild(img);		
*/
	img = document.createElement("img");
	img.id = "nextFolio";
	img.onmouseover = function(){this.style.cursor = 'pointer';}	
	img.onclick = new Function("workspace.clickToolbar('nextFolio',this)");
	img.src = "../media/images/manuscript/folio_next_inact.gif"
	img.alt = localize["toolbarNextFolioText"];
	img.title = localize["toolbarNextFolioText"];
	this.toolbar.appendChild(img);

	img = document.createElement("img");
	img.id = "prevFolio";
	img.onmouseover = function(){this.style.cursor = 'pointer';}	
	img.onclick = new Function("workspace.clickToolbar('prevFolio',this)");	
	img.src = "../media/images/manuscript/folio_prev_inact.gif"
	img.alt = localize["toolbarPrevFolioText"];
	img.title = localize["toolbarPrevFolioText"];
	this.toolbar.appendChild(img);	

/*
	img = document.createElement("img");
	img.id = "splitscreen";
	img.onmouseover = function(){this.style.cursor = 'pointer';}	
	img.onmouseover = function(){this.style.cursor = 'pointer';}	
	img.onclick = new Function("workspace.clickToolbar('spliscreen',this)");	
	img.src = "../media/images/manuscript/splitscreen.gif"
	img.alt = localize["toolbarSplitscreenText"];
	img.title = localize["toolbarSplitscreenText"];	
	this.toolbar.appendChild(img);
*/	
	img = document.createElement("img");
	img.id = "bookmark";
	img.onmouseover = function(){this.style.cursor = 'pointer';}	
	img.onclick = new Function("workspace.clickToolbar('bookmark',this)");	
	img.src = "../media/images/manuscript/bookmark.gif"
	img.alt = localize["toolbarBookmarkText"];
	img.title = localize["toolbarBookmarkText"];
	this.toolbar.appendChild(img);	
		
	img = document.createElement("img");
	img.id = "print";
	img.onmouseover = function(){this.style.cursor = 'pointer';}	
	img.onclick = new Function("workspace.clickToolbar('print',this)");	
	img.src = "../media/images/manuscript/print.gif"
	img.alt = localize["toolbarPrintText"];
	img.title = localize["toolbarPrintText"];
	this.toolbar.appendChild(img);
}

Workspace.prototype.clickToolbar = function(icon, imgObj)
{
	if(icon == "nextFolio")
		navigate("next");
	else if(icon == "prevFolio")
		navigate("prev");
	else if(icon == "fullscreen")
		this.handleFullscreen(imgObj);
	else if(icon == "bookmark")
	{
		title = "Codex Sinaiticus";
		url = document.location.href;
		if (window.sidebar) { // Mozilla Firefox Bookmark
			window.sidebar.addPanel(title, url,"");
		} else if( window.external ) { // IE Favorite
			window.external.AddFavorite( url, title); }
		else { // Opera Hotlist
		var elem = document.createElement('a');
			alert(localize["noBookmark"]);
		 }
	}
	else if(icon == "zoom")
	{
		if(zoom.enabled)
		{
			zoom.enabled = false;
			imgObj.src = "../media/images/manuscript/zoom_inact.gif";
			zoom.imgContainer.style.cursor = "default";					
		}
		else
		{
			zoom.enabled = true;
			zoom.imgContainer.style.cursor = "url(../media/openhand.cur),pointer";
			imgObj.src = "../media/images/manuscript/zoom_act.gif";
		}
	}
	else if(icon == "print")
	{
	    this.showPopupLayer();
	}
}

Workspace.prototype.loadCSS = function()
{

	this.fullscreenCSS = document.createElement("link");
	this.fullscreenCSS.setAttribute("href","../media/manuscript_fullscreen.css");
	this.fullscreenCSS.setAttribute("media","all");
	this.fullscreenCSS.setAttribute("rel","stylesheet");
	this.fullscreenCSS.setAttribute("type","text/css");
	document.getElementsByTagName("head")[0].appendChild(this.fullscreenCSS);

	document.styleSheets[document.styleSheets.length-1].disabled = true;

}



Workspace.prototype.handleFullscreen = function(imgObj)
{
	 
	 if(Prototype.Browser.IE)
	 {
		this.transcriptionContainer.style.width = "0px";
		this.translationTextContainer.style.width = "0px";
		this.manuscriptContainer.parentNode.style.display = "none";
	 }

	if(!this.fullscreen)
	{
	    for(var i = 0; i<document.styleSheets.length; i++)
	    {
	        if(document.styleSheets[i].href!=null && document.styleSheets[i].href!='' && document.styleSheets[i].href.match(/fullscreen/))
	        {
		            document.styleSheets[i].disabled = false;
		            this.fullscreen = true;
		            imgObj.src = "../media/images/manuscript/fullscreen_act.gif";
		    }
		}
	}
	else
	{
		for(var i = 0; i<document.styleSheets.length; i++)
	    {
	        if(document.styleSheets[i].href!=null && document.styleSheets[i].href!='' && document.styleSheets[i].href.match(/fullscreen/))
	        {
		        document.styleSheets[i].disabled = true;
		        this.fullscreen = false;
		        imgObj.src = "../media/images/manuscript/fullscreen_inact.gif";
  		    }
		}
	}

			
	this.setCookie();
	this.setWorkspace();	
}

Workspace.prototype.handleManuscript = function(menuObj)
{
	if(this.manuscriptShow)
	{
	    if(!this.physDescShow)
	    {
	        this.manuscriptShow = false;
	        if(this.emptyWorkspace()==0)
	        {
	            this.manuscriptShow = true;
	            return;
	        }
		    menuObj.getElementsByTagName("img")[0].src = "../media/images/manuscript/option_inactive.gif";
		    linking.enabled = false;
		}
	}
	else 
	{
		menuObj.getElementsByTagName("img")[0].src = "../media/images/manuscript/option_active.gif";	
		this.manuscriptShow = true;
		linking.enabled = true;
	}
	this.setCookie();
	this.setWorkspace();	
}

Workspace.prototype.handleTranscription = function(menuObj)
{
	if(this.transcriptionShow)
	{
		this.transcriptionShow = false;	
		if(this.emptyWorkspace()==0)
	    {
	        this.transcriptionShow = true;
	        return;
	    }
		menuObj.getElementsByTagName("img")[0].src = "../media/images/manuscript/option_inactive.gif";

	}
	else
	{

        $('DO_physicalDescription').down(0).src = "../media/images/manuscript/option_inactive.gif";
		menuObj.getElementsByTagName("img")[0].src = "../media/images/manuscript/option_active.gif";	
		this.transcriptionShow = true;
		this.physDescShow = false;
	    ph.hideRaster();
	}
	this.setCookie();
	this.setWorkspace();	
}

Workspace.prototype.handleTranslation = function(menuObj)
{
	if(this.translationShow)
	{
	    this.translationShow = false;		
		if(this.emptyWorkspace()==0)
	    {
	        this.translationShow = true;
	        return;
	    }	
		menuObj.childNodes[0].src = "../media/images/manuscript/option_inactive.gif";	
	}
	else
	{
		menuObj.childNodes[0].src = "../media/images/manuscript/option_active.gif";	
		 $('DO_physicalDescription').down(0).src = "../media/images/manuscript/option_inactive.gif";
		this.translationShow = true;
		this.physDescShow = false;
		ph.hideRaster();
	}
	this.setCookie();
	this.setWorkspace();		
}

Workspace.prototype.handlePhysDesc = function(menuObj)
{		        
	if(this.physDescShow)
	{
	    this.physDescShow = false;		
		if(this.emptyWorkspace()==0)
	    {
	        this.physDescShow = true;
	        return;
	    }	
		this.physDescContainer.parentNode.style.display = "none";
		menuObj.childNodes[0].src = "../media/images/manuscript/option_inactive.gif";
		ph.hideRaster();
	}
	else
	{
	    
		menuObj.childNodes[0].src = "../media/images/manuscript/option_active.gif";

		$('DO_translation').down(0).src = "../media/images/manuscript/option_inactive.gif";
		$('DO_transcription').down(0).src = "../media/images/manuscript/option_inactive.gif";
		$('DO_manuscript').down(0).src = "../media/images/manuscript/option_active.gif";

        this.transcriptionShow = false;
        this.translationShow = false;
		this.physDescShow = true;
		this.manuscriptShow = true;
		ph.generateRaster();
		linking.deselectWord();
	}
	this.setCookie();
	this.setWorkspace();		
}

Workspace.prototype.setWorkspace = function()
{
    
    var containerBorder = 8;
    var relManuscriptWidth = 0.55;
    var relTranscriptionHeight = 0.55;
    var marginBetween = 10;
    if(Prototype.Browser.IE)
    {
        if(workspace.fullscreen)
            var offsetWidth = 3;
        else
            var offsetWidth = 2;
    }
    else
        var offsetWidth = 0;
   

    var wsWidth = workspace.workspaceElem.getDimensions().width;
    //innerWindowHeight-footerTopMargin-2*border-topFromDivisionWorkspace-divsionHeadHeight-footerHeight
    var wsHeight = document.viewport.getHeight()-parseInt($('footer').getStyle('marginTop').replace("px",""))-16-$('manuscriptWorkspace').positionedOffset().top-24-$('footer').getHeight();

    if(wsHeight < 200) wsHeight = 200;
        
    var manWidth = wsWidth - 2*containerBorder;

    //first of all set manuscript size
    if(workspace.manuscriptShow)
    {
        workspace.manuscriptContainer.parentNode.style.display = "block";
        if(!workspace.transcriptionShow && !workspace.translationShow && ! workspace.physDescShow)
        {
            zoom.resize(manWidth, wsHeight);
            workspace.manuscriptContainer.parentNode.style.cssFloat = "none";
            workspace.manuscriptHead.style.width = manWidth + "px"
        }
        else
        {
            zoom.resize(manWidth*relManuscriptWidth, wsHeight);
            workspace.manuscriptContainer.parentNode.style.cssFloat = "left";
            workspace.manuscriptHead.style.width = manWidth*relManuscriptWidth + "px"
        }
     }
     else
        workspace.manuscriptContainer.parentNode.style.display = "none";
    
    
    //handle physical description
    if(workspace.physDescShow)
    {
        workspace.physDescContainer.style.height = (wsHeight - containerBorder) + "px";
        workspace.physDescContainer.parentNode.style.display = "block";
        if(workspace.manuscriptShow)
        {
                    workspace.physDescContainer.style.width = (manWidth - zoom.containerWidth - marginBetween - 2*containerBorder - containerBorder - offsetWidth) + "px";
            workspace.physDescContainer.parentNode.style.marginLeft = (zoom.containerWidth + marginBetween + 2*containerBorder) +"px";	
        }
        else
        {
            workspace.physDescContainer.parentNode.style.marginLeft = "0px";	
             workspace.physDescContainer.style.width = (manWidth - containerBorder - offsetWidth)+"px";
            }
    }
    else
        workspace.physDescContainer.parentNode.style.display = "none";

   
    //handle transcription
    if(workspace.transcriptionShow)
    {
        if(!workspace.translationShow)
            workspace.transcriptionContainer.style.height = (wsHeight - containerBorder) + "px"; 
        else
            workspace.transcriptionContainer.style.height = (wsHeight*relTranscriptionHeight - containerBorder) + "px";
             
        workspace.transcriptionContainer.parentNode.style.display = "block";
        
        if(workspace.manuscriptShow)
        {
            workspace.transcriptionContainer.parentNode.style.marginLeft = (zoom.containerWidth + marginBetween + 2*containerBorder) +"px";
            workspace.transcriptionContainer.style.width = (manWidth - zoom.containerWidth - marginBetween - 2*containerBorder  - containerBorder - offsetWidth) + "px";
        }
        else
        {
            workspace.transcriptionContainer.parentNode.style.marginLeft = "0px";
            workspace.transcriptionContainer.style.width = (manWidth - containerBorder - offsetWidth)+"px";
        }
    }
    else
        workspace.transcriptionContainer.parentNode.style.display = "none";
        
    //handle translation
    if(workspace.translationShow)
    {
        workspace.translationTextContainer.parentNode.style.display = "block";

        if(!workspace.transcriptionShow)
        {
            workspace.translationTextContainer.style.height = (wsHeight - containerBorder) + "px"; 
            workspace.translationTextContainer.parentNode.style.marginTop = "0px";
        }
        else
        {
            workspace.translationTextContainer.parentNode.style.marginTop = marginBetween+"px";
            workspace.translationTextContainer.style.height = (wsHeight*(1-relTranscriptionHeight) - containerBorder - marginBetween - 2*containerBorder  - 24) + "px";
        }
                     
        if(workspace.manuscriptShow)
        {
            workspace.translationTextContainer.parentNode.style.marginLeft = (zoom.containerWidth + marginBetween + 2*containerBorder) +"px";
            workspace.translationTextContainer.style.width = (manWidth - zoom.containerWidth - marginBetween - 2*containerBorder - containerBorder - offsetWidth) + "px";
        }
        else
        {
            workspace.translationTextContainer.parentNode.style.marginLeft = "0px";
            workspace.translationTextContainer.style.width = (manWidth - containerBorder - offsetWidth)+"px";
        }
        
    }
    else
        workspace.translationTextContainer.parentNode.style.display = "none";      
}

Workspace.prototype.setTranslationContent = function()
{
			params = {lid:this.language, q:this.quire, f:this.folio, s:this.side};
			var myAjax = new Ajax.Updater(
				{success: this.translationTextContainer}, 
				this.translationTextURL, 
				{
					method: 'get', 
					parameters: params,
					onFailure: workspace.translationTextError
			});
}

Workspace.prototype.setFolioList = function(ddList)
{
			params = {q:ddList[ddList.selectedIndex].value};
			var myAjax = new Ajax.Updater(
				{success: $('folioNo')}, 
				this.getFolioListUrl, 
				{
					method: 'get', 
					parameters: params,
					onFailure: workspace.folioListError
			});
}

Workspace.prototype.translationTextError = function()
{
	$('folioNo').innerHTML = "";
}

Workspace.prototype.folioListError = function()
{
	workspace.translationTextContainer.innerHTML = localize["translationTextError"];
}

Workspace.prototype.setTranslation = function()
{
	this.changeTranslationLanguage(this.language);
}

Workspace.prototype.changeTranslationLanguage = function(lang)
{
	verses = $A($("transcriptionText").getElementsByTagName("p"));
	if(verses.length > 0)
	{
		this.verseStart = verses[0].id;
		this.verseEnd = verses[verses.length-1].id;
			
		
		transHead = $A(this.translationTextHead.getElementsByTagName("span"));
		for(i=0;i<transHead.length;i++)
		{
			if(transHead[i].id!="translLang_"+lang)
			{
					$(transHead[i]).removeClassName("head_element_active");
			}
			else
			{		
					$(transHead[i]).addClassName("head_element_active");
			}
		}

		this.language = lang;
		this.setTranslationContent();
	}
	else
		window.setTimeout("workspace.changeTranslationLanguage('"+lang+"')",1000);

}

Workspace.prototype.loadCookie = function()
{
    var allCookies = document.cookie.split( ';' );
    
    var cookieFound = false;
    
    for(i = 0; i < allCookies.length; i++)
    {
        var tmpCookie = allCookies[i].split("=");
        var cookieName = tmpCookie[0].replace(/^\s+|\s+$/g, '');
        
        if(cookieName == "CSworkspace") 
        {
            cookieFound = true;
            var cookieValue = eval("("+unescape( tmpCookie[1].replace(/^\s+|\s+$/g, '') )+")");
            if(cookieValue.fullscreen) this.handleFullscreen($("fullscreen"));
            if(cookieValue.physicalDescription)
                this.handlePhysDesc($("DO_physicalDescription"));
            else
            {
                if(!cookieValue.translation) this.handleTranslation($("DO_translation"));
                if(!cookieValue.transcription) this.handleTranscription($("DO_transcription"));
            }
            if(!cookieValue.manuscript) this.handleManuscript($("DO_manuscript"));
           
            if(cookieValue.transcriptionType==null)
                transcription.changeView(0);
            else
                transcription.changeView(cookieValue.transcriptionType);
        }
        
        if(!cookieFound)
        {
            transcription.changeView(0);
            this.setTranslation();
        }
      
        tmpCookie = null;
		cookieName = '';
     }
}

Workspace.prototype.setCookie = function()
{
    
    var today = new Date();
    today.setTime( today.getTime() );
    
    expires = 3600000*24*30;
    var expires_date = new Date( today.getTime() + (expires) );
    
    var cookieString = "{";
    cookieString += "fullscreen:"+this.fullscreen+",";
    cookieString += "manuscript:"+this.manuscriptShow+",";
    cookieString += "transcription:"+this.transcriptionShow+",";
    cookieString += "translation:"+this.translationShow+",";
    cookieString += "physicalDescription:"+this.physDescShow+",";
    cookieString += "transcriptionType:"+transcription.type;
    cookieString += "}";
    
    document.cookie = "CSworkspace="+escape(cookieString)+"; expires="+expires_date.toGMTString();
}

Workspace.prototype.emptyWorkspace = function()
{
    return (this.manuscriptShow | this.transcriptionShow | this.translationShow | this.physDescShow);
}

Workspace.prototype.setPopupLayer = function()
{
    var popupLayer = document.createElement("div");
        Event.observe(popupLayer,"click",workspace.hidePopupLayer);
        popupLayer.id="popupLayer";
        popupLayer.style.zIndex = "1000";
        popupLayer.style.position = "absolute"; 
        popupLayer.style.top = "0px";
        popupLayer.style.width = "100%";        
        popupLayer.style.height = $("main").getHeight() + "px";
        popupLayer.style.background = "black";
        popupLayer.style.display = "none";
        
        $('printPanel').style.left = (document.viewport.getWidth()/2-$('printPanel').getWidth()/2) + "px"
        
	    document.getElementsByTagName("body")[0].appendChild(popupLayer);
	    document.getElementsByTagName("body")[0].appendChild($('printPanel'));
        new Effect.Opacity('popupLayer', {to: 0});	    
}

Workspace.prototype.showPopupLayer = function()
{
   	
    $('popupLayer').style.display = "block";
    new Effect.Opacity('popupLayer', {from:0, to: 0.75, duration:0.3});
    window.setTimeout("$('printPanel').show()", 300);
}

Workspace.prototype.hidePopupLayer = function()
{
     $('printPanel').hide();
     $('popupLayer').fade({duration:0.3});
     new Effect.Opacity('popupLayer', {to: 0});
}

Workspace.prototype.print = function(path)
{
    var inputs = $('printPanel').getElementsByTagName("input");
    var paramString = "";
    for(var i = 0; i < inputs.length; i++)
    {
        if(inputs[i].checked != "")
            paramString += inputs[i].name + "=" + inputs[i].value + "&"
    }
    paramString += "lg=" + this.language + "&quireNo=" + this.quire + "&folioNo=" + this.folio + "&side=" + this.side;
    var myPath = path + "/print.aspx?"+paramString
    
    //var newWindow = window.open(myPath,"Codex Sinaiticus","width=750, location=no, menubar=yes, scrollbars=yes");
    myPath = myPath.replace(/\/{2,}/,"/")
    window.open(myPath, "Zweitfenster", "width=750,left=100,top=100,location=no, menubar=yes, scrollbars=yes");
    
}

workspace = new Workspace();



