﻿//changes the details displayed on menu hover
function MenuChange(elem, detailsId)
{
    if(elem && elem.id != '' && elem.className.indexOf('selected') == -1)
    {
        var parent = elem.parentNode;
        if(parent)
        {
            var menuElements = parent.getElementsByTagName(elem.tagName);
            if(menuElements!=null && menuElements.length > 0)
            {
                for(var i=0; i<menuElements.length;i++)
                {
                    if(menuElements[i].id == '' || menuElements[i].className.indexOf('selected') < 0)
                    {
                        continue;
                    }
                    menuElements[i].className = menuElements[i].className.replace('selected','');
                }
            }
            elem.className = elem.className.replace('selected','')+' selected';
        }
        var menuDetails = document.getElementById(detailsId);
        //debugger
        if(menuDetails)
        {
            var allDetails = menuDetails.getElementsByTagName('div');
            if(allDetails!= null && allDetails.length > 0)
            {
                for(var i = 0; i<allDetails.length; i++)
                {
                    if(allDetails[i].id == '')
                    {
                        continue;
                    }
                    
                    if(allDetails[i].id.replace('main-','') != elem.id.replace('menu-',''))
                    {
                        allDetails[i].className = allDetails[i].className.replace('hide','')+' hide';
                    }
                    else
                    {
                        allDetails[i].className = allDetails[i].className.replace('hide','show');
                        allDetails[i].style.zIndex = 1;
                    }
                }
            }
        }
    }
}
//changes the details when using the portfolio navigation buttons
function ChangeSite(direction, detailsId, menuId)
{
    var menuDetails = document.getElementById(detailsId);
    //get the selected index
    if(menuDetails)
    {
        var allDetails = menuDetails.getElementsByTagName('div');
        if(allDetails!= null && allDetails.length > 0)
        {
            var prevIndex = -1;
            var nextIndex = -1;
            var selectedIndex = -1;
            var lastIndex = -1;
            var firstIndex = -1;
            for(var i = 0; i<allDetails.length; i++)
            {
                if((i+1) == allDetails.length && prevIndex == -1)
                {
                    prevIndex = lastIndex;
                }
                if(allDetails[i].id == '')
                {
                    continue;
                }
                if(firstIndex == -1)
                {
                    firstIndex = i;
                }
                if(allDetails[i].className.indexOf('hide') == -1)
                {
                    allDetails[i].className = allDetails[i].className+' hide';
                    selectedIndex = i;
                    if(lastIndex > -1)
                    {
                        prevIndex = lastIndex;
                    }
                }
                lastIndex = i;
                if(nextIndex == -1 && selectedIndex!=-1 && selectedIndex < lastIndex)
                {
                    nextIndex = lastIndex;
                }
            }
            if(nextIndex == -1 && firstIndex > -1)
            {
                nextIndex = firstIndex;
            }
            if(direction == -1)
            {
                //move backward
                allDetails[prevIndex].className = allDetails[prevIndex].className.replace('hide','show');
                SetSelectedMenuItem(allDetails[prevIndex].id, menuId);
            }
            else
            {
                //move forward
                allDetails[nextIndex].className = allDetails[nextIndex].className.replace('hide','show');
                SetSelectedMenuItem(allDetails[nextIndex].id, menuId);
            }
        }
    }
}
function SetSelectedMenuItem(currentDetailsId, menuContainerId)
{
    var menuId = currentDetailsId.replace('main','menu');
    var menuHolder = document.getElementById(menuContainerId);
    if(menuHolder)
    {
        var menuItems = menuHolder.getElementsByTagName('div');
        if(menuItems!=null && menuItems.length > 0)
        {
            for(var i=0; i< menuItems.length; i++)
            {
                if(menuItems[i].id == '')
                {
                    continue;
                }
                if(menuItems[i].id == menuId)
                {
                    menuItems[i].className = menuItems[i].className.replace('selected','')+' selected';
                }
                else
                {
                    menuItems[i].className = menuItems[i].className.replace('selected','');
                }
            }
        }
    }
}
function CheckURLModulePart(currentURL)
{
    if(currentURL != '')
    {
        var index = currentURL.indexOf('#');
        if(index > 0)
        {
            var moduleName = currentURL.substring(index+1);
            if(moduleName != '')
            {
                var selectedMenu = 'menu-'+moduleName.toLowerCase();
                MenuChange(document.getElementById(selectedMenu), 'menuDetails');
            }
        }
    }
}
function SetHoverClass(elem, hoverClass)
{
    if(elem)
    {
        if(elem.className.indexOf('selected') <= 0)
        {
            elem.className = elem.className.replace(hoverClass,'')+' '+hoverClass;
        }
    }
}
function RemoveHoverClass(elem, hoverClass)
{
    if(elem)
    {
        elem.className = elem.className.replace(hoverClass,'');
    }
}