if(!$chk(window['wb'])) {
	var wb = {};
}

wb.SubmenuRotator = new Class({

	options: {
		
		visible_styles: {
						'height': 24,
						'overflow': 'hidden',
						'opacity': 1
						},
		
		hidden_styles: {
						'height': 0,
						'overflow': 'hidden',
						'opacity': 0
						},
		
		restore_delay: 800
	},
	
	item_id_prefix: 'item_',
	
	default_item: '',
	
	rotationItems: {},
	
	current_item_identifier: null,
	
	restore_timer: null,
	
	initialize: function(rotationItems, options) {
		this.setOptions(options);
		this._setupRotationItems(rotationItems);
	},
	
	_setupRotationItems: function(items) {
		var parsed_items = {};
		for (var i=0; i < items.length; i++) {
			
			// Get identification
			var item               = items[i];
			var item_number        = item.get('id').match(/\d+?$/);
			var item_ref           = this.item_id_prefix+item_number;
			var is_default         = !item.hasClass('hiddenSubNav');
			
			// Store the default subnavid
			if(is_default) {
				this.default_item  = item_ref;
			}
			
			// Setup styles
			var initial_styles     = is_default ? this.options.visible_styles : this.options.hidden_styles;
			initial_styles['display'] = 'block';
			
			item.setStyles(initial_styles);

			// Attach animation
			var item_animation         = new Fx.Morph(item, {duration: 200, transition: Fx.Transitions.linear});
			item['rotation_animation'] = item_animation;

			// Add restore functionality
			item.addEvent('mouseleave', this.startRestoreTimer.bind(this));
			item.addEvent('mouseenter', this.endRestoreTimer.bind(this))
			
			// Store item
			parsed_items[item_ref] = item;
		};
		
		// Store items hash
		this.rotationItems = parsed_items;
	},
	
	itemForIdentifier: function(identifier) {
		return this.rotationItems[identifier];
	},
	
	currentItem: function() {
		var current_item = null;
		if($chk(this.current_item_identifier)) {
			current_item = this.itemForIdentifier(this.current_item_identifier);
		}
		return current_item;
	},
	
	_setCurrentItem: function(identifier) {
		this.current_item_identifier = identifier;
	},
	
	isCurrent: function(identifier) {
		return identifier == this.current_item_identifier;
	},
	
	_hideCurrentItem: function() {
		if($chk(this.current_item_identifier)) {
			this._hideItem(this.current_item_identifier);
		}
	},
	
	_showItem: function(identifier) {
		for(var item_identifier in this.rotationItems) {
			var item = this.itemForIdentifier(item_identifier);
			if(identifier == item_identifier) {
				var visible_styles       = this.options.visible_styles;	
				item.rotation_animation.start(visible_styles);
			} else {
				item.rotation_animation.start(this.options.hidden_styles);
			}
		}
	},
	
	showMenu: function(identifier) {
		$clear(this.restore_timer);
		this._showItem(identifier);
	},

	hideMenu: function(identifier) {
		this.startRestoreTimer();
	},
	
	startRestoreTimer: function() {
		$clear(this.restore_timer);
		this.restore_timer = this.restore.delay(this.options.restore_delay, this);	
	},
	
	endRestoreTimer: function() {
		$clear(this.restore_timer);
	},
	
	restore: function() {
		this._showItem(this.default_item);
	}

});
wb.SubmenuRotator.implement(new Options);
wb.SubmenuRotator.implement(new Events);


// Navigation rotation setup
wb.navigation_rotator = null;
wb.setup_navigation_rotation = function() {
	var subnavigationItems = document.getElements('.subNavigation');
	wb.navigation_rotator = new wb.SubmenuRotator(subnavigationItems);
};
window.addEvent('domready', wb.setup_navigation_rotation);

// Navigation rotation methods
wb.show_hidden_nav = function (id, current) {
	if($chk(wb.navigation_rotator)) {
		wb.navigation_rotator.showMenu(wb.navigation_rotator.item_id_prefix+id);
	}
}
wb.hide_hidden_nav = function (id, current) {
	if($chk(wb.navigation_rotator)) {
		wb.navigation_rotator.hideMenu(wb.navigation_rotator.item_id_prefix+id);
	}
}
