/**
 * File with slideBox class
 *
 * @author XShady
 * @project itzone
 */


/**
 * Class for creating sliding topics box
 *
 */
slideBox = {
	initialize:
	    function(id, delay, changeInterval, itemsCount)
	    {
	        this.id = (id != '') ? id : 'topicsbox';
	        this.delay = (delay != '') ? delay : 2;
	        this.interval = (changeInterval != '') ? changeInterval + this.delay : 3;
	        this.element = document.getElementById(this.id + '_inner');

	        if (!this.element)
	        {
	            if (this.debug)
	                window.alert('slideBox.initialize() error: Element "' + this.id + '" not found !');
	            return false;
			}
			this.itemsCount = (itemsCount != '') ? itemsCount : 10;

			this.actualPage = 0;
			this.switchSelection(0);

			this.timer = this.timer2 = false;
			this.effect = null;
			this.orientation = 'x';
			this.effect = scrollElement;
			this.pageWidth = 0;
			this.minWaitTime = 0.2;
			this.nextSelection = -1;
			this.inSlide = false;
			this.nextSlideDelay = 0;
			this.linkTimers = [];
			this.linkMouseDelay = 0.5;
			
			// Set neccessary events
			//Page.addEvent('topicsbox_wrapper', 'mouseover', 'slideBox.startSliding()');
			//Page.addEvent('topicsbox_wrapper', 'mouseout', 'slideBox.startSliding()');
			// todo: dont reset all the counter, make it just few second l8te instead...

			// Yeah let's start our nice sliding!
			this.startSliding();

			return true;
		},

	/**
	 * Sliding of pages
	 *
	 */
	slidePage:
	    function()
	    {
			this.switchPage(((this.actualPage + 1) < this.itemsCount) ? this.actualPage + 1 : 0, true);
		},

	startSliding:
	    function()
		{
		    this.stopSliding();
		    if (this.nextSlideDelay > 0)
		    {
		        this.timer = Page.setInterval('slideBox.startSliding();', this.nextSlideDelay * 1000);
	        	this.nextSlideDelay = 0;
			}
			else
			    this.timer = Page.setInterval('slideBox.slidePage();', this.interval * 1000);
		},

	stopSliding:
	    function()
		{
		    Page.clearInterval(this.timer);
		},

	switchPage:
		function(pageNumber, autoSlide)
		{
		    if (this.inSlide)
		    {
		        this.nextSelection = pageNumber;
		        return;
		    }
		    this.inSlide = true;
		    this.nextSelection = -1;
			
			// If page is switched by user, make next slide delay twice longer
			if (autoSlide != true)
				this.nextSlideDelay = this.interval;

		    pageNumber = (pageNumber < this.itemsCount && pageNumber >= 0) ? pageNumber : 0;
		    this.pageWidth = (!this.pageWidth) ? document.getElementById(this.id + '_wrapper').clientWidth : this.pageWidth;
		    var scrollX = (this.actualPage - pageNumber) * this.pageWidth;

			this.effect = new scrollElement(this.id + '_inner', scrollX, 'x', this.delay);
			this.effect.objName = 'slideBox.effect';
			this.effect.onFinish = 'slideBox.onSlideFinish()';
			this.effect.startScroll();
			this.switchSelection(pageNumber);
			this.actualPage = pageNumber;

			return true;
		},

	onSlideFinish:
	    function()
	    {
	        this.inSlide = false;

	        if (this.nextSelection != -1)
	        {
	            this.switchPage(this.nextSelection);
	            this.startSliding();
			}
		},

	/**
	 * Handling of articles links
	 *
	 */
	switchSelection:
	    function(linkNumber)
	    {
			document.getElementById(this.id + '_link' + this.actualPage).className = '';
			document.getElementById(this.id + '_link' + linkNumber).className = 'select';
		},
		
	linkOnMouseOver:
	    function(linkNumber)
	    {
	        this.linkTimers[linkNumber] = Page.setInterval('slideBox.switchPage(' + linkNumber + ', false);', this.linkMouseDelay * 1000);
		},
		
	linkOnMouseOut:
	    function(linkNumber)
	    {
	        Page.clearInterval(this.linkTimers[linkNumber]);
		}
}