﻿// Content support
var documentisloading = false;

function initializeOverlay() 
{  
  var objbody = document.getElementsByTagName("body").item(0);
	if (objbody) {
	  // Dynamically add overlay html content
	  var objoverlay = document.createElement("div");
	  objoverlay.setAttribute("id", "overlayprogress");
	  objoverlay.style.display = "none";	  
	  objbody.appendChild(objoverlay); 
	  
	  var objloading = document.createElement("div");
		objloading.setAttribute("id", "loadingprogress");
		objoverlay.appendChild(objloading);		
	}
}

function resizeDocumentOverlay()
{
  if (documentisloading) {
    contentobject = $("iframe_content"); 
    if ((!contentobject) && (parent)) {
      contentobject = parent.$("iframe_content");
      // Opera 'get element from parent level' error:
      // Set contentobject to null to prevent getRect/prototype/scriptaculous error in Opera. This way 
      // document is updated but without effect. When contentobject is on same level works without problem.
      if (window.opera) { 
        contentobject = null;
      }
    }
    overlaydiv = $("overlayprogress");
    if ((!overlaydiv) && (parent)) {
      overlaydiv = parent.$("overlayprogress");
    }
    
    if ((contentobject) && (overlaydiv)) {
      var rect = KMMeasurement.getRect(contentobject);
      KMMeasurement.setRect(overlaydiv, rect);       		
    }  
  }
}

// Show overlay with infinite progress indicator
function documentStartLoading(contentframe) 
{        
  documentisloading = true;

  /*
  // Overlay uitgezet op verzoek van VROM
  overlaydiv = $("overlayprogress");
  if ((!overlaydiv) && (parent)) {
    overlaydiv = parent.$("overlayprogress");
  }
  
  if (overlaydiv) {
    documentisloading = true;
    resizeDocumentOverlay();
		new Effect.Appear(overlaydiv, { duration: 0.2, from: 0.0, to: 0.8 });
  } 
  */ 
}

// Hide overlay with infinite progress indicator
function documentStopLoading(locator)
{ 
  documentisloading = false;  
  _documentStopLoadingCallBack();
  /*
  // Overlay uitgezet op verzoek van VROM
  documentisloading = false;
  new Effect.Fade("overlayprogress", { duration: 0.2, afterFinish: _documentStopLoadingCallBack});  
  */
}

// Called after document loading progress is gone and optionally
// scrolls document into view.
function _documentStopLoadingCallBack(obj)
{
 var statelist = KMStateList.getStateList();
 if (statelist) {
   var currentdocumentid = statelist.values().valueOf(BBOnlineSession.CURRENTDOCUMENTID);
   if (currentdocumentid) {
     currentdocument = new BBOnlineDocument(currentdocumentid,
       statelist.values().valueOf(BBOnlineSession.CURRENTDOCUMENTTITLE),
       statelist.values().valueOf(BBOnlineSession.CURRENTDOCUMENTORIGIN));
     if ((currentdocument.locator) && (currentdocument.locator != "")) {
       scrollDocumentIntoView(currentdocument.locator);
     }
   }
 }  
}

function updateDocument(opendocument, contentframe, origin, noprogress) 
{
  var statepage = KMStateList.getStatePage();
  if (statepage) {
    statepage._updateDocument(opendocument, contentframe, origin, noprogress);
  }
}

// Opens given document.
// opendocument can either be an instance of BBOnlinineDocument or string representing a document id. 
// Value for contentframe is optional. Origin is optional and is a string description of the origin of the 
// open request. Set noprogress to true to avoid the infinite progress animation to be shown.
function _updateDocument(opendocument, contentframe, origin, noprogress) 
{
  if (opendocument) {            
    contentframe = getContentFrame(contentframe);  
            
    var reqdocument;
    if (typeof opendocument == "string") {
      reqdocument = new BBOnlineDocument(opendocument, null, origin); 
    } else {
      reqdocument = opendocument;
    }        
    
    // hide possible gw iframe
    hideGW();
    
    // Update origin
    reqdocument.setOrigin(origin);
        
    // Determine whether document is already loaded
    var statelist = KMStateList.getStateList();
    if (statelist) {
      var currentdocumentid = statelist.values().valueOf(BBOnlineSession.CURRENTDOCUMENTID);
      if (currentdocumentid) {
        currentdocument = new BBOnlineDocument(currentdocumentid,
          statelist.values().valueOf(BBOnlineSession.CURRENTDOCUMENTTITLE),
          statelist.values().valueOf(BBOnlineSession.CURRENTDOCUMENTORIGIN));
      
        if (currentdocument.sameDocument(reqdocument.id)) {
          // Document already loaded, just synchronize document and tree
          // Exception for questresults, do not sync instead proceed
          if (origin != BBOnlineOrigins.QUEST) {
            scrollDocumentIntoView(currentdocument.locator);
            synchronizeToc(reqdocument);
            return true;
          }
        }
      }      
      
      // Store new current document in state list
      statelist.values().update(BBOnlineSession.CURRENTDOCUMENTID, reqdocument.fullid);
      statelist.values().update(BBOnlineSession.CURRENTDOCUMENTTITLE, reqdocument.title);
      statelist.values().update(BBOnlineSession.CURRENTDOCUMENTORIGIN, reqdocument.origin);
	  
	  // Google Analytics
	  var gaq = this._gaq == null ? top._gaq : this._gaq;
	  if (gaq != null)
	  {
	    gaq.push(['_trackPageview', "/content?id=" + reqdocument.fullid]); 
	  }
	    
    }                   
        
    // Show progress indicator
    if ((noprogress == null) || (!noprogress)) {
      documentStartLoading(contentframe);
    }   
    
    // Attempt to synchronize toc for new document
    synchronizeToc(reqdocument);     
    
    if (reqdocument.getAssigned()) {
      contentframe.document.location.href = "content.aspx?origin=" + origin + "&documentid=" + reqdocument.fullid;  
    } else {
      // No document location
      if (reqdocument.title != "") {
        displayDocumentText(reqdocument.title, "", "", contentframe);
      } else {
        displayDocumentText("onbekend document!", "", "", contentframe);
      }
    }
  } else {
    return false;
  }
}

