// JavaScript Document

/* anzeigen */
function OAS_RICH() {}

function displayTime(elementId){
    now=new Date();
    output=now.toString();
    field=document.getElementById(elementId);
    if(field === null) { return; }

    h = now.getHours();
    m = now.getMinutes();
    if (m<10){m="0"+m;}
    s = now.getSeconds();
    if (s<10){s="0"+s;}
    D = now.getDate();
    M = now.getMonth()+1;
    Y = now.getFullYear();
    output=D+"."+M+"."+Y+" "+h+":"+m+":"+s+" Uhr ";

    field.innerHTML=output;
    setTimeout("displayTime('"+elementId+"')",1000);
}

//Reiter
function showReiter(num){
  if (num==1) {
    document.getElementById('leser1').style.display="block";
    document.getElementById('leser2').style.display="none";
    document.getElementById('reiterHead1').style.backgroundColor='#ffffff';
    document.getElementById('reiterHead1').style.borderBottomColor='#ffffff';
    document.getElementById('reiterHead2').style.backgroundColor='#d9d4cb';
    document.getElementById('reiterHead2').style.borderBottomColor='#CAC6BF';
    return;
  }
  if (num==2){
    document.getElementById('leser2').style.display="block";
    document.getElementById('leser1').style.display="none";
    document.getElementById('reiterHead2').style.backgroundColor='#ffffff';
    document.getElementById('reiterHead2').style.borderBottomColor='#ffffff';
    document.getElementById('reiterHead1').style.backgroundColor='#d9d4cb';
    document.getElementById('reiterHead1').style.borderBottomColor='#CAC6BF';
    document.getElementById('reiterHead1').style.borderRightWidth='1px';
    return;
  }
}

function ftvlaunch(video,quality) {
  var LeftPosition = (screen.width) ? (screen.width-665)/2 : 0;
  var TopPosition = (screen.height) ? (screen.height-430)/2 : 0;
  var popwin = window.open('http://www.zoomin.tv/videoplayer/index.cfm?id='+video+'&mode=normal&quality='+ quality +'&pid=rponline','MTV6_window','toolbar=no,width=665,height=430,top='+TopPosition+',left='+LeftPosition+',directories=no,status=no,statusbar=0,scrollbars=no,resizable=no,menubar=no');
  if (popwin) { popwin.focus(); }
}

function Go(x,t) {
  if(x == "none") { return;
  } else {
    if (typeof(t) == 'undefined') {
      location.href = x;
    } else {
      newwin = window.open(x,t);
      newwin.focus();
    }
  }
}

function owin(datei,name,width,height) {
  var sw = screen.width;
  var sh = screen.height;
  if (width>sw-50) { width = sw-50; }
  if (height>sh-100) { height = sh-100; }
  var w = window.open(datei,name,'width='+width+',height='+height+',scrollbars=yes,directories=no,'+
                      'resizable=yes,toolbar=no,location=no,status=no,menubar=no');
  if(w) { w.focus(); }
  return(w);
}

function owinfull(datei,name,width,height) {
  var sw = screen.width;
  var sh = screen.height;
  if (width>sw-50) { width = sw-50; }
  if (height>sh-100) { height = sh-100; }
  var w = window.open(datei,name,'width='+width+',height='+height+',scrollbars=yes,directories=yes,'+
                      'resizable=yes,toolbar=yes,location=yes,status=yes,menubar=yes');
  if(w) { w.focus(); }
  return(w);
}

function doopener(path)
{
  if (top.window.opener && !top.window.opener.closed) {
    top.window.opener.location=path;
    top.window.close();
    // top.window.opener.focus();
    return false;
  }
  return true;
}

function doremote(path)
{
  if (top.window.opener && !top.window.opener.closed) {
    top.window.opener.location=path;
    return false;
  }
  return true;

}
var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + //all caps
             "abcdefghijklmnopqrstuvwxyz" + //all lowercase
             "0123456789+/=";               //all numbers plus +

