/*encode: utf-8*/

function accordion(elmId, opt){
	jQuery(function(){
		new Accordion(elmId, opt);
	});
}
function Accordion(elmId, opt){
	var $ = jQuery;
	var elmRoot;
	if(typeof elmId === 'string'){
		elmRoot= $('#'+elmId);
	}else{
		elmRoot= $(elmId);
	}
	if(!elmRoot.length){return;}
	var btnExp= 'li > h3',
		itemExp= 'li';
	if(opt){
		('btnExp' in opt) && (btnExp= opt.btnExp);
		
	}
	var items= elmRoot.find(itemExp),
		btns= elmRoot.find(btnExp),
		curItem,
		heightCur,
		heightNormal;
	items.each(function(i, _item){
			_item= $(_item);
			/*
			console.log('_item.height is: ' + _item.height());
			console.log('_item.css(\'height\') is: ' + _item.css('height'));
			console.log('_item[0].offsetHeight is: ' + _item[0].offsetHeight);
			*/
			var btn= $(btns[i]);
			btn.attr('i', i);
			_item.attr('i', i);
			_item.css('overflow', 'hidden');
			if(_item.attr('class')){
				if(!curItem && (_item.attr('class').indexOf('cur') !== -1)){
					curItem= _item;
					heightCur= _item.height();
				}
				if(!heightNormal && _item.attr('class').indexOf('cur') === -1){
					heightNormal= _item.height();
				}
			}else{
				if(!heightNormal){
					heightNormal= _item.height();
					// - (parseInt(_item.css('paddingTop')) || 0 + parseInt(_item.css('paddingBottom')) || 0 + parseInt(_item.css('borderTopWidth')) || 0 + parseInt(_item.css('borderBottomWidth')) || 0)
				}
			}
			var animateItem;
			btn.click(function(){
					if(curItem === _item){return;}
					animateItem && animateItem.stop();
					curItem.css('height', heightCur);
					_item.css('height', heightNormal);
					_item.addClass('cur');
					
					animateItem= curItem;
					curItem= _item;
					//console.log(heightCur, heightNormal);
					animateItem.animate({'height': heightNormal},
										{
										'step2': function(n){
												//alert(n, animateItem.css('height'));
												_item.css('height', Math.max(Math.abs(heightCur + heightNormal - n), 0));
											},
										'duration': 300,
										//easing: settings.easing,
										'complete': function(){
												animateItem.removeClass('cur');
												animateItem.css('height', '');
												_item.css('height', '');
											}
										});
					return false;
				});
		});
	//alert(heightCur);
	//alert(heightNormal);
}