window.addEvent('domready', function() {
// automatic mouse over 
$$('img.mo').each(function(img) {
	var src = img.getProperty('src');
	var extension = src.substring(src.lastIndexOf('.'),src.length)
  var src1 = src.replace(extension,'-mo' + extension);
  new Asset.images([src1]);
	img.addEvent('mouseenter', function() { img.setProperty('src',src1); });
	img.addEvent('mouseleave', function() { img.setProperty('src',src); });
});
new Autocompleter.Request.JSON($('Suche_01'), '/suggest.php', {
		'indicatorClass': 'autocompleter-loading'
	});
var box = new CeraBox();
$$('.lightbox').each(function(el) { box.addItems(el); } );

$$('.like').each(function(el) {
  var data = new Hash;
  data.action='like';
  data.id=el.id.substr(5);
  data.url=document.URL;
  xajax(el,data);
});

$$('.rezlink').each(function(el) {
  var data = new Hash;
  data.action='rezlink';
  data.id=el.id.substr(10);
  data.url=document.URL;
  xajax(el,data);
});

$$('.rezension').each(function(el) {
  var data = new Hash;
  data.action='rezension';
  data.id=el.id.substr(4);
  data.url=document.URL;
  xajax(el,data);
});

$$('.xajax').each(function(el) {
  el.addEvent('click',function () {
    var data=new Hash;
    data.url=el.get('href');
    xxajax(el,'href',data);
    return false;
  } );
});

});

xxajax = function (el,func,data) {
  var request = new Request.JSON({
      url: '/_ajax/'+func,
      onComplete: function(r) {
        if (r) {
          if (r.tip) { new Bubble(el,{ content:r.tip } ); }
          if (r.ajax) {
            el.removeEvents('click');
            el.addEvent('click',function() { 
              xxajax(el,r.ajax);
              return false;
            });
          }
          if (r.update) {
            Object.each(r.update,function (v,k) { 
              if ($(k)) { $(k).set('html',v); }
            });
          }
          if (r.html) el.set('html', r.html);
          if (r.alert) { alert (r.alert); }
          if (r.js) { eval (r.js); }
        }
      },
      data: { 
        json: JSON.encode(data)
      }
  }).send();
}

xajax = function (el,data) {
  var request = new Request.JSON({
      url: '/ajax.php',
      onComplete: function(r) {
        if (r.tip) {
          new Bubble(el,{ content:r.tip } );
        }
        if (r.ajax) {
          el.removeEvents('click');
          el.addEvent('click',function() { 
            xajax(el,r.ajax);
            return false;
          });
        }
        if (r.html) el.set('html', r.html);
      },
      data: { 
        json: JSON.encode(data)
      }
  }).send();
}

sendrez = function (elem) {
  var data = new Hash;
  var txt='';
  elem.getParent('form').getElements('input,select,textarea').each( function (el) {
    if (el.name) data[el.name]=el.value;
  } );
  var err="";
  if (!data.titel) err+='Bitte Titel eingeben!\n';
  if (!data.description) err+='Bitte Rezension eingeben!\n';
  if (err=="") {
    xajax(elem.getParent('form').getParent(),data);
  } else {
    alert(err);
  }
}

rezfunc = function (elem,act,id) {
  var data = new Hash;
  data.action='rezension';
  data.saction=act;
  data.id=id;
  xajax(elem,data);
}


