
var selectedMenuItem = null;
var expandMenuActive = false;

function pageLoad(){
    section = document.URL.slice(document.URL.lastIndexOf("#")+1);
    if(document.getElementById(section)){
        expandSection(section);
    }
    
}

function swapImage(imageName,title){
    var photoAnch = document.getElementById('photo');
    photoAnch.href='images/gallery/large/'+ imageName + '.jpg';
    photoAnch.title=title;
    photoAnch.firstChild.src = 'images/gallery/med/'+ imageName + '.jpg';
    var photoTitle = document.getElementById('phototitleright');
    if(photoTitle)
        photoTitle.firstChild.nodeValue = title;
}


function expandMenu(menuID){
    if(expandMenuActive != true){
        expandMenuActive = true;
        var overlayDuration = 0.5;
        var oldMenuItem = selectedMenuItem;
        if(oldMenuItem != null){
            if(oldMenuItem != menuID){
                selectedMenuItem = menuID;
                new Effect.SlideDown(oldMenuItem, {duration: overlayDuration, from: 1.0, to: 0.0,afterFinish: function(){hideSection(oldMenuItem);}});
                new Effect.SlideDown(menuID, {duration: overlayDuration, from: 0.0, to: 1.0,afterFinish: function(){expandMenuActive = false;}});
            }else{
                new Effect.SlideDown(oldMenuItem, {duration: overlayDuration, from: 1.0, to: 0.0,afterFinish: function(){hideSection(oldMenuItem);}});
                selectedMenuItem = null;
                expandMenuActive = false;
            }
        }else{
            selectedMenuItem = menuID;
            new Effect.SlideDown(menuID, {duration: overlayDuration, from: 0.0, to: 1.0,afterFinish: function(){expandMenuActive = false;}});
        }
    }
}

    function appearMenu(menuID){
        if(expandMenuActive != true){
            expandMenuActive = true;
            var overlayDuration = 0.5;
            var oldMenuItem = selectedMenuItem;
            if(oldMenuItem != null){
                if(oldMenuItem != menuID){
                    selectedMenuItem = menuID;
                    new Effect.Appear(oldMenuItem, {duration: overlayDuration, from: 1.0, to: 0.0,afterFinish: function(){appearNewMenu(oldMenuItem, menuID, overlayDuration)}});
                }else{
                    new Effect.Appear(oldMenuItem, {duration: overlayDuration, from: 1.0, to: 0.0,afterFinish: function(){hideSection(oldMenuItem);}});
                    selectedMenuItem = null;
                    expandMenuActive = false;
                }
            }else{
                selectedMenuItem = menuID;
                new Effect.Appear(menuID, {duration: overlayDuration, from: 0.0, to: 1.0,afterFinish: function(){expandMenuActive = false;}});
            }
        }
    }

    function appearNewMenu(oldMenuItem, menuID, overlayDuration){
        hideSection(oldMenuItem);
        new Effect.Appear(menuID, {duration: overlayDuration, from: 0.0, to: 1.0,afterFinish: function(){expandMenuActive = false;}});
    }


function closeSection(sectionID){
    var overlayDuration = 0.4;
    var overlayOpacity = 1.0;
    var topSectionID = 'top_sect_' + sectionID;
    var mainSectionID = 'sect_' + sectionID;
    if(document.getElementById(mainSectionID).style.display != 'none'){
        new Effect.Appear(topSectionID, { duration: overlayDuration, from: 0.0, to: overlayOpacity});
        new Effect.SlideDown(mainSectionID, { duration: overlayDuration, from: 1.0, to: 0.0,afterFinish: function(){hideSection(mainSectionID);}});
    }
}

function expandSection(sectionID){
    var overlayDuration = 0.4;
    var overlayOpacity = 1.0;
    if(document.getElementById(sectionID).className != 'opensection'){
        var topSectionID = 'top_sect_' + sectionID;
        var mainSectionID = 'sect_' + sectionID;
        if(document.getElementById(mainSectionID).style.display == 'none'){
            new Effect.Appear(topSectionID, { duration: overlayDuration, from: overlayOpacity, to: 0.0,afterFinish: function(){hideSectionHeader(topSectionID);}});
            new Effect.SlideDown(mainSectionID, { duration: overlayDuration, from: 0.0, to: 1.0});
        }
    }
}

function hideSectionHeader(sectionname){
    document.getElementById(sectionname).style.display = 'none';
}

function hideSection(sectionname){
    document.getElementById(sectionname).style.display = 'none';
}

function closeAllSections(){
    var projectHome = document.getElementById('projecthome');
    for(i=0;i<projectHome.childNodes.length;i++){
        var child = projectHome.childNodes.item(i);
        if(child.nodeName.toUpperCase() == 'DIV'){
            var divSectClass = child.attributes.getNamedItem('class'); 
            if(divSectClass && divSectClass.nodeValue == 'section'){
                closeSection(child.id);
            }
       }
    }
    document.getElementById('sects_close').style.display = 'none';
    document.getElementById('sects_expand').style.display = 'block';
}

function verifyFormSubmit(formname){
    if (confirm("Do you agree to have Rough Brothers contact you regarding this email.")){
        document.getElementById('mailerinput').value=formname;
        document.getElementById('thisform').submit();
    }else{
        alert("Sorry but we are unable to submit the form right now.");
    };
}

function expandAllSections(){
    var projectHome = document.getElementById('projecthome');
    for(i=0;i<projectHome.childNodes.length;i++){
        var child = projectHome.childNodes.item(i);
        if(child.nodeName.toUpperCase() == 'DIV'){
            var divSectClass = child.attributes.getNamedItem('class'); 
            if(divSectClass && divSectClass.nodeValue == 'section'){
                expandSection(child.id);
            }
       }
    }
    document.getElementById('sects_expand').style.display = 'none';
    document.getElementById('sects_close').style.display = 'block';
}