$(document).ready(function(){
    
    var contentState = 1;
    var contentTableWidth = $('.contentTable').css('width');
    centerBoxIntro();
    $('#jumpTo').css('height', $('#jumpTo').outerHeight());
    
    // Checks the value of Cookie  and click showHideContent if 0
    var checkCookie = getCookie('c_content');
    if(checkCookie == 0) {
        showHideContentTable(0, 0); //Hide ContentTable
        contentState = 0; //Reset state to 0
         $('#jumpTo').addClass('contentTableClosed');
        $('#showHideContentTable').addClass('contentTableClosed');
    }
    
    //console.log(contentState);
    //Show Hide Content Table
    $("#showHideContentTable").toggle( 
        function(){
            if(contentState == 1) showHideContentTable(0);
            else showHideContentTable(contentTableWidth);
            //$('#jumpTo').hide("blind", { direction: "horizontal" }, 'fast');
        },
        function(){
            if(contentState == 1) showHideContentTable(contentTableWidth);
            else showHideContentTable(0);
           //$('#jumpTo').show("blind", { direction: "horizontal" }, 'fast');
        }
    );

});



/************************************************
 *                                              *
 *           functions to open and              *
 *            close content list                *
 *                                              *
 ***********************************************/    
//function to close content table
function showHideContentTable(w){
    
    if(w == 0) {
        contentState = 0;
        h = $('.contentTable + div.box').height()*0.5;
    }
    else {
        contentState = 1;
        h = '98%';
    }
    setCookie('c_content',contentState,365);
    
    $('.contentTable').animate({
        width : w,
        height : h
    }, 'fast', function(){
        adjustContentTable(w)
    });

}

function adjustContentTable(w){
      
    if(w == 0) {
        centerBoxIntro(0);
        $('#jumpTo').addClass('contentTableClosed');
        $('#showHideContentTable').addClass('contentTableClosed');
    }
    else {
        centerBoxIntro();
        $('#jumpTo').removeClass('contentTableClosed');
        $('#showHideContentTable').removeClass('contentTableClosed');
    }
}

function centerBoxIntro(m){
    if(m == undefined) m = (Math.abs($('.contentTable').outerHeight()-$('.contentTable + div.box').outerHeight()))*0.5;
    $('.contentTable + div.box').css('margin-top', m);
    return m;
}



/************************************************
 *                                              *
 *             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;
}
