// Wanneer er in het menu geklikt wordt, moet er soms nog wat gechecked worden.
// Dit kan door de 'FollowLinkInterrupter' te koppelen aan een functie die true of false teruggeeft
var FollowLinkInterrupter = null;
var LastInterruptedLink = "";
function followLink( link ) {
  LastInterruptedLink = link;

	var doFollow = true;
	if ( FollowLinkInterrupter != null ) {
		doFollow = FollowLinkInterrupter();
	}
	
	if ( doFollow ) {
		document.location = link;
	}
}

function mainOptionUitloggen() {
	document.location = baseURL +'authenticatie/loguit/';
}

function mainOptionBestellen() {
    var mainOption = menuLeft.getActiveOption();

    if ( mainOption != null ) {
        if ( mainOption.name == 'Bestellen' ) {
            if ( mainOption.expanded ) {
                mainOption.collapse();
            } else {
                document.location = baseURL +'bestellen/';
            }
        } else {
            document.location = baseURL +'bestellen/';
        }
    } else {
	    document.location = baseURL +'bestellen/';
	}
}

function BestellenCollapseExpand( expand ) {
	if ( document.getElementsByName( 'aboveBodyDiv' )[0] != null ) {
	    if( expand ) {
	        document.getElementsByName( 'aboveBodyDiv' )[0].className = 'txtLeft';
	        //document.getElementById( "ReferentieDiv" ).style.visibility = "visible";
	        document.getElementById( "ReferentieDiv" ).className = 'txtLeft';
	    } else {
	        document.getElementsByName( 'aboveBodyDiv' )[0].className = 'notVisible';
	        //document.getElementById( "ReferentieDiv" ).style.visibility = "hidden";
	        document.getElementById( "ReferentieDiv" ).className = 'notVisible';
	    }
	}
}

function MenuLeft() {
	// public functions
	this.addMainoption  = addMainoption;
	this.draw           = draw;
	this.clickMainOption = clickMainOption;
	this.collapseMainOptions = collapseMainOptions;
	this.getActiveOption = getActiveOption;
	
	// public vars
	this.menuMainOptions = [];
	this.drawTable;
	this.activeOption;
	this.activeSuboption;

    function getActiveOption() {
        for( var i = 0; i < this.menuMainOptions.length; i++ ) {
            if ( this.menuMainOptions[ i ].active ) {
                return this.menuMainOptions[ i ];
            }
        }
        return null;
    }
	
	function addMainoption( id, name, customaction, nofollowlink, onCollapseExpand ) {
        newMainOption = new MenuMainOption( this, this.menuMainOptions.length, id, name, customaction, ( nofollowlink == "1" ), onCollapseExpand );
		this.menuMainOptions[ this.menuMainOptions.length ] = newMainOption;
		
		return newMainOption;
	}
	
	function draw( aDrawTable ) {
	    this.drawTable = aDrawTable;

	    for ( var i = 0; i < this.menuMainOptions.length; i++ ) {
	        // elke mainoption krijgt z'n eigen table
	        var tableMain = document.createElement('table');
	        tableMain.className = "mainoptionTable";
	        tableMain.cellPadding = 0;
	        tableMain.cellSpacing = 0;
	        tableMain.appendChild( document.createElement('tbody') );
	        	        	        
	        // table even toevoegen
	        this.drawTable.appendChild( tableMain );
	        //fiks om ruimte even groot te maken als op www.iperen.com
	        var ruimte = this.drawTable.appendChild( document.createElement('tr') );
	        ruimte.className = 'ruimteCelMenu';   
	         
	        // de mainoption vertellen welke table hij moet gaan gebruiken
	        this.menuMainOptions[ i ].setTable( tableMain );
	        
	        // tekenen maar
    	    if ( this.menuMainOptions[ i ].active ) {
    	        this.menuMainOptions[ i ].expand();
    	    } else {
    	        this.menuMainOptions[ i ].collapse();
    	    }
	    }
	}
	
	function collapseMainOptions( index ) {
		for ( var i = 0; i < this.menuMainOptions.length; i++ ) {
			if ( index != i ) {
			    if ( this.menuMainOptions[ i ].getInitialized() ) {
		            this.menuMainOptions[ i ].collapse();
	            }
	        }
        }
    }
	
	function clickMainOption( mainoptionIndex ) {
	    if ( this.menuMainOptions[ mainoptionIndex ].expanded ) {
	        this.menuMainOptions[ mainoptionIndex ].collapse();
	    } else {
	        this.menuMainOptions[ mainoptionIndex ].expand();
	    }
	}
}

