document.observe('dom:loaded', function() {
// Fixes voor IE6:
/*@cc_on
	if (document.all && (navigator.userAgent.toLowerCase().indexOf("msie 6.") != -1))
	{
		$('topmenu').style.position = 'absolute';
		$('topmenu').style.width    = '507px';
	}
@*/

	$('search_submitBtn').observe('mouseover', function() {
		$('search_submitBtn').down().setStyle({textDecoration: 'underline'});
	});
	$('search_submitBtn').observe('mouseout', function() {
		$('search_submitBtn').down().setStyle({textDecoration: 'none'});
	});

	$('callback_submit').observe('mouseover', function() {
		$('callback_submit').down().setStyle({textDecoration: 'underline'});
	});
	$('callback_submit').observe('mouseout', function() {
		$('callback_submit').down().setStyle({textDecoration: 'none'});
	});

	$('callback').onclick = $$('#callbackform .close')[0].onclick = function() {
		$('callbackform').toggle();
		return false;
	};

	$('callbackform').onsubmit = function() {
		$('callbackform').request({
			parameters: { isAjax: 'true' },
			onComplete: processCallbackForm
		});
		return false;
	}

	$('callbackform').hide();

	$('extlink').target = '_blank';

	$$('.submitBtn').each(function(oBtn) {
		oBtn.observe('mouseover', function(event) {
			Event.element(event).style.color = '#db002f';
		});
		oBtn.observe('mouseout', function(event) {
			Event.element(event).style.color = '#174588';
		});
	});

	sfHover();
});

function processCallbackForm(oRequest)
{
	var oResponse = oRequest.responseJSON;

	if (!oResponse.isValid)
	{
		var sGenderError = oResponse.aErrors.gender;
		var sNameError   = oResponse.aErrors.name;
		var sPhoneError  = oResponse.aErrors.phone;
		var sReasonError = oResponse.aErrors.reason;

		if (sGenderError != false)
		{
			var oLabel1 = $('callbackform').getElementsBySelector('label[for=callback_gender_m]');
			oLabel1 = oLabel1[0];
			oLabel1.style.color = '#f00';
			oLabel1.style.textDecoration = 'blink';
			oLabel1.title = sGenderError;

			var oLabel2 = $('callbackform').getElementsBySelector('label[for=callback_gender_f]');
			oLabel2 = oLabel2[0];
			oLabel2.style.color = '#f00';
			oLabel2.style.textDecoration = 'blink';
			oLabel2.title = sGenderError;
		}
		else
		{
			var oLabel1 = $('callbackform').getElementsBySelector('label[for=callback_gender_m]');
			oLabel1 = oLabel1[0];
			oLabel1.style.color = '';
			oLabel1.style.textDecoration = 'none';
			oLabel1.title = '';

			var oLabel2 = $('callbackform').getElementsBySelector('label[for=callback_gender_f]');
			oLabel2 = oLabel2[0];
			oLabel2.style.color = '';
			oLabel2.style.textDecoration = 'none';
			oLabel2.title = '';
		}

		if (sNameError != false)
		{
			var oLabel = $('callbackform').getElementsBySelector('label[for=callback_name]');
			oLabel = oLabel[0];
			oLabel.style.color = '#f00';
			oLabel.style.textDecoration = 'blink';
			oLabel.title = sNameError;
		}
		else
		{
			var oLabel = $('callbackform').getElementsBySelector('label[for=callback_name]');
			oLabel = oLabel[0];
			oLabel.style.color = '';
			oLabel.style.textDecoration = 'none';
			oLabel.title = '';
		}

		if (sPhoneError != false)
		{
			var oLabel = $('callbackform').getElementsBySelector('label[for=callback_phone]');
			oLabel = oLabel[0];
			oLabel.style.color = '#f00';
			oLabel.style.textDecoration = 'blink';
			oLabel.title = sPhoneError;
		}
		else
		{
			var oLabel = $('callbackform').getElementsBySelector('label[for=callback_phone]');
			oLabel = oLabel[0];
			oLabel.style.color = '';
			oLabel.style.textDecoration = 'none';
			oLabel.title = '';
		}

		if (sReasonError != false)
		{
			var oLabel = $('callbackform').getElementsBySelector('label[for=callback_reason]');
			oLabel = oLabel[0];
			oLabel.style.color = '#f00';
			oLabel.style.textDecoration = 'blink';
			oLabel.title = sReasonError;
		}
		else
		{
			var oLabel = $('callbackform').getElementsBySelector('label[for=callback_reason]');
			oLabel = oLabel[0];
			oLabel.style.color = '';
			oLabel.style.textDecoration = 'none';
			oLabel.title = '';
		}
	}
	else
	{
		var oLabel1 = $('callbackform').getElementsBySelector('label[for=callback_gender_m]');
		oLabel1 = oLabel1[0];
		oLabel1.style.color = '';
		oLabel1.style.textDecoration = 'none';
		oLabel1.title = '';

		var oLabel2 = $('callbackform').getElementsBySelector('label[for=callback_gender_f]');
		oLabel2 = oLabel2[0];
		oLabel2.style.color = '';
		oLabel2.style.textDecoration = 'none';
		oLabel2.title = '';

		var oLabel3 = $('callbackform').getElementsBySelector('label[for=callback_name]');
		oLabel3 = oLabel3[0];
		oLabel3.style.color = '';
		oLabel3.style.textDecoration = 'none';
		oLabel3.title = '';

		var oLabel4 = $('callbackform').getElementsBySelector('label[for=callback_phone]');
		oLabel4 = oLabel4[0];
		oLabel4.style.color = '';
		oLabel4.style.textDecoration = 'none';
		oLabel4.title = '';

		var oLabel5 = $('callbackform').getElementsBySelector('label[for=callback_reason]');
		oLabel5 = oLabel5[0];
		oLabel5.style.color = '';
		oLabel5.style.textDecoration = 'none';
		oLabel5.title = '';

		$('callback_submit').replace('<div style="text-align: right; width: 450px">' + oResponse.sNotice + '</div>');
	}
}

