var defaultMenuWidth = 246;         // Default menubreedte
var changeWidthTimer;               // Timer om het menu te verbreden

var menuWidth = 246;                // Hoe breed is het menu nu
var menuWidthTo = 246;              // Hoe breed moet het menu worden

var contentTop = -1000;             // Top van content voor 't onload effect
var contentTopTo = 0;               // Top To van content voor 't onload effect

var contentOnderAanPagina = false;  // Moet de content onderaan de pagina plakken?
var activeMenu = "";

var isSlideWell = false;

function setPositions()
{
    // Breedte van het browservenster
    var clientWidth = document.documentElement.clientWidth;
    // Hoogte van het browservenster
    var clientHeight = document.documentElement.clientHeight;

    var margin = 0.2;
    if (clientWidth <= 1024) margin = 0.15;

    if (isSlideWell) {
        margin = 0;
        //menuWidth = 133;
        //menuWidthTo = 133;
        //defaultMenuWidth = 133;
    }
    
    // Breedte van 'menuHolder'
    var menuWidth = document.getElementById("menuHolder").style.width.replace("px", "");
	if (menuWidth == "") menuWidth = defaultMenuWidth; // Indien leeg, dan beginbreedte
	else menuWidth = menuWidth * 1; // Anders casten voor de zekerheid
		
	// Waar moet 'menuHolder' komen?
	var leftMargin = Math.round(clientWidth * margin);
    
    document.getElementById("menuHolder").style.marginLeft = leftMargin + "px";
    document.getElementById("menuBack").style.marginLeft = leftMargin + "px";
    document.getElementById("contentHolder").style.marginLeft = leftMargin + menuWidth + "px";
    
    if (document.getElementById("flashBack"))
    {
        // Bij een window resize de breedte van flashBack berekenen (nodig voor firefox)
        document.getElementById("flashBack").style.height = Math.min(840, clientHeight) + "px";
    }
    
    if (contentOnderAanPagina == true)
    {
        PageHoogte = 0;
        if (document.getElementById("stream1Tbl")) 
            PageHoogte += document.getElementById("stream1Tbl").clientHeight;
        if (document.getElementById("stream2Tbl")) 
            PageHoogte += document.getElementById("stream2Tbl").clientHeight + 20;
            
        docH = document.documentElement.clientHeight;
        if (docH > 840) docH = 840;
    
        contentTopTo = docH - PageHoogte;
        setTimeout("changeTop()", 1000);
    }
    
    window.onresize = setPositions;
}

function init(scrollBottom, doScroll)
{
    contentOnderAanPagina = scrollBottom;
    
    setPositions();
    
    if (doScroll == false)
    {
        contentTop = contentTopTo;
        setTimeout("changeTop()", 1300);
    } else {
        document.getElementById("contentHolder").style.top = contentTop + "px";
        setTimeout("changeTop()", 2200);
    }
   
}

function changeTop()
{ 
    var obj = document.getElementById("contentHolder");
 
	if ((Math.abs(contentTop - contentTopTo)) >= 2)
	{
		contentTop = contentTop + ((contentTopTo - contentTop) / 4.8);
		obj.style.top = contentTop + "px";
		changeTopTimer = setTimeout("changeTop()", 20);
	}
	else
	{
		obj.style.top = contentTopTo + "px";
		try
		{
	        clearTimeout(changeTopTimer);

        }
        catch(err)
        {
                //Nothing
        }
	}
}

function changeWidth()
{   
    var obj = document.getElementById("menuHolder");
 
	if ((Math.abs(menuWidth - menuWidthTo)) >= 2)
	{
		menuWidth = menuWidth + ((menuWidthTo - menuWidth) / 2.8);
		obj.style.width = menuWidth + "px";
		changeWidthTimer = setTimeout("changeWidth()", 20);
	}
	else
	{
	    if (menuWidthTo < 256)
	        document.getElementById(activeMenu).style.display = "none";  
		obj.style.width = menuWidthTo + "px";
	    clearTimeout(changeWidthTimer);
	}
	
	// Alles weer op de goede plek zetten
	setPositions();
}