function MenuMainOption( owner, index, id, name, customaction, nofollowlink, onCollapseExpand ) {
	// public functions
	this.addSuboption = addSuboption;
	this.setActive    = setActive;
	this.setTable     = setTable;
	this.getInitialized = getInitialized;
	this.expand       = expand;
	this.collapse     = collapse;
	this.customClick  = customClick;
	this.draw         = draw;
	
	// public vars
	this.owner = owner;
	this.index = index;
	this.id = id;
	this.name = name;
	this.customaction = customaction;
	this.nofollowlink = nofollowlink;
	this.onCollapseExpandHdl = onCollapseExpand;
	
	this.active = false;
	this.expanded = false;
	
	this.menuSubOptions = [];
	
	// private vars
	var self = this;
	var initialized = false;
	var mainoptionTable;
	var mainoptionCell;
	var rowName, cellName;
	
	function addSuboption( modulename, name, href ) {
        newSubOption = new MenuSubOption( this, this.menuSubOptions.length, modulename, name, href );
        this.menuSubOptions[ this.menuSubOptions.length ] = newSubOption;
        
        return newSubOption;
	}
	
	function setActive( isActive ) {
	    this.active = isActive;
	}
	
	function setTable( tbl ) {
	    mainoptionTable = tbl;
	}
	
	function expand() {
	    owner.collapseMainOptions( self.index );
	    	    
	    self.expanded = true;
	    self.draw();
	    
	    if ( self.onCollapseExpandHdl != "" ) {
	        eval( self.onCollapseExpandHdl +"( true );" );
	    }
	}
		
	function collapse() {
        self.expanded = false;
	    self.draw();
	    
	    if ( self.onCollapseExpandHdl != "" ) {
	        eval( self.onCollapseExpandHdl +"( false );" );
	    }
	}
	
	function draw() {
		if ( !initialized ) {
	        // de cell waar de naam in moet komen
	        //mainoptionTable.border = 1; //Handig voor bouwen
	        rowName = mainoptionTable.tBodies[0].insertRow( 0 );
		    rowName.className = "mainoptionNameRow";
		    cellName = rowName.insertCell( 0 );
		    cellName.colSpan = 2;
		    //cellName.style.width = '1000px';
		    //cellName.className = "mainoptionNameCell"; 
		    if ( self.active ) {
			    cellName.innerHTML =  "<table border='0' cellpadding='0' cellspacing='0'>"+
			                          "<tr>"+ 
			                          "  <td><img src='/images/empty.gif' style='width:10px;'/></td>"+
			                          "  <td class='mainoptionNameCell2' style='color: #FA9A05;'>"+ self.name +"</td>"+
									  "</tr>"+
			                          "</table>";		    			    
		    } else {
			    cellName.innerHTML =  "<table border='0' cellpadding='0' cellspacing='0'>"+
			                          "<tr>"+ 
			                          "  <td><img src='/images/empty.gif' style='width:10px;'/></td>"+
			                          "  <td class='mainoptionNameCell2'>"+ self.name +"</td>"+
									  "</tr>"+
			                          "</table>";		    			    	
		    }
		}
				
		if ( self.expanded ) {  
		    rowName.onclick = collapse;
		    
		    cellName.style.borderBottom = "";
		    	    
		    // cellen aanmaken om het op een uitgeklapt ding te laten lijken
		    var rowSpace = mainoptionTable.tBodies[0].insertRow( 1 );
		    rowSpace.className = "mainoptionSpaceRow";
		    var cellSpaceLeft = rowSpace.insertCell( 0 );
		    cellSpaceLeft.className = "mainoptionSpaceLeftCell";
		    cellSpaceLeft.innerHTML = '<img style="width:12px;height:1px;" src="/images/empty.gif">';
		    var cellSpaceRight = rowSpace.insertCell( 1 );
		    cellSpaceRight.className = "mainoptionSpaceRightCell";
		    cellSpaceRight.innerHTML = '<img style="width:130px;height:1px;" src="/images/empty.gif">';
		    var rowSpace2 = mainoptionTable.tBodies[0].insertRow( 2 );
		    var cellSpaceLeft2 = rowSpace2.insertCell( 0 );
		    cellSpaceLeft2.innerHTML = '<img style="width:12px;height:1px;" src="/images/empty.gif">';
		    var cellSpaceRight2 = rowSpace2.insertCell( 1 );
		    		    
		    // ook subopties tekenen
		    for ( var i = 0; i < this.menuSubOptions.length; i++ ) {
			    // elke suboptie z'n eigen table
		        var tableSub = document.createElement('table');
		        tableSub.className = "suboptionTable";
		        tableSub.cellPadding = 0;
		        tableSub.cellSpacing = 0;
		        tableSub.border = 0;
		        
		        if ( i == 0 ) {
		            tableSub.style.borderTop = "#c7d4c7 1px solid";
		        }
		        
		        if ( i == ( this.menuSubOptions.length -1 ) ) {
		            tableSub.style.borderBottom = "#c7d4c7 1px solid";
		        }
		        
		        tableSub.appendChild( document.createElement('tbody') );
		        
		        // table even toevoegen
		        cellSpaceRight2.appendChild( tableSub );
		        
		        // de mainoption vertellen welke table hij moet gaan gebruiken
		        this.menuSubOptions[ i ].setTable( tableSub );
			    
			    this.menuSubOptions[ i ].draw();
	        }
	    } else {
	        initialized = false;
	    
	        rowName.onclick = expand;
	        
	        //cellName.style.borderBottom = "#c7d4c7 1px solid"; lijn onder main optie
	        
	        // subopties weghalen
		    for ( var i = 0; i < this.menuSubOptions.length; i++ ) {
		        this.menuSubOptions[ i ].hide();
	        }
	        
	    	if ( mainoptionTable.tBodies[0].rows.length > 1 ) {
	            mainoptionTable.tBodies[0].deleteRow( 1 );
	            mainoptionTable.tBodies[0].deleteRow( 1 );
	        }
	    }
	    
	    if ( self.customaction != "" ) {
	        rowName.onclick = customClick;
	    }  
	    
	    initialized = true;
	}
	
	function customClick() {
	    if ( self.nofollowlink ) {
            document.location = self.customaction;
        } else {
 	        document.location = "javascript:followLink( '"+ self.customaction +"' );";
        }
	}
	
	function getInitialized() {
	    return initialized;
	}
}

