AXController = function(menuId)
{
	this.menuId = menuId ? menuId : "menu-content";
	this.currentPosition = -1;
	this.topics = new Array();
	this.currentTopic = null;
	this.currentSelectedTopic = null;
	this.currentTopicIndex = -1;
	this.topicsUL = $('topics');
	this.duration = 0;
	this.xmlDoc = null;
	this.isChangingTopic = false;
	//Events
	this.topicChange = null;
	this.topicPositionChange = null;
	
	//Methods
	this.init = function()
	{
		try
		{
			if(!isLive)
			{
			    this.getTopics(this.menuId);
			}
			else
			{
				var o = $$($(this.menuId)+' ul.topics');
				if(o != null && o.length > 0)
				{
					o[0].style.display = "none";
				}	
			}
		}
		catch(e)
		{
		}
	};
	
	this.selectTopic = function(e)
	{
	    this.isChangingTopic = true;
		var i = 0;
		
        if(typeof(e) != "number")
        {
            var node = Event.findElement(e, 'li');
            if(node.tagName) 
            {
                i = parseInt(node.id.substring(5));
            }
        }
        else
        {
	        i = e;
        }
		
		this.setTopic(i);
			
		//fire event
		if(this.topicChange != null) 
		{
			this.topicChange(this.currentTopic, true);
		}
	};
	
	this.setTopic = function(topicIndex)
	{
	    var topicNode = null; 
	    var selectId = null;//old selection show?
		
		this.isChangingTopic = true;
		
		if(topicIndex < 0 || topicIndex >= this.topics.length)
		{
		    topicIndex = 0;
		}
		
		//select topic
		this.currentTopicIndex = topicIndex;
		topicNode = $('topic'+topicIndex);
		if(topicNode != null && topicNode.style.display == "none")
		{
			//find previous display topic
			for(var j=topicIndex-1;j>-1;j--)
			{
			    if($('topic'+j).style.display != "none")
			    {
			        this.currentSelectedTopic = this.topics[j];
			        break;
			    }
			}			
		}
		else
		{
		    this.currentSelectedTopic = this.topics[topicIndex];
		}
		
		this.currentTopic = this.topics[topicIndex];
		if(this.currentTopic != null)
		{
			if(this.topics.length > i+1)
			{
				this.currentTopic.duration = this.topics[topicIndex+1].beginTime-this.topics[topicIndex].beginTime;
			}
			else
			{
				this.currentTopic.duration = this.duration-this.topics[topicIndex].beginTime;
			}
		}
				
		
		for (var i=0;i<this.topics.length;i++)
		{
		    topicNode = $('topic'+i);
			if(i != topicIndex && topicNode != null)
			{
				topicNode.className = "topic";
			}
		}
		
		//show select topic
	    if(this.currentSelectedTopic != null)
	    {
	        $('topic'+this.currentSelectedTopic.id).className = "topic-selected";	
	    }
	    
	    this.isChangingTopic = false;
	}
	
	this.scrollTopic = function()
	{
	    //scroll into view
		if(useAutomaticMenuPositioning && this.currentSelectedTopic != null)
		{
			var offset = (automaticMenuPositioningOffsetTop != null) ? automaticMenuPositioningOffsetTop:50;
			var menu = this.currentSelectedTopic.linkObj.parentNode.parentNode.parentNode;
			var x = this.currentSelectedTopic.linkObj.offsetTop-menu.offsetTop+offset;
			x = (x>-1)?x:0;
			if(x < menu.scrollHeight)
			{
				menu.scrollTop = x;
			}
		}
	}
	
	// time format hh:mm:ss
	this.getTopics = function(id)
	{
	    var o = null;
	    var nodes = $$('#'+id+' ul.topics li');
	    nodes.each(function(node){
	     
	        var topic = {beginTime:null, id:this.topics.length, imageUrl:"", imageAlt:"", linkObj:null, slideId:null, slideUrl:"", text:""};
	        
	        var a = node.getElementsByTagName('a');
	        if(a != undefined && a.length > 0)
	        {
	            topic.text = a[0].innerHTML;
	            o = document.getElementsByClassName('beginTime', a[0]);
	            if(o != undefined && o.length > 0)
	            {
	                o = o[0].innerHTML;
					o = o.replace("(", "");
					o = o.replace(")", "").strip();
					topic.beginTime = parseInt(toSeconds(o));
	            }
	        }
	        
	        var slide = document.getElementsByClassName('slideId', node);
	        if(slide != undefined && slide.length > 0)
	        {
	            if(slide[0].innerHTML.indexOf("?") > -1)
                {
                    //extract slideId
                    var id = -1;
                    var s = slide[0].innerHTML.split("?");
                    topic.slideUrl = s[0];
                    
                    for(var j=1; j<s.length; j++)
                    {
                        if(s[j].indexOf("slideId") > -1)
                        {
                            topic.slideId = parseInt(s[j].substring(s[j].indexOf("=")+1));
                            break;
                        }
                    }
                }		
                else
                {
                    topic.slideId = parseInt(slide[0].innerHTML);
                    for(var i=topic.id-1; i>-1; i--)
                    {
                        if(this.topics[i].slideUrl != "")
                        {
                            topic.slideUrl = this.topics[i].slideUrl;
                            break;
                        }
                    }
                }
	        }
	        
	        var image = document.getElementsByClassName('imageId', node);
	        if(image != undefined && image.length > 0)
	        {
	            o = image[0].innerHTML;
	            topic.imageUrl = o;		
				if(o.indexOf(';') > -1)
				{
				    var o = o.split(';');
				    topic.imageUrl = o[0];
				    topic.imageAlt = o[1]; 
				}
	        }
	        
	        if(topic.beginTime != null)
	        {
	            topic.linkObj = node;
	            node.id = "topic"+this.topics.length;
	            this.topics.push(topic);
	            if(node.getElementsByTagName('a').length > 0)
	            {
                    Event.observe(node, 'click', this.selectTopic.bindAsEventListener(this), true);
                }
	        }
	    }.bind(this));
	};
	
	this.getTopicIndex = function(s)
	{
		if(s != null)
		{
			if(s != null && s >= 0)
			{
				var found = false;
				var i = 0;
				
				if(this.currentTopic != null && s >= this.currentTopic.beginTime)
				{
					i = this.currentTopicIndex;
				}
				
				for(i; i < this.topics.length; i++)
				{
					if(i + 1 < this.topics.length)
					{
						if(s >= this.topics[i].beginTime && s < this.topics[i+1].beginTime)
						{
							found = true;
							break;
						}
					}
					else if(this.topics[i].beginTime <= s)
					{
						found = true;
						break;
					}
				}
				
				if(found)
				{
					return i;
				}
				else
				{ 
					return -1;
				}
			}
		}
	};
	
	this.previous = function()
	{
		if(this.currentTopicIndex > 0)
		{
			if(this.currentTopic != null && this.currentPosition - this.currentTopic.beginTime <= 5)
			{
			    this.selectTopic(this.currentTopicIndex-1);
			}
			else
			{
			    this.selectTopic(this.currentTopicIndex);
			}
			
			this.scrollTopic();
		}
		else
		{
		    this.selectTopic(0);
		    this.scrollTopic();
		}
	};
	
	this.next = function()
	{
		if(this.currentTopicIndex < this.topics.length)
		{
			this.selectTopic(this.currentTopicIndex+1);
		}
		
		this.scrollTopic();
	};
	
	this.setDuration = function(s)
	{
		this.duration = s;
	};
	
	this.setPosition = function(pos)
	{
		if(pos != null && pos != this.currentPosition && !this.isChangingTopic)
		{
			this.currentPosition = pos;
			var i = this.getTopicIndex(pos);
			
			if(i > -1 && (this.currentTopicIndex == -1 || i != this.currentTopicIndex))
			{
				this.setTopic(i);
				this.scrollTopic();
				//fire event
		        if(this.topicChange != null) 
		        {
			        this.topicChange(this.currentTopic, false);
		        }
			}
		}
	};
	
	this.setSlide = function(id)
	{
	    var j = -1;
	    for (var i=0;i<this.topics.length;i++)
		{
			if(this.topics[i].slideId == id)
			{
			    j = i;
				break;
			}
		}
		
		if(j > -1)
		{
		    this.setTopic(j);
		    this.scrollTopic();
			//fire event
	        if(this.topicChange != null) 
	        {
		        this.topicChange(this.currentTopic, true);
	        }
		}
	};
	
	this.init();
}
