var Rostloch = {
	suchbegriff: 'Suchbegriff / Teilenummer',

	submit: function() {
		with ($('query-keywords')) {
			value = (value == Rostloch.suchbegriff) ? '' : value;
		}
			
		return true;
	},

	init: function() {
		var suchbegriff = Rostloch.suchbegriff;

		$('query-select').addEvent('submit', Rostloch.submit);

		with ($('query-keywords')) {
			if (value == '' || value == suchbegriff) {
				value = suchbegriff;
				setStyle('color', '#AFAF99');
			}
			addEvents({
				'focus': function() {
					if (this.value == suchbegriff) {
						this.value = '';
						this.setStyle('color', '#330');
					}
				},
				'blur': function() {
					if (this.value == '' || this.value == suchbegriff) {
						this.value = suchbegriff;
						setStyle('color', '#AFAF99');
					}
					else {
						this.setStyle('color', '#330');
					}
				}
			});
		}

		$('query-fabrikant').onchange = function() {
			$('query-model').selectedIndex = 0;
			$('query-group').selectedIndex = 0;
			Rostloch.submit();
			this.form.submit();
		};

		$('query-model').onchange = function() {
			$('query-group').selectedIndex = 0;
			Rostloch.submit();
			this.form.submit();
		};
		
		$('query-group').onchange = function() {
			Rostloch.submit();
			this.form.submit();
		};

		if ($('query-payment')) {
			$('query-payment').onchange = function() {
				new XHR({
					method: 'get'
				}).send('/set?f=payment&i=' + $('query-payment').value);
			};
		}

		$$('.iconimage').each(function(obj) {
			obj.getParent().onclick = function() {
				return false;
			};
			obj.addEvent('click', function() {
				var tr_parent = this.getParent().getParent().getParent();
				if (tr_parent.getNext()) {
					if (tr_parent.getNext().hasClass('trimg')) {
						tr_parent.getNext().remove();
						return
					}
				}

				tr = new Element('tr', {
					'class': 'trimg ' + tr_parent.className
				})
				.adopt(
					new Element('td', {
						'colspan': 6
					}).adopt(
						new Element('img', {
							'src': this.getParent().href,
							'alt': '',
							'class': 'imgbig'
						})
					).adopt(
					new Element('p', {
						'class': 'imghint'
					}).setHTML('Abbildung kann vom Original abweichen.')
				)				).injectAfter(tr_parent);
			})
			.setStyle('cursor', 'pointer')
			.setProperty('title', 'Bild ein-/ausblenden');
		});
		// $$('#checkout .text').setProperty('readOnly', true);

		$$('#administrator_users_list .selectable')
			.addEvents({
				'click': function() {
					location.href = 'edit?id=' + this.id;
				},
				'mouseover': function() {
					this.addClass('hover');
				},
				'mouseout': function() {
					this.removeClass('hover');
				}
			})
			.setStyle('cursor', 'pointer');

		if ($('delivery_is_billing')) {
			$('delivery_is_billing').addEvent('click', function() {
				$('delivery_address').setStyle('display', $('delivery_is_billing').checked ? 'none' : 'block');
			});
		}

		if ($('query-payment')) {
			if ($('sofortueberweisung-note')) {
				$('sofortueberweisung-note').setStyle('display', $('query-payment').selectedIndex == 0 ? 'block' : 'none');
			}
			$('query-payment').addEvent('change', function() {
				if ($('sofortueberweisung-note')) {
					$('sofortueberweisung-note').setStyle('display', $('query-payment').value == 1 ? 'block' : 'none');
				}
			});
		}
	}
};

window.addEvent('domready', Rostloch.init);