function MenuSubOption( owner, index, modulename, name, href ) {
	// public functions
	this.addOption = addOption;
	this.hasTable  = hasTable;
	this.setTable  = setTable;
	this.setActive = setActive;
	this.customClick = customClick;
	this.draw      = draw;
	this.hide      = hide;
	
	// public vars
	this.owner = owner;
	this.index = index;
	this.modulename = modulename;
	this.name = name;
	this.href = href;
	
	this.menuOptions = [];
	
	// private vars
	var self = this;
	var active = false;
	
	var suboptionTable = null;
	
	function addOption( name, href, actionname, desc, extra, show, nofollowlink ) {
	    newOption = new MenuOption( name, href, actionname, desc, extra, ( show == "1" ), ( nofollowlink == "1" ) );
        this.menuOptions[ this.menuOptions.length ] = newOption;
        
        return newOption;
	}
	
	function hasTable() {
	    return ( suboptionTable != null );
	}
	
	function setTable( tbl ) {
	    suboptionTable = tbl;
	}
	
	function setActive( act ) {
		active = act;
	}
	
	function draw() {
	    // verloopje tonen
	    var rowSub = suboptionTable.tBodies[0].insertRow( 0 );
	    rowSub.className = "suboptionRow";
	    //var cellVerloop = rowSub.insertCell( 0 );
		//cellVerloop.innerHTML = '<img src="/images/verloop.gif"/>';
		
		rowSub = suboptionTable.tBodies[0].insertRow( 1 );
		rowSub.className = "suboptionRow";
		rowSub.onclick = customClick;
		
	    var cellName = rowSub.insertCell( 0 );
	    cellName.className = "suboptionCell";
		cellName.innerHTML = self.name;
		
		if ( active ) {
		    cellName.style.fontWeight = "bold";
		    cellName.style.color = "#FA9A05";
		    if ( this.menuOptions.length > 0 ) {
		        cellName.style.padding = "3px 3px 1px 3px";
		    }
		}
		
		// opties tekenen
		for ( var i = 0; i < this.menuOptions.length; i++ ) {
		    this.menuOptions[ i ].setTable( suboptionTable );
		    this.menuOptions[ i ].draw();
		}
	}
	
	function hide() {
	    try {
	        suboptionTable.tBodies[0].deleteRow( 1 );
	        suboptionTable.tBodies[0].deleteRow( 0 );
	    } catch( error ) { }
	}
	
	function customClick() {
        document.location = "javascript:followLink( '"+ self.href +"' );";
	}
}

