var accordion;
var disableanims = false;
var animsduration = 500;

if (disableanims) {
	animsduration = 0;
}

function sumArr(arr) {
	var sum = 0;
	arr.each(function(el){sum += el;});
	return sum;
}

function sumArrO(arr) {
	var sum = 0;
	arr.each(function(el){$each(el,function(value, key){
		sum += value.toInt();})
	});
	return sum;
}



//dzialania rozpoczynajace sie po zaladowaniu DOM (zblizone do onLoad)
window.addEvent('domready', function(){
var init = true;

	//sliders
	$$('li.toggleslide', 'li.toggleslide2h').each(function(li){
		var toogler = li.getElement('h4');
		//var toogler = $$('li h4.toggler');
		var block = li.getElement('ul');
		
 		li.expanded = (li.hasClass('expanded')) ? true : false;
 		
		var fx = new Fx.Slide(block, {
			duration: animsduration, //disable animation if 0
			onStart:function(){
				////console.log('expanded:',this.getParent().expanded);
				if (block.getParent().getParent().expanded == false) { block.getParent().getParent().addClass('expanded') };
				//block.getParent().getParent().expanded = (block.getParent().getParent().expanded == true) ? false : true;
				//console.log('slide started');
			},
			onComplete:function(){
				////console.log('expanded:',this.getParent().expanded);
				if (block.getParent().getParent().expanded == true) { block.getParent().getParent().removeClass('expanded') } else { block.getParent().getParent().addClass('expanded') };
				block.getParent().getParent().expanded = (block.getParent().getParent().expanded == true) ? false : true;
				//console.log('slide complete');
			}
		});
		
		(li.hasClass('expanded')) ? fx.show() : fx.hide();
 
		toogler.addEvent('click', function(){
			fx.toggle();
			////console.log('expanded:',this.getParent().expanded);
			//if (this.getParent().expanded == true) { this.getParent().removeClass('expanded') } else { this.getParent().addClass('expanded') };
			//this.getParent().expanded = (this.getParent().expanded == true) ? false : true;
		});
 
	});
	
	//accordion
	accordion = new Accordion('h3.atStart', 'ul.atStart', {
		display: -1,
		show: -1,
		opacity: false,
		alwaysHide: true,
		duration: animsduration, //disable animation if 0
		onActive: function(toggler, element){
			toggler.addClass('expanded');
			////console.log("onactivate");
		},
		
		onBackground: function(toggler, element){
			var harr = $$(element.getChildren()).getStyles('height','padding-top','padding-bottom');
			var hsum = sumArrO(harr);

			if (element.offsetHeight>0) {element.setStyle('height', hsum+"px");}
			toggler.removeClass('expanded');
			//console.log('onbackgr',' h:',hsum);
		},
		
		onComplete:function(){
			//if (init !== true) { //init check not-needed
				el = $(this.elements[this.previous]);
				if (el.offsetHeight>0) {el.setStyle('height','auto');}
			//}
			//init = false;
		}
	}, $('accordion'));
	
	//IE Fix Loads the tab(1) then return to tab(0)
	//accordion.showThisHideOpen(1);
	//accordion.showThisHideOpen(0);
	
	//IE Fix - call not defined function on accordion, not clear why but fixes gaps
	//TODO: fix that call - in older IE may show popup with alert about errors in javascript page code/find why calling not existing functions on Accordion fixes gaps in IE
	//accordion.IEgapFixNotExistingFunctCall();
	
}); 
