﻿var svpnav = new Object();
    
    svpnav.menuItems = [];
    svpnav.currentTopMenuHtml = '';
    svpnav.currentSubMenuHtml = '';
    
    svpnav.navItem = function(name, link, parentLink) {
        this.name = name;
        this.link = link;
        this.parentLink = parentLink;
    }
    
    svpnav.isCurrentChild = function(link) {
        for(var i=0;i<svpnav.menuItems.length;i++) {
            if(svpnav.menuItems[i].link==svpnav.currentLink && svpnav.menuItems[i].parentLink==link) {
                return true;
                break;
            }
        }
        return false;
    }
    
    svpnav.buildSubStructure = function(strParentLink) {
        var strTempHtml='';
        if(svpnav.menuItems.length>0) {strTempHtml = '<ul>';}
        for(var i=0;i<svpnav.menuItems.length;i++) {
            var currMenuItem = svpnav.menuItems[i];
            if(currMenuItem.parentLink==strParentLink) {
                if(svpnav.currentLink==currMenuItem.link) {
                    strTempHtml+='<li class="active"><a href="'+currMenuItem.link+'">'+currMenuItem.name+'</a></li>';
                }
                else {
                    strTempHtml+='<li><a href="'+currMenuItem.link+'">'+currMenuItem.name+'</a></li>';
                }
                
            }
        }
        
        if(svpnav.menuItems.length>0) {strTempHtml += '</ul>';}
        
        svpnav.currentSubMenuHtml=strTempHtml;
    }
    
    svpnav.buildMenuStructure = function() {
        // build a list with topmenu -items
        svpnav.currentTopMenuHtml='';
        var bSubmenuIsBuilt = false; // this is just to increase the performance
        for(var i=0;i<svpnav.menuItems.length;i++) {
            var currMenuItem = svpnav.menuItems[i];
            if(currMenuItem.parentLink=='') {
                // check if current is selected, and if selected AND has subitems, build a submenu and 
                // this is just the condition for when ... ?
                // also check if this is svpnav.currentLink, or if any children to this is the current link
                
                if(!bSubmenuIsBuilt) {
                    if(currMenuItem.link==svpnav.currentLink) { // the current is a topmenu -item, it may have child-stuff
                        svpnav.currentTopMenuHtml+='<div class="button active"><div class="side1"></div><div class="inside"><a href="'+currMenuItem.link+'">'+currMenuItem.name+'</a></div><div class="side2"></div></div>';
                        svpnav.buildSubStructure(currMenuItem.link);
                        bSubmenuIsBuilt=true;
                    }
                    else if(svpnav.isCurrentChild(currMenuItem.link)) { // the current is child-item, make sure to make that one active
                        //alert('yes, its a childnode');
                        svpnav.currentTopMenuHtml+='<div class="button active"><div class="side1"></div><div class="inside"><a href="'+currMenuItem.link+'">'+currMenuItem.name+'</a></div><div class="side2"></div></div>';
                        svpnav.buildSubStructure(currMenuItem.link);
                        bSubmenuIsBuilt=true;
                    }
                    else { // this is not selected
                        svpnav.currentTopMenuHtml+='<div class="button"><a href="'+currMenuItem.link+'">'+currMenuItem.name+'</a></div>';
                    }
                }
                else { // this is not selected
                    svpnav.currentTopMenuHtml+='<div class="button"><a href="'+currMenuItem.link+'">'+currMenuItem.name+'</a></div>';
                }
            }
        }
    }