//
//  Load accordions on onload
// 
Event.observe(window, 'load', loadAccordions, false);

//
//	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'});
	});  
	
}

function loadTipAccordions () {
	var tipAccordion = new accordion('content-forum', {
		startHeight : 40,
		classNames : {
			toggle : 'toggle',
			toggleActive : 'toggleActive',
			content : 'forum-article-text',
			toggleInActive : 'toggleInactive'
		}
	});
	var tipAccordions = $$('.toggle');
	tipAccordions.each(function(accordion) {
		if (accordion.next(0).scrollHeight > 40) {
			$(accordion.next(0)).setStyle({height: '40px'});
		}
	});
}


//
// Cookie functions
//
// JavaScript Document
var expDays = 30;
var exp = new Date();
exp.setTime(exp.getTime() + (expDays*24*60*60*1000));

function getCookieVal (offset) {
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1)
		endstr = document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}
function GetCookie (name) {
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen) {
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg)
			return getCookieVal (j);
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) break;
	}
	return null;
}
function SetCookie (name, value) {
	var argv = SetCookie.arguments;
	var argc = SetCookie.arguments.length;
	var expires = (argc > 2) ? argv[2] : exp;
	var path = (argc > 3) ? argv[3] : null;
	var domain = (argc > 4) ? argv[4] : null;
	var secure = (argc > 5) ? argv[5] : false;
	document.cookie = name + "=" + escape (value) +
		((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
		((path == null) ? "" : ("; path=" + path)) +
		((domain == null) ? "" : ("; domain=" + domain)) +
		((secure == true) ? "; secure" : "");
}
function DeleteCookie (name) {
	var exp = new Date();
	exp.setTime (exp.getTime() - 1);
	var cval = GetCookie (name);
	document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}



/**
 * Set height of inline lightview container, based on
 * height of content
 */
function setLvHeight (uid, width, maxHeight, title) {
	
	var elObj = document.getElementById('item' + uid);
	var linkObj = document.getElementById('link' + uid);
	
	// get height of element (can differ between ie and ff)
	var height = (elObj.offsetHeight < maxHeight) ? elObj.offsetHeight : maxHeight;

	// set height of element to activate scrolling
	if (height < maxHeight)
		elObj.style.height = height + 'px';
	else
		elObj.style.height = (height - 34) + 'px'; // subtract height of menubar
	
	linkObj.title = title +' | | width:' + width + ',height:' + height;
	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);});};
});


/**
 * Remove langid from searchelement
 */
jQuery(document).ready(function($) {
	if ($('div#header-search').length) {
		$('div#header-search').find('input[name=langid]').remove();
	}
});


/**
 *	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');
	}
});


/**
 * Resize jpg avatar
 */
function resizeImage (id, maxWidth, maxHeight) {

	var image = jQuery('#' + id);
	var ratio = 0;  // Used for aspect ratio
    var width = image.width();    // Current image width
    var height = image.height();  // Current image height
    var marginTop = 0;
    var marginLeft = 0;
	
	// Check if the current width is larger than the max
    if(width > maxWidth){
        ratio = maxWidth / width;   // get ratio for scaling image
        image.css("width", maxWidth); // Set new width
        image.css("height", height * ratio);  // Scale height based on ratio
        height = height * ratio;    // Reset height to match scaled image
        width = width * ratio;    // Reset width to match scaled image
        }
 
        // Check if current height is larger than max
    if(height > maxHeight){
        ratio = maxHeight / height; // get ratio for scaling image
        image.css("height", maxHeight);   // Set new height
        image.css("width", width * ratio);    // Scale width based on ratio
        width = width * ratio;    // Reset width to match scaled image
    }
    
    if (height < maxHeight) {
    	marginTop = (maxHeight - height) / 2;
    }
    
    if (width < maxWidth) {
    	marginLeft = (maxWidth - width) / 2;
    }
    
    image.css("margin-top",marginTop + "px");
    image.css("margin-left",marginLeft + "px");
    image.show();
}

