window.addEvent('domready',function(){
  $$('a.hp-teaser').each(function(teaser) {
    enlargement = 5;
    
    teaser.origsize = {x:teaser.getSize().x,y:teaser.getSize().y};
    teaser.setStyles({
      width:teaser.origsize.x-parseInt(teaser.getStyle('padding-left'))-parseInt(teaser.getStyle('padding-right')),
      height:teaser.origsize.y-parseInt(teaser.getStyle('padding-top'))-parseInt(teaser.getStyle('padding-bottom'))
    });

    teaser.getFirst('img').setStyles({position:'absolute'});
    teaser.getFirst('img').origsize = {x:teaser.getFirst('img').getSize().x,y:teaser.getFirst('img').getSize().y};

    if(teaser.getFirst('img').origsize.x!=teaser.getFirst('img').origsize.y)
      teaser.getFirst('img').enlargement = new Array(enlargement,Math.round((teaser.getFirst('img').origsize.y/teaser.getFirst('img').origsize.x)*enlargement));
    else
      teaser.getFirst('img').enlargement = new Array(enlargement,enlargement);
    

    teaser.addEvent('mouseenter',function() {

      if($chk(this.effect)) this.effect.cancel();
      this.effect = new Fx.Morph(this.getFirst('img'),{
        duration: 100,
        onComplete: function(){

          if(!$chk(this.tooltip))
          {
            if((this.tiptext = $chk(this.getFirst('.tool-tip-text')) ? this.getFirst('.tool-tip-text').get('html') : ($chk(this.title) ? this.title : ''))!='')
            {
              this.tooltip = new Element('div', {'class':'tool-tip','style':'width:'+this.origsize.x+'px'});
              new Element('div',{'class':'tip'}).set('html',this.tiptext).inject(this.tooltip);
              new Element('div',{'class':'tip-bottom'}).inject(this.tooltip);
          
              this.tooltip.inject($$('body')[0]).setStyles({
                top:this.getPosition().y - this.tooltip.getSize().y,
                left:this.getPosition().x,
                marginTop: -20,
                opacity:0,
                display:'block'
              });
          
              this.effect = new Fx.Morph(this.tooltip,{
                duration: 150,
                transition: Fx.Transitions.Sine.easeOut,
                onComplete: function(){ $empty(this.tipeffect); }.bind(this)
              }).start({
                opacity: [this.tooltip.getStyle('opacity'),1],
                marginTop: [this.tooltip.getStyle('margin-top'),0]
              }); 
            }
          }
        
        }.bind(this)
      }).start({
        width:[this.getFirst('img').getSize().x,this.getFirst('img').origsize.x+this.getFirst('img').enlargement[0]*2],
        height:[this.getFirst('img').getSize().y,this.getFirst('img').origsize.y+this.getFirst('img').enlargement[1]*2],
        top:[this.getFirst('img').getPosition(this).y,parseInt(this.getStyle('padding')) - this.getFirst('img').enlargement[1]],
        left:[this.getFirst('img').getPosition(this).x,this.getFirst('img').enlargement[0]]
      });
    });
    
    teaser.addEvent('mouseleave',function() {
      if($chk(this.effect)) this.effect.cancel();
      this.effect = new Fx.Morph(this.getFirst('img'),{
        duration: 250,
        onComplete: function(){

          if($chk(this.tooltip))
          {
            this.effect = new Fx.Morph(this.tooltip,{
              duration: 150,
              transition: Fx.Transitions.Sine.easeOut,
              onComplete: function(){ this.tooltip.destroy(); this.tooltip = false; }.bind(this)
            }).start({
              opacity: [this.tooltip.getStyle('opacity'),0],
              marginTop: [this.tooltip.getStyle('margin-top'),20]
            }); 
          }

        }.bind(this)
      }).start({
        width:[this.getFirst('img').getSize().x,this.getFirst('img').origsize.x],
        height:[this.getFirst('img').getSize().y,this.getFirst('img').origsize.y],
        top:[this.getFirst('img').getPosition(this).y,parseInt(this.getStyle('padding'))],
        left:[this.getFirst('img').getPosition(this).x,this.getFirst('img').enlargement[0]*2]
      });
    });

  });
  

  $$('.single-tooltip').each(function(elmnt,i){
    if((elmnt.tiptext = $chk(elmnt.getFirst('.tool-tip-text')) ? elmnt.getFirst('.tool-tip-text').get('html') : ($chk(elmnt.title) ? elmnt.title : ''))!='')
    {
      elmnt.addEvent('mouseenter',function(){
        if(!$chk(this.tooltip))
        {
          size = 200;
          
          this.tooltip = new Element('div', {'class':'tool-tip','style':'width:'+size+'px;text-align:center'});
          new Element('div',{'class':'tip'}).set('html',this.tiptext).inject(this.tooltip);
          new Element('div',{'class':'tip-bottom'}).inject(this.tooltip);

          this.tooltip.inject($$('body')[0]).setStyles({
            top:this.getPosition().y - this.tooltip.getSize().y,
            left:this.getPosition().x + this.getSize().x/2 - size/2,
            marginTop: -20,
            opacity:0,
            display:'block'
          });
          
        }
        
        if($chk(this.effect)) this.effect.cancel();

        this.effect = new Fx.Morph(this.tooltip,{
          duration: 150,
          transition: Fx.Transitions.Sine.easeOut,
          onComplete: function(){ $empty(this.tipeffect); }.bind(this)
        }).start({
          opacity: [this.tooltip.getStyle('opacity'),1],
          marginTop: [this.tooltip.getStyle('margin-top'),0]
        }); 

      }.bind(elmnt));
      
      elmnt.addEvent('mouseleave',function(){
        if($chk(this.tooltip))
        {
          if($chk(this.effect)) this.effect.cancel();
          
          this.effect = new Fx.Morph(this.tooltip,{
            duration: 150,
            transition: Fx.Transitions.Sine.easeOut,
            onComplete: function(){ this.tooltip.destroy(); this.tooltip = false; }.bind(this)
          }).start({
            opacity: [this.tooltip.getStyle('opacity'),0],
            marginTop: [this.tooltip.getStyle('margin-top'),20]
          }); 
        }      
      }.bind(elmnt));

    }  
  });

  $$('#footer img').each(function(img){ img.addEvent('mouseenter',function(){ this.altText = this.alt; this.alt = ''; }); });
  $$('#footer img').each(function(img){ img.addEvent('mouseleave',function(){ this.alt = this.altText; }); });
  $$('.header img').each(function(img){ img.addEvent('mouseenter',function(){ this.altText = this.alt; this.alt = ''; }); });
  $$('.header img').each(function(img){ img.addEvent('mouseleave',function(){ this.alt = this.altText; }); });
  
  if($chk(input = $$('#searchbox input.query')))
  {
    input.set('rel',input.get('value'));
    input.addEvent('focus',function(){
      if(this.get('value') == this.get('rel')) this.set('value','');
    });
    input.addEvent('blur',function(){
      if(this.get('value') == '') this.set('value',this.get('rel'));
    });
  }
  
  
  if($chk($$('.bookmarks')) && $$('.bookmarks')!=null)
  {
    $$('.bookmarks').each(function(elmnt){
      elmnt.addEvent('click',function(){
        if(!$chk(this.isvisible) || this.isvisible==false)
        {
          if(!$chk(this.tooltip))
          {
            size = 200;
            
            URL = escape(location.href);
            TITLE = escape(document.title);
            
            bookmarks = [
              {'icon':'delicious.gif','title':'del.ico.us','target':'_blank','url':'http://del.icio.us/post?url='+URL+'&amp;title='+TITLE},
              {'icon':'digg.gif','title':'DIGG','target':'_blank','url':'http://digg.com/submit?phase=2&url='+URL+'&amptitle='+TITLE},
              {'icon':'folkd.gif','title':'FolkD','target':'_blank','url':'http://www.folkd.com/submit/'+URL},
              {'icon':'google.gif','title':'Google','target':'_blank','url':'http://www.google.com/bookmarks/mark?op=add&amp;bkmk='+URL+'&amp;title='+TITLE},
              {'icon':'linkarena.gif','title':'LinkArena','target':'_blank','url':'http://linkarena.com/bookmarks/addlink/?url='+URL+'&amp;title='+TITLE},
              {'icon':'misterwong.gif','title':'Mister Wong','target':'_blank','url':'http://www.mister-wong.de/index.php?action=addurl&amp;bm_url='+URL+'&amp;title='+TITLE},
              {'icon':'newsvine.gif','title':'Newsvine','target':'_blank','url':'http://www.newsvine.com/_tools/seed&save?u='+URL+'&amp;h='+TITLE},
              {'icon':'reddit.gif','title':'Reddit','target':'_blank','url':'http://reddit.com/submit?url='+URL+'&amp;title='+TITLE},
              {'icon':'stumbleupon.gif','title':'StumbleUpon','target':'_blank','url':'http://www.stumbleupon.com/submit?url='+URL+'&amp;title='+TITLE},
              {'icon':'windows.gif','title':'Windows','target':'_blank','url':'https://favorites.live.com/quickadd.aspx?marklet=1&amp;mkt=en-us&amp;url='+URL+'&amp;title='+TITLE+'&amp;top=1'},
              {'icon':'yahoo.gif','title':'Yahoo','target':'_blank','url':'http://myweb2.search.yahoo.com/myresults/bookmarklet?t='+TITLE+'&amp;d=&amp;tag=&amp;u='+URL},
              {'icon':'yigg.gif','title':'YIGG','target':'_blank','url':'http://www.yigg.de/neu?exturl='+URL+'&amp;exttitle='+TITLE},
              {'icon':'cross.png','target':'_self','url':'javascript:void(0)'}
              // {'icon':'bookmark.png','title':'Browser','target':'_self','url':'javascript:if(window.sidebar){window.sidebar.addPanel(\''+TITLE+'\',\''+URL+'\',\'\')}else if(window.opera && window.print){var elem=document.createElement(\'a\');elem.setAttribute(\'href\',\''+URL+'\');elem.setAttribute(\'title\',\''+TITLE+'\');elem.setAttribute(\'rel\',\'sidebar\');elem.click()}else if(document.all){window.external.AddFavorite(\''+TITLE+'\',\''+URL+'\')}'}
            ];            

            size = bookmarks.length*22;
            
            this.tiptext = '';
            for(i=0; i<bookmarks.length; i++)
            {
              if($chk(bookmarks[i].title))
                title = ' title="'+bookmarks[i].title+'-'+elmnt.get('text')+'"';
              else
                title = '';
              
              this.tiptext+='<a class="bookmark" style="padding:0 2px" href="'+bookmarks[i].url+'" target="'+bookmarks[i].target+'"'+title+'><img src="'+TEMPLATE_DIR+'/img/bookmarks/'+bookmarks[i].icon+'" style="display:inline;position:static;width:16px;height:16px" /></a>';
            }
            
            this.tooltip = new Element('div', {'class':'tool-tip','style':'width:'+size+'px;text-align:center'});
            new Element('div',{'class':'tip'}).set('html',this.tiptext).inject(this.tooltip);
            new Element('div',{'class':'tip-bottom'}).inject(this.tooltip);
  
            this.tooltip.inject($$('body')[0]).setStyles({
              top:this.getPosition().y - this.tooltip.getSize().y,
              left:this.getPosition().x + this.getSize().x - size/2 - 20,
              marginTop: -20,
              opacity:0,
              display:'block'
            });
            
            this.tooltip.getElements('.bookmark').each(function(link){
              link.addEvent('click',function(){
                elmnt.fireEvent('click');
              }.bind(elmnt))
            }.bind(elmnt));
            
          }
          
          if($chk(this.effect)) this.effect.cancel();
  
          this.effect = new Fx.Morph(this.tooltip,{
            duration: 150,
            transition: Fx.Transitions.Sine.easeOut,
            onComplete: function(){ $empty(this.tipeffect); }.bind(this)
          }).start({
            opacity: [this.tooltip.getStyle('opacity'),1],
            marginTop: [this.tooltip.getStyle('margin-top'),0]
          }); 
          
          this.isvisible = true;
        }
        else
        {
          if($chk(this.tooltip))
          {
            if($chk(this.effect)) this.effect.cancel();
            
            this.effect = new Fx.Morph(this.tooltip,{
              duration: 150,
              transition: Fx.Transitions.Sine.easeOut,
              onComplete: function(){ this.tooltip.destroy(); this.tooltip = false; }.bind(this)
            }).start({
              opacity: [this.tooltip.getStyle('opacity'),0],
              marginTop: [this.tooltip.getStyle('margin-top'),20]
            }); 
          }      

          this.isvisible = false;
        }
      }.bind(elmnt));
    });
  }
  
  
  // webcam bilder update
  if($chk(cams = $$('img.webcam')))
  {
    cams.each(function(el){
      if($chk(interval = el.get('rel')))
      {
        el.osrc = el.src;
        el.interval = (function(){ this.src = this.osrc+'?r='+$random(1,9999); }).periodical(1000*interval,el);
      }
    });
  }
  
  
});
