window.addEvent('domready', function() {

	// motor search forms behaviour
	var motorBrandModelForm = $('form_motor_brand_model');
	var brandSelect = $('brand');
	var modelSelect = $('model');

	if (brandSelect) {
		brandSelect.addEvent('change', function() {
			motorBrandModelForm.submit();
		});
	}

	if (modelSelect) {
		modelSelect.addEvent('change', function() {
			motorBrandModelForm.submit();
		});
	}

	var motorCodeForm = $('form_motor_code');
	var motorCodeInput = $('motor_code');

	if (motorCodeInput) {
		motorCodeInput.addEvent('focus', function() {
			if (this.value == this.defaultValue) {
				this.value = '';
			}
		});
	}

	if (motorCodeInput) {
		motorCodeInput.addEvent('blur', function() {
			if (this.value == '') {
				this.value = this.defaultValue;
			}
		});
		motorCodeInput.addEvent('change', function() {
			motorCodeForm.submit();
		});
	}

	// add images overlay
	var images = ($('cnr-ctt-main').getElements('.cnr-ctt-motorbox img').combine($$('div.image').getElements('img'))).flatten();

	images.each( function(image) {

		var imageHeight = image.getSize().y - 2; // minus 2px border

		var overlayImage = new Element('img', {
			'src': '/layouts/asiamotoren/images/watermark.png',
			'class': 'overlay',
			'height': imageHeight,
			'styles': {
				'margin-top': -imageHeight - 3 // margin-bottom 3px
			}
		});
		overlayImage.inject(image, 'after');
	});
});
