﻿Event.observe(window, "load", OnInitializeApplicationSkin);

var ibrareatop = 100;
var ibrleftmainarea = 200;
var ibrpubviewertop = 121;
var ibrinfobartop = ibrpubviewertop;
var ibrstatusarea = 20;
var heightmargin = 40;

function OnInitializeApplicationSkin()
{
  Event.observe(document, MAGMA.EVENT.APPLICATION.LOADED, OnApplicationSkinLoaded);
}

function ExpandPanel() 
{  
  window.setTimeout("ShowPageCustom('" + startuppanel + "')", 100);     	
}

function OnApplicationSkinLoaded(host)
{
	host = self;		
	Element.show("headerbar");

  Proxy.GetEventSink().observe(UPLATFORM.BOOKCOMPILER.UPDATED, OnBookCompiler_Updated);
  Proxy.GetEventSink().observe(UPLATFORM.BOOKCOMPILER.USEUPDATE, OnBookCompiler_UseUpdate);
}

function SetScreenElements()
{
	var pw = parseInt(document.body.clientWidth);
	var ph = parseInt(document.body.clientHeight);
	
	
	var spacebefore = 0;
	
  
  // toolbar
  var col_toolbar = $("maintoolbar").getElementsByTagName("DIV");
  var div_toolbar = null;
  for (i=1;i< col_toolbar.length;i++) {
    div_toolbar = col_toolbar[i];    
    spacebefore += Element.getWidth(col_toolbar[i-1]) + 1; 
    div_toolbar.style.left = (spacebefore - i) +  "px";
  }
  
	var dasboardarea = $("dashboardarea");
	if (dashboardarea) {
		dashboardarea.style.left = "0px";
		dashboardarea.style.top = ibrareatop + "px";
		var dbaw = (pw);
		if (dbaw < 200) {
		  dbaw = 200;	
		}
		var dbah = ph - ibrareatop;
		if (dbah < 200) {
		  dbah = 200;	
		}		
	  dashboardarea.style.width = dbaw + "px";
	  dashboardarea.style.height = dbah + "px";  	  	
	}
	
	var librarytocarea = $("librarytocarea");
	if (librarytocarea) {
		
		librarytocarea.style.left = "0px";
		librarytocarea.style.top = ibrareatop + "px";
	  
	  
	  var ltah = (ph - ibrareatop - heightmargin);
	  //var ltaw = 324;
	  var ltaw = 700;
	  if (ltah < 200) {
	    ltah = 200;	
	  }	  
	  
  
	  librarytocarea.style.height = ltah + "px";  
	  librarytocarea.style.width = ltaw + "px";  	 	
	}
	
	var searcharea = $("searcharea");
	if (searcharea) {
		searcharea.style.left = "0px";
		searcharea.style.top = ibrareatop + "px";
	  var saw =  parseInt(pw * 0.8);
		if (saw < 600) {
		  saw = 600;	
		}
		if (saw > 1000) {
	    saw = 1000;  
	  }
		var sah = (ph - ibrareatop - heightmargin);
		if (sah < 0) {
		  sah = 0;	
		}
	  searcharea.style.width = saw + "px"; 
	  searcharea.style.height = sah + "px";  	  	
	}

	var sidebar = $("sidebar");
	if (sidebar) {
		sbtop = (ph / 3);
		if (sbtop < 50) {
		  sbtop = 50;	
		}
	  sidebar.style.top = sbtop + "px"; 
	}
	
	var pubviewer = $("publicationviewerframe");
  if (pubviewer) {   
		 var pvh = (ph - ibrpubviewertop);
	   if (pvh < 100) {
	     pvh = 100;	
	   }
	   var pvw = (pw);
	   if (pvw < 200) {
	     pvw = 200;	
	   }  
		 pubviewer.style.left = "0px";
		 pubviewer.style.top =  (ibrpubviewertop - 1) + "px";
		 pubviewer.style.width = pvw + "px";
		 pubviewer.style.height = pvh + "px";   
  }
  
  var docinfo = $("documentinfobar");
  var docinfotext = $("docinfotext");
  var docinfobuttons = $("docinfobuttons");
  var docinfoextra = $("docinfo_extra");
  var docinfoactivedocs = $("docinfo_activedocs");
  var docinfofavorites = $("docinfo_favoritesarea");

  if ((docinfo) && (docinfotext) && (docinfobuttons) && (docinfoextra) && (docinfoactivedocs) && (docinfofavorites)) {
    var diw = (pw);
	   if (diw < 200) {
	     diw = 200;	
	   }  
	  docinfo.style.height = "20px";
	  docinfo.style.left = "0px";
	  docinfo.style.top = (ibrinfobartop - Element.getHeight(docinfo) - 1) +  "px"; 
	  docinfo.style.width = diw + "px"; 
	  
	  docinfotext.style.top = (ibrinfobartop - Element.getHeight(docinfo) + 1) +  "px"; 
	  docinfotext.style.left = "4px";
	  docinfotext.style.height = "20px";
	  docinfotext.style.width = (diw - 100) + "px"; 
	  
	  docinfobuttons.style.height = "24px";  
	  docinfobuttons.style.top = (ibrinfobartop - Element.getHeight(docinfo)) +  "px"; 
	  docinfobuttons.style.width = "200px";
	  docinfobuttons.style.left = (diw - 190) + "px";
	    
	  docinfoextra.style.top = ibrpubviewertop +  "px";
	  docinfoextra.style.left = "0px";
	  docinfoextra.style.width = diw + "px"; 	
	  docinfoextra.style.height = "150px"; 
	  
	  docinfoactivedocs.style.top = ibrareatop +  "px";
	  docinfoactivedocs.style.left = "0px";	  
	  var diadh = (ph - ibrareatop - heightmargin);
	  if (diadh < 200) {
	    diadh = 200;	
	  }	  
	  var diadw = 700;	  
	  docinfoactivedocs.style.width = diadw + "px"; 
	  docinfoactivedocs.style.height = diadh + "px";  	
	  
	  
	  docinfofavorites.style.top = ibrareatop +  "px";	  
	  docinfofavorites.style.left = "0px";
	  var difh = (ph - ibrareatop - heightmargin);
	  if (difh < 200) {
	    difh = 200;	
	  }	  
	  var difw = 700;
	  docinfofavorites.style.width = difw + "px"; 
	  docinfofavorites.style.height = difh + "px"; 
  }
  
  var stbar = $("statusbar")
  if (stbar) {
    stbar.style.top = (ph - 24) + "px";
    stbar.style.left = "8px";   
    var stbw = (pw - 32);
	  if (stbw < 200) {
	    stbw = 200;	
	  }   
    stbar.style.width = stbw + "px"
  }
  
  var licarea = $("licarea");
  if (licarea) {
    licarea.style.left = (pw - Element.getWidth(licarea) - 8) + "px";
    licarea.style.top = (ph) + "px";	
  }
  
  var releasearea = $("releasearea");
  if (releasearea) {
  	var ral = pw- Element.getWidth(releasearea) - 100;
  	if (ral < 100) {
  	  ral = 100;	
  	}
    releasearea.style.left = (ral) + "px";
    releasearea.style.top = (ph) + "px";	
  }
  
  var reageren = $("reagerenarea");
  if (reageren) {
  	var real = parseInt((pw / 2) - (Element.getWidth(reageren) / 2));
  	if (real < 0) {
  	  real = 0;	
  	}
  	var reah = parseInt((ph / 2) - (Element.getHeight(reageren) / 2));
  	if (reah < 0) {
  	  reah = 0;	
  	}
  	
    reageren.style.left = (real) + "px";
    reageren.style.top = (reah) + "px";	
  }
}

