/* !Form Validator 
 * 
 * @author Mario Fischer <mario@chipwreck.de>
 * 
 * */
 
function isVisible(elem)
{
	var visible = true;
	if (!elem) {
		return false;
	}
	if (!elem.getParent()) {
		return false;
	}
 
	if (elem.getParent().getStyle('display') == 'none') {
		visible = false;
	}
	elem.getParents().each(function(el) {
		if (el.getStyle('display') == 'none') {
			visible = false;
		}
	});
	return visible;
}

var FormValidator = new Class({
 
	// initialization
	initialize: function(form_id, options)
	{
		this.options = $extend({
			errorClass: 'error',
			errorClassLabel: 'error'
		}, options || {});
 
		if (!$(form_id)) {
			// window.console.log("Form with id '"+form_id+"' not found");
			return;
		}
		
		this.myform = $(form_id);
		this.myform.addEvent('submit', this.onSubmit.bind(this));
		this.myfields = $$('#'+form_id+' input, #'+form_id+' select, #'+form_id+' textarea');
 
		// Bind events
		that = this;
		this.myfields.each(function(el) {
			el.addEvent('blur', function(e) {
				// window.console.log("adding for " + el.getProperty("name"));
				that.validate(e);
			}.bind(this));
		});
				
	},
	
	// on Submit: if valid, submit - otherwise show alert
	onSubmit: function(event)
	{
		if (event) {
			new Event(event).stop();
		}
		var nothing_visible = true;
		this.myfields.each(function(el) {
			if (isVisible(el)) {
				nothing_visible = false;
			}
		});
		if (nothing_visible) {
//			alert("1st elem invisible -> so do submit");
			this.myform.submit();
			return;
		}		
		
		if (this.validate()) {
			this.myform.submit();
		}
		else {
			alert("Bitte die Eingaben überprüfen.");
		}
	},
	
	// Validate the form
	validate: function(event)
	{
		if (event) {
			new Event(event).stop();
		}
		var okay = true;
		that = this;
		this.myfields.each(function(el) {
			
			
			if (isVisible(el) && el.getProperty("id") != 'plz' && el.getProperty("id") != 'ort') {
			
				if (el.type.toLowerCase() == 'radio') {
					var radio_okay = false;
					var radios = that.myform[el.getProperty("name")];		
					for (var i = 0; i < radios.length; i++) {
						if (radios[i].checked) {
							radio_okay = true;
						}
					}
					if (!radio_okay) {
						okay = false;
						that.markError(el, true);
					}
					else {
						that.markError(el, false);
					}
				}
				else if (el.type.toLowerCase() == 'select-one') {
					if (el.value == '') {
						that.markError(el, true);
						okay = false;
					}
					else {
						that.markError(el, false);
					}
				}
				else if (el.type.toLowerCase() == 'textarea') {
					if (el.value== '') {
						that.markError(el, true);
						okay = false;
					}
					else {
						that.markError(el, false);
					}
				}
				else if (el.value == '') {
					that.markError(el, true);
					okay = false;
				}
				else {
					that.markError(el, false);
				}
			}
		});
		if ($('successor')) {
			$('successor').removeProperty('disabled');
		}
		return okay;
	},
	
	// Mark the given element as erroneous or not
	markError: function(el, has_error) {
		var name = el.getProperty("name");
		if (has_error) {
			el.addClass(this.options.errorClass);
			if ($(name)) {
				$(name).addClass(this.options.errorClassLabel);
			}
		}
		else {
			el.removeClass(this.options.errorClass);
			if ($(name)) {
				$(name).removeClass(this.options.errorClassLabel);
			}
		}
	}
});

window.addEvent('domready',function() {
	
	var form_valid_1 = new FormValidator('fragenForm');
	// var form_valid_2 = new FormValidator('contactform');
	
	var currentItem = null;
	$$('.metaBox').set({opacity:0})
	
	$$('div#metaNavigation ul li').each(function(item){

		item.addEvent('click',function(event){
			event.stopPropagation();

			if (currentItem){
				currentItem.getElement('.metaBox').fade(0);
				currentItem.morph('.lidefault');
			}
			
			currentItem = this;
			this.morph('.liactive'); 
			this.getElement('.metaBox').fade(1);
			
		})
	})
	
	if ($defined($('closeToggler'))){
		$('closeToggler').addEvent('click', function(event){
			event.stopPropagation();
			if (currentItem){
				currentItem.getElement('.metaBox').fade(0);
				currentItem.morph('.lidefault');
			}
		});
	}
	
	$$('body')[0].addEvent('click',function(event){

		if (currentItem){
			currentItem.getElement('.metaBox').fade(0);
			currentItem.morph('.lidefault');
		}
	})

	
	
	// Columnheight sync
	syncColumnHeight();

	// Produktdetailseite minheight
	if ($defined($('productDetailBox')) && $('productButton')){
		
		var minheight = $('productDetailHeadline').getHeight() + $('productDetailInfos').getHeight() + $('productButton').getHeight() + 36;
		var containerHeight = $('productDetailBox').getHeight() ;
		var attr = (Browser.Engine.trident) ? 'height' : 'min-height';

		if (minheight > containerHeight){
			$('productDetailBox').setStyle(attr, minheight + 20);
		}
	}
	
	
	$$('div.boxSection div.product').each(function(item){

		item.addEvent('mouseenter', function(event){
			this.getElements('div.productHighlight').addClass('active');
		})
		
		item.addEvent('mouseleave', function(event){
			this.getElements('div.productHighlight').removeClass('active');
		})
	})

	// Inputfelder leeren auf Click
	if ($defined($('artikelsuche'))){
		$('artikelsuche').addEvent('click', function(){
			if (this.value == "Artikelsuche") this.value = "";
		})
	}

	// Inputfelder leeren auf Click
	if ($defined($('loginName'))){
		$('loginName').addEvent('click', function(){
			if (this.value == "E-Mail") this.value = "";
		})
	}
})
	
	
	
var syncColumnHeight = function(){
	// Columnheight sync
	if ($defined($('sideSection')) && $defined($('mainSection'))){
	
		// contextBar ?
		if ($('sideSection').getHeight() >= $('mainSection').getHeight()){
		
			if ($defined($('contextBar'))){
				if ($('sideSection').getHeight() >= $('contextBar').getHeight()){
					$('mainSection').setStyle('height', $('contextBar').getHeight() + 100);
				}
			
			}
		
			$('mainSection').setStyle('height', $('sideSection').getHeight());
		}
	}
	
}
	
	