function MenuOption( name, href, actionname, desc, extra, show, nofollowlink ) {
    // public functions
    this.setTable    = setTable;
    this.customClick = customClick;
    this.setActive = setActive;
    this.draw = draw;
    
    // public vars
    this.name         = name;
    this.href         = href;
    this.actionname   = actionname;
    this.desc         = desc;
    this.extra        = extra;
    this.show         = show;
    this.nofollowlink = nofollowlink;

    
    // private vars
    var self = this;
    var active = false;
    
    var optionTable = null;
    
    function setTable( tbl ) {
	    optionTable = tbl;
	}
	
	function setActive( a ) {
		active = a;
	}
	
    function draw() {
        if ( self.show ) {
            var rowSub = optionTable.tBodies[0].insertRow( optionTable.tBodies[0].rows.length );
	        rowSub.className = "optionRow";
	        var cellName = rowSub.insertCell( 0 );
    	    cellName.className = "optionCell";
    	    
    	    if ( self.name != "" ) {
    	        if( active ){
	    	        cellName.innerHTML = '-&nbsp;<font style="text-decoration:underline; color: #FA9A05;">'+ self.name +'</font>';
	    	    } else {
    	            cellName.innerHTML = '-&nbsp;<font style="text-decoration:none;">'+ self.name +'</font>';
	    	    }
	    	    cellName.onclick = customClick;	    	    
	    	} else {
	    		if ( self.extra != "" ) {
	    		    cellName.innerHTML = self.extra;
	    		} else {
	    		    cellName.innerHTML = '<img src="/images/empty.gif" border="0" style="height:4px"/>';
	    		}
	    	}
	    }
	}

	function customClick() {
		if ( self.nofollowlink ) {
	        document.location = self.href;
	    } else {
	        document.location = "javascript:followLink( '"+ self.href +"' );";
	    }
	}
}

function openOogstopbrengstRegistreren(){
    showPopWin( "/akker/bwc_bewerkingsdatum/?hoofdbewerkingssoort=3", 600, 400, null  );
}
