var showAllDebugFieldsets;






function adjustFieldsetStyles() {
    var allFieldsets = Ext.query( '.debugFieldset' );

    for( j=0; j<allFieldsets.length;j++) {

        currentSegment = allFieldsets[j];

        children = Ext.get(currentSegment).dom.childNodes;

        for(var i=0;i<children.length;i++) {
            if( Ext.get(children[i]) ) {

                if( Ext.get(children[i]).dom.tagName == 'LEGEND' && Ext.get(children[i+1]) ) {

                    fieldsetStyle = Ext.get(currentSegment).dom.style;

                    for(jj=i+1;jj<children.length;jj++) {
                         if( Ext.get(children[jj]) && Ext.get(children[jj]).dom.tagName=='DIV' ) {
                            relevantItem  = Ext.get(children[jj]);
                         }
                    }

                    //fieldsetStyle.height        = relevantItem.getComputedHeight() + 50 + 'px';
                    if(typeof(relevantItem)!='undefined') {
                        fieldsetStyle.cssFloat      = relevantItem.getStyle('float');
                        fieldsetStyle.styleFloat    = relevantItem.getStyle('float');
                        fieldsetStyle.overflow      = relevantItem.getStyle('overflow');
                    }

                    width = Ext.get(currentSegment).getComputedWidth();
                    span = children[i].childNodes[0];
                    if( span ) {
                        //span.style.width = width - 10 + 'px';
                    }
                }
            }
        }
    }
}


function removeHeights() {
    var allFieldsets = Ext.query( '.debugFieldset' );
    for( j=0; j<allFieldsets.length;j++) {
        currentSegment = Ext.get(allFieldsets[j]);
        if( currentSegment.getStyle('height') ) {
            currentSegment.setStyle( 'height','' );
        }
    }
}


var debugInsertAjaxDiv =''
someFn = function(s1,s2) {
    document.getElementById(debugInsertAjaxDiv).innerHTML = s1.responseText;
}


function debugGetRequest(queryStringx,debugInsertAjaxDivTemp,params) {
    debugInsertAjaxDiv = debugInsertAjaxDivTemp;
    Ext.Ajax.request({
        url: '/artikel/zeigen/'+queryStringx,
        success: someFn,
        params: {'debugMain[explainQuery][query]': params},
        failure: function() { alert('urg')},
        method:'POST'
    });
}





