/**
 * This function resizes an element with given id to span entire client
 * area. 
 *
 * This function does not yet take elements in account which bound width of
 * element to resize (absolute and floated elements).
 *
 * Margins are deprecated and not used anymore. They are calculated instead.
 * 
 * I am not sure if calculation works in all means, but it works for now.
 *
 * TODO: Make more use of jQuery in this function to get rid of browser 
 * junctions.
 *
 * margin_* are DEPRECATED:
 * You may provide margins for width and height which will decrease
 * size of area.
 * 
 * Example:
 * 
 * <code>
 * <body onresize="cbClientArea('map', 8, 8)">
 *
 * <div id="container">
 * <div id="map" style="width: 200px; height: 200px"></div>
 * </div>
 *
 * </body>
 * </code>
 *
 * Discussion: The container element is important for cbClientArea() to be
 * able to calculate the offsetTop and offsetLeft values.
 *
 */

// taken from http://www.mozilla.org/docs/web-developer/sniffer/browser_type.html
//
//
//
function cbMaximizeHeight(id)
{
   // make IE happy
   if (jQuery.browser.msie)
      Text = function(){};

   var element = document.getElementById(id);
   var offsetTop = 0;
   var offsetBottom = 0;
   var ie6Height = 0;

   // first sum all offsetTops of containing elements
   for (var elem = element; elem; elem = elem.offsetParent)
   {
      offsetTop += elem.offsetTop;
      
      var ie6t = parseInt(jQuery(elem).css('margin-top'));
      if (!ie6t) ie6t = 0;
      //console.debug('ie6t ' + ie6t);

      var ie6b = parseInt(jQuery(elem).css('margin-bottom'));
      if (!ie6b) ie6b = 0;
     // console.debug('ie6b ' + ie6b);

      ie6Height += ie6t + ie6b ;
    //  console.debug('ieHeight ' + ie6Height);

      // now calculate height of following elements
      var sibl = jQuery(elem).next()
      sibl.each(function(i, item){
         if (jQuery(item).css('float') == 'none' && 
             jQuery(item).css('display') != 'none')
         {
            offsetBottom += jQuery(item).outerHeight({margin: true}) ;
            ie6Height += jQuery(item).height();
         }
      });
   }
   //console.debug("ie6Height: " + ie6Height + ', offsetTop: ' + offsetTop + ', offsetBottom: ' + offsetBottom);

   var height = jQuery(window).height();
   // this is to make it REAL failsafe
   if (!height) height = 600 ;

   var agt=navigator.userAgent.toLowerCase();
   if (agt.indexOf("msie 6.") != -1)
   {
      height = height - offsetTop - ie6Height;
   }
   else
   {    
      height = height - offsetTop - offsetBottom;
      height -= (jQuery(element).outerHeight({margin: true}) - jQuery(element).height());
   }

   //window.alert("Here 2");

   //console.debug("height: " + height);
   jQuery(element).height(height);
}

function cbClientArea(id, margin_width, margin_height)
{
    if (jQuery.browser.msie)
        Text = function(){};
    
    if (margin_width == null) margin_width = 0 ;
    if (margin_height == null) margin_height = 0 ;

    var mapElem = document.getElementById(id) ;

    var offsetTop    = 0 ;
    var offsetLeft   = 0 ;

    // height of all following elements.
    var offsetBottom = 0 ;

    var offsetWidth = 0 ;

    var minWidth = 0 ;
    var ie6Width = 0 ;
    var ie6Height = 0 ;

    // first sum all offsetTops of containing elements
    for (var elem = mapElem; elem; elem = elem.offsetParent) 
    {
        offsetTop    += elem.offsetTop ;
        //offsetLeft   += elem.offsetLeft ;

        mw = parseInt(jQuery(elem).css('min-width')) ;
        if (mw && mw > minWidth)
            minWidth = mw;

        ie6Width += parseInt(jQuery(elem).css('margin-right')) + parseInt(jQuery(elem).css('margin-left'));
        ie6Height += parseInt(jQuery(elem).css('margin-top')) + parseInt(jQuery(elem).css('margin-bottom'));

        offsetWidth += jQuery(elem).outerWidth({margin: true}) - jQuery(elem).width();

        // now calculate height of following elements
        var sibl = jQuery(elem).next()
        sibl.each(function(i, item){

           if (jQuery(item).css('float') == 'none' && 
               jQuery(item).css('display') != 'none')
           {
              offsetBottom += jQuery(item).outerHeight({margin: true}) ;
              ie6Height -= jQuery(item).height();
           }
        });
    }

    var height = jQuery(window).height();
    var width = jQuery(window).width();

//
    // this is to make it REAL failsafe
    if (!height) height = 600 ;
    if (!width)  width = 800 ;

    var agt=navigator.userAgent.toLowerCase();

    //window.alert("agent: " + agt);
    //window.alert("ie6Width: " + ie6Width + ', ie6Height: ' + ie6Height);

    if (agt.indexOf("msie 6.") != -1)
    {
        height = height - offsetTop - ie6Height;
        width -= ie6Width;
    }
    else
    {    
        height = height - offsetTop - offsetBottom;
        width  = width - offsetWidth ;
    }

    if (minWidth)
       minWidth = minWidth - (jQuery(mapElem).outerWidth({margin: true}) - jQuery(mapElem).width());

    if (width < minWidth)
        width = minWidth;

    //window.alert("Here 2");
    height -= jQuery(mapElem).outerHeight({margin: true}) - jQuery(mapElem).height();

    //window.alert("width: " + width);
    jQuery(mapElem).width(width);
    //window.alert("height: " + height);
    jQuery(mapElem).height(height);
    
    //window.alert("Here 4");

    //mapElem.style.height = height + 'px' ;
    //mapElem.style.width = width + 'px' ;
}
