var GWE = new Object();
// !GWE.Options
	GWE.Options = new Object({
		Animation: {
			duration:	.5
		},
		Links: ['a.arrow']
	});
// !GWE.Link
	GWE.Link = Class.create({
		initialize: function(el) {
			this.element	= $(el).insert({ bottom: '<img class="arrow-img" src="/img/a-bg.gif" alt="" />' });
		}
	});
// !GWE.Menu
	GWE.Menu = Class.create({
		initialize: function(el) {
			this.elements			= new Object();
			this.elements.menu		= typeof el == "string" ? $(el) : el;
			this.elements.actives	= $$('#' + this.elements.menu.id + '>li.active') || 0;
			this.elements.lis		= $$('#' + this.elements.menu.id + '>li');
			this.elements.lis.invoke('observe', 'mouseover', this.mouseover.bind(this));
			document.observe('mouseover', this.menuout.bind(this));
		},
		mouseover: function(ev) {
			var element	= Event.element(ev);
			var hovered	= element.hasClassName('hover') ? element : element.up('.hover');
			if (!hovered) {
				$$('#' + this.elements.menu.id + '>li.hover').invoke('removeClassName', 'hover');
				if (element.nodeName == "LI")
					element.addClassName('hover');
				else element.up('li').addClassName('hover');
			}
		},
		menuout: function(ev) {
			var element	= Event.element(ev);
			if (!element.descendantOf(this.elements.menu)) {
				$$('#' + this.elements.menu.id + '>li.hover').invoke('removeClassName', 'hover');
				if (this.elements.actives)
					this.elements.actives.invoke('addClassName', 'hover');
			}
		}
	});
// !GWE.Search
	GWE.Search = Class.create({
		initialize: function(el) {
			this.elements			= new Object();
			this.elements.form		= typeof el == "string" ? $(el) : el;
			this.elements.search	= $('searchterm');
			this.elements.search.observe('focus', this.focus.bind(this));
			this.elements.search.observe('blur', this.blur.bind(this));

			/*new Ajax.Autocompleter('searchterm', 'autocomplete_choices', this.elements.form.action, {
				minChars: 2,
				afterUpdateElement: function() {
					alert (this);
				}
			});*/
		},
		focus: function() {
			if (this.elements.search.value == GWE.Options.Values.Searchterm[GWE.Options.Language])
				this.elements.search.value = '';
		},
		blur: function() {
			if (this.elements.search.value.empty())
				this.elements.search.value = GWE.Options.Values.Searchterm[GWE.Options.Language];
		}
	});
// !GWE.Toplink
	GWE.Toplink = Class.create({
		initialize: function(el) {
			this.element		= $(el);
			this.element.href	= 'javascript:void(0)';
			this.element.observe('click', this.scrollTop);
		},
		scrollTop: function() {
			new Effect.ScrollTo('container', { duration: GWE.Options.Animation.duration });
			return false;
		}
	});
// !GWE.Textarea
	GWE.Textarea = Class.create({
		initialize: function(el) {
			this.element		= $(el).insert({ 'after': '<span class="remaining" style="display: none;"></span>' });
			this.element.rem	= this.element.next('.remaining').hide();

			this.element.observe('focus', this.focus.bind(this));
			this.element.observe('paste', this.update.bind(this));
			this.element.observe('input', this.update.bind(this));
			this.element.observe('keyup', this.update.bind(this));
			this.element.observe('blur', this.blur.bind(this));
		},
		focus: function() {
			this.update();
			new Effect.Appear(this.element.rem, { duration: GWE.Options.Animation.duration });
		},
		update: function() {
			var template	= new Template(GWE.Options.Values.Remaining[GWE.Options.Language]);
			var maxlength	= $w(this.element.className).last().substr(3);
			var remaining	= maxlength - this.element.value.length;
			this.element.rem.update(template.evaluate({ remaining: remaining, maxlength: maxlength }));

			if (remaining < 1)
				this.element.value = this.element.value.substring(0, maxlength);
		},
		blur: function() {
			new Effect.Fade(this.element.rem, { duration: GWE.Options.Animation.duration });
		}
	});
