﻿
var _blnMainFrameLoaded = false;
var _objTimerID = null;
var _strExpandID = null;
var _objExpandTimerID = null;
var _arrCollapseList = new Array();
var _strSelectedID = '';
var _blnLeftMenuHidden = false;


function initPage()
{
    stretchPage(); 
}

function updateRsLink()
{
   var rsLink = document.getElementById('aReadspeaker')
   if (rsLink)
   {
       document.getElementById('aReadspeaker').href = _strReadspeakerUrl + encodeURIComponent(window.top.Eskion.Main.location.href);
       //document.getElementById('aReadspeaker').href = _strReadspeakerUrl + encodeURIComponent('http://www.terneuzen.nl/rscontent.jsp?objectid=14926');
       document.getElementById('aReadspeaker').style.visibility = 'visible';
   }
}

function stretchPage()
{       
    if (_blnMainFrameLoaded)
    {           
        try
        {
            if (document.getElementById('frmMain').src.toLowerCase().indexOf(_strStartPageID) > -1)
                _blnStartPage = true;
            else
                _blnStartPage = false;
        }
        catch(ex)
        {
            _blnStartPage = false;
        }
        
        document.getElementById('frmMain').height = '0';
        updateHeight();
    }
}

function updateHeight()
{
	var frmMain = document.getElementById('frmMain');
	var objBottomMenu = document.getElementById('divBottomMenu');
	var intMainY = getElementY(frmMain);
	var intMinHeight = 0;
	var intMainHeight = intMinHeight;
	var intHeight = 0;
	var intScrollTop = 0;
	var intBottomHeight = 0;	
	   
    if (objBottomMenu)
    intBottomHeight = objBottomMenu.offsetHeight;
    intMinHeight = document.body.clientHeight - intMainY - intBottomHeight; 
    
    try
    {            
        if (frmMain.contentWindow && frmMain.contentWindow.document && frmMain.contentWindow.document.body)
        {	            
            intScrollTop = frmMain.contentWindow.document.body.scrollTop;
            intMainHeight = frmMain.contentWindow.document.body.scrollHeight;
        }
    }
    catch (ex)
    {
	    intMainHeight = intMinHeight;
    }

    if (intMinHeight > intMainHeight)
        intHeight = intMinHeight;
    else
        intHeight = intMainHeight;    
		
    frmMain.height = intHeight;
	    
	try
	{
		if (frmMain && frmMain.contentWindow && frmMain.contentWindow.document && frmMain.contentWindow.document.body)
		{
		    frmMain.contentWindow.document.body.scrollTop = 0;
	
		    if (intScrollTop > 0)
			    document.body.scrollTop = intScrollTop + intMainY;
	    }
	}
	catch(ex)
	{
	
	}
}

function getElementY(objElement)
{
    var intY = 0;
    var intIEOffsetFix = 0;
    var intScrollTop = document.documentElement.scrollTop || document.body.scrollTop;
    
    if (window.top == window.self && navigator.appName.toLowerCase() == 'microsoft internet explorer' && window.event && document.compatMode == 'BackCompat')
        intIEOffsetFix = 2;
    
    if (typeof objElement.getBoundingClientRect != 'undefined')
    {
        var objBody = document.documentElement || document.body;
        return (objElement.getBoundingClientRect().top - intIEOffsetFix - objBody.clientTop);
    }
    else
    {
        while (objElement.tagName.toLowerCase() != 'body' && objElement.tagName.toLowerCase() != 'html')
        { 
            intY += objElement.offsetTop;
            
            if (objElement.clientTop)
                intY += objElement.clientTop;
            	                
            if (objElement.offsetParent)
                objElement = objElement.offsetParent;
            else
                break;
        } 
        
        if (objElement.tagName.toLowerCase() == 'body' || objElement.tagName.toLowerCase() == 'html')
            intY += objElement.offsetTop;
    
        return intY - intIEOffsetFix - intScrollTop;
    }        
}

function search()
{
	if (document.getElementById('txtSearchBox').value != "")
        window.top.Eskion.Main.location.href = 'search/searchresults.aspx?Q=' + encodeURIComponent(document.getElementById('txtSearchBox').value);
}

function searchBoxFocus(txtSearchBox)
{
	if (txtSearchBox.value == 'doorzoek de site')
		txtSearchBox.value = '';

        txtSearchBox.className += ' Active';
}

function searchBoxBlur(txtSearchBox)
{
	if (txtSearchBox.value == '')
		txtSearchBox.value = 'doorzoek de site';
    
    txtSearchBox.className = txtSearchBox.className.replace(' Active', '');
}

function openLogin() 
{
	var top = (screen.height-240)/2;
	var left = (screen.width-350)/2;
	window.open('login/login.aspx','Login','top='+top+',left='+left+',width=350,height=240,toolbar=no,menubar=no,status=no,resizable=no,scrollbars=no');
}

