
function draw_impression(slot,index){
  var t = getTopWindow();
  if((!('impressions' in t))||(!(slot in t.impressions))||(t.impressions[slot].length==0))return false;
  index=parseInt(index);

  if(isNaN(index))index=0;
  if(index>=t.impressions[slot].length)index=0;
  var sizes = {impression_one:[468,60],impression_two:[120,600]};
  if(!(slot in sizes))return false;
  var data = t.impressions[slot][index];
  var delay = 0;
  try{
    if(t.impressions[slot].length>1){
      if(('delay' in t.impressions[slot][index])&&(typeof(t.impressions[slot][index].delay)==='number'))delay=t.impressions[slot][index].delay*1000;
      else delay=15000;
      }
    if(!('iFrameElement' in data)){
      data.iFrameElement = document.createElement('iframe');
      data.iFrameElement.frameBorder = 0;
      data.iFrameElement.width = sizes[slot][0];
      data.iFrameElement.height = sizes[slot][1];
      data.element.appendChild(data.iFrameElement);
      data.iFrameElement.src = data.img.src;
      for(K in t.impressions[slot]){
        t.impressions[slot][K].iFrameElement=data.iFrameElement;
        }
      }
    if(!('imageElement' in data)){
      // *** attention ladies, iframe.contentDocument does not work on ie < 8
      var doc = data.iFrameElement.contentWindow.document;
      doc.open();
      doc.write(
        '<html><head><title>on the fly impression</title>'
        + '<style type="text/css">*{margin:0;padding:0;border:none 0;cursor:pointer;}</style>'
        + '<body><div><img id="view_impression"/></div></body></html>');
      doc.close();
      data.imageElement = doc.getElementById('view_impression');
      if(data.imageElement){
        for(var K in t.impressions[slot]){
          t.impressions[slot][K].imageElement = data.imageElement;
          }
        }
      else delete data.imageElement;
      }
    data.imageElement.src     = data.img.src;
    data.iFrameElement.height = sizes[slot][1];
    data.iFrameElement.width  = sizes[slot][0];
    data.imageElement.height  = sizes[slot][1];
    data.imageElement.width   = sizes[slot][0];
    data.imageElement.title   = data.title;
    data.imageElement.ownerDocument.onclick = function(){
      var err;
      try{window.parent.openWindow(data.link,750,550,'yes');}
      catch(err){return false;}
      return true;
      };
    }
  catch(err){return false;}
  if(delay>0){
    window.setTimeout('draw_impression("' + slot + '",' + (index+1).toString() + ');',delay);
    }
  return true;
  };

function set_impression_data(impressionData,implicitDraw){
  if(!('offer' in impressionData)||(typeof(impressionData.offer)!=='object'))return false;
  var offers = [],i,buf,err;
  implicitDraw = (implicitDraw)? true:false;
  if(impressionData.offer.constructor===Array){
    offers = impressionData.offer;
    }
  else offers = [impressionData.offer];
  var data = [],slotId = '';
  function incomingDateParse(dateString){
    var r = false,err;
    var buf = dateString.match(/^(\d+)\.(\d+)\.(\d+)\s+(\d+):(\d+):(\d+)$/);
    if(buf){
      try{r = new Date(parseInt(buf[3]),parseInt(buf[2])-1,parseInt(buf[1]),parseInt(buf[4]),parseInt(buf[5]),parseInt(buf[6]),0);}
      catch(err){r=false;}
      }
    return r;
    };
  for(i=0;i<offers.length;i++){
    try{
      if(typeof(offers[i].categories.category)==='string')offers[i].categories.category=[offers[i].categories.category];
      buf = ' ' + offers[i].categories.category.join(' ') + ' ';
      buf = {
        id : offers[i].id
        ,created : incomingDateParse(offers[i].creationDate)
        ,modified : incomingDateParse(offers[i].modificationDate)
        /* impression category name pattern */
        ,impression : buf.replace(/.*\W[0-9]+(-[a-zA-Z0-9]+)*-(impression\w+)\W.*/,'$2')
        ,categories : offers[i].categories.category
        ,link : offers[i].links.ibe.url
        ,title : offers[i].title
        };
      buf.img = new Image();
      buf.img.src = offers[i].links.pic.url;
      if(slotId==='')slotId = buf.impression;
      buf.element = document.getElementById(slotId);
      if(!buf.element)buf=false;
      }
    catch(err){buf=false;}
    if(buf!==false)data.push(buf);
    }
  if((data.length>0)&&(slotId.length>0)){
    var t = getTopWindow();
    if(!('impressions' in t))t.impressions={};
    t.impressions[slotId] = data;
    if(implicitDraw===true){
      draw_impression(slotId,0);
      }
    }
  };
/*
 * Use this to set the impression data
 * @param string Name - ID of the div to Use
 * @param array of objects Data - each element connsists at least of title,link and image. target is optional
 * @todo: after deletion of all phpcms stuff delete the above deprecated functions
 */
function setImpression(Name,Data){
  var r = false,e;
  if((typeof(Data)!='object')
    ||(Data.constructor!=Array)
    ||(Data.length==0)
    ||((e=document.getElementById(Name))===null)
    ||(e.constructor!=HTMLDivElement)
    )return r;
  function Impression(Div){
    if(this.constructor!=arguments.callee)return false;
    this.element = Div;
    this.data = [];
    this.speed = 10;
    this.index = -1;
    this.nofollow = true;
    this.interval = null;
    this.setData = function(Data){
      if((typeof(Data)!='object')||(Data.constructor!=Array)||(Data.length<1))return false;
      var k,s,a = [];
      for(k in Data){
        if((typeof(Data[k])=='object')&&('title' in Data[k])&&('link' in Data[k])&&('image' in Data[k])){
          a.push('<a href="' + Data[k].link + '"' + ((this.nofollow == true)? ' rel="nofollow"':'') + (('target' in Data[k])? ' target="' + Data[k].target + '"':'') + ' title="' + Data[k].title + '" class="impressionm pop"><img src="' + Data[k].image + '" alt="' + Data[k].title + '"/></a>');
          }
        }
      if(a.length == 0)return false;
      if(typeof(this.interval)=='number'){
        window.clearInterval(this.interval);
        this.interval = null;
        }
      this.data = a;
      this.index = -1;
      this.cycle();
      };
    this.cycle = function(){
      this.index++;
      if(this.index >= this.data.length)this.index = 0;
      this.element.innerHTML = this.data[this.index];
      /**
       * special attention for a[target=pop] below this
       */
      if('refreshPop' in window)window.refreshPop();
      if(this.data.length === 1){
        window.clearInterval(this.interval);
        this.interval = null;
        }
      else if(typeof(this.interval)!=='number')this.interval = window.setInterval('document.getElementById("' + this.element.id + '").impression.cycle();',this.speed*1000);
      };
    };
  if('impression' in e)delete e.impression;
  e.impression = new Impression(e);
  return e.impression.setData(Data);
  };

if('YUI' in this)YUI().use('dom','node',function(Y){
  Y.on("domready",function(){
  	if('impressionData' in window){
  	  var e,k,kk,s,o = impressionData;
  	  if(Y.Lang.isObject(impressionData)){
  	    for(var k in impressionData){
  	      setImpression(k,o[k]);
  	      }
  	    try{
          delete window.impressionData;
          }
        catch(ERR){}
  	    }
  	  }
  	});
  });
