
function formatNumber(nStr)
{
  nStr += '';
  x = nStr.split('.');
  x1 = x[0];
  x2 = x.length > 1 ? ',' + x[1] : '';
  var rgx = /(\d+)(\d{3})/;
  while (rgx.test(x1)) {
    x1 = x1.replace(rgx, '$1' + '.' + '$2');
  }
  return x1 + x2;
}

function isLoadingEffectActive(id) {
  var element = $(id);
  if(element == undefined) {
    element = $$(id)[0];
    if(element == undefined) {
      return;
    }
  }
  id = element.id;

  var divId = 'loading-'+id+'-animation-1';
  var div = $(divId);
  if(!div.visible()) {
    return false;
  } else {
    return true;
  }
}

function addOption(id,name,value) {
  var option = document.createElement("option");
  option.text = name;
  option.value = value;
  try {
    $(id).add(option,null);
  }
  catch(ex) {
    $(id).add(option); // IE only
  }
}

 function setSelectedIndexByValue(select, value) {
    var selectedIndex = undefined;
    for (var i = 0;i<select.options.length;i++) {
        if (!isNaN(i) && select.options[i] != undefined && select.options[i].value == value) {
            selectedIndex = i;
            break;
        }
    }
    select.selectedIndex = selectedIndex;
}

function toggleLoadingEffect(id,callback,text,state,zIndex) {
  var element = $(id);
  if(element == undefined) {
    element = $$(id)[0];
    if(element == undefined) {
      return;
    }
  }
  if(zIndex == undefined) {
    zIndex = 0;
  }
  id = element.id;

  var divId = 'loading-'+id+'-animation-1';
  var div2Id = 'loading-'+id+'-animation-2';
  var div,div2;
  div = $(divId);
  div2 = $(div2Id);

  // text fallback
  if(typeof text == 'undefined' || !text ) {
    text = 'Inhalte werden geladen...';
  }
  
  // calculate positions
  if(div2) {
    div2.innerHTML = '<img style="vertical-align: middle" src="http://img.godmode-trader.de/loading.gif" alt="">' + text;
  }
  if(!div || !div2) {
    if(!div) {
      div = $(document.createElement('div'));
      div.id = divId;
      element.up().appendChild(div);
      div.hide();
      div.oncontextmenu = function() {return false;};
      div.style.backgroundColor = '#FFFFFF';
      div.style.position = 'absolute';
      if (element.getStyle('zIndex')>0) {
        div.style.zIndex = element.getStyle('zIndex') + (zIndex != 0?zIndex:1);
      } else {
        div.style.zIndex = zIndex;
      }
    }
    if(!div2) {
      div2 = $(document.createElement('div'));
      div2.id = div2Id;
      div2.oncontextmenu = function() {return false;};
      div.appendChild(div2);
      div2.innerHTML = '<img style="vertical-align: middle" src="http://img.godmode-trader.de/loading.gif" alt="">' + text;
      div2.style.position = 'relative';
      div2.style.textAlign = 'center';
    }
  }
  // find a zindex for the new layers
  var dimensions = element.getDimensions();
  var offset = Position.positionedOffset(element);

  div.style.left = offset[0]+'px';
  div.style.top = offset[1]+'px';

  div.style.width = dimensions.width+'px';
  div.style.height = dimensions.height+'px';

  div2.style.top = ((dimensions.height/2)-15)+'px';

  if(state == undefined) {
    if(div.visible()) {
      state = false;
    } else {
      state = true;
    }
  }
  if(!div.visible() || state == true) {
    Element.setOpacity(div,0.7);
    div.show();

    return true;
  } else {
    new Effect.Fade(div,{afterFinish: callback});

    return false;
  }

}

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('skytag') ) {
      document.getElementById('skytag').style.display = "block";
    }
    /* 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;
    }

    if( typeof(ads_background_image_position) == 'string' ) {
      document.getElementsByTagName('body')[0].style.backgroundPosition = ads_background_image_position;
    }


    /* BIGSIZE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  */
    if( checkElementExists('bigsizetag') ) {

      /* Sets Background to BIGSIZE   */
      if( typeof(ads_trackinglink_bigsize) == 'string' ) {
        document.getElementById('bigsizetag').onclick = function(){ window.open(ads_trackinglink_bigsize) };
      }

      /* Sets Alignment to BIGSIZE    */
      if( typeof(ads_align_bigsize) == 'string' ) {
        document.getElementById('bigsizetag').style.textAlign = ads_align_bigsize;
      }

    }

    /* SKYTAG ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  */
    if( checkElementExists('skytag') ) {

      /* Sets Link to SKY    */
      /*if( typeof(ads_trackinglink_sky) == 'string' ) {
       
       
        if( document.all ) {
            $('banner_background').onclick = function() { window.open(ads_trackinglink_sky); }
        } else {
            $('banner_background').onclick = function(e) {
                                        if( Event.pointerX(e) >= (800+ parseInt($$('#skytag object')[0].width)) ||
                                            Event.pointerY(e) >= parseInt($$('#skytag object')[0].height)
                                        ) {
                                             window.open(ads_trackinglink_sky);
                                        } else {
                                            return false;
                                        }
                                  }
        }                                                        
                              
        document.getElementById('skytag').style.background = document.getElementsByTagName('body')[0].style.backgroundColor;
      }*/

      /* Top margin to SKY    */
      if( typeof(ads_offset_top_sky) == 'string' ) {
        var set_absolute_sky = true;
        document.getElementById('skytag').style.top = ads_offset_top_sky;
      }

      /* Left margin to SKY    */

      if( typeof(ads_offset_left_sky) == 'string' ) {
        document.getElementById('skytag').style.marginLeft  = ads_offset_left_sky;
        document.getElementById('skytag').style.left        = '0px';
      }

      if( set_absolute_sky ) {
        document.getElementById('skytag').style.zIndex = 999;
        document.getElementById('skytag').style.position = 'absolute';
      }

      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  = window.innerHeight + window.scrollMaxY;
    }  else if( document.body.scrollHeight > document.body.offsetHeight ) {
      pageWidth   = document.body.offsetWidth;
      pageHeight  = document.body.scrollHeight;
    } else {
      pageWidth   = document.body.offsetWidth;
      pageHeight  = document.body.offsetHeight + document.body.offsetTop;
    }

    if( document.documentElement ) {
        pageHeight  = document.documentElement.clientHeight +  document.documentElement.scrollTop;
    }

    var set_absolute_sky = false;
    restWidth = pageWidth - 817;

    resizeSkyWorkerInterval = setInterval( 'resizeSkyWorker(' + restWidth + ',' + pageHeight + ');', 200 );
}

