var WHGG = {

	init: function(){
		if ($$('.votingtrigger')) WHGG.openVoting();
		if ($$('.friendtrigger')) WHGG.sendAFriend();
		//WHGG.setMaxLengthEvents();
	},

	load: function(){
		if(fieldsToHighlight.length > 0 && fieldsToHighlight[0] != '') WHGG.highlightErrors();
		if(fieldsToHighlight.length > 0 && fieldsToHighlight[0] != '') WHGG.resetErrors();
	},

	setMaxLengthEvents: function() {
		$('grund').addEvent('blur', function (){
		if(activeAlert == false) {
			WHGG.countmax();
		}
		})
	},

	countmax: function() {
		max = 300;
		wert = 0+$('grund').get('value').length;
		if (wert > (max-1))
		{
			alert("Es dürfen nicht mehr als " + max + " Zeichen eingegeben werden!");
			grund = $('grund').get('value').substring(0,max);
			$('grund').value = grund;
		}
	 },


	highlightErrors: function(){
		fieldsToHighlight.each(function(item){
			if(item=='einverstaendnis') {
				var myFx = new Fx.Tween($('div-einverstaendnis'));
				myFx.start('background-color', '#fff', '#E2001A');
				myFx.set('width', '21px');
				myFx.set('height', '23px');
			} else {
				var myFx = new Fx.Tween(item);
				myFx.start('background-color', '#fff', '#E2001A');
				myFx.set('color', '#fff');
			}
		});
	},

	resetErrors: function(){
		fieldsToHighlight.each(function(item){
			if(item!='einverstaendnis') {
				$(item).addEvent('click', function (){
					var myFx = new Fx.Tween(item);
					if($(item).getProperty('type')=='select-one')
					{
						myFx.start('background-color', '#E2001A', '#E5E5E5');
						myFx.set('color', '#303030');
						this.removeEvents('click');
					} else {
						myFx.start('background-color', '#E2001A', '#fff');
						myFx.set('color', '#303030');
						this.removeEvents('click');
					}
				})
			} else {
				$('einverstaendnis').addEvent('click', function (){
					var myFx = new Fx.Tween('div-einverstaendnis');
					myFx.start('background-color', '#E2001A', '#fff');
					myFx.set('color', '#303030');
					this.removeEvents('click');
				})
			}
		});
	},

	openVoting: function(){
		var height = 0;
		$$('.votingtrigger').each(function(el){
			el.addEvent('click', function (){
				id	 = this.get('id').replace('gg_', '');

				var myChain = new Chain();

				myChain.chain(
					function prepare(){
						if (!$('lightboxBg')) {
							if (Browser.Engine.trident && Browser.Engine.version <= 4) {
								var background = new Element('div', {
									'id': 'lightboxBg',
									'styles': {
										'opacity': .8,
										'width': window.document.getCoordinates().width,
										'height': window.document.getScrollSize().y
									},
									'events': {
										'click': function(){
											WHGG.closeLightbox();
										}
									}
								}).inject($('WeberHaus'), 'top');

								var content = new Element('div', {
									'id': 'lightboxSmall',
									'styles': {
										'visibility': 'hidden',
										'left': document.window.getCoordinates().width/2-190
									},
									'html': '<div class="head"></div><div id="lightboxContent" class="body group"></div><div class="foot"></div>'
								}).inject($('WeberHaus'), 'top');
							} else {
								var background = new Element('div', {
									'id': 'lightboxBg',
									'styles': {
										'opacity': .8
									},
									'events': {
										'click': function(){
											WHGG.closeLightbox();
										}
									}
								}).inject($('WeberHaus'), 'top');

								var content = new Element('div', {
									'id': 'lightboxSmall',
									'styles': {
										'visibility': 'hidden',
										'left': document.window.getCoordinates().width/2-190
									},
									'html': '<div class="head"></div><div id="lightboxContent" class="body group"></div><div class="foot"></div>'
								}).inject($('WeberHaus'), 'top');
							}
						}
					},
					function makeRequestAndShow(){
						var plan = new Request.HTML({
							url: 'index.php?type=306&action=getCaptcha&ggId='+id+'&gg_sort_type='+$('sort_type').get('value'),
							method: 'get',
							evalScripts: true,
							update: $('lightboxContent'),
							onSuccess: function(){
								function show(){
									(function(){
										if ($('lightboxSmall')) {
											lHeight = $('lightboxSmall').getCoordinates().height;

											var morph = new Fx.Morph('lightboxSmall', {
												duration: 1000,
												transition: Fx.Transitions.Elastic.easeOut
											});

											if (Browser.Engine.trident && Browser.Engine.version <= 4) {
												morph.start({
													'top': (document.window.getScrollSize().y/2 + (document.window.getScroll().y/2))-(lHeight/2)*2,
													'opacity': [0, 1]
												});
											} else {
												morph.start({
													'top': (document.window.getCoordinates().height/2)-(lHeight/2),
													'opacity': [0, 1]
												});
											}

											morph.onComplete = function(){
												morph.cancel();
												WeberHaus.compatibility();
											}
										}
									}).delay(200);
								}

								window.addEvent('load', show());
							}
						}).get();
					}
				);

				myChain.callChain();
				myChain.callChain();
			})
		});
	},

	sendAFriend: function(){
		var height = 0;
		$$('.friendtrigger').each(function(el){
			el.addEvent('click', function (){
				id	 = this.get('id').replace('gg_', '');

				var myChain = new Chain();

				myChain.chain(
					function prepare(){
						if (!$('lightboxBg')) {
							if (Browser.Engine.trident && Browser.Engine.version <= 4) {
								var background = new Element('div', {
									'id': 'lightboxBg',
									'styles': {
										'opacity': .8,
										'width': window.document.getCoordinates().width,
										'height': window.document.getScrollSize().y
									},
									'events': {
										'click': function(){
											WHGG.closeLightbox();
										}
									}
								}).inject($('WeberHaus'), 'top');

								var content = new Element('div', {
									'id': 'lightbox',
									'styles': {
										'visibility': 'hidden',
										'left': document.window.getCoordinates().width/2-470
									},
									'html': '<div class="head"></div><div id="lightboxContent" class="body group"></div><div class="foot"></div>'
								}).inject($('WeberHaus'), 'top');
							} else {
								var background = new Element('div', {
									'id': 'lightboxBg',
									'styles': {
										'opacity': .8
									},
									'events': {
										'click': function(){
											WHGG.closeLightbox();
										}
									}
								}).inject($('WeberHaus'), 'top');

								var content = new Element('div', {
									'id': 'lightbox',
									'styles': {
										'visibility': 'hidden',
										'left': document.window.getCoordinates().width/2-470
									},
									'html': '<div class="head"></div><div id="lightboxContent" class="body group"></div><div class="foot"></div>'
								}).inject($('WeberHaus'), 'top');
							}
						}
					},
					function makeRequestAndShow(){
						var plan = new Request.HTML({
							url: 'index.php?type=308&ggId='+id,
							method: 'get',
							evalScripts: true,
							update: $('lightboxContent'),
							onSuccess: function(){
								function show(){
									(function(){
										if ($('lightbox')) {
											lHeight = $('lightbox').getCoordinates().height;

											var morph = new Fx.Morph('lightbox', {
												duration: 1000,
												transition: Fx.Transitions.Elastic.easeOut
											});

											if (Browser.Engine.trident && Browser.Engine.version <= 4) {
												morph.start({
													'top': (document.window.getScrollSize().y/2 + (document.window.getScroll().y/2))-(lHeight/2)*2,
													'opacity': [0, 1]
												});
											} else {
												morph.start({
													'top': (document.window.getCoordinates().height/2)-(lHeight/2),
													'opacity': [0, 1]
												});
											}

											morph.onComplete = function(){
												morph.cancel();
												WeberHaus.compatibility();
											}
										}
									}).delay(200);
								}

								window.addEvent('load', show());
							}
						}).get();
					}
				);

				myChain.callChain();
				myChain.callChain();
			})
		});
	},
	
	closeLightbox: function(){
		[$('lightboxBg'), $('lightboxSmall'), $('lightbox')].each(function(el){ if(el) { el.dispose(); } });
	},

	sendCaptcha: function(){
            /*
		sendIt = new Request({
			url: 'index.php?type=306&ggId=' + $('gg_id').get('value') + '&action=sendCaptcha&cr=' + $('captcha_response').get('value'),
			method: 'get',
			onSuccess: function(txt,xmlTxt) { WHGG.checkCaptcha(txt); }
		}).send();
                */
		sendIt = new Request.JSON({
			url: 'index.php',
			method: 'get',
            noCache: true,
			onSuccess: function(responseJSON,responseText) { WHGG.checkCaptcha(responseJSON,responseText); }
		}).get({'type': '306', 'ggId': $('gg_id').get('value'), 'action': 'sendCaptcha', 'cr': $('captcha_response').get('value'), 'gg_sort_type':$('gg_sort_type').get('value')});
	},

	sendFriendCaptcha: function(){
		sendIt = new Request.JSON({
			url: 'index.php',
			method: 'get',
            noCache: true,
			onSuccess: function(responseJSON,responseText) { WHGG.checkFriendCaptcha(responseJSON,responseText); }
		}).get({'type': '308', 'ggId': $('gg_id').get('value'), 'action': 'sendFriendCaptcha', 'cr': $('captcha_response_friend').get('value'), 'emails':$('emails').get('value'), 'name':$('name').get('value'), 'email':$('email').get('value')});
	},
	
	checkFriendCaptcha: function(a,b) {
		var obj = JSON.decode(b);
		if(obj.code=='1') {
			if ($('lightboxContent')) {
			 $('lightboxContent').empty().adopt(
				 new Element(
								'div',
								{
									id: 'captcha-success',
									html: obj.htmlsource
								}
							)
			 );
			}

		} else if(obj.code=='3') {
			alert('Leider ist ein Fehler aufgetreten. versuchen Sie es bitte noch einmal.');
		} else if(obj.code=='2') {
			WHGG.wrongFriendCaptcha(obj.htmlsource);
		} else {
			alert('Leider ist ein Fehler aufgetreten. versuchen Sie es bitte noch einmal.');
		}
	},

	checkCaptcha: function(a,b) {
		var obj = JSON.decode(b);
		if(obj.code=='1') {
			if ($('gg-captcha')) {
			 $('gg-captcha').empty().adopt(
				 new Element(
								'div',
								{
									id: 'captcha-success',
									html: '<h2 class="redItalic">Vielen Dank.</h2><br />Ihre Stimme wurde gespeichert.<br /><br />'
								}
							),
				new Element('a', {
					'href': 'javascript: void(0);',
					'class': 'red',
					'html': 'Weiter',
					'text': 'Weiter',
					'events': {
						'click': function(){
							WHGG.closeLightbox();
						}
					}
				})
			 );
			}

			if(obj.uid && $('div-star-'+obj.uid)) {
				$('div-star-'+obj.uid).innerHTML = obj.stars_html;
			}

			$$('.votingtrigger').each(function(el){
				el.removeClass('votingtrigger');
				//el.removeEvent('click', '');
				el.innerHTML = 'Bereits abgestimmt';
				el.getParent().addClass('button-inaktiv');
			});

		} else if(obj.code=='3') {
			alert('Leider ist ein Fehler aufgetreten. versuchen Sie es bitte noch einmal.');
		} else if(obj.code=='2') {
			WHGG.wrongCaptcha(obj.htmlsource);
		} else {
			alert('Leider ist ein Fehler aufgetreten. versuchen Sie es bitte noch einmal.');
		}
	},
	
	wrongCaptcha: function(htmlsource) {
		if ($('lightboxSmall')) {
			$('lightboxSmall').innerHTML = '<div class="head"></div><div id="lightboxContent" class="body group">' + htmlsource + '</div><div class="foot"></div>';
		}
		//newFreeCap($('captcha_reload').get('rel'), 'Sorry, we cannot autoreload a new image. Submit the form and a new image will be loaded.');

	},
	
	wrongFriendCaptcha: function(htmlsource) {
		if ($('lightbox')) {
			$('lightbox').innerHTML = '<div class="head"></div><div id="lightboxContent" class="body group">' + htmlsource + '</div><div class="foot"></div>';
		}
	}

};

window.addEvent('domready', WHGG.init);
window.addEvent('load', WHGG.load);