// !GWE.Summe
	GWE.Amount = Class.create({
		initialize: function(el) {
			this.element		= $(el);
			this.element.adds	= $$('input.summand');
			//this.element.adds.invoke('observe', 'blur', this.update.bind(this));
			this.element.adds.invoke('observe', 'focus', this.update.bind(this));
			this.element.adds.invoke('observe', 'input', this.update.bind(this));
			this.element.adds.invoke('observe', 'paste', this.update.bind(this));
			this.element.adds.invoke('observe', 'keyup', this.update.bind(this));
			this.update();
		},
		update: function() {
			var summe	= new Number(0);
			for (var i = 0; i < this.element.adds.length; i++) {
				// check value on non-alpha
				if (!isNaN(parseInt(this.element.adds[i].value)))
					this.element.adds[i].value = parseInt(this.element.adds[i].value);

				// make sum
				if (parseInt(this.element.adds[i].value))
					summe = (summe + parseInt(this.element.adds[i].value));
				else this.element.adds[i].value = '';
			}
			this.element.value	= summe;
		}
	});
// !GWE.Applicanter
	GWE.Applicanter = Class.create({
		initialize: function(opts) {
			this.elements				= new Object();
			this.elements.applicants	= $A(opts.elements);
			this.elements.affected		= opts.affected || 0;
			this.elements.add			= opts.add.observe('click', this.add.bind(this));
			this.elements.remove		= opts.remove.observe('click', this.remove.bind(this)).hide();

			$$('.noscript').invoke('hide').invoke('removeClassName', 'noscript');
			this.elements.add.href = this.elements.remove.href = 'javascript: void(0);';
			this.elements.add.show();
		},
		add: function() {
			var toShow	= this.elements.applicants.findAll(function(a) {
				return !a.visible();
			});

			if (toShow.size() > 0) {
				if (!this.elements.add.visible() && toShow.size() < this.elements.applicants.size())
					new Effect.Appear(this.elements.add, { duration: GWE.Options.Animation.duration });
				if (!this.elements.remove.visible())
					new Effect.Appear(this.elements.remove, { duration: GWE.Options.Animation.duration });

				if (this.elements.affected)
					new Effect.Appear($(this.elements.affected + toShow.first().id), { duration: GWE.Options.Animation.duration });
				new Effect.Appear($(toShow.first()), { duration: GWE.Options.Animation.duration });
			}
			if (toShow.size() <= 1)
				new Effect.Fade(this.elements.add, { duration: GWE.Options.Animation.duration });
		},
		remove: function() {
			var toHide	= this.elements.applicants.findAll(function(a) {
				return a.visible();
			});

			if (toHide.size() > 0) {
				if (!this.elements.add.visible())
					new Effect.Appear(this.elements.add, { duration: GWE.Options.Animation.duration });
				if (toHide.size() == 1)
					new Effect.Fade(this.elements.remove, { duration: GWE.Options.Animation.duration });

				if (this.elements.affected)
					new Effect.Fade($(this.elements.affected + toHide.last().id), { duration: GWE.Options.Animation.duration });
				new Effect.Fade($(toHide.last()), { duration: GWE.Options.Animation.duration });
			} else new Effect.Fade(this.elements.remove, { duration: GWE.Options.Animation.duration });
		}
	});