function findPosX(obj) {
    var curleft = 0;
    if(obj.offsetParent)
        while(1)
        {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
}

function findPosY(obj) {
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
}

function findPos(obj) {
   return {left:findPosX(obj),top:findPosY(obj)};
}

var cacheTimesShown = false;

function showCacheTimes() {

    allFieldsets = Ext.query('.debugFieldset .recursionLevel_3');

    for(var i=0; i<allFieldsets.length;i++) {
        var overlay = document.createElement("DIV");
        overlay.style.position = 'absolute';
        overlay.style.left = findPosX(allFieldsets[i]) + 'px';
        overlay.style.top = findPosY(allFieldsets[i]) + 'px';
        overlay.style.width = Ext.get(allFieldsets[i]).getComputedWidth() +'px';
        overlay.style.height = Ext.get(allFieldsets[i]).getComputedHeight() +'px';
        overlay.style.zIndex = 7000;
        overlay.style.background    = 'green';
        overlay.style.minHeight = '80px';
        overlay.style.opacity   = 0.5;
        overlay.id              = 'cacheOverlay'+i;
        overlay.className       = 'cacheOverlay';

        var segmentname = allFieldsets[i].id.replace('debugSegment_','');

        lifetime = cacheObjectsJsonArray[segmentname].lifetime;

        if(lifetime<=60) {
            realLifetime = lifetime + ' secs';
        }

        if(lifetime>60 && lifetime <= 3600) {
            realLifetime = lifetime/60;
            realLifetime = realLifetime.toFixed(1) + ' mins';
        }

        if(lifetime>=3600 && lifetime < 86400) {
            realLifetime = lifetime/3600;
            realLifetime = realLifetime.toFixed(1) + ' hrs';
        }

        if(lifetime >= 86400) {
            realLifetime = lifetime/86400;
            realLifetime = realLifetime.toFixed(1) + ' days';
        }

        overlay.innerHTML = '<span style="font:50px verdana;font-weight:bold;opacity:1">' + realLifetime + '</span>';
        document.body.appendChild(overlay);
    }
    cacheTimesShown = true;
}

function hideCacheTimes() {
    cacheOverlay = Ext.query('.cacheOverlay');
    for(var i=0; i<cacheOverlay.length;i++) {
         Ext.get( cacheOverlay[i] ).remove();
    }
    cacheTimesShown = false;
}

function toggleCacheTimes() {
    if(cacheTimesShown) {
        hideCacheTimes();
    } else {
        showCacheTimes();
    }
}


function hideDebugFieldsets() {
    array = Ext.query('.debugFieldset');
    for(i=0;i<array.length;i++) {
        if( array[i].className.indexOf('tidy_error') == -1 ) {
            Ext.get(array[i]).addClass('unobtrusive');
        } else {
            Ext.get(array[i]).addClass('obtrusive');
        }
    }
    removeHeights();
    bgTools.setCookie( 'BG_DEBUG_MODE', 'design', 10, '/');

    Ext.get('site').setWidth(990);
    Ext.get('site_info_bar').setWidth(990);

}

function showDebugFieldsets() {
    array = Ext.query('.debugFieldset');
    for(i=0;i<array.length;i++) {
        Ext.get(array[i]).removeClass('unobtrusive');
    }
    adjustFieldsetStyles();
    bgTools.setCookie( 'BG_DEBUG_MODE', 'coder', 10, '/');

    Ext.get('site').setWidth(Ext.get('site').getWidth()-8);
    Ext.get('site_info_bar').setWidth(Ext.get('site_info_bar').getWidth()-8);
    Ext.get('maincontent').setWidth(Ext.get('maincontent').getWidth()-8);

}

if( typeof(json) != 'undefined' ) {
    setTimeout( 'fixDebugBug()',2000);
}

fixDebugBug = function() {
    Ext.get('site').setWidth(Ext.get('site').getWidth()-8);
    Ext.get('site_info_bar').setWidth(Ext.get('site_info_bar').getWidth()-8);
    Ext.get('site_bar').setWidth(Ext.get('site_bar').getWidth()-8);
    Ext.get('maincontent').setWidth(Ext.get('maincontent').getWidth()-8);
    Ext.get('content').setWidth(Ext.get('maincontent').getWidth()-8);
}


function toggleDebugFieldsets() {
     if( showAllDebugFieldsets ) {
          hideDebugFieldsets();
          Ext.get('debugFieldsetToggle').dom.innerHTML = 'DESIGN MODE';
     } else {
          showDebugFieldsets();
          Ext.get('debugFieldsetToggle').dom.innerHTML = 'DEBUG MODE';
     }
     showAllDebugFieldsets = !showAllDebugFieldsets;
}

function checkElementExists(checkMe) {
  var oElement, oId;
  if (document.getElementById
      && (oElement = document.getElementById(checkMe))
      && (oId = oElement.id)) {
      return true;
  } else {
    return false;
  }
}


function adjustAds() {
    if( document.getElementById('adDeployReal_sky') ) {
      document.getElementById('adDeployReal_sky').style.display = "block";
      document.getElementById('adDeployReal_sky').style.zIndex  = 999;
    }
    /* Sets background color of body    */
    if( typeof(ads_background_color) == 'string' ) {
      if( ads_background_color.charAt(0) != '#' ) {
        ads_background_color = '#' + ads_background_color;
      }
      document.getElementsByTagName('body')[0].background = '';                            
      document.getElementsByTagName('body')[0].style.backgroundColor = ads_background_color;

    }

    /* Sets background image of body    */
    if( typeof(ads_background_image) == 'string' ) {
      document.getElementsByTagName('body')[0].background = '';
      document.getElementsByTagName('body')[0].style.backgroundImage = 'url(' + ads_background_image + ')';
    }

    if( typeof(ads_background_image_norepeat) == 'string' ) {
      document.getElementsByTagName('body')[0].style.backgroundRepeat = ads_background_image_norepeat;
    }




    /* BIGSIZE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  */
    if( checkElementExists('adDeployReal_bigsize') ) {

      /* Sets Background to BIGSIZE   */
      if( typeof(ads_trackinglink_bigsize) == 'string' ) {
        document.getElementById('adDeployReal_bigsize').onclick = function(){ window.open(ads_trackinglink_bigsize) };
      }

      /* Sets Alignment to BIGSIZE    */
      if( typeof(ads_align_bigsize) == 'string' ) {
        document.getElementById('adDeployReal_bigsize').style.textAlign = ads_align_bigsize;
      }

    }

    /* SKYTAG ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  */
    if( checkElementExists('adDeployReal_sky') ) {

      /* Top margin to SKY    */
      if( typeof(ads_offset_top_sky) == 'string' ) {
        var set_absolute_sky = true;
        document.getElementById('adDeployPH_sky').style.top = ads_offset_top_sky;
      }
      
      jQuery(document).ready( function() { 
          if( jQuery.browser.msie && parseInt(jQuery.browser.version) < 8) {
              jQuery('#adDeployPH_sky').css('top','4px'); } 
          }
      );

      /* Left margin to SKY    */

      if( typeof(ads_offset_left_sky) == 'string' ) {
        //////////document.getElementById('adDeployPH_sky').style.marginLeft  = ads_offset_left_sky;
        //////////document.getElementById('adDeployPH_sky').style.left        = '0px';
      }

      if( set_absolute_sky ) {
        document.getElementById('adDeployPH_sky').style.zIndex = 999;
        document.getElementById('adDeployPH_sky').style.position = 'absolute';
      }


      if( typeof(ads_background_image_position) == 'string' ) {

          splitted = ads_background_image_position.split(' ');
          offset = parseInt(splitted[0].replace('px','')) + parseInt(document.getElementById('adDeployReal_sky').style.left.replace('px','')) - 3;
          document.getElementsByTagName('body')[0].style.backgroundPosition = offset + 'px 0px';
      }

      if( typeof(Prototype) == 'object' ) {

        var divs_a    = $$("#skytag div a");

        if( divs_a.length > 0) {
          //divs_a[0].style.display = 'none';
        }


      }

   }
}

var resizeSkyWorkerInterval;

var cnt = 0;

function resizeSky() {


    /* Resizes the banner DIV on the right side */
    if( window.innerHeight && window.scrollMaxY )  {
      pageWidth   = window.innerWidth;
      pageHeight  = jQuery('#site').height();
    }  else if( document.body.scrollHeight > document.body.offsetHeight ) {
      pageWidth   = document.body.offsetWidth;
      pageHeight  = jQuery('#site').height();
    } else {
      pageWidth   = document.body.offsetWidth;
      pageHeight  = jQuery('#site').height();
    }

    if( document.documentElement ) {
        pageHeight  = jQuery('#site').height();
    }

    var set_absolute_sky = false;
    restWidth = pageWidth - 850;

    resizeSkyWorker(restWidth,pageHeight-50);

    //resizeSkyWorkerInterval = setInterval( 'resizeSkyWorker(' + restWidth + ',' + pageHeight + ');', 200 );
}

function resizeSkyWorker( restWidth, pageHeight ) {

    if( document.getElementById('adDeployReal_background') ) {

        if( window.innerHeight )  {
            pageWidth   = window.innerWidth;
            pageHeight  = jQuery('#site').height();
        }   else {
            pageWidth   = document.body.offsetWidth;
            pageHeight  = jQuery('#site').height();
        }

        pageHeight  = jQuery('#site').height();

        bback = document.getElementById('banner_background_adition');

        document.getElementById('adDeployReal_background').style.zIndex = -2;
        document.getElementById('adDeployReal_background').style.position = 'absolute';
        document.getElementById('adDeployReal_background').style.left = '0';
        document.getElementById('adDeployReal_background').style.top = '0';

        if( document.getElementById('banner_background_adition') ) {
            document.getElementById('banner_background_adition').style.width          = pageWidth + 'px';
            document.getElementById('banner_background_adition').style.height         = pageHeight + 'px';

            if( typeof(ads_background_color) == 'string' ) {
                document.getElementById('banner_background_adition').style.backgroundColor = ads_background_color;
            } else {
                document.getElementById('banner_background_adition').style.backgroundColor = 'transparent';
            }
            document.getElementById('banner_background_adition').style.zIndex = -1;
        }


    }

    if ( typeof(showMouseMove) != 'undefined' ) {

        if (window.addEventListener) {
            window.addEventListener('DOMMouseScroll', wheel, false);
        }

        window.onmousewheel = document.onmousewheel = wheel;

        document.onmousemove = MouseMoved;
    }

    clearInterval(resizeSkyWorkerInterval);
}

// Mail Encryption

function CryptMailto()
{
    var n = 0;
    var r = "";
    var s = "mailto:"+document.forms[0].emailField.value;
    var e = document.forms[0].emailField.value;

    e = e.replace( /@/, " [at] ");
    e = e.replace( /\./g, " [dot] ");

    for( var i=0; i < s.length; i++ )
    {
        n = s.charCodeAt( i );
        if( n >= 8364 )
        {
            n = 128;
        }
        r += String.fromCharCode(n+1);
    }
    document.forms[0].cyptedEmailField.value = r;
    document.forms[0].HTMLCyptedEmailField.value = "<a href=\"javascript:linkTo_UnCryptMailto('"+ r +"');\">"+ e +"</a>";
}

function UnCryptMailto( s )
{
    var n = 0;
    var r = "";
    for( var i = 0; i < s.length; i++)
    {
        n = s.charCodeAt( i );
        if( n >= 8364 )
        {
            n = 128;
        }
        r += String.fromCharCode( n - 1 );
    }
    return r;
}

function linkTo_UnCryptMailto( s )
{
    location.href=UnCryptMailto( s );
}


// ---- neu

function getOffset(strID){
  var objStart = document.getElementById(strID);
  // Startwert
  var intOffsetLeft = objStart.offsetLeft;
  // Offset-Objekt
  var objOffset = {
    left: objStart.offsetLeft,
    top: objStart.offsetTop
  }

  // Elternelement mit Offset ermitteln
  var objHelpOffset = objStart.offsetParent;

  // Solange OffsetElemente existieren
  while(objHelpOffset){
    // Offsets addieren
    objOffset.left += objHelpOffset.offsetLeft;
    objOffset.top += objHelpOffset.offsetTop;
    // Nächstes OffsetElement
    objHelpOffset = objHelpOffset.offsetParent;
  }

  return objOffset;  // Offset-Objekt zurückgeben; Zugriff über .left und .top
}
//if( typeof(window.onload)=='undefined' ) {
    window.onload= function () {
        var siteBeginLeft = getOffset( 'site' ).left;
        var siteEndLeft   = 790 + siteBeginLeft;

        var windowHeight = jQuery('#site').height();
        var windowWidth =  windowWidth = document.documentElement.scrollWidth || document.body.scrollWidth - 19 ;

        var helpLeft = document.createElement("div");
        helpLeft.style.backgroundColor="transparent";
        helpLeft.style.height=windowHeight+"px";
        helpLeft.style.zIndex= "998";
        helpLeft.style.width= siteBeginLeft+"px";
        helpLeft.style.position="absolute";
        helpLeft.style.top="0";

        var helpRight = document.createElement("div");
        helpRight.style.backgroundColor="transparent";
        helpRight.style.height=windowHeight+"px";
        helpRight.style.zIndex="998";
        helpRight.style.width=  (windowWidth - siteEndLeft) + "px";
        helpRight.style.position="absolute";
        helpRight.style.top="0";
        helpRight.style.left= siteEndLeft+"px";

        if( document.getElementById('banner_background_adition') ) {
            helpRight.onclick = document.getElementById('banner_background_adition').onclick;
            helpLeft.onclick = document.getElementById('banner_background_adition').onclick;
        }


        var bgo = document.getElementById('bgo');
        if (typeof bgo === 'object') {
            bgo.appendChild( helpLeft );
            bgo.appendChild( helpRight);
        }
    };
//}