function ShowPage(id)
{ 
  if (allowtogglemainpage) { 
	  Proxy.SingleInstance.Show(id);  
		var area = $(id + "area");
		var linkbutton = $("linkbutton_" + id);
		if ((area) && (linkbutton)) { 
		  if (area.getAttribute("status") != "show") { 
		  	allowtogglemainpage = false;	
		  	linkbutton.setAttribute("status", "selected");
		  	linkbutton.className = "largelinkbuttonselected"; 	
		  	Effect.SlideDown(area, {duration: 0.5, afterFinish:AllowToggleMainPage});	  	
		    area.setAttribute("status", "show");	          	    
		  }
		}
  }
}

function HidePage(id)
{ 
	if (allowtogglemainpage) {
		var area = $(id + "area");
		var linkbutton = $("linkbutton_" + id);
		if ((area) && (linkbutton)) {
			if (area.getAttribute("status") == "show") {
				allowtogglemainpage = false;
		  	linkbutton.setAttribute("status", "");  
		  	linkbutton.className = "largelinkbutton";
		  	Effect.SlideUp(area, {duration: 0.5, afterFinish:AllowToggleMainPage});	
		    area.setAttribute("status", "hide");  	    
		  }
		}
  }
}


function TogglePageCustom(id)
{ 
  var area = $(id + "area");
  if (area) {
    if (area.getAttribute("status") != "show") {
      ShowPageCustom(id);	
    } else {
      HidePageCustom(id);	
    }	         	 
  }	
}