function seHideMenu()
{
    try
    {
        for (i=0; i<seSubMenu.length; i++)
        {
           document.getElementById(seSubMenu[i]).style.display = "none"; 
	    }
	}
	
	catch(err)
    {
    }
}

var menuWaitTimer = null;
var menuWait = 1000;
var nextMenu = 10;

function seOpenSubMenu(menuNID)
{
    //seHideMenu();
    nextMenu = menuNID;
    menuWait = 10;
   
    //if (menuWidthTo > 256)
	//{
    //    menuNID = document.getElementById(menuNID);
    //    if (menuNID) menuNID.style.display = "";
    // }
    menuWaitTimer = setTimeout("seDoOpenSubMenu()", 20);
}

function seOnMenu(menuNID)
{
    menuWaitTimer = clearTimeout(menuWaitTimer);
    nextMenu = menuNID;
    activeMenu = nextMenu;
}

function seDoOpenSubMenu()
{
    menuWaitTimer = clearTimeout(menuWaitTimer);
    if (menuWait > 0)
    {
        menuWait--;
        menuWaitTimer = setTimeout("seDoOpenSubMenu()", 20);    
    } else {
        activeMenu = nextMenu;
        seHideMenu();
        if (menuWidthTo > 256)
	    {
            menuObj = document.getElementById(activeMenu);
            if (menuObj) menuObj.style.display = "";
        }
    }
}

function seOpenCloseMenu(menuNID)
{
    seHideMenu();
    document.getElementById(menuNID).style.display = "";  
    activeMenu = menuNID;

    if (menuWidthTo == 590)
    {
        menuWidthTo = 246;
    } else {
        menuWidthTo = 590;
    }
    
    changeWidth();
}

function seCloseMenu()
{
    menuWidthTo = 246;
    changeWidth();
}

function test(w)
{
    menuWidthTo = w;
    changeWidth();
}

/**** Slideshow volledige pagina ****/

var slideshowPos = 0;
var slideshowSpeed = 20;
var slideshowWait = 10000;
var slideshowCount = 0;
var slideshowTimer = null

function initSlideshow(ele) {
	slideshowCount = document.getElementById('slideshowCountField').value;
	slideshowTimer = setTimeout("startSlideshow('"+ele+"')", slideshowWait);
}

function startSlideshow(ele) {
	var obj = document.getElementById(ele);
	if (obj != null) {
		if (slideshowPos < slideshowCount) {
			slideshowPos++;
			startSlide(ele, -slideshowPos * 950, 0, slideshowSpeed);
			slideshowTimer = setTimeout("startSlideshow('"+ele+"')", slideshowWait);
		}
		else
		{
			slideshowPos = 0;
			obj.style.marginLeft = "0px";
			slideshowTimer = setTimeout("startSlideshow('"+ele+"')", 0);
		}
	}
}

// ------------------------------------ SliderClass ----------------------------------

// Globale variabelen
var slideArray = new Array();
var toFast = 950;

// Basis klasse van een slide div
function sliderClass()
{
	this.obj = null;	// ID van element dat slide
	this.fromX = 0;		// Start / huidige pos X
	this.toX = 0;		// Eind positie X
	this.fromY = 0;		// Start / huidige pos X
	this.toY = 0;		// Eind positie Y
	this.speed = 10;	// Snelheid in x stappen bij het einde
	this.timer = null; 
}

