$(function() {
	
	(function(elem) {
		// Private attributes
		var _mainBlock = elem;
		var _currentInterval = null;
		var _items = null;
		var _numItems = 0;
		var _currentItem = 0;
		var _itemsMargin = 15;
		var _elemProperties = null;
		var _tracker;
		
		// Private methods
		var _trackEvent = function(event, id) {
			try {
				if(!_tracker) {
					_tracker = _gat._getTrackerByName();
				}
				 _tracker._trackEvent('Highlights', event, id);
			} catch (e) {
			}
		}
		
		var _changeHighlight = function() {
			var nextItemIndex = _currentItem + 1;
			if(nextItemIndex > _numItems - 1) nextItemIndex = 0;
			
			var i = 0;
			_items.each(function() {
				var value = 0;								
				if(i != _currentItem) {
					value = parseInt($(this).css("z-index")) + 1;
				}
				$(this).css("z-index", value);
				++i;
			});
			
			$(_items[_currentItem]).css("visibility","hidden");
			
			$(_items[nextItemIndex]).css("visibility","visible"); 
			
			_currentItem = nextItemIndex;
		};
		
		var _displayAll = function() {	
			_trackEvent("Mouseenter", $("a", _items[_currentItem]).attr("id"));
			$(_mainBlock).css("height", _numItems * (_elemProperties.height + _elemProperties.bottom + _itemsMargin) + "px");
			
			var i = 0;
			var bottom = _elemProperties.bottom;
			
			clearInterval(_currentInterval);
			
			
						
			_items.each(function() {
				var value = _numItems - 1 - parseInt($(this).css("z-index"));
				if(i != _currentItem) bottom = 15;
				else bottom = _elemProperties.bottom;
				
				$(this).css("visibility","visible");
				
				$(this).stop().clearQueue().animate({
					"bottom" : value * (_elemProperties.height - bottom) + "px"
				}, 300 * value);
				++i;
			});
		};
		
		var _hideAll = function() {
			_trackEvent("Mouseleaves", $("a", _items[_currentItem]).attr("id"));
			var i = 0;
			var callback = function() {};
			_items.each(function() {
				var value = _numItems - 1 - parseInt($(this).css("z-index"));
				
				if(i == _numItems - 1) {
					callback = function() {
						$(this).css("visibility", i == _currentItem ? "visible" : "hidden");						
						$(_mainBlock).css("height", (_elemProperties.height + _elemProperties.bottom) + "px");
						_initInterval();
					};
				} else {
					callback = function() {						
						$(this).css("visibility", i == _currentItem ? "visible" : "hidden");
					};
				}
				
				$(this).stop().clearQueue().animate({
					"bottom" : _elemProperties.bottom + "px"
				}, 300 * value, callback);
				++i;
			});
		};
		
		var _initInterval = function() {			
			_currentInterval = setInterval(_changeHighlight, 5000);
		};
		
		// Constructor
		(function() {
			if(_mainBlock) {
							
				_items = $("ul li", _mainBlock);
				_numItems = _items.length;
				
				var i = 0;
				_items.each(function() {
					$(this).css({
						"z-index" : _numItems - i - 1,
						"visibility" : i == 0 ? "visible" : "hidden"
					});
					++i;
				});
				
				_elemProperties = {
					"height" : $(_items[_currentItem]).height(), 
					"bottom" : parseInt($(_items[_currentItem]).css("bottom"))
				};
				
				_initInterval();
				
				$(_mainBlock).mouseenter(_displayAll).mouseleave(_hideAll);
			}
		})();
	})($("#highlight #highlight-block")[0]);
	
});