function ShowPageCustom(id)
{ 
  if (allowtogglemainpage) { 
	  Proxy.SingleInstance.Show(id);  
		var area = $(id + "area");
		if (area) { 
		  if (area.getAttribute("status") != "show") { 
		  	allowtogglemainpage = false;	 	
		  	Effect.Appear(area, {duration: 0.5, afterFinish:AllowToggleMainPage});	  	
		    area.setAttribute("status", "show");	          	    
		  }
		}
  }
}

function HidePageCustom(id)
{ 
	if (allowtogglemainpage) {
		var area = $(id + "area");
		if (area) {
			if (area.getAttribute("status") == "show") {
				allowtogglemainpage = false;
		  	Effect.Fade(area, {duration: 0.5, afterFinish:AllowToggleMainPage});	
		    area.setAttribute("status", "hide");  	    
		  }
		}
  }
}

function OpenExternal(id)
{

}

function RegisterCustomElements()
{
  Proxy.SingleInstance.Register("dashboard", function() {HidePageCustom("dashboard");});   
Proxy.SingleInstance.Register("opendocsarea", function() {CollapseOpenDocsAreaSBR("opendocs");});    
Proxy.SingleInstance.Register("favoritesarea", function() {CollapseFavoritesAreaSBR("favorite");});  
}

function ShowFootNote(fnobj)
{   
  var rightcolumnwidth = 200;
  var correction = 56;
  var contentframe = Proxy.GetContext().UPlatform.GetContentFrame();
  if (contentframe) {
    if (contentframe.document) {
    	var notetextinner = "Geen voetnoottekst gevonden";
    	var notetext = contentframe.document.getElementById("fntxt_" + fnobj.id);
    	if ((notetext) && (notetext.innerHTML != '')) {
    	  notetextinner = notetext.innerHTML;	
    	} 
    	
    	var fnleft  = parseInt(document.body.clientWidth) - rightcolumnwidth - correction - parseInt(contentframe.document.body.clientWidth);
		  new Tip(fnobj, notetextinner,  {title:'Voetnoot ' + fnobj.id, style: 'elegant', hideAfter: 0.5,
		  	hook: { tip: 'topLeft', target: 'bottomRight' }, offset: { x:fnleft, y:132 } });		  		
		  
		  Event.observe(fnobj, 'prototip:hidden', RemoveFootNote);
		}	   
  } 	
}

function RemoveFootNote(event) {
  Tips.remove( event.element() );
}


function ToggleOpenDocsAreaSBR(id)
{
	var docinfo_activedocs = $("docinfo_activedocs");
	if (docinfo_activedocs.getAttribute("status") != "expanded") {
	  ExpandOpenDocsAreaSBR(id);      	
	} else {
	  CollapseOpenDocsAreaSBR(id);	
	}
}

function ExpandOpenDocsAreaSBR(id) 
{  
  if (allowtoggleopendocs) {
	  var docinfo_activedocs = $("docinfo_activedocs");
	  var linkbutton = $("linkbutton_" + id);
	  if ((docinfo_activedocs) && (linkbutton)) { 
	  	if (docinfo_activedocs.getAttribute("status") != "expanded") {	
	  		Proxy.SingleInstance.Show("opendocsarea");
	      docinfo_activedocs.setAttribute("status","expanded");
	      linkbutton.className = "largelinkbuttonselected"; 
	      linkbutton.setAttribute("status", "selected");  
	      allowtoggleopendocs = false;
	      Effect.SlideDown(docinfo_activedocs, {duration:0.5, afterFinish:function() {allowtoggleopendocs=true;}});
	    }	
	  }
	}
}

