//
//  Load accordions on onload
// 
Event.observe(window, 'load', loadAccordions, false);

//
// Browser detection
//
var browser = '';
jQuery.each(jQuery.browser, function(i,val) {
	if (jQuery.browser.msie && jQuery.browser.version.substr(0,1) == '6') {
		browser = 'ie6';
	} else if(jQuery.browser.msie){
		browser = 'ie';
	} else {
		browser = 'other';
	}
});


//
//	Set up all accordions
//
function loadAccordions() {
	var verticalAccordion = new accordion('content-wrapper', {
		classNames : {
			toggle : 'accordion_toggle',
			toggleActive : 'accordion_toggle_active',
			content : 'accordion_content'
		}
	});
	
	var zwiSlidedown = new slidedown('content-wrapper', {
		classNames :{
			toggle : 'slidedown_toggle',
			toggleActive : 'slidedown_toggle_active',
			content : 'slidedown_content'
		}
	});
	
	var faqAccordion = new accordion('content-wrapper', {
		classNames : {
			toggle : 'faq_toggle',
			toggleActive : 'faq_toggle_active',
			content : 'faq_content'
		}
	});

	var verticalAccordions = $$('.accordion_toggle');
	verticalAccordions.each(function(accordion) {
		$(accordion.next(0)).setStyle({height: '0px'});
	});
	
	var faqAccordions = $$('.faq_toggle');
	faqAccordions.each(function(accordion) {
		$(accordion.next(0)).setStyle({height: '0px'});
	});  
}

/**
 * Set height of inline lightview container, based on
 * height of content
 */
function setLvHeight (uid, width, maxHeight, title)
{
	var padding = (browser == 'ie') ? 5 : 20;
	
	// move item to #product-items container
	var $elObj = jQuery('div#item' + uid);
	var $linkObj = jQuery('a#link' + uid);
	jQuery('#product-items').append($elObj);
	
	var height = ($elObj.height() < maxHeight) ? $elObj.height() : maxHeight;
	
	// set height of element to activate scrolling
	if (height < maxHeight) {
		$elObj.height(height);
		//$elObj.css('height',height);
	} else {
		$elObj.height(height - 34); // subtract height of menubar
	}
	$linkObj.attr('title', title +' | | width:' + width + ',height:' + (height + padding));
	return false;
}

/**
 * Make items in productoverview the same height
 */
jQuery(document).ready(function($){
	if ($('.materiaal-row').length) {$('.materiaal-row').each(function(index){var maxHeight = 200;$(this).find('.column > a').each(function(i){if ($(this).height() > maxHeight) {maxHeight = $(this).height();};});$(this).find('.column > a').height(maxHeight);});};
});


/**
 *	Create iform toggle
 */
jQuery(document).ready(function($) {
	if ($('div.iform').length) {
		// retrieve formparts containing toggles
		$('div.toggle').each(function(index) {
			// get fieldset to hide
			var $fieldset = $('fieldset#'+ this.id.substring(7) +'');
			var content = $fieldset.html();

			// get the radiobuttons to switch fieldset on/off
			var $switchOffRadio = $('div.toggle:eq(' + index +') input:radio[value=0]');
			if ($switchOffRadio.length > 0) {
				$switchOffRadio.click(function() {
					$fieldset.empty().hide();
				});
				
				var $switchOnRadio = $('div.toggle:eq(' + index +') input:radio[value=1]');
				$switchOnRadio.click(function() {
					$fieldset.html(content).show();
				});
				
				// get first state
				var $selectedRadio = $('div.toggle:eq(' + index +') input:radio:checked');
				if ($selectedRadio.attr('value') == 1) {
					$fieldset.html(content).show();
				} else {
					$fieldset.empty().hide();
				}
			}


			
			//* Checkboxes *//
			// get the checkbox to switch fieldset on/off
			var $switchCheck = $('div.toggle:eq(' + index +') input:checkbox');
			if ($switchCheck.length > 0) {
				$switchCheck.click(function() {
					if ($switchCheck.is(':checked')) {
						$fieldset.html(content).show();
					} else {
						$fieldset.empty().hide();
					}
				});
				// get first state
				if ($switchCheck.is(':checked')) {
					$fieldset.html(content).show();
				} else {
					$fieldset.empty().hide();
				}
			}
		
		});
		
		// add some classes for ie6 compatibility
		$('div.iform input[type=checkbox]').addClass('input-checkbox');
		$('div.iform input[type=hidden]').addClass('input-hidden');
	}
});


/**
 *	Make ankeilers in same row the same height
 */
jQuery(document).ready(function($) {
	if ($('div.ankeilers-2').length) {
		$('div.ankeilers-2 a.ankeiler-first').each(function(index) {
			var height = ($(this).height() > $(this).next().height()) ? $(this).height() :  $(this).next().height();
			$(this).height(height);
			$(this).next().height(height);
		});
	}
});


/**
 * Remove langid from searchelement
 */
jQuery(document).ready(function($) {
	if ($('div#header-search').length) {
		$('div#header-search').find('input[name=langid]').remove();
	}
});


/**
 * IE6: set height of header-wrapper 
 */
jQuery(document).ready(function($) {
	if (browser == 'ie6' && $('div#header-wrapper').length) {
		$('div#header-wrapper').height($('#header-background').height());
	}
});

/**
 * Onclick for navigation menu
 */
jQuery(document).ready(function($){
	if ($('#nav-subhome').length) {
		$elements = $('#nav-subhome > ul > li');
		$elements.click(function(event) {
			$el = $(event.currentTarget);
			$elements.each(function(i, el){
				if (!$el.hasClass('open')) {
					$(this).removeClass('open');
				}
			});
			$el.toggleClass('open');
		});
	}
});