// Init slide classe en start de beweging
function startSlide(ele, toX, toY, speed)
{
	// Init nieuwe klasse
	var mySlider	= new sliderClass();
	mySlider.obj	= document.getElementById(ele);
	mySlider.toX	= toX;
	mySlider.toY	= toY;
	mySlider.speed	= speed;
	
	if (mySlider.obj != null) // Bestaat de slide wel
	{   
		// Haal begin pos op
		mySlider.fromX = Math.round((mySlider.obj.style.marginLeft).replace("px", ""));
		if (mySlider.fromX == "") mySlider.fromX = 0; // onbekend dus 0
		
		mySlider.fromY = Math.round((mySlider.obj.style.marginTop).replace("px", ""));
		if (mySlider.fromY == "") mySlider.fromY = 0; // onbekend dus 0
		
		// Als te ver weg dan stukje skippen voor X
		if (Math.abs(mySlider.fromX-mySlider.toX) > toFast) {
			if (mySlider.fromX < mySlider.toX) mySlider.fromX = mySlider.toX - toFast;
			if (mySlider.fromX > mySlider.toX) mySlider.fromX = mySlider.toX + toFast;
		}
		
		// Als te ver weg dan stukje skippen voor Y
		if (Math.abs(mySlider.fromY-mySlider.toY) > toFast) {
			if (mySlider.fromY < mySlider.toY) mySlider.fromY = mySlider.toY - toFast;
			if (mySlider.fromY > mySlider.toY) mySlider.fromY = mySlider.toY + toFast;
		}
		
		// Alleen iets doen als het nog niet gedaan is
		if (Math.round((mySlider.obj.style.marginLeft).replace("px", "")) != mySlider.toX || Math.round((mySlider.obj.style.marginTop).replace("px", "")) != mySlider.toY)
		{
			slideArray[ele] = mySlider; // Opslaan
			doSlide(ele); // Start bewegin
		}
	}
	
	mySlider = null;
}

// Laat de slider een stapje bewegen en zet timeout voor de volgende stap
function doSlide(ele)
{
	var mySlider = slideArray[ele];
	
	if (mySlider != null)
	{   
	    var xDiff = (Math.abs(mySlider.fromX - mySlider.toX));
	    var yDiff = (Math.abs(mySlider.fromY - mySlider.toY));
	    
		if (xDiff > 0.1 || yDiff > 0.1) 
		{
    		mySlider.fromX += (mySlider.toX-mySlider.fromX)/mySlider.speed;
			mySlider.fromY += (mySlider.toY-mySlider.fromY)/mySlider.speed;
			mySlider.obj.style.marginLeft = Math.round(mySlider.fromX) + 'px';
			mySlider.obj.style.marginTop  = Math.round(mySlider.fromY) + 'px';
			slideArray[ele] = mySlider;
			slideArray[ele].timer = setTimeout("doSlide('"+ele+"')", 15);
		}
		else {
			mySlider.timer = clearTimeout(mySlider.timer);
			slideArray[ele] = null;
		}
		mySlider = null;
	}
}

// ------------------------------------ SliderClass ----------------------------------

/**** Slideshow afbeelding element ****/

var slideshow2Pos = 0;
var slideshow2Speed = 20;
var slideshow2Wait = 5500;
var slideshow2Count = 0;
var slideshow2Timer = null

function initSlideshow2(ele, countField) {
	slideshow2Count = document.getElementById(countField).value;
	slideshow2Timer = setTimeout("startSlideshow2('"+ele+"')", slideshow2Wait);
}

function startSlideshow2(ele) {
	var obj = document.getElementById(ele);
	if (obj != null) {
		if (slideshow2Pos < slideshow2Count) {
			slideshow2Pos++;
			startSlide(ele, -slideshow2Pos * 360, 0, slideshow2Speed);
			slideshow2Timer = setTimeout("startSlideshow2('"+ele+"')", slideshow2Wait);
		}
		else
		{
			slideshow2Pos = 0;
			obj.style.marginLeft = "0px";
			slideshow2Timer = setTimeout("startSlideshow2('"+ele+"')", 0);
		}
	}
}

function BeforeClickHandler(node)
{
    if (node.Expanded == false) {
        node.Expand();
    }
    else
    {
        node.Collapse();
    }
}


function BeforeClientClickHandler(node, eventArgs)
{

   //disable doubleclick event    
   node.Toggle();
   
   
}