function hideCatalogues()
{
	$$('.group').each(function(oGroup) {
		var sGroup = '.' + oGroup.down().rel;

		if (sGroup != ".")
		{
			if (oGroup.down().className != 'active')
			{
				$$(sGroup).each(function(oSubGroup) {
					oSubGroup.hide();
				});
			}
			else
			{
				$$(sGroup).each(function(oSubGroup) {
					var sSubGroup = '.' + oSubGroup.down().rel;

					if (sSubGroup != ".")
					{
						if (oSubGroup.down().className != 'active')
						{
							$$(sSubGroup).each(function(oProduct) {
								oProduct.hide();
							});
						}
					}
				});
			}
		}
	});
}

function prepareCatalogues()
{
	$$('.group').each(function(oGroup) {
		var oSpan = oGroup.down().down();

		oSpan.onclick = function() {
			var sGroup  = '.' + this.up().rel;
			var bIsOpen = (this.innerHTML == '-');

			if (bIsOpen)
			{
				closeMenu(this.up().up());
			}
			else
			{
				openMenu(this.up().up());
			}

			return false;
		};
	});

	$$('.subgroup').each(function(oGroup) {
		var oSpan = oGroup.down().down();

		oSpan.onclick = function() {
			var sGroup  = '.' + this.up().rel;
			var bIsOpen = (this.innerHTML == '-');

			if (bIsOpen)
			{
				closeMenu(this.up().up());
			}
			else
			{
				openMenu(this.up().up());
			}

			return false;
		};
	});
}

function closeMenu(oItem)
{
	if (oItem.hasClassName('subgroup'))
	{
		$$('.' + oItem.down().rel).each(function(oProduct) {
			oProduct.hide();
		});

		oItem.down().down().innerHTML = '+';
	}
	else if (oItem.hasClassName('group'))
	{
		$$('.' + oItem.down().rel).each(function(oSubGroup) {
			closeMenu(oSubGroup);
			oSubGroup.hide();
		});

		oItem.down().down().innerHTML = '+';
	}
}

function openMenu(oItem)
{
	if (oItem.hasClassName('group'))
	{
		$$('.group').each(function(oGroup) {
			//closeMenu(oGroup);
		});

		$$('.' + oItem.down().rel).each(function(oSubGroup) {
			if (oSubGroup.hasClassName('subgroup'))
			{
				//closeMenu(oSubGroup);
				oSubGroup.show();
			}
		});

		oItem.down().down().innerHTML = '-';
	}
	else if (oItem.hasClassName('subgroup'))
	{
		$$('.group').each(function(oGroup) {
			if (oItem.hasClassName(oGroup.down().rel))
			{
				$$('.' + oGroup.down().rel).each(function(oSubGroup) {
					if (oSubGroup != oItem)
					{
						//closeMenu(oSubGroup);
					}
					else
					{
						$$('.' + oItem.down().rel).each(function(oProduct) {
							if (oProduct.hasClassName('product'))
							{
								oProduct.show();
							}
						});
					}
				});
			}
		});

		oItem.down().down().innerHTML = '-';
	}
}

var sfHover = function() {
	var sfEls = document.getElementById("topmenu").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover = function() {
			this.className = "sfhover";

			/** edited for IE7 compatibility **/
			var aUls = this.childNodes;
			for (var j=0; j<aUls.length; j++) {
				if (aUls[j].nodeName.toUpperCase() == "UL") {
					aUls[j].style.left = "auto";
				}
			}
			/** --- **/
		}
		sfEls[i].onmouseout = function() {
			this.className = "";

			/** edited for IE7 compatibility **/
			var aUls = this.childNodes;
			for (var j=0; j<aUls.length; j++) {
				if (aUls[j].nodeName.toUpperCase() == "UL") {
					aUls[j].style.left = "-999em";
				}
			}
			/** --- **/
		}
	}
}