// ================================================================================
//
// Cookiehandling
//
// hilfreiche Links:
// - http://techpatterns.com/downloads/javascript_cookies.php
//
// ----------------------------------------------------------------------
//
// ================================================================================

function Set_Cookie( name, value, expires, path, domain, secure )
{
  // set time, it's in milliseconds
  var today = new Date();
  today.setTime( today.getTime() );

  /*
  if the expires variable is set, make the correct
  expires time, the current script below will set
  it for x number of days, to make it for hours,
  delete * 24, for minutes, delete * 60 * 24
  */
  if ( expires )
  {
    expires = expires * 1000 * 60 * 60 * 24;
  }
  var expires_date = new Date( today.getTime() + (expires) );

  document.cookie = name + "=" +escape( value ) +
    ( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
    ( ( path ) ? ";path=" + path : "" ) +
    ( ( domain ) ? ";domain=" + domain : "" ) +
    ( ( secure ) ? ";secure" : "" );
}

// this fixes an issue with the old method, ambiguous values
// with this test document.cookie.indexOf( name + "=" );
function Get_Cookie( check_name ) {
        // first we'll split this cookie up into name/value pairs
        // note: document.cookie only returns name=value, not the other components
        var a_all_cookies = document.cookie.split( ';' );
        var a_temp_cookie = '';
        var cookie_name = '';
        var cookie_value = '';
        var b_cookie_found = false; // set boolean t/f default f

        for ( i = 0; i < a_all_cookies.length; i++ )
        {
                // now we'll split apart each name=value pair
                a_temp_cookie = a_all_cookies[i].split( '=' );
                // and trim left/right whitespace while we're at it
                cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

                // if the extracted name matches passed check_name
                if ( cookie_name == check_name )
                {
                        b_cookie_found = true;
                        cookie_value=false;
                        if (a_temp_cookie[1]>'')
                           cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
                        return cookie_value;
                        break;
                }
                a_temp_cookie = null;
                cookie_name = '';
        }
        if ( !b_cookie_found )
        {
                return null;
        }
}

// this deletes the cookie when called
function Delete_Cookie( name, path, domain ) {
  if ( Get_Cookie( name ) ) document.cookie = name + "=" +
    ( ( path ) ? ";path=" + path : "") +
    ( ( domain ) ? ";domain=" + domain : "" ) +
     ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}


// ================================================================================
/*  Autor, Entwicklung 8/2002 Kristof Lipfert Duesseldorf    */
 /*  Version 2005-12-09                                  */
function detect() {
  var x;
  if(document.ids)x='nc4';
  else if( document.all && !document.getElementById )x='ie4';
  else if( window.opera && !document.createElement )x='op5';
  else if( window.opera && window.getComputedStyle )  {
            if(document.createRange)x='op8';
              else if(window.navigate)x='op7.5';
                               else x='op7.2';                   }
  else if( window.opera && document.compatMode )x='op7';
  else if( window.opera && document.releaseEvents )x='op6';
  else if( document.contains && !window.opera )x='kq3';
  else if(window.pkcs11&&window.XML)x='f15';
  else if( window.getSelection && window.atob )x='nn7';
  else if( window.getSelection && !document.compatMode )x='nn6';
  else if( window.clipboardData && document.compatMode )
    x=window.XMLHttpRequest? 'IE7' : 'IE6';
  else if( window.clipboardData ){x='ie5';
       if( !document.createDocumentFragment ) x+='.5';
       if( document.doctype && !window.print ) x+='m';}
  else if( document.getElementById && !document.all ) x='op4';
  else if( document.images && !document.all ) x='nn3';
  else if(document.clientWidth&&!window.RegExp)x='kq2';
  else x='???';
  return x;
}

var sCookiename = "design";

window.design=Get_Cookie(sCookiename);
// var bCookieW=Get_Cookie("screenw");
// var bCookieH=Get_Cookie("screenh");

// alert(window.design);


// aktuelle Bildschirmaufloesung setzen
// document.cookie = "screenw=" + escape(screen.width) +";path=/";
// document.cookie = "screenh=" + escape(screen.height) +";path=/";

/*
 * CMS-915Hintergrundbild auf grossen Bildschirmen - das brauchts nicht mehr
 * 
// wenn Cookie fuer Bildschirmaufloesung nicht exisitierte und
// neues Cookie gesetzt werde konnte, dann Seite neu laden
// (CSS wird serverseitig generiert)
if ((!bCookieH || !bCookieW) && Get_Cookie("screenw") && Get_Cookie("screenh")) {
  document.location.reload();
}
*/


var bReload=0;
Delete_Cookie("unicolor", "/");

window.setDesign = function(sDesign, keepIt) {
  var previousDesign = window.design;
  if(previousDesign==sDesign) return;
  
  sBgImage='';
  if (sDesign.indexOf("unicolor")==0) window.setUnicolor(1);
  else {
    window.setUnicolor(0);
    // CMS-915Hintergrundbild auf grossen Bildschirmen
    // die in PHP definierten Auflösungen gibts neu auch in der JS-Welt - s. inc_designs.php
    sBgImage='/static/design/' + sDesign + '/'+getBgImage();
  }

  window.design = sDesign;
  
  // Cookie speichern
  var expires = "";
  if (keepIt)
  {
  var cookieDate = new Date();
  cookieDate.setTime(cookieDate.getTime()+(10000*24*60*60*1000));
  var expires = "; expires="+cookieDate.toGMTString();
  }
  document.cookie = sCookiename + "=" + escape(sDesign) +expires+"; path=/;";
      
  if (bReload) document.location.reload();
  else {
    var oBody = document.getElementById("body");
    if (oBody) {
      oBody.style.backgroundImage ="url(" + sBgImage + ")";

      // und dann noch das preview-bildli einfetten
      var oLi = document.getElementById('design_sel_'+previousDesign);
      if(oLi) oLi.className = "";
      oLi = document.getElementById('design_sel_'+sDesign);
      if(oLi) oLi.className = "active";
    } else document.location.reload();
  }
  
}

window.setUnicolor = function(iUnicolor) {
  // alert("setUnicolor = " + iUnicolor);
  Set_Cookie("unicolor", iUnicolor, 10000, "/");
  document.cookie = "unicolor=" + escape(iUnicolor) +";path=/;";  
}

/**
 * ermittelt Name der Bilddatei fuer Hintergrund.
 * vordefinierte Aufloesungen sind in aSizeBgImage
 * @return string Dateiname, z.B. 1600x1200.jpg
 */
function getBgImage(){
  for (s in aSizeBgImage) {
      if (screen.width<aSizeBgImage[s]["w"] && screen.height<aSizeBgImage[s]["h"]){
          return aSizeBgImage[s]["w"]+'x'+aSizeBgImage[s]["h"]+'.jpg';
      }
  }
  // hierher sollte er nicht mehr kommen ... Fallback ist '1600x1200.jpg'
  return '1600x1200.jpg';
}

function showDesign(li) {
  document.getElementById('search').style.visibility='hidden';
  document.getElementById('header').style.zIndex = 101;
}
function hideDesign(li) {
  document.getElementById('search').style.visibility='visible';
  document.getElementById('header').style.zIndex = 10;
}


window.setFont = function( size ) {
  var oFontSize = document.getElementById("fontsizes");
  if(!oFontSize) return;
  size = +size;
  switch(size) { case 10: case 11: case 12: break; default: size=11; break; }

  // Groesse anpassen
  var sBaseURL = oFontSize.href.substring(0, oFontSize.href.indexOf("base"));
  oFontSize.href = sBaseURL + "base" + size + ".css";
  if(detect()=="IE7" && navigator.userAgent.indexOf("MSIE 6.0")>0) {
    o10 = document.getElementById("base_10");
    o11 = document.getElementById("base_11");
    o12 = document.getElementById("base_12");
    var sFilterOff = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='/static/global/base_40.png',sizingMethod='scale');";
    var sFilterOn = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='/static/global/base_100.png',sizingMethod='scale');";
    if(o10) { o10.style.filter = (size==10) ? sFilterOn : sFilterOff; }
    if(o11) { o11.style.filter = (size==11) ? sFilterOn : sFilterOff; }
    if(o12) { o12.style.filter = (size==12) ? sFilterOn : sFilterOff; }
  }

  var prev = Get_Cookie("fontsize");
  if(prev==null || prev==size) return;

  // und im Cookie merken...
  Set_Cookie("fontsize", size, 10000, "/");

  // Teaser wieder anpassen, falls die Groesse geaendert hat...
  if(window.fixHeight) { document.location.reload( false ); }

  return false;
}

