/* global functions go here */

/** GETBROWSERSIZE */

function getBrowserSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }

  return {"width":myWidth,"height":myHeight};
}

/** GETBROWSERSIZE */

/*--------------------------------------------------------------------------*/

/* COOKIE */

function set_cookie(name,value,expires) {
  if (expires) document.cookie = name + '=' + escape(value) + '; expires=' + expires.toGMTString() + '; path=/';
  else document.cookie = name + '=' + escape(value) + '; path=/';
}

function get_cookie(name) {

  var dcookie = document.cookie;
  var cname = name + "=";
  var clen = dcookie.length;
  var cbegin = 0;
  while (cbegin < clen) {
    var vbegin = cbegin + cname.length;
    if (dcookie.substring(cbegin, vbegin) == cname) {
      var vend = dcookie.indexOf (";", vbegin);
      if (vend == -1) vend = clen;
      return unescape(dcookie.substring(vbegin, vend));
    } cbegin = dcookie.indexOf(" ", cbegin) + 1;
    if (cbegin == 0) break;
  }
  return null;
}

function del_cookie(name) {
  document.cookie = name + '=' + '; expires=Thu, 01-Jan-70 00:00:01 GMT; path=/';
}

function dump_cookies() {
  if (document.cookie == '') document.write('No Cookies Found');
  else {
    thisCookie = document.cookie.split('; ');
    for (i=0; i<thisCookie.length; i++) {
      document.write(thisCookie[i] + '<br \/>');
    }
  }
}

/* COOKIE */

/*--------------------------------------------------------------------------*/

/* ENGINE DETECTION */
/* if ( Engine.isMSIE ) {} */
if( !isAdmin ) {
  var Engine = {
    detect: function() {
      var UA = navigator.userAgent;
      this.isKHTML = /Konqueror|Safari|KHTML/.test(UA);
      this.isGecko = (/Gecko/.test(UA) && !this.isKHTML);
      this.isOpera = /Opera/.test(UA);
      this.isMSIE  = (/MSIE/.test(UA) && !this.isOpera);
      this.isMSIE7 = this.isMSIE && !(/MSIE 6\./.test(UA) && !this.isOpera);
      this.isMSIE6 = this.isMSIE && !this.isMSIE7;
    }
  }
  Engine.detect();
}
/* ENGINE DETECTION */

/*--------------------------------------------------------------------------*/

/* SERVER NAME */

function getServerName() {
  var href = document.location.href;
  var rExp = /^http*:\/\//;
  var rExp2 = /\/.*$/;
  var href2 = href.replace(rExp, '')
  var href3 = href2.replace(rExp2, '')
  return href3;
}

/* SERVER NAME */

/*--------------------------------------------------------------------------*/

/* CONSOLE LOG */

function loggi(myvar,win) {

  try{
    console.log(myvar);
  } catch(e){}
}

/* CONSOLE LOG */

/*--------------------------------------------------------------------------*/

/* BASE 64 */

function base64_encode(str)
{
  var r=[];
  var i=0;
  var d=[];
  for (i=0; i<str.length; i++) {
    d[i] = str.charCodeAt(i);
  }
  i=0;
  var dl=d.length;
  if ((dl%3) == 1) {
    d[d.length] = 0; d[d.length] = 0;
  }
  if ((dl%3) == 2) {
    d[d.length] = 0;
  }
  while (i<d.length) {
    r[r.length] = b64[d[i]>>2];
    r[r.length] = b64[((d[i]&3)<<4) | (d[i+1]>>4)];
    r[r.length] = b64[((d[i+1]&15)<<2) | (d[i+2]>>6)];
    r[r.length] = b64[d[i+2]&63];
    i+=3;
  }
  if ((dl%3) == 1) {
    r[r.length-1] = r[r.length-2] = '=';
  }
  if ((dl%3) == 2) {
    r[r.length-1] = '=';
  }
  var t=r.join('');
  return t;
}

function base64_decode(t)
{
  var d = [];
  var i = 0;
  var retval = '';
  t=t.replace(/\n|\r/g, '');
  t=t.replace(/=/g, '');
  while (i<t.length) {
    d[d.length] = (f64[t.charAt(i)]<<2) | (f64[t.charAt(i+1)]>>4);
    d[d.length] = (((f64[t.charAt(i+1)]&15)<<4) | (f64[t.charAt(i+2)]>>2));
    d[d.length] = (((f64[t.charAt(i+2)]&3)<<6) | (f64[t.charAt(i+3)]));
    i+=4;
  }
  if (t.length%4 == 2) {
    d = d.slice(0, d.length-2);
  }
  if (t.length%4 == 3) {
    d = d.slice(0, d.length-1);
  }
  for (i=0; i<d.length; i++) {
    retval += String.fromCharCode(d[i]);
  }
  return retval;
}

function base64_init() {
  var b64s='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
  b64 = [];
  f64 = [];
  for (var i=0; i<b64s.length ;i++) {
    b64[i] = b64s.charAt(i);
    f64[b64s.charAt(i)] = i;
  }
}

/* BASE 64 */


/*--------------------------------------------------------------------------*/

