AXController = function(topicsId)
{
	this.topicsULId = topicsId != null ? topicsId : "topics";
	this.currentPosition = -1;
	this.topics = new Array();
	this.currentTopic = null;
	this.currentSelectedTopic = null;
	this.currentTopicIndex = -1;
	this.topicsUL = document.getElementById('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.topicsULId);
			}
			else
			{
				if(this.topicsUL != null)
				{
					this.topicsUL.style.display = "none";
				}	
			}
		}
		catch(e)
		{
		}
	};
	
	this.addTopic = function addTopic(beginTime, text, slideUrl, slideId, linkObj, imageUrl, imageAlt)
	{
		var o = new Object();
		o.id = this.topics.length;
		o.slideId = slideId;
		o.slideUrl = slideUrl;
		o.text = text;
		o.beginTime = Number(beginTime);
		o.imageUrl = imageUrl;
		o.imageAlt = imageAlt;
		o.linkObj = linkObj;
		this.topics.push(o);
	};
	
	this.selectTopic = function(e)
	{
	    //when user clicks or prev/next
		//if(this != arguments.callee._oScope){
		//	return arguments.callee.apply(arguments.callee._oScope, arguments);
		//};
		
		this.isChangingTopic = true;
		var topicIndex = null;
		
		if(typeof(e) != "number")
		{
		    //user click
			var obj = null;
			if (e.target)
			{   
			    obj = e.target;
			}
			else if (e.srcElement)
			{
			    obj = e.srcElement;
			}
			
			if(obj != null)
			{
				//get topic id
				for(var i=0; i<this.topics.length; i++)
				{
				    if(obj.parentNode.nodeName == "A")
					{
						obj = obj.parentNode;
					}
					if(this.topics[i].linkObj == obj)
					{
						topicIndex = i;
						break;
					}
				}
			}
		}
		else
		{
			topicIndex = e;
		}
		
		this.setTopic(topicIndex);
			
		//fire event
		if(this.topicChange != null) 
		{
			this.topicChange(this.currentTopic, true);
		}
	};
	
	//this.selectTopic._oScope = this;
	
	this.setTopic = function(topicIndex)
	{
	    this.isChangingTopic = true;
	    
	    var selectId = null;//old selection show?
		
		if(topicIndex < 0)
		{
		    topicIndex = 0;
		}
		if(topicIndex > this.topics.length)
		{
		    topicIndex = this.topics.length-1;
		}
		
		//select topic
		this.currentTopicIndex = topicIndex;
		if(this.topics[topicIndex].linkObj.parentNode.style.display == "none")
		{
			//find previous display topic
			for(var j=topicIndex-1;j>-1;j--)
			{
			    if(this.topics[j].linkObj.parentNode.style.display != "none")
			    {
			        this.currentSelectedTopic = this.topics[j];
			        break;
			    }
			}			
		}
		else
		{
		    //alert("visible topic");
		    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++)
		{
			if(i != topicIndex && this.topics[i].linkObj != null)
			{
				this.topics[i].linkObj.parentNode.className = "topic";
			}
		}
		
		//show select topic
	    if(this.currentSelectedTopic != null)
	    {
		    this.currentSelectedTopic.linkObj.parentNode.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 = document.getElementById(id);
		if(o != null && o.nodeName == "UL")
		{
			var li = o.getElementsByTagName('li');
			var obj = null;
			var title = null;
			var slideId = null;
			var slideUrl = "";
			var imageUrl = "";
			var imageAlt = "";
			var beginTime = null;
			
			for(var i=0; i<li.length; i++)
			{
				try
				{
					obj = li[i].getElementsByTagName('a');
					if(obj.length > 0)
					{
						title = obj[0];
					}
					else
					{
						title = li[i];
					}
					
					obj = getElementsByClassName(li[i], 'slideId');
					if(obj.length > 0)
					{
						if(obj[0].innerHTML.indexOf("?") > -1)
                        {
                            //extract slideId
                            var id = -1;
                            var s = obj[0].innerHTML.split("?");
                            slideUrl = s[0];
                            
                            for(var j=1; j<s.length; j++)
                            {
                                if(s[j].indexOf("slideId") > -1)
                                {
                                    slideId = parseInt(s[j].substring(s[j].indexOf("=")+1));
                                    break;
                                }
                            }
                        }		
                        else
                        {
                            slideId = parseInt(obj[0].innerHTML);
                        }	
					}
				
					obj = getElementsByClassName(title, 'beginTime');
					if(obj.length > 0)
					{
						beginTime = obj[0].innerHTML;
						beginTime = beginTime.replace("(", "");
						beginTime = beginTime.replace(")", "");
						beginTime = toSeconds(beginTime);
					}
					
					
					obj = getElementsByClassName(li[i], 'imageId');
					if(obj != null && obj.length > 0)
					{
						imageUrl = obj[0].innerHTML;
						
						if(imageUrl.indexOf(';') > -1)
						{
						    var imgUrl2 = imageUrl.split(';');
						    imageUrl = imgUrl2[0];
						    imageAlt = imgUrl2[1]; 
						}
					}
					
					if(beginTime != null)
					{
						this.addTopic(beginTime, title.innerHTML, slideUrl, slideId, title, imageUrl, imageAlt);
					}
					
					if(beginTime != null)
					{
						if(li[i].getElementsByTagName('a').length > 0)
						{
						    li[i].onclick = this.selectTopic.bindAsEventListener(this);
						    //EventManager.Add(li[i],'click',this.selectTopic);
						}
						else
						{
						    alert("Error! Cannot add click event.");
						}			
					}
				}
				catch(ex)
				{
					alert(ex);
				}	
			}
		}
	};
	
	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();
}