window.setDesign( window.design );
window.setFont( Get_Cookie("fontsize") );

function openLink(url) {
  if(!url) return false;
  if(url.indexOf("javascript:")>=0) {
     url=url.replace("javascript:", "");
     url=url.replace("return ", "");
     eval(url);
     return false;
  }
  if(url.indexOf("http")>=0) {
    window.open(url);
    return false;
  }
  document.location.href = url;
  return false;
}

function goHome() {
  var oHome = document.getElementById("home");
  if(!oHome) return;
  document.location.href = oHome.firstChild;
}

function fixHR() {
  var aoHr = document.getElementsByTagName("hr");
  var i=aoHr.length;
  while(i-->0) {
    var oHr = aoHr[i];
    if(oHr.className=="light") {
      oHr.outerHTML = "<div class='hr_light'></div>";
    } else {
      oHr.outerHTML = "<div class='hr'></div>";
    }
  }
}

function fixBox() {
  var aoBox = document.getElementsByTagName("div");
  var i=aoBox.length;
  while(i-->0) {
    var oBox = aoBox[i];
    if(oBox.className!="box") continue;
    oBox.style.background = "";
    oBox.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='/static/global/frequency_single_column.png',sizingMethod='scale')";
    oBox.style.height = "1px";
    oBox.innerHTML = "<div class='ie6_relative'>" + oBox.innerHTML + "</div>";
  }
}