function decode64(inp) {
  inp=inp.replace(/<.*?>/g,"");
  inp=inp.replace(/\n|\r| |\t/g,""); inp=inp.replace(/=/g,"");
  var out = ""; //This is the output
  var chr1, chr2, chr3 = ""; //These are the 3 decoded bytes
  var enc1, enc2, enc3, enc4 = ""; //These are the 4 bytes to be decoded
  var i = 0; //Position counter

  inp = inp.replace(/[^A-Za-z0-9\+\/\=]/g, "");

  do { //Heres the decode loop.

    //Grab 4 bytes of encoded content.
    enc1 = keyStr.indexOf(inp.charAt(i++));
    enc2 = keyStr.indexOf(inp.charAt(i++));
    enc3 = keyStr.indexOf(inp.charAt(i++));
    enc4 = keyStr.indexOf(inp.charAt(i++));

    chr1 = (enc1 << 2) | (enc2 >> 4);
    chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
    chr3 = ((enc3 & 3) << 6) | enc4;

    //Start to output decoded content
    out = out + String.fromCharCode(chr1);

    if (enc3 != 64) {
      out = out + String.fromCharCode(chr2);
    }
    if (enc4 != 64) {
      out = out + String.fromCharCode(chr3);
    }

    //now clean out the variables used
    chr1 = chr2 = chr3 = "";
    enc1 = enc2 = enc3 = enc4 = "";

  } while (i < inp.length); //finish off the loop

  return out;
}

function whichBrowser() {
  var browser = navigator.appName;
  var version = navigator.appVersion.substring(0, 1);
  var agent = navigator.userAgent;
  if ((browser == "Microsoft Internet Explorer") && (version >= 4)) { return 'ie'; }
  // Firefox und Netscape
  if ((browser == "Netscape") && (version >= 4 && version < 5)) { return 'ns'; }
  // nur Netscape
  if ((browser == "Netscape") && (agent.search(/Netscape/)) > 0) { return 'ns7'; }
  return '';
}

function setHomePage(siteURL) {
  var browser = whichBrowser();
  if (browser == 'ie') {
    document.body.style.behavior = "url(#default#homepage)";
    document.body.setHomePage(siteURL);
  } else {
    document.location.href = '/info/startseite.php';
  }
}

function addToFavorites(siteURL, description) {
  var browser = whichBrowser();
  if (browser == 'ie') {
    window.external.AddFavorite(siteURL, description);
  } else if (browser == 'ns') {
    alert("Drücken Sie Strg+D zum Bookmarken dieser Seite!");
  } else {
    alert("Die Seite kann nicht automatisch den Favoriten hinzugefügt werden!");
  }
}

function getPx(str) {
  // strips the "px" from style values
  if (str.indexOf("px") != -1) {
    return Number(str.split("px")[0]);
  } else {
    return 0;
  }
}

/// Eilmeldungscode ///
var eilmeldungFlag = false;
var gScrollerEilmeldg = null;
var _yui_init = false;
var eiltext = '';
var eilcount = 0;

function eilmeldungStatus() {
  if(document.getElementById('mainarea') === null) { return; }
  try {
    var eilmeldungscheck = new Ajax.Request(
      '/alive/eilmeldung.html?id='+((new Date()).getTime()),
      { method: "get", onSuccess: showResult });
  } catch (e) {
    //alert(e.description);
  }
  emStatus2Timer = window.setTimeout('eilmeldungStatus()', 10000);
}

window.setTimeout('eilmeldungStatus()', 30000);