// !GWE.Blogger
	GWE.Blogger = Class.create({
		initialize: function(form, opts) {
			this.options			= opts || 0;
			this.options.disableVal	= opts.disableVal || GWE.Options.Values.BlogCat;
			this.options.enableVal	= opts.enableVal || GWE.Options.Values.BlogSend;
			this.elements			= new Object();
			this.elements.form		= form;
			this.elements.form.id	= 'edit';
			this.elements.sel		= $$('select.select_blog').first().observe('change', this.changePid.bind(this)).observe('focus', this.changePid.bind(this));
			this.elements.sub		= $$('input.sendBlogPost').first();
			this.elements.sub.rel	= this.elements.sub.disable().value;
			this.elements.sub.value	= this.options.disableVal[GWE.Options.Language];
			this.elements.del		= $$('input.deleteBlogPost').first();
			this.elements.del.rel	= this.elements.del.disable().value;
			this.elements.del.value	= this.options.disableVal[GWE.Options.Language];
			this.elements.target	= $$('input[name="' + this.options.targetId + '"]').first();
			this.changePid();
/*
			var target				= this.elements.target.value;
			if (this.elements.target.value)
				$$('select.select_blog option').each(function(o) {
					if (o.readAttribute('value') == target) {
						o.selected = true;
						throw $break;
					}
				});
*/
		},
		changePid: function() {
			if (this.elements.sel.options[this.elements.sel.selectedIndex].value) {
				if (this.elements.target)
					this.elements.target.value		= this.elements.sel.options[this.elements.sel.selectedIndex].value;
				this.elements.sub.enable().value	= this.elements.sub.rel;
				this.elements.del.enable().value	= this.elements.del.rel;
			} else {
				if (this.elements.target)
					this.elements.target.value		= this.elements.sel.options[this.elements.sel.selectedIndex].value;
				this.elements.sub.disable().value	= this.options.disableVal[GWE.Options.Language];
				this.elements.del.disable().value	= this.options.disableVal[GWE.Options.Language];
			}
			return false;
		}
	});
// !GWE.BlogPreview
	GWE.BlogPreview = Class.create({
		initialize: function(article, opts) {
			this.options			= opts || 0;
			this.elements			= new Object();
			this.elements.article	= $(article).hide();
			this.elements.formular	= this.options.button.up('form');
			this.elements.button	= this.options.button.observe('click', this.buildPreview.bind(this));
		},
		buildPreview: function() {
			this.elements.formular.getElements().each((function(el) {
				if ($(this.options.preSel + el.name)) {
					if (el.name == 'date') {
						var d = new Date(parseInt(el.getValue()) * 1000);
						var e = [
							d.getDate(),
							d.getMonth() < 9 ? '0' + (d.getMonth() + 1) : d.getMonth() + 1,
							d.getFullYear()
						];
						var f = [
							d.getHours() < 10 ? '0' + d.getHours() : d.getHours(),
							d.getMinutes() < 10 ? '0' + d.getMinutes() : d.getMinutes()
						];
						$(this.options.preSel + el.name).update(e.join('.') + ' ' + f.join(':'));
					} else if (el.name == 'text')
						$(this.options.preSel + el.name).update(el.next('.mceEditor').select('iframe').first().contentWindow.document.getElementsByTagName('body')[0].innerHTML);
					else if (el.name == 'singleSRC') {
						var value = el.readAttribute('value');
						var target = $(this.options.preSel + el.name);
						if (value && !value.blank())
							target.show().src = el.readAttribute('value');
						else target.hide();
					} else $(this.options.preSel + el.name).update(el.getValue());
				}
			}).bind(this));

			this.elements.article.show()
		}
	});
// !GWE.Language
	GWE.Language = Class.create({
		initialize: function(select) {
			this.elements			= new Object();
			this.elements.select	= $(select).observe('change', this.changeLanguage.bind(this));
			this.elements.submit	= this.elements.select.next('.submit').hide();
			this.elements.form		= this.elements.select.up('form');
		},
		changeLanguage: function() {
			this.elements.form.submit();
		}
	});
// !GWE.Teaser
	GWE.Teaser = Class.create({
		initialize: function(list, extender) {
			this.elements			= new Object();
			this.elements.list		= $(list);
			this.elements.extender	= extender ? extender : this.elements.list.next('a.extender');
			this.elements.expander	= this.elements.list.select('a.expander');
			this.elements.toextend	= this.elements.list.select('.toextend.noscript').invoke('hide').invoke('removeClassName', 'noscript').reverse();

			this.elements.extender.visible	= false;
			this.elements.list.select('.toextend').each(function(te) { te.visible	= false; });
			this.elements.list.select('.expandable').invoke('hide').invoke('removeClassName', 'noscript');
			this.elements.extender.show().removeClassName('invisible').observe('click', this.extend.bind(this));
			this.elements.expander.invoke('observe', 'click', this.expand.bind(this));
		},
		extend: function() {
			Effect.multiple(
				this.elements.toextend.reverse(),
				this.elements.extender.visible ? Effect.Fade : Effect.Appear,
				{ speed: .3 }
			);
			this.elements.extender.visible = this.elements.extender.visible ? false : true;
		},
		expand: function(ev) {
			var element		= ev.element();
				element.stopObserving();
			var parent		= element.up('.toextend');
			var toexpand	= parent.select('.expandable');
			if (!parent.visible) {
				element.select('.dots').invoke('hide');
				Effect.multiple(toexpand, Effect.Appear, {
					speed: 0,
					afterFinish: (function() {
						element.observe('click', this.expand.bind(this));
						parent.visible	= true;
					}).bind(this)
				});
			} else {
				element.select('.dots').invoke('show');
				Effect.multiple(toexpand, Effect.Fade, {
					speed: 0,
					afterFinish: (function() {
						element.observe('click', this.expand.bind(this));
						parent.visible	= false;
					}).bind(this)
				});
			}
		}
	});
