(function (){

  var container, close, content, frame, label;
  var moveBottom, moveInterval;
  
  function init(){
  
    function Get_Cookie(name){
      var start = document.cookie.indexOf( name + "=" );
      var len = start + name.length + 1;
      if ((!start) && (name != document.cookie.substring( 0, name.length ))){
        return null;
      }
      if (start == -1) return null;
      var end = document.cookie.indexOf( ";", len );
      if (end == -1) end = document.cookie.length;
      return unescape( document.cookie.substring( len, end ) );
    }
    
    function Set_Cookie(name, value, expires, path, domain, secure){
      var today = new Date();
      today.setTime(today.getTime());
      if (expires){
        expires = expires * 1000;
      }
      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" : "" );
    }
    
    var ad = parseInt(Get_Cookie("ad"));
    if (ad == 1){
      Set_Cookie("ad", "2", 0);
    }
    if (ad == 2){
      return;
    }
  
    if (typeof ads != "undefined"){
      if (typeof ads == "object" && ads instanceof Array){
        var ad_obj = ads[Math.floor(Math.random() * ads.length)];
        var ad_image = ad_obj['image'];
        var ad_link = ad_obj['link'];
      }
    }
  
    container = document.createElement("DIV");
    frame = document.createElement("DIV");
    close = document.createElement("DIV");
    content = document.createElement("DIV");
    label = document.createElement("A");
    
    container.style.background = "transparent";
    container.style.border = "0 none";
    container.style.bottom = "0px";
    container.style.display = "none";
    container.style.height = "188px";
    container.style.margin = "0";
    container.style.padding = "0";
    container.style.position = "fixed";
    container.style.right = "0px";
    container.style.width = "166px";
    
    frame.style.background = "url('http://www.mp3databaza.com/images/window.png') no-repeat";
    frame.style.border = "0 none";
    frame.style.height = "188px";
    frame.style.left = "0";
    frame.style.margin = "0";
    frame.style.opacity = "0.7";
    frame.style.filter = "alpha(opacity=70)";
    frame.style.padding = "0";
    frame.style.position = "absolute";
    frame.style.top = "0px";
    frame.style.width = "166px";
    
    close.style.background = "transparent url('http://www.mp3databaza.com/images/close.png') no-repeat 0 0";
    close.style.border = "0 none";
    close.style.cursor = "pointer";
    close.style.height = "17px";
    close.style.position = "absolute";
    close.style.right = "10px";
    close.style.top = "10px";
    close.style.width = "31px";
    
    content.style.background = "black no-repeat";
    if (typeof ad_image == "string"){
      content.style.backgroundImage = "url('" + ad_image + "')";
    }
    else{
      content.style.backgroundImage = "url('http://www.mp3databaza.com/images/reklama-150x150.png')";
    }
    content.style.border = "0 none";
    content.style.color = "lime";
    content.style.cursor = "pointer";
    content.style.height = "150px";
    content.style.left = "8px";
    content.style.margin = "0";
    content.style.padding = "0";
    content.style.position = "absolute";
    content.style.top = "30px";
    content.style.width = "150px";
    
    label.style.background = "url('http://www.mp3databaza.com/images/bullet.png') no-repeat 0 4px";
    label.style.border = "0 none";
    label.style.color = "#dddddd";
    label.style.paddingLeft = "8px";
    label.style.position = "absolute";
    label.style.left = "10px";
    label.style.top = "8px";
    label.style.textDecoration = "underline";
    label.style.font = "normal 13px Arial"
    label.href = "http://mp3databaza.com/index.php?stranka=reklama";
    label.innerHTML = "Chcem REKLAMU!";
    
    label.onmouseover = function (){
      this.style.textDecoration = "none";
    }
    label.onmouseout = function (){
      this.style.textDecoration = "underline";
    }
    
    close.onmouseover = function (){
      this.style.backgroundPosition = "0 -17px";
    }
    close.onmouseout = function (){
      this.style.backgroundPosition = "0 0";
    }
    close.onclick = function (){
      Set_Cookie("ad", "2", 600);
      unmoveStart();
    }
    
    content.onclick = function (){
      if (typeof ad_link == "string"){
        location.href = ad_link;
      }
      else if (typeof ad_link == "object" && ad_link instanceof Array){
        
      }
      else{
        location.href = "http://www.redhotchilipeppers.sk/news.php";
      }
    }
    
    container.appendChild(frame);
    container.appendChild(close);
    container.appendChild(content);
    container.appendChild(label);
    
    if (document.body == null){
      if (window.addEventListener){
        window.addEventListener("load", show, false);
      }
      else if (window.attachEvent){
        window.attachEvent("onload", show)
      }
    }
    else{
      show();
    }
    
  }
  
  function show(){
    document.body.appendChild(container);
    moveStart();
  }
  
  function moveStart(){
    moveBottom = -200;
    moveInterval = window.setInterval(move, 20);
    container.style.bottom = "-200px";
    container.style.display = "block";
  }
  
  function move(){
    if (moveBottom < 0){
      moveBottom ++;
      container.style.bottom = moveBottom + "px";
    }
    else{
      moveStop();
    }
  }
  
  function moveStop(){
    window.clearInterval(moveInterval);
    moveInterval = null;
  }
  
  function unmoveStart(){
    if (moveInterval != null){
      window.clearInterval(moveInterval);
    }
    moveInterval = window.setInterval(unmove, 20);
  }
  
  function unmove(){
    if (moveBottom > -200){
      moveBottom --;
      container.style.bottom = moveBottom + "px";
    }
    else{
      unmoveStop();
    }
  }
  
  function unmoveStop(){
    window.clearInterval(moveInterval);
    document.body.removeChild(container);
    container = null;
    frame = null;
    close = null;
    content = null;
    label = null;
  }
  
  init();
  
})();