function resizeSkyWorker( restWidth, pageHeight ) {

    if( document.getElementById('banner_background_adition') ) {
      document.getElementById('banner_background_adition').style.width          = restWidth + 'px';
      document.getElementById('banner_background_adition').style.height         = pageHeight + 'px';
      //document.getElementById('banner_background_adition').style.backgroundColor = '#fff';
      clearInterval(resizeSkyWorkerInterval);
      //alert( document.getElementById('skytag').style.width + ' x ' + document.getElementById('skytag').style.height );
    }
}



/**
*
*  Base64 encode / decode
*  http://www.webtoolkit.info/
*
**/

var Base64 = {

    // private property
    _keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",

    // public method for encoding
    encode : function (input) {
        var output = "";
        var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
        var i = 0;

        input = Base64._utf8_encode(input);

        while (i < input.length) {

            chr1 = input.charCodeAt(i++);
            chr2 = input.charCodeAt(i++);
            chr3 = input.charCodeAt(i++);

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

            if (isNaN(chr2)) {
                enc3 = enc4 = 64;
            } else if (isNaN(chr3)) {
                enc4 = 64;
            }

            output = output +
            this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
            this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);

        }

        return output;
    },

    // public method for decoding
    decode : function (input) {
        var output = "";
        var chr1, chr2, chr3;
        var enc1, enc2, enc3, enc4;
        var i = 0;

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

        while (i < input.length) {

            enc1 = this._keyStr.indexOf(input.charAt(i++));
            enc2 = this._keyStr.indexOf(input.charAt(i++));
            enc3 = this._keyStr.indexOf(input.charAt(i++));
            enc4 = this._keyStr.indexOf(input.charAt(i++));

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

            output = output + String.fromCharCode(chr1);

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

        }

        output = Base64._utf8_decode(output);

        return output;

    },

    // private method for UTF-8 encoding
    _utf8_encode : function (string) {
        string = string.replace(/\r\n/g,"\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {

            var c = string.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }

        return utftext;
    },

    // private method for UTF-8 decoding
    _utf8_decode : function (utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;

        while ( i < utftext.length ) {

            c = utftext.charCodeAt(i);

            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i+1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i+1);
                c3 = utftext.charCodeAt(i+2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }

        }

        return string;
    }

}