function recommend(url, title)
{
    var win = new Window('modal_window', {className: "dialog", width:330, height:530, zIndex:150, opacity:1, resizable: false, maximizable: false, minimizable: false});
    win.setTitle(title);
    win.setAjaxContent(url, {asynchronous:true});
    win.setDestroyOnClose();
    win.showCenter(true);
}

function fragantCodePopup(url, title)
{
    var win = new Window('modal_window', {className: "dialog", width:500, height:300, zIndex:150, opacity:1, resizable: false, maximizable: false, minimizable: false});
    win.setTitle(title);
    win.setAjaxContent(url, {asynchronous:true});
    win.setDestroyOnClose();
    win.showCenter(true);
}

function loginPopup()
{
    var win = new Window('modal_window', {className: "dialog", width:265, height:100, zIndex:150, opacity:1, resizable: false, maximizable: false, minimizable: false});
    win.setTitle('Login');
    win.setAjaxContent('/content/loginPopup', {asynchronous:true});
    win.setDestroyOnClose();
    win.showCenter(true);
}

/* PhotoGalleryLightBox class */
var PhotoGalleryLightBoxConfiguration = {
  LeftButton: '/images/photogallery/lightbox_left.gif', 
  RightButton: '/images/photogallery/lightbox_right.gif', 
  DoubleArrow: '/images/photogallery/doublearrow_right.gif'
}

function PhotoGalleryLightBox(images, options)
{
  this.images = images, // path to images
  this.options = options, // name, urls (Array), titles (Array)
  this.shown = false, 
  this.currentIndex, 
  this.win = false, 
  this.show = function(index)
  {    
    this.currentIndex = index; 
    var obj = this; 
    
    if (this.win == false)
    {
      this.win = new Window('modal_window_'+obj.options.name, {className: "dialog", width:600, height:450,  zIndex:150, opacity:1, resizable: false, maximizable: false, minimizable: false, closeCallback: function() { obj.shown = false; return true; }});
      this.win.setTitle('');
      this.win.setHTMLContent(this.buildLayout());
      
      // workaround for ie
      Window.prototype._fixIEOverlapping = function()
      {
      }
    }
    
    if (this.options.tracking && this.options.tracking.show)
    {
      if (typeof urchinTracker != "undefined")
      {
        urchinTracker(this.options.tracking.show);
      }
    }

    $('imageBoxButtonLinkleft'+obj.options.name).onclick = function() { obj.before(false); return false; };
    $('imageBoxButtonLinkright'+obj.options.name).onclick = function() { obj.next(false); return false; };
    
    this.update();
  }, 
  this.buildLayout = function()
  {
    var html = '<div id="imageBox'+this.options.name+'"></div>';
    var link = '';
    
    if (this.options.link)
    {
      link = '<div id="imageBoxLink'+this.options.name+'" style="position:absolute; top:10px; right:0px; display:none; width:85px; color:black"><img src="'+PhotoGalleryLightBoxConfiguration.DoubleArrow+'"/>&nbsp;<a href="'+this.options.link+'" class="gray underline">'+this.options.linktext+'</a></div>';
    }
    else
    {
      link = '';
    }
    
    return '<div style="position:relative;top:0px;left:0px;">' + this.createButton("left") + html + this.createButton("right") + link + '</div>';
  }, 
  this.createButton = function(direction)
  {
    var style = '';
    
    if (direction == "left")
    {
      style = "position:absolute; left:1px; top:10px";
    }
    else
    {
      style = "position:absolute; right:1px; top:10px";
    }
    
    var image = "";
    
    if (direction == "left")
    {
      image = PhotoGalleryLightBoxConfiguration.LeftButton;
    }
    else
    {
      image = PhotoGalleryLightBoxConfiguration.RightButton;
    }
    
    var html = '<div id="imageBoxButton'+direction+this.options.name+'" style="'+style+'"><a href="#" id="imageBoxButtonLink'+direction+this.options.name+'"><img src="' + image  + '"/></a></div>';
    
    return html;
  }
  this.next = function(check)
  {
    if (this.currentIndex >= this.images.length-1)
    {
      return false; 
    }
    
    if (check) return true;
    
    this.currentIndex++; 
    
    if (this.options.tracking && this.options.tracking.browsing)
    {
      if (typeof urchinTracker != "undefined")
      {
        urchinTracker(this.options.tracking.browsing);
      }
    }
    
    this.update();
  }, 
  this.before = function(check)
  {
    if (this.currentIndex-1 < 0)
    {
      return false; 
    }
    
    if (check) return true;
    
    this.currentIndex--;
    
    if (this.options.tracking && this.options.tracking.browsing)
    {
      if (typeof urchinTracker != "undefined")
      {
        urchinTracker(this.options.tracking.browsing);
      }
    }
    
    this.update();
  }, 
  this.update = function()
  {    
    var obj = this; 
    
    if (this.next(true))
    {
      if ($('imageBoxButtonright'+obj.options.name))
      {
        $('imageBoxButtonright'+obj.options.name).style.display = "block";
      }
      
      if ($('imageBoxLink'+obj.options.name))
      {
        $('imageBoxLink'+obj.options.name).style.display = "none";
      }
    }
    else
    {
      if ($('imageBoxButtonright'+obj.options.name))
      {
        $('imageBoxButtonright'+obj.options.name).style.display = "none";
      }
      
      if ($('imageBoxLink'+obj.options.name))
      {
        $('imageBoxLink'+obj.options.name).style.display = "block";
      }
    }
    
    if (this.before(true))
    {
      if ($('imageBoxButtonleft'+obj.options.name))
      {
        $('imageBoxButtonleft'+obj.options.name).style.display = "block";
      }
    }
    else
    {
      if ($('imageBoxButtonleft'+obj.options.name))
      {
        $('imageBoxButtonleft'+obj.options.name).style.display = "none";
      }
    }
        
    var image = new Image();    
    
    var url = this.options.urls[this.currentIndex];
    var title = this.options.titles[this.currentIndex];
      
    image.onload = function()
    {      
      var imageWidth, imageHeight; 
      
      if (image.width > image.height)
      {
        if (image.width < 400)
        {
          imageWidth = image.width + "px;";
        }
        else
        {
          imageWidth = "400px;"; 
        }
        
        imageHeight = false; 
      }
      else
      {
        if (image.height < 400)
        {
          imageHeight = image.height + "px;";
        }
        else
        {
          imageHeight = "400px;"; 
        }
        
        imageWidth = false; 
      }
          
      if ($('imageBoxButtonleft'+obj.options.name))
      {
        $('imageBoxButtonleft'+obj.options.name).style.top = (450/2 - 78/2) + "px";
      }
      
      if ($('imageBoxButtonright'+obj.options.name))
      {
        $('imageBoxButtonright'+obj.options.name).style.top = 450/2 - 78/2 + "px";
      }
      
      if ($('imageBoxLink'+obj.options.name))
      {
        $('imageBoxLink'+obj.options.name).style.top = 450/2 - 20 + "px";
      }
      
      if (obj.shown == false)
      {        
        obj.win.showCenter(true);
        obj.shown = true; 
      }
      
      var content = '<table border="0" height="450" width="600"><tr><td align="center" valign="middle"><span><a href="'+url+'"><img src="'+image.src+'" style="padding:3px; background:white; border:7px solid #E5E5E3;'+(imageWidth ? ' width:'+imageWidth : '')+(imageHeight ? ' height:'+imageHeight : '')+'"/></a></span><p style="clear:both; font-size:12px; color:black; margin-top:5px">'+title+'</p></td></tr></table>'; 
      
      $('imageBox'+obj.options.name).update(content);
    }
    
    image.onerror = function()
    {
      alert("You got an error!");
    }
    
    image.src = this.images[this.currentIndex];
  }
}