function printDocument()
{
    window.top.Eskion.Main.focus();
    window.top.Eskion.Main.print();
}

function reLogin()
{
    window.top.location.href = 'login/relogin.aspx';
}

function getURLForHash(strHash, objCallback)
{
    var strURL = '';
    var strTreeCode = '';

    if (strHash.indexOf('menuid:') == 0) 
    {
        strHash = strHash.substr(7);
        Ajax.xmlHttpRequest('GetURLForHash.ashx?MenuItemID=' + strHash, function(objXmlHttp) { getURLForHashCompleted(objXmlHttp, objCallback); });
    }
    else 
    {
        if (_strSelectedID) 
        {
            document.getElementById('a_' + _strSelectedID).className = 'TopMenuItem';
        }

        objCallback('');
    }
}

function getURLForHashCompleted(objXmlHttp, objCallback)
{
    if (document.getElementById('frmMain').style.visibility == 'hidden')
        document.getElementById('frmMain').style.visibility = '';

    if (objXmlHttp.responseXML.getElementsByTagName("contenturl") && objXmlHttp.responseXML.getElementsByTagName("contenturl")[0].firstChild)
    {
        strTreeCode = objXmlHttp.responseXML.getElementsByTagName("treecode")[0].firstChild.nodeValue;
        strURL = objXmlHttp.responseXML.getElementsByTagName("contenturl")[0].firstChild.nodeValue;
        selectItem(strTreeCode);

        objCallback(strURL);
    }
    else
        objCallback('');
}

function selectItem(strID) 
{
    strID = strID.substr(0, 11);
    
    if (_strSelectedID)
    {
        document.getElementById('a_' + _strSelectedID).className = 'TopMenuItem';
    }
    var objElement = document.getElementById('a_' + strID)
    if (objElement)
    {
        document.getElementById('a_' + strID).className = 'TopMenuItem Selected';
    }    
    
    _strSelectedID = strID;
}

function showMenu(strID) 
{
    var divDropDownMask = document.getElementById('divDropDown_' + strID);
    var divMenuContainer = null;
    var aMenuItem = document.getElementById('a_' + strID);
    var intScrollTop = document.body.scrollTop;
    var frmShim = document.getElementById('frmShim');
    
    hideMenuStop();

    if (strID != _strExpandID) 
    {
        hideMenu();

        if (_arrCollapseList[strID]) 
        {
            clearInterval(_arrCollapseList[strID]);
            delete _arrCollapseList[strID];
        }

        if (!divDropDownMask)
            return;

        divMenuContainer = divDropDownMask.firstChild;
        divDropDownMask.style.display = '';
        divDropDownMask.style.width = divMenuContainer.offsetWidth + 'px';
        divDropDownMask.style.height = divMenuContainer.offsetHeight + 'px';
        divDropDownMask.style.left = aMenuItem.getBoundingClientRect().left + 'px';
        divDropDownMask.style.top = aMenuItem.getBoundingClientRect().top + intScrollTop + aMenuItem.offsetHeight + 'px';

        if (navigator.appVersion.indexOf('MSIE') > -1) 
        {
            frmShim.style.width = divDropDownMask.style.width;
            frmShim.style.height = divDropDownMask.style.height;
            frmShim.style.left = divDropDownMask.style.left;
            frmShim.style.top = divDropDownMask.style.top;
            frmShim.style.display = '';
            frmShim.style.filter = 'Alpha(Opacity=0)';
        }
        
        startSlideAnimation(strID);
    }
}

function hideMenuStart() 
{
    _objTimerID = setTimeout(function() { hideMenu(); }, 1000);
}

function hideMenuStop() 
{
    clearTimeout(_objTimerID);
    _objTimerID = null;
}

function hideMenu() 
{
    if (_strExpandID) 
    {
        clearInterval(_objExpandTimerID);
        startSlideAnimation(_strExpandID, true);
    }
}

function startSlideAnimation(strID, blnReverse) 
{
    var divDropDownMask = document.getElementById('divDropDown_' + strID);
    var divMenuContainer = divDropDownMask.firstChild;

    var objAnimation = new ElementAnimation(divMenuContainer);
    objAnimation.fromY = divMenuContainer.offsetHeight * -1;
    objAnimation.toY = 0;
    objAnimation.animateReverse = blnReverse;
    objAnimation.ClientEventHandlers.onAnimationComplete = function() { slideAnimationComplete(strID, blnReverse) };
    objAnimation.accelerationMode = 1;
    objAnimation.duration = 300;

    divMenuContainer.style.overflow = 'visible';

    _strExpandID = null;
    _objExpandTimerID = null;

    if (blnReverse)
        _arrCollapseList[strID] = objAnimation.animate();
    else 
    {
        _strExpandID = strID;
        _objExpandTimerID = objAnimation.animate();
    }
}

