/* ======================================================================== */
/*                                                                          */
/*   File      : dynamiclayout.js                                           */
/*   Purpose   : provide dynamic layout for besserreden.de                  */
/*                                                                          */
/*   (C) July 2009 by Ralf Westram                                          */
/*                                                                          */
/*   Permission to use, copy, modify, distribute and sell this software     */
/*   and its documentation for any purpose is hereby granted without fee,   */
/*   provided that the above copyright notice appear in all copies and      */
/*   that both that copyright notice and this permission notice appear      */
/*   in supporting documentation.                                           */
/*                                                                          */
/*   Ralf Westram makes no representations about the suitability of this    */
/*   software for any purpose.  It is provided "as is" without express or   */
/*   implied warranty.                                                      */
/*                                                                          */
/* ======================================================================== */



function unscrolledDivLayout(id) {
    var div = document.getElementById(id);
    if (div) {
        with (div) {
            style.height = '';
            style.overflow = 'visible';
        }
    }
}
function scrolledDivLayout(id, height) {
    var div = document.getElementById(id);
    if (div) {
        with (div) {
            style.height = height+"px";
            style.overflow = 'auto'; 
        }
    }
}

function unscrolledLayout() {
    unscrolledDivLayout("justbody");
    unscrolledDivLayout("logobody");
    unscrolledDivLayout("numbody");
    unscrolledDivLayout("linebody");
}


function dynamicLayout(){
    var browserHeight = getBrowserHeight();

    var footPos = browserHeight-40;
    if (footPos<710) {
        unscrolledLayout();
    }
    else {
        var footOff = footPos-710;

        scrolledDivLayout("justbody", 563+footOff);
        scrolledDivLayout("logobody", 451+footOff);
        scrolledDivLayout("numbody",  463+footOff);
        scrolledDivLayout("linebody", 538+footOff);
    }
}


function getBrowserWidth(){
    if (window.innerWidth) {
        return window.innerWidth;
    }  
    else if (document.documentElement && document.documentElement.clientWidth != 0) {
        return document.documentElement.clientWidth;
    }
    else if (document.body) {
        return document.body.clientWidth;
    }      
    return 0;
}

function getBrowserHeight(){
    if (window.innerHeight) {
        return window.innerHeight;
    }  
    else if (document.documentElement && document.documentElement.clientHeight != 0) {
        return document.documentElement.clientHeight;
    }
    else if (document.body) {
        return document.body.clientHeight;
    }      
    return 0;
}

//addEvent() by John Resig
function addEvent( obj, type, fn ){ 
   if (obj.addEventListener){ 
      obj.addEventListener( type, fn, false );
   }
   else if (obj.attachEvent){ 
      obj["e"+type+fn] = fn; 
      obj[type+fn] = function(){ obj["e"+type+fn]( window.event ); } 
      obj.attachEvent( "on"+type, obj[type+fn] );
   }
}

function printClicked() {
    unscrolledLayout();
    window.print();
    //    dynamicLayout(); // do not switch back - breaks printing!

    // Return false prevents the browser from following the
    // link and jumping to the top of the page after printing
    return false;
}

function resetZoom() {
    chooseStyle("raspberry", "session");
}
function resetZoomSometimes() {
    var body  = document.getElementsByTagName('body');
    var reset = 1;
    if (body.length>0) {
        var B    = body[0];
        var clss = B.getAttribute('class');
        if (clss) {
            if (clss == 'p_num') reset = 0;
        }
    }
    if (reset == 1) resetZoom();
}

function zoomClicked() {
    chooseStyle("zoomed", "session");
    dynamicLayout();
    updateIcons();
}
function unzoomClicked() {
    resetZoom();
    dynamicLayout();
    updateIcons();
}

function createIcon(gfx, fun) {
    var oLink = document.createElement('a');
    var oImg  = document.createElement('img');

    oImg.src      = gfx;
    oLink.appendChild(oImg);
    oLink.onclick = fun;
    
    return oLink;
}

function updateIcons() {
    if (!document.getElementById || !document.createTextNode) {return;} // Check for DOM support
    if (!window.print) {return;} // Check that the browser supports window.print

    var pdiv = document.getElementById('dynamenu');
    if (pdiv) {
        var content = document.createElement('span');
        content.id  = 'icons';

        var icon;
        var selected = getCookie("mysheet");

        if (selected == "zoomed") icon = createIcon('gfx/unzoom.gif', unzoomClicked);
        else icon                      = createIcon('gfx/zoom.gif', zoomClicked);

        content.appendChild(icon);

        icon = createIcon('gfx/print.gif', printClicked);
        content.appendChild(icon);

        var oldcontent = document.getElementById('icons');
        if (oldcontent) {
            pdiv.replaceChild(content, oldcontent);
        }
        else {
            pdiv.appendChild(content);
        }
    }
}

function initEvents() {
    addEvent(window, 'resize', dynamicLayout);
    resetZoomSometimes();
    dynamicLayout();
    updateIcons();
}

addEvent(window, 'load', initEvents);

// -----------------------
//      links

function isExternalTarget(href) {
    return href.substring(0, 5) == 'http:';
}
function followLink(dest) {
    /* alert('followLink called'); */
    if (isExternalTarget(dest)) { // external link
        var width  = window.width;
        var height = window.height;

        if (width == null) { width = window.outerWidth; }
        if (height == null) { height = window.outerHeight; }
        if (width == null) { width = window.innerWidth; }
        if (height == null) { height = window.innerHeight; }
        if (width == null) { width = 800; }
        if (height == null) { height = 600; }

        /* alert("width=" + width + ",height=" + height); */

        NewWin = window.open(dest, "besserreden_extlinks",
                             "menubar=yes,resizable=yes,location=yes,toolbar=yes,status=yes,width=" + width + ",height=" + height );

        /* NewWin.location.href = dest; */
    }
    else {
        window.location.href = dest;
    }


    return false;
}