function showResult(aj_result) {

  var result = eval('(' + aj_result.responseText + ')');
  var toparea = null;
  var mainarea = null;
  var top = 0;

  if(result.meldung != '') {
    // Eilmeldungstext vorhanden ...

    // wenn YUI noch nicht geladen ist, dann nachladen
    if(!_yui_init) {
      if(loadJS(new Array('/layout/js/yui/yahoo/yahoo-min.js',
                          '/layout/js/yui/dom/dom-min.js',
                          '/layout/js/yui/event/event-min.js',
                          '/layout/js/yui/animation/animation-min.js',
                          '/layout/js/text_scroller.js')) === true) {
        _yui_init = true;
      }
      // da das Laden ein paar Sekunden dauert, warten wir auf den naechsten Timer
      return;
    }

    // QuickHack, Eilmeldung neu laden, da das Laufband gelegentlich nach links verschwindet
    if(eilcount++ > 5) { eiltext = ''; }

    // Wenn die Meldung sich nicht geaendert hat, dann ist nichts zu tun
    if(result.meldung == eiltext) { return; }

    // Meldungstext speichern
    eiltext = result.meldung;

    var eilmeldung = '';
    if(result.link != '') {
      eilmeldung = '<a href="../rponline_14_03_08-Dateien/' + result.link + '" target="_top">' + result.meldung + '</a>';
    } else {
      eilmeldung = result.meldung;
    }

    if(!eilmeldungFlag) {
      toparea  = $('toparea');
      mainarea = $('mainarea');
      eil      = $('eilmeldung');
      if(toparea !== null) {
        // Portalseite
        top = 60 + getPx(YAHOO.util.Dom.getStyle(toparea,'top'));
        toparea.style.top = top + 'px';
        top = 60 + getPx(YAHOO.util.Dom.getStyle(mainarea,'top'));
        mainarea.style.top = top + 'px';
      } else {
        // sonstige Seiten
        top = 60 + getPx(YAHOO.util.Dom.getStyle(mainarea,'top'));
        mainarea.style.top = top + 'px';
	  	}
      if($('produktmarke')) {
        mainarea.style.top = '160px';
        eil.style.top = '100px';
      }
      eil.style.height = '60px';
      eil.style.display = 'block';
    }

    // Eilmeldung ist sichtbar
    eilmeldungFlag = true;

    $('scrolling_container_eilmeldung').innerHTML = '<div class="eilmeldung" id="eilmeld_1">' + eilmeldung + '</div>';

    if(gScrollerEilmeldg !== null) {
      window.clearTimeout(gScrollerEilmeldg.animTimer);
    }
    gScrollerEilmeldg = null;
    gScrollerEilmeldg = new TextScroller('wrapper_eilmeldung', 'scrolling_container_eilmeldung', 'eilmeldung', 'hor', 0, true, 6/100);
    gScrollerEilmeldg.init();

  } else {

    eiltext = '';
    // Eilmeldungstext ist leer ...
    if(gScrollerEilmeldg !== null) {
      gScrollerEilmeldg.continousScroll = false;
      gScrollerEilmeldg = null;

      if(eilmeldungFlag) {
        toparea  = $('toparea');
        mainarea = $('mainarea');
        eil      = $('eilmeldung');
        eil.style.display = 'none';
        eil.style.height = '0px';
        $('scrolling_container_eilmeldung').innerHTML = '';
        if(toparea !== null) {
          // Portalseite
          top = getPx(YAHOO.util.Dom.getStyle(toparea,'top')) - 60;
          toparea.style.top = top + 'px';
          top = getPx(YAHOO.util.Dom.getStyle(mainarea,'top')) - 60;
          mainarea.style.top = top + 'px';
        } else {
          // sonstige Seiten
          top = getPx(YAHOO.util.Dom.getStyle(mainarea,'top')) - 60;
          mainarea.style.top = top + 'px';
  	  	}
  	  }
  	  eilmeldungFlag = false;
    }

  }
}

/*
 * load JavaScript files at runtime
 */
function loadJS(scripts) {
  if(!(scripts instanceof Array)) { return false; }
  var to_be_loaded = new Array();
  var head = document.getElementsByTagName("head")[0];
  var loaded_scripts = head.getElementsByTagName("script");

  for (var i=0; i<scripts.length; i++) {
    var found = false;
    for (var j=0; j<loaded_scripts.length; j++) {
      if (typeof loaded_scripts[j] === 'object') {
        var src = loaded_scripts[j].getAttribute('src');
        if(src !== null && src == scripts[i]) {
          found = true;
        }
      }
    }
    if (!found) {
      to_be_loaded.push(scripts[i]);
    }
  }
  scripts = to_be_loaded;

  var current = 0;
  //var savetitle = document.title;
  var is_ie = whichBrowser() == 'ie' ? true : false;
  var evt = is_ie ? "onreadystatechange" : "onload";

  function loadNextScript() {
    if (current > 0 && is_ie &&
      !/loaded|complete/.test(window.event.srcElement.readyState)) {
      return;
    }
    if (current < scripts.length) {
      var url = scripts[current++];
      //document.title = "[Eilmeldung: loading script " + current + "/" + scripts.length + "]";
      var script = document.createElement("script");
      script.type = "text/javascript";
      script.src = url;
      script[evt] = loadNextScript;
      head.appendChild(script);
    //} else {
      //document.title = savetitle;
    }
  };
  loadNextScript();
  return true;
}

function Mover(text,color) {
  text.style.cursor='pointer';
  if (typeof(color)=='undefined') {
    text.style.color='#f06414';
  } else if (color=='anz') {
    text.style.color='#006eab';
  }
  window.status=text.getElementsByTagName("a")[0].href;
}
function Mout(text) {
  text.style.color='';
  window.status='';
}

/**
 * switch tab in "reitermodul" box
 */
function switch_tab(base,num)
{
    var el;
    for (var i=1; ; i++) {
        if (! (el = $(base+'_'+i)) )
            return;
        if (i == num) {
            el.style.display = '';
            $(base+'_reiter_'+i).className='aktiv';
        } else {
            el.style.display = 'none';
            $(base+'_reiter_'+i).className='';
        }
    }
}