function slideAnimationComplete(strID, blnReverse) 
{
    var divDropDownMask = document.getElementById('divDropDown_' + strID);

    if (blnReverse && _arrCollapseList[strID]) 
    {
        divDropDownMask.style.display = 'none';
        delete _arrCollapseList[strID];
        
        if (navigator.appVersion.indexOf('MSIE') > -1) 
        {
            document.getElementById('frmShim').style.display = 'none';
        }
    }
}

function toggleLeftMenu()
{
    var objLeftMenuContainer = document.getElementById('tdLeftMenuContainer');
    var objLeftMenuHideButton = document.getElementById('divLeftMenuHideButton');
    var objLeftMenuHeader = document.getElementById('divLeftMenuHeader');
    objLeftMenuHeader.style.display = 'none'; 
    if (objLeftMenuContainer && objLeftMenuHideButton && objLeftMenuHeader)
    {
        if (_blnLeftMenuHidden)
        {
            objLeftMenuContainer.className = 'LeftMenuContainer';
            objLeftMenuHideButton.className = 'LeftMenuHideButton HideMode';

            objLeftMenuHeader.style.display = 'block';            
            MenuTree_Show('objMenuTree');            
            _blnLeftMenuHidden = false;
        }   
        else
        {
            objLeftMenuContainer.className = 'LeftMenuContainer LeftMenuHidden';
            objLeftMenuHideButton.className = 'LeftMenuHideButton ShowMode';

            objLeftMenuHeader.style.display = 'none';  
                      
            MenuTree_Hide('objMenuTree');
            _blnLeftMenuHidden = true;
        } 
    }
}


this.ElementAnimation = function(objElement) 
{
    var _objInstance = this;
    var _intStartTime = null;
    var _fltQuotient = 0;

    this.timerID = null;
    this.element = objElement;
    this.fromY = null;
    this.toY = null;
    this.duration = 300;
    this.accelerationMode = 0;
    this.animateReverse = false;

    this.ClientEventHandlers = new function() 
    {
        this.onAnimationFrame = null;
        this.onAnimationComplete = null;
    }

    this.animate = function() 
    {
        if (_objInstance.accelerationMode == 0)
            _fltQuotient = 1 / _objInstance.duration;
        else
            _fltQuotient = Math.PI / (2 * _objInstance.duration);

        if (_objInstance.framesPerSecond > 500)
            _objInstance.framesPerSecond = 500;

        _intStartTime = new Date().getTime();
        _objInstance.timerID = setInterval(function() { animate(); }, 10);
        animate();

        return _objInstance.timerID;
    }

    function animate() 
    {
        var intElapsedTime = new Date().getTime() - _intStartTime;
        var intValue = 0;
        var intScrollLeft = document.documentElement.scrollLeft || document.body.scrollLeft;
        var intScrollTop = document.documentElement.scrollTop || document.body.scrollTop;

        if (intElapsedTime >= _objInstance.duration)
            clearInterval(_objInstance.timerID);

        if (_objInstance.fromY != _objInstance.toY) 
        {
            intValue = getTransitionValue(_objInstance.fromY, _objInstance.toY, intElapsedTime);
            _objInstance.element.style.top = intValue + 'px';
        }

        if (intElapsedTime < _objInstance.duration) 
        {
            if (_objInstance.ClientEventHandlers.onAnimationFrame)
                _objInstance.ClientEventHandlers.onAnimationFrame(_objInstance.element);
        }
        else 
        {
            if (_objInstance.ClientEventHandlers.onAnimationComplete)
                _objInstance.ClientEventHandlers.onAnimationComplete(_objInstance.element);
        }
    }

    function getTransitionValue(intFromValue, intToValue, intElapsedTime) 
    {
        var intValue = 0;
        var intFrameValue = 0;
        var fltPercentage = 1;

        if (intElapsedTime < _objInstance.duration) 
        {
            if (_objInstance.accelerationMode == 0)
                fltPercentage = (intElapsedTime * _fltQuotient);
            else if (_objInstance.accelerationMode == 1)
                fltPercentage = Math.abs(Math.sin(intElapsedTime * _fltQuotient));
            else
                fltPercentage = (1 - Math.abs(Math.cos(intElapsedTime * _fltQuotient)));
        }

        if (intToValue > intFromValue) 
        {
            if (intElapsedTime < _objInstance.duration)
                intFrameValue = Math.round(fltPercentage * (intToValue - intFromValue));
            else
                intFrameValue = (intToValue - intFromValue);

            if (_objInstance.animateReverse)
                intValue = intToValue - intFrameValue;
            else
                intValue = intFromValue + intFrameValue;
        }
        else 
        {
            if (intElapsedTime < _objInstance.duration)
                intFrameValue = Math.round(fltPercentage * (intFromValue - intToValue));
            else
                intFrameValue = (intFromValue - intToValue);

            if (_objInstance.animateReverse)
                intValue = intToValue + intFrameValue;
            else
                intValue = intFromValue - intFrameValue;
        }

        return intValue;
    }
}
