onload = function(){
    refreshShortcutsHeight();
    showHideMainContent();
    showHideMainContent();

    shortcut_blocks = getElementsByClassName('ocpn_sc_container');
    if (shortcut_blocks.length > 0){
        //shortcut_blocks[0].style.display = 'block';
        shortcut_blocks[0].style.display = 'none';
        if (shortcut_blocks.length > 1){
            for (var i=1; i<shortcut_blocks.length; i++){
                shortcut_blocks[i].style.display = 'none';
            }
        }
    }

    hideSideBlocks();
}

/*
 * Hides side blocks
 */
var sideBlocks = getElementsByClassName('sideblock');

function hideSideBlocks(){
    for (var i=1; i<sideBlocks.length; i++){
        hideSideBlock(i);
    }

    if (showMoreInfo == false){
        hideSideBlock(1);
    }
}

function hideSideBlock(i){
    elementToggleHide(sideBlocks[i], true, function(el) {
        return findParentNode(el, 'DIV', 'sideblock');
    }, 'Show  block', 'Hide  block');
}

/*
 * Navigator tool functions
 */

function showHideMainContent(){
    var collapse = (document.getElementById('ocpn_body').style.display == 'none');

    if (collapse){
        showMainContent(false);
        showShortcuts(false);
        //showMenu(false);
        showStatusBar(false);
        showHideBlock('ocpn_statusbar_collapse');
    }else{
        showMainContent(true);
        showShortcuts(true);
        //showMenu(true);
        showStatusBar(true);
        showHideBlock('ocpn_statusbar_collapse');
    }   
}

function showMenu(collapse){
    showHideBlock('ocpn_menu', collapse);
    if (collapse){
        document.getElementById('ocpn_header').style.height = 'auto';
    }else{
        document.getElementById('ocpn_header').style.height = '52px';
    }
}

function showStatusBar(collapse){
    showHideBlock('ocpn_statusbar', collapse);
}

function showMainContent(collapse){
    showHideBlock('ocpn_body', collapse);
}

function showShortcuts(collapse){
    showHideBlock('ocpn_shortcuts', collapse);
}

function showHideBlock(idname){
    var collapse = (document.getElementById(idname).style.display == 'none');

    if (collapse){
        collapseBlock(idname, false);
    }else{
        collapseBlock(idname, true);
    }

    if ((showMoreInfo) && (idname.search(/ocpn_scid_*/) != -1)){
        moreInfo(idname, collapse);
    }
}

function moreInfo(idname, collapse){
    if (collapse){
        var ajax_response = search_ajax('./ocp_style/javascript/navigator/ajax_pages/getlink.php?name=' + idname);
        if (ajax_response != 'null'){
            document.getElementById('ocpn_searchresult').innerHTML = '<div style="width:470px;margin:4em auto;text-align:center"><img id="indicator" src="/ocp_style/styles/navigator/images/ajax-loader.gif" alt="Loading..." /></div>';
            collapseBlock('ocpn_content', true);
            document.getElementById('ocpn_searchresult').innerHTML = ajax_response;
            collapseBlock('ocpn_searchresult', false);
        }
    }else{
        collapseBlock('ocpn_content', false);
        document.getElementById('ocpn_searchresult').innerHTML = '';
        collapseBlock('ocpn_searchresult', true);
    }
}

function collapseBlock(idname, collapse){
    if (collapse){
        document.getElementById(idname).style.display = 'none';
    }else{
        document.getElementById(idname).style.display = 'block';
    }
}

/*
 *  View at glance options
 */
var collapsed = true;

function viewAtGlance(){
    var elements = getElementsByClassName('ocp_section_container');
    collapsed = !collapsed;
    for (var i=0; i<elements.length; i++){
        collapseBlock(elements[i].getAttribute("id"), collapsed);
    }
}

/*
 *  Search function
 */

function processSearch(file){
   collapseBlock('ocpn_content', true);
   document.getElementById('ocpn_searchresult').innerHTML = '<div style="width:470px;margin:4em auto;text-align:center"><img id="indicator" src="' + file + '/ocp_style/styles/navigator/images/ajax-loader.gif" alt="Loading..." /></div>';
   collapseBlock('ocpn_searchresult', false);
   // collapseBlock('ocpn_shortcuts', true);
   var url = file + '/search/OCP_Search.php?ocpsearch=' + document.getElementById('ocpn_search').value;
   var return_button = '<div style="float:right"><a href="javascript:returnFromSearch()"><img src="' + file + '/pix/famfam/arrow_rotate_clockwise.png" border="0" /></a></div>';
   document.getElementById('ocpn_searchresult').innerHTML = return_button + search_ajax(url);
}

