/*
	Download Page
	----------------------------------------------------------------------------------------------------	
	This object contains methods needed to operate the download page.
 */
springo.downloadPage = function() {
	var s = springo.getSpringo();
	var _me;
	var _panels = [];
	var _current;
	var _image;
	var _timer;
	
	// Consts
	var CLASS_SELECTED = "selected";
	var INTERVAL = 4000;
	
	return {
		/*
			init
			--------------------------------------------------------------------------------------------		
			This function initialzies the download page.
		 */	
		init: function(panels, imageId) {
			_me = this;
			_panels = panels;
			_image = YUD.get(imageId);
			
			// Initialize common tasks to all pages (login, footer).
			s.page.init();			
//			s.autocomplete.init({
//					textBoxId: "txtSearch",
//					containerId: "autocomplete_container",
//					source: "search"
//				});
			
			// Initialize panels.
			_me.initPanels();
			
			// Select first panel.
			_me.select(0);
			
			// Play slide show.
			_me.play();
		},
		
		initPanels: function() {
		
			for (var i = 0; i < _panels.length; i++) {
				_me.initPanel(i);
			}
		},
		
		initPanel: function(which) {
			
			var panel = _panels[which];
			
			// Set index.
			panel.index = which;
			
			// Get element.
			panel.element = YUD.get(panel.id);
			
			// Preload image.
			var img = new Image();
			img.src = panel.url;
			
			// Add click listener.
			YUE.addListener(panel.element, "click", function() { 
					_me.stop();
					_me.changePanel(panel);
				});
		},
		
		changePanel: function(which) 
		{
			
			_me.deselect(_current);
			_me.select(which);
		},
		
		getPanel: function(which) 
		{
			
			if (typeof(which) == "number") {
				return _panels[which];
			} else {
				return which;
			}
		},
		
		select: function(which) {
		
			
			var panel = _me.getPanel(which);
			YUD.addClass(panel.element, CLASS_SELECTED);
			var NumObj;
			if (which.index!=undefined)
			{
			  NumObj=document.getElementById("numberselected"+which.index);
			}
			else
			{
			  NumObj=document.getElementById("numberselected"+which);
			}
			
			if (NumObj!=undefined)
			{
			  YUD.addClass(NumObj,'Se');
			}
			
			
			_image.src = panel.url;
			_current = panel;
		},
		
		deselect: function(which) {
			
			
			var panel = _me.getPanel(which);
			
			if (panel != undefined) 
			{
				YUD.removeClass(panel.element, CLASS_SELECTED);
				var NumObj=document.getElementById("numberselected"+which.index);
			    if (NumObj!=undefined)
			    {
			      YUD.removeClass(NumObj,'Se');
			    }
			}
		},
		
		play: function() {
			_timer = setInterval(function() { _me.next(); }, INTERVAL);
		},
		
		next: function() {
			
			var total = _panels.length;
			
			// Get the index of the next panel.
			var index = _current.index + 1;
			if (index == total) {
				index = 0;
			}
			
			_me.changePanel(index);
		},
		
		stop: function() {
			clearTimeout(_timer);
		}
	} // Return
}(); // Download