var UserLinkToolTip = {
  setup: function()
  {   
    var userlinks = $$('a span.user-tooltip');

    for (var userLinkIndex = 0; userLinkIndex < userlinks.length; userLinkIndex++)
    {
      var userLinkEle = userlinks[userLinkIndex];
      if (typeof userLinkEle == 'object') 
      {
        userLinkEle.id = "user-tooltip-" + userLinkIndex; 
        userLinkEle.observe('mouseover', function(event) { UserLinkToolTip.detachMouseOverEvent(Event.element(event), event); });
        userLinkEle.observe('mouseout', function(event) { UserLinkToolTip.detachMouseOutEvent(Event.element(event)); });
      }
    }    
  }, 
  manager: {
    shouldHideElements: new Array(), 
    currentTimeout: null, 
    setCurrentTimeout: function(timeout)
    {
      this.currentTimeout = timeout; 
    }, 
    clearCurrentTimeout: function()
    {
      if (this.currentTimeout != null)
      {
        window.clearTimeout(this.currentTimeout);
        this.currentTimeout = null;
      }
    }, 
    cleanExcept: function(userLinkEle)
    {
      $$('a span.user-tooltip').each(function(ele)
      {
        if (ele != userLinkEle && ele.getStyle('display') != 'none')
        {
          if ($(ele.id + "-container"))
          {
            $(ele.id + "-container").hide();
          }
        }
      });
    }
  }, 
  detachMouseOverEvent: function(userLinkEle, event)
  {    
    this.manager.clearCurrentTimeout();
    var timeout = window.setTimeout(function() { UserLinkToolTip.popupToolTip(userLinkEle, event); }, 800);
    this.manager.setCurrentTimeout(timeout);
  }, 
  detachMouseOutEvent: function(userLinkEle)
  {
    this.manager.clearCurrentTimeout();
    UserLinkToolTip.hideToolTip(userLinkEle);
  },
  popupToolTip: function(userLinkEle, event)
  {    
    this.manager.cleanExcept(userLinkEle);
            
    if ($(userLinkEle.id + "-container") != null)
    {
			this.positionizeTooltip($(userLinkEle.id + "-container"), event);
      $(userLinkEle.id + "-container").appear();
    }
    else
    {      
      var container = this.createToolTipContainer(userLinkEle, event);      
			this.positionizeTooltip(container, event);
      document.body.appendChild(container);
			container.appear();
    }
  }, 
	positionizeTooltip: function(ele, event)
	{
		var posX = Event.pointerX(event) + 5 + "px";
		var posY = Event.pointerY(event) + 5 + "px";
				
		ele.style.left = posX;
		ele.style.top = posY;
	}, 
  hideToolTip: function(userLinkEle)
  {
    if ($(userLinkEle.id + "-container"))
    {
      $(userLinkEle.id + "-container").hide();
    }
  },
  createToolTipContainer: function(userLinkEle)
  {
    var container = new Element('div', { id: userLinkEle.id + "-container", style: 'position:absolute; index-z:100; display:none' } );
    
    container.innerHTML = '<img src="/images/indicator.gif" style=""/>'; 
    
    new Ajax.Updater(container, '/nutzer/' + userLinkEle.getAttribute('user-nickname') + '/tooltipp');
    
    return container; 
  }
};

document.observe("dom:loaded", function() {
  UserLinkToolTip.setup();
});