function returnFromSearch(){
    collapseBlock('ocpn_content', false);
   // collapseBlock('ocpn_shortcuts', false);
    collapseBlock('ocpn_searchresult', true);
}

/*
 *  Click to Call functions
 */
function clickToCall(file){
    collapseBlock('ocpn_content', true);
    var url = file + '/lib/javascript/dmenu/dmenu_ajax/click_to_call.php';
    var return_button = '<div style="float:right"><a href="javascript:returnFromSearch()"><img src="' + file + '/pix/famfam/arrow_rotate_clockwise.png" border="0" /></a></div>';
    document.getElementById('ocpn_searchresult').innerHTML = return_button + search_ajax(url);
    collapseBlock('ocpn_searchresult', false);
}

/*
 * Fill floating layer
 */
function fillFloatingLayer(url){
    document.getElementById('float_layer').innerHTML = '<div id="close_floatlayer"><a href="javascript:floatLayerSetVisible(\'float_layer\')">X</a></div>' + search_ajax(url);
}

/*
 *  Refresh shortcuts menu height
 */
function refreshShortcutsHeight(){
    document.getElementById('ocpn_shortcuts').style.height = document.getElementById('ocpn_body').offsetHeight + 'px';
}

/*
 *Shows a tab in main page
 */
function showTab(idname, idcontent, url){
    collapseBlock(idcontent, false);
    collapseBlock(idname, false);
    document.getElementById(idname).innerHTML = search_ajax(url);
}

/*
 * Ajax change content in tool bar
 */

function search_ajax(url){
    if (window.XMLHttpRequest) {
        xmlhttp = new XMLHttpRequest();
    } else {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.open("GET", url, false);
    xmlhttp.send(null);

    return xmlhttp.responseText;
}


/*
 * Navbar buttons opacity
 */
function allSectionsVisible(visible){
    var sections = getElementsByClassName('navbar_section_image');
    for (var i=0; i<sections.length; i++){
        buttonVisible(sections[i], visible);
    }
}

function buttonVisible(element, visible){
    if (visible == true){
        element.style.opacity = '1';
    }else{
        element.style.opacity = '0.5';
    }
}

/*
 * Useful functions
 */
function collapseByClass(classname){
    elements = getElementsByClassName(classname);
    for (var i=0;i<elements.length;i++){
        elements[i].style.display = 'none';
    }
}

//  from http://snipplr.com/view/1696/get-elements-by-class-name/
function getElementsByClassName(classname, node) {
    if(!node) node = document.getElementsByTagName("body")[0];
    var a = [];
    var re = new RegExp('\\b' + classname + '\\b');
    var els = node.getElementsByTagName("*");
    for(var i=0,j=els.length; i<j; i++)
        if(re.test(els[i].className))a.push(els[i]);
    return a;
}

    /*
     * Float layer
     */
    x = '5';
    y = '5';
    function floatLayerSetVisible(obj)
    {
        scroll(0, 0);
        obj = document.getElementById(obj);
        obj.style.visibility = (obj.style.visibility == 'visible') ? 'hidden' : 'visible';
        obj.style.display = (obj.style.display == 'block') ? 'none' : 'block';
    }
    function floatLayerPlaceIt(obj)
    {
        obj = document.getElementById(obj);
        if (document.documentElement)
        {
            theLeft = document.documentElement.scrollLeft;
            theTop = document.documentElement.scrollTop;
        }
        else if (document.body)
        {
            theLeft = document.body.scrollLeft;
            theTop = document.body.scrollTop;
        }
        theLeft += x;
        theTop += y;
//        obj.style.left = theLeft + 'px' ;
//        obj.style.top = theTop + 'px' ;
        obj.style.left = theLeft + '%' ;
        obj.style.top = theTop + '%' ;
        setTimeout("floatLayerPlaceIt('float_layer')",500);
    }
    window.onscroll = setTimeout("floatLayerPlaceIt('float_layer')",500);



function changeClassName(oldname, newname){
    var elements = getElementsByClassName(oldname);
    for (var i=0; i<elements.length; i++){
        elements[i].className=newname;
    }
}

// function for header
function otherPhrase(inc){
    if (inc){
        actuall = actuall + 1;
    }else{
        actuall = actuall - 1;
    }

    if (actuall < 0){
        actuall = phrases.length -1;
    }else if (actuall > phrases.length -1){
        actuall = 0;
    }

    document.getElementById('iswitcher_text').innerHTML = phrases[actuall];
}