// Gets reference to content frame or throws exception. 
// Parameter contentframe is optional.
function getContentFrame(contentframe)
{
  var resultframe;
  if (!contentframe) {
    if (parent) {
      resultframe = parent.frames["iframe_content"];
    } else {
      throw("parent met iframe_content niet beschikbaar.");
    }
  } else {
    resultframe = contentframe;
  }
  
  if (!resultframe) {
    throw("iframe_content niet beschikbaar.");
  } else {
    return resultframe;
  }
}

// Displays given text. contentframe is optional. Optionally supply style
// for an inline style or styleclass.
function displayDocumentText(text, style, styleclass, contentframe)
{
  if (text) {
    contentframe = getContentFrame(contentframe);    
  
    if (encodeURI) {
      text = encodeURI(text);
      if (style) {
        style = encodeURI(style);
      }
      if (styleclass) {
        styleclass = encodeURI(styleclass);
      }
    } else {
      text = escape(text);
      if (style) {
        style = escape(style);
      }
      if (styleclass) {
        styleclass = escape(styleclass);
      }
    }
  
    var url = "content.aspx?text=" + text;
    if ((style) &&  (style != "")) {
      url += "&style=" + style;
    }
    if ((styleclass) && (styleclass != "")) {
      url += "&class=" + styleclass;
    }
        
    contentframe.document.location.href = url;
  }
}

function scrollDocumentIntoView(locator) {
  var contentframe = top.frames["iframe_content"];
  if (contentframe) {
    locatorobj = contentframe.document.getElementById(locator); 
    if (locatorobj) {
      locatorobj.scrollIntoView(true);
    }                       
  }    
}

// Stub for _sycnhronizeToc, makes sure to switch context to default page
function synchronizeToc(bbonlinedocument)
{
  var statepage = KMStateList.getStatePage();
  if (statepage) {
    statepage._synchronizeToc(bbonlinedocument);
  }
}

// Only call this method in the context of the default page
function _synchronizeToc(bbonlinedocument, callBack)
{
  // Attempt to synchronize tree with given document. Given document might represent a string
  // representation of the document or an instance of BBOnlineDocument.
  if ((bbonlinedocument) && (TreeView_content)) {
    // Only perform in case toc is visible    
    if ((!TreeView_content.Visible) && (TreeView_content.Visible == "undefined")) {
      return false;    
    }
    
    var syncdocument;
    if (typeof bbonlinedocument == "string") {
      syncdocument = new BBOnlineDocument(bbonlinedocument, null, null); 
    } else {
      syncdocument = bbonlinedocument;
    } 
    
    if ((syncdocument) && (syncdocument.getAssigned()) &&
        (syncdocument.origin != BBOnlineOrigins.TOC)) {                 
      node = TreeView_content.findNodeById(syncdocument.fullid); 
      if ((!node) && (!callBack)) {
        //node = TreeView_content.findNodeById(syncdocument.id);
        PageMethods.FullQuestId(syncdocument.fullid, onSyncDocumentIdCallBack);
      }
       
      if (node) {           
        var statelist = KMStateList.getStateList();
        if (statelist) {
          statelist.values().update("documentupdating", "true");
        }
        try {
          node.select();
        } finally {
          if (statelist) {
            statelist.values().update("documentupdating", "false");
          }
        }
      }                           
    }
  }
}

// Ajax callback method used from _synchronizeToc
function onSyncDocumentIdCallBack(id)
{
  if (id != null) {
    _synchronizeToc(id, true);
  }
}

if (typeof(Sys) !== "undefined") Sys.Application.notifyScriptLoaded();