/* PROTOTYPE EXTEND */

// AJAX Eval

if( !isAdmin ) {

  Ajax.Eval = Class.create();
  Object.extend(Object.extend(Ajax.Eval.prototype, Ajax.Request.prototype), {
    initialize: function(url, pars) {
      this.transport = Ajax.getTransport();
      this.setOptions({method:'post', parameters:pars});
      this.options.onComplete = (function(transport) {
        eval(transport.responseText);
      });
      this.request(url);
    }
  });

}

/* PROTOTYPE EXTEND */

/*--------------------------------------------------------------------------*/

/* URL DECODE*/

function URLDecode(psEncodeString) {
  var lsRegExp = /\+/g;
  return unescape(String(psEncodeString).replace(lsRegExp, " "));
}

/* URL DECODE*/


/*--------------------------------------------------------------------------*/


/* MENAGE DEFAULT FIELD TEXT FUCUS BLUR */

function  defaultText(element){

  $(element).onblur = function(){
    if(this.value=='') this.value= this.defaultValue;
  }

  if ( $(element).defaultValue == '' ) $(element).defaultValue = $(element).value;

  if($(element).value == $(element).defaultValue) $(element).value='';

}

/* MENAGE DEFAULT FIELD TEXT FUCUS BLUR */


/* PNG CORRECTION */
function correctPNG() {
  try{
    var arVersion = navigator.appVersion.split("MSIE");
    var version = parseFloat(arVersion[1]);

    if ((version >= 5.5) && (document.body.filters) ) {

      for(var i=0; i<document.images.length; i++) {

        var img = document.images[i];
        var imgName = img.src.toUpperCase();

        if ( imgName.indexOf('.PNG') > 0 ) {
          img.style.cssText = img.style.cssText + "width:" + img.width + "px; height:" + img.height + "px;filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\"" + img.src + "\", sizingMethod='scale');";
          img.src = "templates/html/master/img/transparent.gif";
        }

      }
    }
  } catch(e){
    loggi(e)
  }

}

/* PNG CORRECTION */

/* TEXT ELLIPSE*/

// usage

//    .ellipsis-child {
//      width:200px;
//    }
//    .ellipsis {
//      height:100px;
//      width:200px;
//      background:#fff;
//      overflow:hidden;
//    }

//    <div class="ellipsis">
//      <h3>title</h3>
//      <span style="width:200px;" class="ellipsis-child">
//        text
//      </span>
//    </div>

function ellipsis(){

  function _cutter(e,a,t){

    var end = ( Element.hasClassName(e,'with-end') )? '...': '';
    var i=0;
    while($(e).parentNode.scrollHeight > $(e).parentNode.offsetHeight && a.size() > 0){
      a.pop(); a.pop(); t = a.join(' '); e.firstChild.nodeValue = t + end ;
      //loggi(t);
      //loggi(i++);
    }
    ( end == e.firstChild.nodeValue )? e.firstChild.nodeValue = '' : null;
    return a.size();
  }

  try{
    var es = $$('.ellipsis-child');

    es.each(
      function(e){
        if( null !== e.firstChild ){
          if( null !== e.firstChild.nodeValue ){
            var t = e.firstChild.nodeValue;
            var a = $w(t);
            var size = _cutter(e,a,t);
          }
        }
      }
    );
  } catch(ex){

    loggi(Object.values(ex))
  }

}

/* TEXT ELLIPSE*/


/* OPEN POPUP */

function winPopup(anchor, width, height, name, focus){

  popup=window.open(anchor.href, ''+ name +'', 'width=' + width + ', height=' + height + ', scrollbars=yes');

  if(focus == true){
    popup.focus();
  }
  return false;

}


/* TABS */
function tabs(){
  try{
    document.getElementsByClassName('tabgroup').each(function(tab_group){
       new Control.Tabs(tab_group);
    });
  } catch(e) {
    loggi(e);
  }
}

/* CORRECT LEFT COLUMN HEIGHT */
function correctLeftColumnHeight(){
  try{
    var colToCheck = '';
    if($('verzeichnis')){
      colToCheck = 'verzeichnis';
      container = 'rightCol';
    }
    if($('articledetail')){
      colToCheck = 'articledetail';
      if($('rightCol').offsetHeight >= $('relatedCol').offsetHeight){
        container = 'rightCol';
      }else{
        container = 'relatedCol';
      }
    }

    if(colToCheck != ''){
      if(Engine.isMSIE6){
        $(colToCheck).style.height = $(container).offsetHeight+'px';
      }else{
        $(colToCheck).style.minHeight = $(container).offsetHeight+'px';
      }
    }

  }catch(e){
    loggi(e);
  }
}

/* EVENTS */

if ( !isAdmin )  Event.onDOMReady(ellipsis); //domFunction(ellipsis, {});
if ( !isAdmin )  Event.onDOMReady(correctPNG); //domFunction(correctPNG, {});
if ( !isAdmin )  Event.onDOMReady(tabs); //domFunction(tabs, {});
if ( !isAdmin )  Event.onDOMReady(correctLeftColumnHeight); //domFunction(correctLeftColumnHeight, {});

