WsCookie = new Class({
	initialize: function(Data, Fill) {
		this.DataFill = Fill || null;
		this.Data = Data;
		this.Data.load();
		if(this.length() === 0 && !this.emptied()) {
			if($type(this.DataFill) == 'array') {
				for(x = 0; x < this.DataFill.length; x++) {
					this.add(this.DataFill[x]);
				}
			} else if($type(this.DataFill) == 'object') {
				this.add(this.DataFill);
			}
		}
	},
	add: function(obj) {
		key = this.generatekey(6);
		this.Data.set(key, obj);
		return key;
	},
	remove: function(key) {
		this.Data.erase(key);
		if(this.length() === 0) {
			this.set('is_emptied', true);
		}
	},
	get: function(key) {
		return this.Data.get(key);
	},
	set: function(key, value) {
		this.Data.set(key, value);
	},
	enable: function() {
		this.Data.set('is_disabled', false);
	},
	disable: function() {
		this.Data.set('is_disabled', true);
	},
	disabled: function() {
		if(this.Data.get('is_disabled') === null || this.Data.get('is_disabled') === false) {
			return false;
		} else {
			return true;
		}
	},
	keys: function() {
		all = arguments[0] || false;
		if(all) {
			return this.Data.getKeys();
		} else {
			Keys = this.Data.getKeys();
			Keys.erase('is_disabled');
			Keys.erase('is_emptied');
			return Keys;
		}
	},
	length: function() {
		return this.keys().length;
	},
	empty: function() {
		this.Data.each(function(value, key, hash) { 
			if(key != 'is_disabled' && key != 'is_emptied') {
				hash.erase(key);
			}
		});
		this.Data.set('is_emptied', true);
	},
	emptied: function() { 
		if(this.Data.get('is_emptied') === null || this.Data.get('is_emptied') === false) {
			return false;
		} else {
			return true;
		}
	},
	generatekey: function(length) {
		Length = length || 6;
		Letters = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz';
		Key = '';
		for(i = 0; i < length; i++) {
			Key += Letters.substr($random(0, (Letters.length - 1)), 1);
		}
		return this.Data.has(Key) ? generatekey(Length) : Key;
	}
});
Last = new Class({
	initialize: function() {
		this.draw();
		this.drawctrl();
	},
	draw: function() {
		if($('search_past')) {
			if(WsCookieLast.disabled() === false) {
				for(i = 0; i < WsCookieLast.keys().length; i++) {
					Set = WsCookieLast.get(WsCookieLast.keys()[i]);
					Set.q = Set.q.replace(/\+/g, ' ');
					SetClass = 'main' + ((i % 2) === 0 ? ' hilight' : '');
					ElementSet = new Element('li', {
						'class': SetClass
					});
					ElementSetCtrl = new Element('span', {
						'class': 'ctrl',
						'html': '<a href="#delete" onclick="WsLast.remove(this, \''+WsCookieLast.keys()[i]+'\')"><img src="/img/ico.trash.gif" alt="delete" /></a>'
					});
					ElementSetQuery = new Element('a', {
						'href': '/'+(Set.l != 'de'?Set.l+'_':'')+Set.q.replace(/\s+/g, '_')+'.html',
						'html': Set.q
					});
					ElementSetList = new Element('ul', {
						'class': 'similar'
					});
					for(j = 0; j < Set.s.length; j++) {
						ElementSetList.appendChild(new Element('li', {
							'class': (j == 0 ? 'first' : ''),
							'html': '<a href="/'+(Set.l != 'de'?Set.l+'_':'')+Set.s[j].replace(/\+/g, '_')+'.html">'+Set.s[j].replace(/\+/g, ' ')+'</a> '
						}));
					}
					// ElementSet.appendChild(ElementSetDate);
					ElementSet.appendChild(ElementSetCtrl);
					ElementSet.appendChild(ElementSetQuery);
					ElementSet.appendChild(ElementSetList);
					$('search_past').appendChild(ElementSet);
				}
			}
		}
	},
	drawctrl: function() {
		if($('search_past')) {
			if($('search_past_ctrl')) {
				$('search_past_ctrl').parentNode.removeChild($('search_past_ctrl'));
			}
			if(WsCookieLast.disabled()) { 
				ElementCtrl = new Element('p', {
					'id': 'search_past_ctrl', 
					'html': '<a href="#enable" onclick="WsLast.enable(); return false">Aktivieren</a>'
				});
			} else {
				ElementCtrl = new Element('p', {
					'id': 'search_past_ctrl', 
					'html': '<a href="#disable" onclick="WsLast.disable(); return false">Deaktivieren</a> | <a href="#delete" onclick="WsLast.empty(); return false;">Alle löschen</a>'
				});			
			}
			$('search_past').parentNode.appendChild(ElementCtrl);
		}
	},
	remove: function(obj, key) {
		while(obj.className.indexOf('main') !== 0) {
			obj = obj.parentNode;
		}
		WsCookieLast.remove(key);
		obj.parentNode.removeChild(obj);
		j = 0;
		for(i = 0; i < $('search_past').getElements('li').length; i++) {
			if($('search_past').getElements('li')[i].className.test("^main\s?")) {
				if((j % 2) == 0) {
					$('search_past').getElements('li')[i].className = 'main hilight';
				} else {
					$('search_past').getElements('li')[i].className = 'main';
				}
				j++;
			}
		}
	},
	disable: function() {
		WsCookieLast.disable();
		for(i = 0; i < $('search_past').getElementsByTagName('*').length; i++) {
			$('search_past').removeChild($('search_past').lastChild);
		}
		this.drawctrl();
	},
	enable: function() {
		WsCookieLast.enable();
		$('search_past_ctrl').parentNode.removeChild($('search_past_ctrl'));
		this.draw();
		this.drawctrl();
	},
	empty: function() {
		WsCookieLast.empty();
		while($('search_past').lastChild) {
			$('search_past').removeChild($('search_past').lastChild);
		}
	}
});
Fav = new Class({
	initialize: function() {
		this.draw();
	},
	drawed: false,
	draw: function() {
		if($('favourites')) {
			if(WsCookieFav.disabled() === false) {
				for(i = 0; i < WsCookieFav.keys().length; i++) {
					Key = WsCookieFav.keys()[i];
					Set = WsCookieFav.get(Key);
					Set.purl = Set.u;
					ElementSet = new Element('li', {
						'id': Key,
						'html': '<div class="favourite"><a class="list_link" href="' + Set.u + '" onclick="window.open(this.href)"><img src="http://www.mr-thumb.de/kunterbunt.php?url=' + Set.purl + '&size=120" border="0" alt="' + Set.t + '" /></a></div>'
					});
					ElementSet.onmouseover = function(e) {
						if(!e) e = window.event;
						WsFav.tooltip('show', e, this);
					}
					ElementSet.onmouseout = function(e) {
						if(!e) e = window.event;
						WsFav.tooltip('hide', e, this);
					}
					$('favourites').getElement('ul').insertBefore(ElementSet, $('favourites').getElement('ul').lastChild);
				}
				this.drawed = true;
			} else {
				$('favourites').className = 'disabled';
			}
		}
	},
	remove: function(obj, key) {
		obj.parentNode.removeChild(obj);
		WsCookieFav.remove(key);
	},
	addcheck: false,
	add: function() {
		if(!this.addcheck) {
			if(WsCookieFav.disabled()) {
				this.hide();
			}
			ElementSet = new Element('li', {
				'html': '<form action="javascript:WsFav.send()" id="favouriteform" name="favouriteform" method="post"><input type="text" class="text" value="Titel" name="favname" /> <input type="text" class="text" value="URL" name="favurl" /> <input type="image" src="/img/btn.save.gif" value="speichern" /> <a href="#cancel" onclick="WsFav.cancel(); return false"><img src="/img/btn.cancel.gif" /></a></form>'
			});
			$('favourites').getElement('ul').insertBefore(ElementSet, $('favourites').getElement('ul').lastChild);
			this.addcheck = true;
		}
	},
	addObj: function() {
		
	},
	cancel: function() {
		if($('favouriteform')) {
			$('favourites').getElement('ul').removeChild($('favouriteform').parentNode);
			this.addcheck = false;
		}
	},
	send: function() {
		formelem = document.forms['favouriteform'].elements;
		if(formelem['favurl'].value != 'URL' && formelem['favname'].value != 'Titel') {
			if(formelem['favurl'].value.indexOf('http://') != 0) {
				temp = 'http://';
				temp += formelem['favurl'].value;
				formelem['favurl'].value = temp;
			}
			Key = WsCookieFav.add({'t':formelem['favname'].value, 'u':formelem['favurl'].value});
			Set = WsCookieFav.get(Key);
			Set.purl = Set.u;
			ElementSet = new Element('li', {
				'id': Key,
				'html': '<div class="favourite"><a class="list_link" href="' + Set.u + '" onclick="window.open(this.href); return false;"><img src="http://www.mr-thumb.de/kunterbunt.php?url=' + Set.purl + '&size=120" border="0" /></a></div>'
			});
			ElementSet.onmouseover = function(e) {
				if(!e) e = window.event;
				WsFav.tooltip('show', e, this);
			}
			ElementSet.onmouseout = function(e) {
				if(!e) e = window.event;
				WsFav.tooltip('hide', e, this);
			}
			$('favouriteform').parentNode.parentNode.replaceChild(ElementSet, $('favouriteform').parentNode);
			this.addcheck = false;
		}
	},
	hide: function() {
		if(WsCookieFav.get('is_disabled') == true) {
			WsCookieFav.set('is_disabled', false);
			$('favourites').className = 'enabled';
			if(!this.drawed) {
				this.draw();
			}
		} else {
			WsCookieFav.set('is_disabled', true);
			$('favourites').className = 'disabled';
		}
	},
	edit: function() {
		
	},
	tooltip: function(tstatus, tevent, tobj) {
		if(tstatus == 'hide') {
			EventTarget = tevent.relatedTarget || tevent.toElement;
			Targets = new Array();
			Targets.extend(tobj.getElementsByTagName('*'));
			Targets.extend($('tooltip').getElementsByTagName('*'));
			if(Targets.contains(EventTarget) === false) {
				$('tooltip').style.display = 'none';
			}
		} else if(tstatus == 'show') {
			$('tooltip').style.display = 'block';
			$('tooltip').style.position = 'absolute';
			$('tooltip').onmouseout = function(e) {
				if(!e) e = window.event;
				WsFav.tooltip('hide', e, this);
			}
			Position = tobj.getCoordinates();
			$('tooltip').style.left = (Position.left + 85) + 'px';
			$('tooltip').style.top = (Position.top - 15) + 'px';
			Key = tobj.id;
			Data = WsCookieFav.get(Key);
			$('tooltiptext').className = 'text';
			$('tooltiptext').innerHTML = Data.t.length > 40 ? (Data.t.substr(0, 40) + '...') : Data.t;
			$('tooltiptext').setAttribute('href', Data.u);
			$('tooltipdelete').innerHTML = 'löschen';
			$('tooltipedit').innerHTML = 'editieren';
			$('tooltipedit').style.display = 'none';
			$('tooltipdelete').onclick = function(e) {
				WsFav.tooltip('confirm', e, tobj);
			}
			$('tooltipedit').onclick= function(e) {
				// editieren...
			}
		} else if(tstatus == 'confirm') {
			$('tooltiptext').className = 'confirmation';
			$('tooltiptext').innerHTML = 'Wollen Sie den Favoriten wirklich löschen?';
			$('tooltipdelete').innerHTML = 'nein';
			$('tooltipdelete').onclick = function(e) {
				WsFav.tooltip('show', e, tobj);
			}
			$('tooltipedit').innerHTML = 'ja';
			$('tooltipedit').style.display = 'inline';
			$('tooltipedit').onclick = function(e) {
				WsFav.remove(tobj, tobj.id);
				$('tooltip').style.display = 'none';
			}
		}
	}
})
mediatainer = function() {
	window.open('http://www.mediatainer-newsticker.de/publisher/content/mediatainer/Impressum.html', 'Mediatainer','width=850,height=500,scrollbars=yes,menubar=no,location=no');
}
window.addEvent('load', function() {
	WsCookieFav = new WsCookie(new Hash.Cookie('WsF', {duration: 365}));
	WsCookieLast = new WsCookie(new Hash.Cookie('WsL', {duration: 365}));
	WsLast = new Last();
	WsFav = new Fav();
	document.forms[0].elements['query'].focus();
});