function CollapseOpenDocsAreaSBR(id)
{
  if (allowtoggleopendocs) {
	  var docinfo_activedocs = $("docinfo_activedocs");
	  var linkbutton = $("linkbutton_" + id);
	  if ((docinfo_activedocs) && (linkbutton)) { 
	  	if (docinfo_activedocs.getAttribute("status") != "collapsed") {
	      docinfo_activedocs.setAttribute("status","collapsed");
	      linkbutton.className = "largelinkbutton"; 
	      linkbutton.setAttribute("status", "");  
	      allowtoggleopendocs = false;
	      Effect.SlideUp(docinfo_activedocs, {duration:0.5, afterFinish:function() {allowtoggleopendocs=true;}});
	    }	
	  }	
	}
}

function ToggleFavoritesSBR(id)
{  
	var docinfo_favorites = $("docinfo_favoritesarea");
	if (docinfo_favorites.getAttribute("status") != "expanded") {
	  ExpandFavoritesAreaSBR(id);      	
	} else {
	  CollapseFavoritesAreaSBR(id);	
	}
}

function ExpandFavoritesAreaSBR(id) 
{  
  if (allowtogglefavorites) {
	  var docinfo_favorites = $("docinfo_favoritesarea"); 
	  var linkbutton = $("linkbutton_" + id);
	  if ((docinfo_activedocs) && (linkbutton)) { 		
	  	if (docinfo_favorites.getAttribute("status") != "expanded") {	
	  		Proxy.SingleInstance.Show("favoritesarea");
	      allowtogglefavorites = false;
	      Effect.SlideDown(docinfo_favorites, {duration:0.5, afterFinish:function() {allowtogglefavorites=true;}});            
	      docinfo_favorites.setAttribute("status", "expanded");	
	      linkbutton.className = "largelinkbuttonselected"; 
	      linkbutton.setAttribute("status", "selected");      
	    }
	  }
	}
}

function CollapseFavoritesAreaSBR(id)
{
  if (allowtogglefavorites) {
	  var docinfo_favorites = $("docinfo_favoritesarea");
	  var linkbutton = $("linkbutton_" + id);
	  if ((docinfo_activedocs) && (linkbutton)) {     
	    if (docinfo_favorites.getAttribute("status") != "collapsed") {	
	    	allowtogglefavorites = false;
	      Effect.SlideUp(docinfo_favorites, {duration:0.5, afterFinish:function() {allowtogglefavorites=true;}});
	      docinfo_favorites.setAttribute("status", "collapsed");	
	      linkbutton.className = "largelinkbutton"; 
	      linkbutton.setAttribute("status", "");  
	    }	
	  }	
	}
}

function DocumentLink(id)
{
	Proxy.GetContext().UPlatform.OpenDocument(id,'system',false);
}

function ShowReageerArea()
{
  var mask = $("masklayer");
  var reageren = $("reagerenarea");
  if ((mask) && (reageren))  {
    Element.show(mask);
    Element.show(reageren);  	
  }  	
}

function HideReageerArea()
{
  var mask = $("masklayer");
  var reageren = $("reagerenarea");
  if ((mask) && (reageren))  {
    Element.hide(reageren);  
    Element.hide(mask);	
  }  
}

function ZendReactie()
{ 
  Proxy.GetContext().UPlatform.FeedBack.Submit($('reactie'), { Id:'reactie', OnSuccess: DoSuccess, OnFailure:DoFailure });
}

  	
function DoSuccess()
{
  var msg = "Uw reactie wordt verzonden.";
  alert(msg);	
  HideReageerArea();
}
  	
function DoFailure(error)
{ 	  
  var msg = "Reactie niet kunnen versturen.\nZijn alle verplichte velden ingevuld?";
  alert(msg);		
}

function OnBookCompiler_Updated(event)
{    			    
  var bookcompiler_print = $("docinfo_printwholedoc");
  var bookcompiler_print_disabled = $("docinfo_printwholedoc_disabled");
  if ((bookcompiler_print) && (bookcompiler_print_disabled)) {
    if (event.memo.Available) {				        
      Element.show(bookcompiler_print);
      Element.hide(bookcompiler_print_disabled);
      
    } else {
      Element.hide(bookcompiler_print);
      Element.show(bookcompiler_print_disabled);		        
    }
  }
} 
  
function OnBookCompiler_UseUpdate(event)
{				    
  if (event.memo.Visible) {  
    window.setTimeout( "PrintDocument()", 1000);
  }
}		

function PrintWholeDocument()
{
  Proxy.GetContext().UPlatform.BookCompiler.Execute('print');	
}