document.observe("dom:loaded", function() {
	
	//Input field focus/blur
	$$('.text').each(function(field){
		field.observe('focus', function(event){
			var el = event.element();
			if(el.hasClassName('password')){
				el.type = 'password';
			};
			
			el.value = el.value == el.getAttribute('default_value') ? '' : el.value;
		});
		field.observe('blur', function(event){
			var el = event.element();
			if(el.value == ''){
				el.value = el.getAttribute('default_value');
				el.type = 'text';
			}
		});
	});
	
	$$('ul#mainMenu li:not([class=current]) a').each(function(link){
		link.observe('mouseover', function(event){
			var el = event.element();
			new Effect.Move(el, {y:-5, duration:0.15});
		});
		link.observe('mouseout', function(event){
			var el = event.element();
			new Effect.Move(el, {y:0, mode: 'absolute', duration:0.15});
		});
	});
	
	$$('ul.productGrid li a.productHref').each(function(link){
		link.observe('mouseover', function(event){
			var el = event.element();
			el.up('li').addClassName('solidBorder');
		});
		link.observe('mouseout', function(event){
			var el = event.element();
			el.up('li').removeClassName('solidBorder');
		});
	});
	
	if($('footer')){
		var footerHeight = $('footer').getHeight();
		$('footer').setStyle({'margin-top': '-' + footerHeight + 'px'});
		$('main').setStyle({'padding-bottom': footerHeight});
	}
	
	equalHeight('.equalHeight');
	initWebshopMenu();
	
	
	$$('#footerSitemap ul li ul li').each(function(child) {
		obsucker(child);
		
	});
});

function obsucker(item, parent){
	parent = parent ? parent : item;
	child = item;
	child.observe('mouseover', function(event){
		var next = parent.next('ul');
		if(next){
			obsucker(next, parent);
			next.show();
		}
	});
	child.observe('mouseout', function(event){
		var next = parent.next('ul');
		if(next){
			obsucker(next, parent);
			next.hide();
		}
	});
}

function equalHeight(group) {
	var tallest = 0;
	var elHeight;
	$$(group).each(function(el) {
		elHeight = el.getHeight();
		if(elHeight > tallest) {
			tallest = elHeight;
		}
	});
	$$(group).each(function(el){
		el.setStyle({'height' : tallest + 'px'});
	});
}

function initWebshopMenu(){
	var lastDescendant;
	var activeSibling = false;
	$$('ul#webshopMenu li.hasChildren').each(function(el){
		if($(el).hasClassName('active')){
			activeSibling = $(el).immediateDescendants().last();
		}else{
			lastDescendant = $(el).immediateDescendants().last();
			if(lastDescendant.nodeName.toLowerCase() == 'ul'){
				$(lastDescendant).hide();
			}
		}
	});
	

	
	$$('ul#webshopMenu > li.hasChildren > a').each(function(element){
		$(element).observe('click', function(e){
			var clickedSibling = $(element).next();
			if(clickedSibling.nodeName.toLowerCase() == 'ul'){
				if(activeSibling && activeSibling != clickedSibling){
					Effect.SlideUp(activeSibling, {duration:.2});
				}
				if(clickedSibling.style.display == 'none'){
					Effect.SlideDown(clickedSibling, {duration:.2});
				}
				activeSibling = clickedSibling;
				e.stop();
			};
		});
	});
}




//jQuery("#footerSitemap ul li ul").superfish({
//	delay:		 1000,							  // one second delay on mouseout 
//	animation:	 {opacity:'show',height:'show'},  // fade-in and slide-down animation 
//	speed:		 'fast',						  // faster animation speed 
//	autoArrows:	 true
//});