/**
 * @author tom
 */

function MSAccordeon(rootElementID, col) {
	this.rootElement = '#' + rootElementID;
	this.color = col;
	this.arrowIcons = { 
		collapse	: 'media/' + this.color + '/collapse-icon.gif', 
		expand		: 'media/' + this.color + '/expand-icon.gif' 
	};
	this.transitionTime = 0.5;
	this.openItemNum = -1;
	
	this.expandedItem = null;
}

MSAccordeon.prototype.setTransitionTime = function(value) {
	this.transitionTime = parseFloat(value);
};

MSAccordeon.prototype.setOpenItemNum = function(num) {
	this.openItemNum = parseInt(num);
}

MSAccordeon.prototype.hideAllItems = function() {
	// closure
	var scope = this;
	
	$$(this.rootElement +' a.item-link').each(
		function(element) {
						
			//alert(element.className);
			//var arrow = document.createElement('IMG');
			//arrow.src = scope.arrowIcons.expand;
			//arrow.style.paddingRight = '3px';
			//new Insertion.Top(element, arrow);
			if ( element.className.indexOf('open') == -1 ) {
				element.next('div').hide();
			} else {
				scope.expandedItem = element.next('div');
			}
		}
	)
};


MSAccordeon.prototype.assignItemToggleLinks = function() {
	var scope = this;
	var transition = Effect.Transitions.linear;
	var itemCnt = 0;

	
	$$(this.rootElement + ' a.item-link').each(
		function(link) {
			link.setAttribute('href', 'javascript:;');
			link.item = link.next('div');
					
			link.item.isOpen = false;
			
			//link.item.addClassName('accordeon-item');
			
			//link.iconImage = link.item.previous().down();
			link.onclick = function() {
				if (null != scope.expandedItem) {
					//expandedItem.previous().down().src = scope.arrowIcons.expand;
					new Effect.BlindUp( 
						scope.expandedItem, 
						{ 
							duration	: scope.transitionTime, 
							delay		: 0, 
							transition	: transition,
							from		: 0,
							to			: 1		 
						} 
					);
				}
				
				if (this.item == scope.expandedItem) {
					scope.expandedItem = null;
					return;
				}
				
				//this.item.previous().down().src = scope.arrowIcons.collapse;
				var effectType = this.item.isOpen ? 'BlindUp' : 'BlindDown';
				var effectType = 'BlindDown';
				//this.iconImage.src = this.item.isOpen ? scope.arrowIcons.expand : scope.arrowIcons.collapse;
				new Effect[effectType]( 
					this.item, 
					{ 
						duration	: scope.transitionTime, 
						delay		: 0, 
						transition	: transition,
						from		: 0,
						to			: 1
					} 
				);
				scope.expandedItem = this.item;
				this.item.isOpen = !this.item.isOpen;
				return false;
			}
			//alert(itemCnt + ' / ' + scope.openItemNum);
			if (itemCnt == scope.openItemNum) {
				//alert(link.parentNode.addClassName);
				link.addClassName('open-item');
			}
		
		itemCnt++;			
		}
			
	)
	
	this.hideAllItems();
};
	
MSAccordeon.prototype.init = function() { 
	this.assignItemToggleLinks(); 
	
	//Logger.log('MSAccordeon init');
};

