/************************************************
 *                                              *
 *           functions to open and              *
 *            close content list                *
 *                                              *
 ***********************************************/    
    
//function to close content table
function closeContent(){
    $("#jumpTo").hide();
    $("#introRight").css({width: '608px', marginLeft: '6px'});
    $("#showHideClose").addClass('hidden');
    $("#showHideOpen").removeClass('hidden');
    contentState = 0;
    setCookie('c_content',contentState,365);
}
//function to open centent table
function openContent(){
    $("#jumpTo").show();
    $("#introRight").css({width: '324px', marginLeft: '290px'});
    $("#showHideOpen").addClass('hidden');
    $("#showHideClose").removeClass('hidden');
    contentState = 1;
    setCookie('c_content',contentState,365);
}
  
    
/************************************************
 *                                              *
 *             Cookie functions                 *
 *                                              *
 ***********************************************/
    
//set cookie
function setCookie(name,value,date){
    //in new date object
    var exdate = new Date();
    //set exdate by getting todays date adding date from the functions 3rd argument
    exdate.setDate(exdate.getDate()+date);
    //writes cookie
    document.cookie = name+ "=" +escape(value)+((date==null) ? "" : ";expires="+exdate.toUTCString());
}
    
//check if cookie exists and return the value
function getCookie(name){
    //checks if any cookies
    if (document.cookie.length>0){
        var cStart = document.cookie.indexOf(name + "=");
        if(cStart !=-1){
            cStart = cStart + name.length+1;
            var cEnd = document.cookie.indexOf(";", cStart);
            if (cEnd==-1){
                cEnd = document.cookie.length;
            }
            return unescape(document.cookie.substring(cStart,cEnd));
        }
    }
return null;
}


    
$(document).ready(function(){
    
    var contentState = 1;

/************************************************
 *                                              *
 *          Building the content list           *
 *                                              *
 ***********************************************/

    //store all the html from first box in a variable
    var intro = $("div.box:first-child").html();
    
    //rebuild the first box with with a div for the intro
    $("div.box:first-child").replaceWith('<div class="box bFull posRelative"><div id="introRight">'+intro+'</div></div>');
    //build a div for the content list
    $("div.box:first-child").prepend('<div id="showHideOpen" class="hidden" ></div><div id="jumpTo" class="left"><h2>Contents</h2><ul></ul><div id="showHideClose"></div></div>');
    //foreach h2 outside fisrt box add an attribute and put h2 text in a anchor and place in jumpTo ul
    $(".box:nth-child(2) h2").each(function(){
        var contList = $(this).text();
        $(this).attr('name', contList);
        $("#jumpTo ul").append('<li><a href="#'+contList+'">'+contList+'</a></li>');
    });
    //place a back to top button before each h2, then remove first back to top
    $(".box:nth-child(2) h2").before('<p class="back_top"><a href="#top" class="tinyBt">back to top</a></p>');
    $(".back_top:first").remove();
    
    //set the height of #jumpTo if smaller than #intoRight
    var leftDiv = $("#jumpTo").height();
    var rightDiv = $("#introRight").height();
	    
    if(rightDiv > leftDiv){
	var height = rightDiv - 3;
	$("#jumpTo").css({'height':+height+'px'});
    }


/************************************************
 *                                              *
 *          Calling open and close              *
 *       functions and animating scroll         *
 *                                              *
 ***********************************************/
    
    //close
    $("#showHideClose").click(function(){
        closeContent();
    });
    //open
    $("#showHideOpen").click(function(){
        openContent();
    });
    
    //animating the scroll
    $("#jumpTo a").click(function(event){
        event.preventDefault();
        var link = $(this).attr('href');
        var target = link.replace(/^.*#/, '');
        var targetOffset = $("h2[name="+target+"]").offset().top;
        $("html, body").animate({scrollTop:  targetOffset+"px"}, 'slow');
    });
    

/************************************************
 *                                              *
 *         Checks the value of Cookie           *
 *        and calls close function if 0         *
 *                                              *
 ***********************************************/
    var checkCookie = getCookie('c_content');
    if(checkCookie == 0){
        closeContent();
    }
     
});