// !GWE.Page
	GWE.Page = Class.create({
		initialize: function() {
			this.elements			= new Object();
			this.elements.menu		= new GWE.Menu('menu') || 0;
			this.elements.toplink	= new GWE.Toplink('top') || 0;
			this.elements.search	= new GWE.Search('searchform') || 0;

			if ($('partnerinstitution_hinzufuegen'))
				this.elements.applicanter	= new GWE.Applicanter({
					elements:	[$('zweite_partnerinstitution')],
					remove:		$('remove_partnerinstitution'),
					add:		$('add_partnerinstitution')
				});

			if ($('antragsteller_hinzufuegen'))
				this.elements.applicanter	= new GWE.Applicanter({
					elements:	[$('antragsteller3'), $('antragsteller4')],
					affected:	'taetigkeit_',
					remove:		$('remove_antragsteller'),
					add:		$('add_antragsteller')
				});

			if ($$('input.hidden').length > 0)
				$$('input.hidden').invoke('disable').invoke('hide');

			if ($$('form.edit').length > 0)
				this.elements.blogger = new GWE.Blogger($$('form.edit').first(), {
					targetId:	'pid',
					disableVal:	GWE.Options.Values.BlogCat,
					enableVal:	GWE.Options.Values.BlogSend
				});

			if ($('blog-preview'))
				this.elements.blogPreview = new GWE.BlogPreview('blog-preview', {
					'button': $('makePreview'),
					'preSel': 'target-'
				});
/*
			if ($('f14'))
				this.elements.blogger	= new GWE.Blogger('f14', {
					targetId:	'project',
					disableVal: GWE.Options.Values.EnclosureCat,
					enableVal: GWE.Options.Values.EnclosureSend
				});
*/
			if ($$('.extendable').length > 0)
				this.elements.teaser	= $$('.extendable').collect((function(list) {
					if (handler = list.next('a.extender'))
						return new GWE.Teaser(list, handler);
				}).bind(this));

			if ($$('input.summe').length > 0)
				this.elements.summe		= new GWE.Amount($$('input.summe').first());

			if ($('lang'))
				this.elements.language	= new GWE.Language('lang');

			if ($$('input.avatarfile').length > 0)
				$$('input.avatarfile').first().writeAttribute({ size: 25 });

			if ($$('textarea').length > 0)
				this.elements.textares	= $$('textarea').collect((function(ta) {
					if (ta.className.indexOf('max') != -1)
						return new GWE.Textarea(ta);
				}).bind(this));

			if (Prototype.Browser.IE && (navigator.appVersion.substr(22,1) < 8)) {
				this.elements.links	= $$(GWE.Options.Links.join(', '));
				this.elements.links.each((function(link, index) {
					this.elements.links[index] = new GWE.Link(link);
				}).bind(this));
			}
		}
	});

var TinyCallback = {
	cleanXHTML: function(f,d) {
		var e = "";
		var b = d.match(/<a[^>]*>/gi);
		if (b!=null) {
			for (var c = 0; c < b.length; c++){
				e = b[c].replace(/target="_blank"/gi,'onclick="window.open(this.href); return false;"');
				d = d.replace(b[c],e)
			}
		}
		return d.replace(/<br>/,"<br />")
	},
	cleanHTML: function(b,a) {
		a = a.replace(/<br \/>/,"<br>");
		a = a.replace(/^\s*/ig,"");
		return a
	}
};