var Bubble = new Class ({
	Implements: [Options, Events],
    
  initialize: function (elements,options) {
		this.setOptions(options);
		var s = this;
		$$(elements).each(function(e) {
			evs = { };
			evs['mouseenter'] = function() { s.show(this); };
			evs['mouseleave'] = function() { s.hide(this); };
			e.addEvents(evs);
		});
		return this;
  },
  
  show: function (el) {
    if (el.tip) {
      clearTimeout(el.tip.retrieve('timeout'));
      return this;
    }
		var tip = this._create(el);
		el.tip=tip;
		return this;
  },
  
  hide: function (el) {
    if (el.tip) {
      clearTimeout(el.tip.retrieve('timeout'));
      el.tip.store('timeout', (function(){ if (el.tip) el.tip.dispose(); el.tip=false; }).delay(200) );
    }
    return this;
  },
  tipenter: function (tip,elem) {
    clearTimeout(tip.retrieve('timeout'));
    return this;
  },
  tipleave: function (tip,el) {
    clearTimeout(el.tip.retrieve('timeout'));
    el.tip.store('timeout', (function(){ if (el.tip) el.tip.dispose(); el.tip=false; }).delay(200) );
    return this;
  },
  
  _create: function (elem) {
		var s = this;
		var oc=this.options.content;
		//var cnt = (typeof(oc) == 'string' ? elem.get(oc) : oc(elem));
		var cnt = (typeof(oc) == 'string' ? oc : oc(elem));
		var cwr = new Element('div').addClass('floating-tip').setStyle('margin', 0);
		var tip = new Element('div').addClass('floating-tip' + '-wrapper').setStyles({ 'margin': 0, 'padding': 0, 'z-index': cwr.getStyle('z-index') }).adopt(cwr);
		cwr.set('html',cnt);
		cwr.set('id','test');
		var body = document.id(document.body);
		tip.setStyles({ 'position': 'absolute', 'opacity': 1, 'display':'' }).inject(body);
		var tipSz = tip.getSize(), trgC = elem.getCoordinates(body);
		var pos = { x: trgC.left, y: trgC.top };
		pos.y += trgC.height;
		pos.x += (trgC.width / 2 - tipSz.x / 2);
		tip.setStyles({ 'top': pos.y, 'left': pos.x });		
		tip.addEvent('mouseenter',function () { s.tipenter(tip,elem); } );
		tip.addEvent('mouseleave',function () { s.tipleave(tip,elem); } );
		return tip;
  }
  
});



