﻿//cleanup variable names
//don't reference links starting w/ #

function createRefLinks(src, target, title, tag, tag2, includename)
{
    var src = document.getElementById(src);
    var target = document.getElementById(target);
    
    //src & target exist
    if(src && target)
    {
        var reflinksid = "reflinks_" + src.id;
        var reflinks = document.getElementById(reflinksid);
        
        //links haven't already been generated for this source
        if(!reflinks)
        {
            //find all links in src   
            var links = src.getElementsByTagName("a");
            
            if(links.length >= 1)
            {
                //ordered list of links
                var ol = document.createElement(tag2);
                ol.id = reflinksid;
                ol.className = "printonly";
                
                //cache function pointer for IE performance
                var olappend = ol.appendChild;     
                
                //
                var linkcount = 0;
                for(var index = 0; index < links.length; index++)
                {  
                    var link = links[index];

                    //don't reference blank or javascript links
                    if(link.href && link.href.indexOf('javascript:') == -1)
                    {
                        linkcount++;

                        //create li
                        var li = document.createElement("li");
                        if(!includename)
                            li.appendChild(document.createTextNode(links[index].href));
                        else
                            li.innerHTML = links[index].innerHTML + " - " + links[index].href;
                            //li.appendChild(document.createTextNode(links[index].innerHTML + " - " + links[index].href));
                        ol.appendChild(li);
                        
                        //add super script # on link
                        var sup = document.createElement("sup");
                        sup.className = "printonly";
                        sup.appendChild(document.createTextNode(linkcount));                
                        links[index].appendChild(sup); 
                    }               
                }
                
                if(linkcount > 0)
                {
                    var h2 = document.createElement(tag);           
                    h2.className = "printonly";
                    h2.appendChild(document.createTextNode(title));               
                    target.appendChild(h2);
                    target.appendChild(ol);
                }
                else
                {
                    ol = null;
                }
            }
        }
    }
}

function printPrep()
{   
    createRefLinks("content", "content", "Links", "h2", "ol", false);
    createRefLinks("relatedlinks", "content", "Related Links", "h3", "ul", true);
    
    //get css size from cookie, defaulting to "normal"    
    var cssSize = hmsa.cookies.getCookie("cssSize");
    document.body.className += " " + (cssSize ? cssSize : "normal");
    
    //show buttons which are hidden by default
    var rightControls = document.getElementById("rightcontrols")
    if(rightControls)
        rightControls.style.display = "block";       
  
}

function resizeCSS()
{
    var currentSize = getCurrentSize();
    var newSize;

    //determine new size
    switch(currentSize){
        case "normal":
            newSize = "large";
            break;
        case "large":
            newSize = "xlarge";
            break;
        default:
            newSize = "normal";
            break;
    }

    //store new size in cookie so we can maintain across pages
    hmsa.cookies.setCookie("cssSize", newSize, null, "hmsa.com");
    
    //swap the css class names
    hmsa.css.swapStyles(document.body, currentSize, newSize);
    
    //redraw sifr headers
    hmsa.sifr.redraw();
}

function getCurrentSize()
{
    var className = document.body.className;

    if(className.indexOf("xlarge") != -1)
        return "xlarge";
    else if(className.indexOf("large") != -1)
        return "large";
    else
        return "normal";
}

hmsa.events.bind(null, "init", printPrep);
hmsa.events.bind(null, "init", setupExpandingLists);

/* 
    Expanding Lists
    ***********************************************************************    
    list only shows a few items and contains an anchor link for "more".
    the anchor, when clicked, will show the remaining items
    ***********************************************************************
    -expanding lists contain the class "expandable"
    -list items to be hidden by default contain the class "expand"
    -when "more" anchor is click the class "showall" is added to list
    
    javascript fallback:
    -list contains "showall" class by default and javascript removes it oninit
    -"more" anchor added dyncamically oninit    
*/

function setupExpandingLists(){
    var lists = hmsa.query.getElementsByClass("expandable");

    if(!lists || lists.length <= 0)
        return;

    var anchor = document.createElement("a");
    anchor.onclick = function(){expandList(this);}
    anchor.appendChild(document.createTextNode("More..."));
    anchor.href = "#";

    for(var i = 0; i < lists.length; i++){
        var list = lists[i];
        hmsa.css.removeStyle(list, "showall");
        hmsa.dom.insertAfter(anchor, list);
    }
}

function expandList(obj){
    hmsa.css.addStyle(obj.previousSibling, "showall");
    hmsa.dom.remove(obj);
}