var FloatingTips = new Class({

	Implements: [Options, Events],

	options: {
		position: 'top',
		center: true,
		content: 'title',
		html: false,
		balloon: true,
		arrowSize: 6,
		arrowOffset: 6,
		distance: 3,
		motion: 6,
		motionOnShow: true,
		motionOnHide: true,
		showOn: 'mouseenter',
		hideOn: 'mouseleave',
		showDelay: 0,
		hideDelay: 0,
		className: 'floating-tip',
		offset: {x: 0, y: 0},
		fx: { 'duration': 'short' }
	},

	initialize: function(elements, options) {
		this.setOptions(options);
		if (!['top', 'right', 'bottom', 'left', 'inside'].contains(this.options.position)) this.options.position = 'top';
		if (elements) this.attach(elements);
		return this;
	},

	attach: function(elements) {
		var s = this;
		$$(elements).each(function(e) {
			evs = { };
			evs[s.options.showOn] = function() { s.show(this); };
			evs[s.options.hideOn] = function() { s.hide(this); };
			e.addEvents(evs);
		});
		return this;
	},

	show: function(element) {
		var old = element.retrieve('floatingtip');
		if (old) if (old.getStyle('opacity') == 1) { clearTimeout(old.retrieve('timeout')); return this; }
		var tip = this._create(element);
		if (tip == null) return this;
		element.store('floatingtip', tip);
		this._animate(tip, 'in');
		this.fireEvent('show', [tip, element]);
		return this;
	},
	
	hide: function(element) {
		var tip = element.retrieve('floatingtip');
		if (!tip) return this;
		this._animate(tip, 'out');
		this.fireEvent('hide', [tip, element]);
		return this;
	},
	
	_create: function(elem) {
		
		var o = this.options;
		var oc = o.content;
		var opos = o.position;
		
		if (oc == 'title') {
			oc = 'floatingtitle';
			if (!elem.get('floatingtitle')) elem.setProperty('floatingtitle', elem.get('title'));
			elem.set('title', '');
		}
		
		var cnt = (typeof(oc) == 'string' ? elem.get(oc) : oc(elem));
		var cwr = new Element('div').addClass(o.className).setStyle('margin', 0);
		var tip = new Element('div').addClass(o.className + '-wrapper').setStyles({ 'margin': 0, 'padding': 0, 'z-index': cwr.getStyle('z-index') }).adopt(cwr);
		
		if (cnt) { 
			if (o.html) cwr.set('html', typeof(cnt) == 'string' ? cnt : cnt.get('html')); 
			else cwr.set('text', cnt); 
		} else { 
			return null;
		}
		
		var body = document.id(document.body);
		tip.setStyles({ 'position': 'absolute', 'opacity': 0 }).inject(body);
		
		if (o.balloon && !Browser.ie6) {
			
			var trg = new Element('div').addClass(o.className + '-triangle').setStyles({ 'margin': 0, 'padding': 0 });
			var trgSt = { 'border-color': cwr.getStyle('background-color'), 'border-width': o.arrowSize, 'border-style': 'solid','width': 0, 'height': 0 };
			
			switch (opos) {
				case 'inside': 
				case 'top': trgSt['border-bottom-width'] = 0; break;
				case 'right': trgSt['border-left-width'] = 0; trgSt['float'] = 'left'; cwr.setStyle('margin-left', o.arrowSize); break;
				case 'bottom': trgSt['border-top-width'] = 0; break;
				case 'left': trgSt['border-right-width'] = 0; 
					if (Browser.ie7) { trgSt['position'] = 'absolute'; trgSt['right'] = 0; } else { trgSt['float'] = 'right'; }
					cwr.setStyle('margin-right', o.arrowSize); break;
			}
			
			switch (opos) {
				case 'inside': case 'top': case 'bottom': 
					trgSt['border-left-color'] = trgSt['border-right-color'] = 'transparent';
					trgSt['margin-left'] = o.center ? tip.getSize().x / 2 - o.arrowSize : o.arrowOffset; break;
				case 'left': case 'right': 
					trgSt['border-top-color'] = trgSt['border-bottom-color'] = 'transparent';
					trgSt['margin-top'] = o.center ?  tip.getSize().y / 2 - o.arrowSize : o.arrowOffset; break;
			}
			
			trg.setStyles(trgSt).inject(tip, (opos == 'top' || opos == 'inside') ? 'bottom' : 'top');
			
		}
		
		var tipSz = tip.getSize(), trgC = elem.getCoordinates(body);
		var pos = { x: trgC.left + o.offset.x, y: trgC.top + o.offset.y };
		
		if (opos == 'inside') {
			tip.setStyles({ 'width': tip.getStyle('width'), 'height': tip.getStyle('height') });
			elem.setStyle('position', 'relative').adopt(tip);
			pos = { x: o.offset.x, y: o.offset.y };
		} else {
			switch (opos) {
				case 'top':     pos.y -= tipSz.y + o.distance; break;
				case 'right': 	pos.x += trgC.width + o.distance; break;
				case 'bottom': 	pos.y += trgC.height + o.distance; break;
				case 'left': 	pos.x -= tipSz.x + o.distance; break;
			}
		}
		
		if (o.center) {
			switch (opos) {
				case 'top': case 'bottom': pos.x += (trgC.width / 2 - tipSz.x / 2); break;
				case 'left': case 'right': pos.y += (trgC.height / 2 - tipSz.y / 2); break;
				case 'inside':
					pos.x += (trgC.width / 2 - tipSz.x / 2);
					pos.y += (trgC.height / 2 - tipSz.y / 2); break;
			}
		}
		
		tip.set('morph', o.fx).store('position', pos);
		tip.setStyles({ 'top': pos.y, 'left': pos.x });
		
		return tip;
		
	},
	
	_animate: function(tip, d) {
		
		clearTimeout(tip.retrieve('timeout'));
		tip.store('timeout', (function(t) { 
			
			var o = this.options, din = (d == 'in');
			var m = { 'opacity': din ? 1 : 0 };
			
			if ((o.motionOnShow && din) || (o.motionOnHide && !din)) {
				var pos = t.retrieve('position');
				if (!pos) return;
				switch (o.position) {
					case 'inside': 
					case 'top':		m['top']  = din ? [pos.y - o.motion, pos.y] : pos.y - o.motion; break;
					case 'right': 	m['left'] = din ? [pos.x + o.motion, pos.x] : pos.x + o.motion; break;
					case 'bottom': 	m['top']  = din ? [pos.y + o.motion, pos.y] : pos.y + o.motion; break;
					case 'left': 	m['left'] = din ? [pos.x - o.motion, pos.x] : pos.x - o.motion; break;
				}
			}
			
			t.morph(m);
			if (!din) t.get('morph').chain(function() { this.dispose(); }.bind(t)); 
			
		}).delay((d == 'in') ? this.options.showDelay : this.options.hideDelay, this, tip));
		
		return this;
		
	}

});


