/**
 * Class for animating multi-paged sliding box
 *
 * @author XShady
 * @project itzone
 */


/**
 * Class for creating sliding topics box
 *
 */
ITzone.ui.slideBox = {
    // General options
	containerId: null,
	autoSlideTime: 7000,
	minMouseOverTime: 1000,
	effectDuration: 1500,
	// Other configurable properties
	linkIdSuffix: '_link',
	pageIdSuffix: '_page',
	wrapperIdSuffix: '_wrapper',
	innerIdSuffix: '_inner',
	selectedClass: 'select',
	// Internal properties
	pagesCount: 0,
	pageWidth: 0,
	currentPage: 0,
	nextPage: -1,
	isSliding: 0,
	autoSlideTimer: null,

	create: function(options) {
	    // Process options
		this.containerId = (options.id) ? options.id : options.containerId;
		this.autoSlideTime = (options.autoSlideTime) ? options.autoSlideTime : this.autoSlideTime;
		this.effectDuration = (options.effectDuration) ? options.effectDuration : this.effectDuration;
		this.minMouseOverTime = (options.minMouseOverTime) ? options.minMouseOverTime : this.minMouseOverTime;
	
	    $(document).ready(function() { ITzone.ui.slideBox.init(); });
	},
	
	init: function() {
	    // Get some data
		this.pageWidth = $('#' + this.containerId + this.wrapperIdSuffix).width();
		this.pagesCount =  $('div[id^="' + this.containerId + this.pageIdSuffix + '"]').size();
		
		// Register events
		for(i = 0; i < this.pagesCount; i++) {
		    link = $('#' + this.containerId + this.linkIdSuffix + i);

			link.data('pageId', i);
			link.data('isMouseOver', 0);
		    link.bind('mouseenter', function(eventElm) {
		        e = eventElm.currentTarget.id;
		        $('#' + e).data('isMouseOver', 1);
				$('#' + e).data('timer', setInterval("if ( $('#" + e + "').data('isMouseOver') ) ITzone.ui.slideBox.slideTo( $('#" + e + "').data('pageId') )", ITzone.ui.slideBox.minMouseOverTime));
			});
			link.bind('mouseleave', function(eventElm) {
		        e = eventElm.currentTarget.id;
		        $('#' + e).data('isMouseOver', 0);
		        clearInterval($('#' + e).data('timer'));
			});
		}
		
		// Start automatic sliding
		this.resetAutoSlide();
	},
	
	slideTo: function(pageId) {
	    if (pageId == this.currentPage) return;
	    if (this.isSliding) {
			this.nextPage = pageId;
			return;
			}

		// Do sliding animation
		var scrollX = Math.abs((pageId - this.currentPage) * this.pageWidth);
		var scrollDir = (pageId > this.currentPage) ? '-' : '+';
		this.isSliding = 1;

        $('#' + this.containerId + this.innerIdSuffix).animate(
			{ "margin-left": scrollDir + '=' + scrollX + 'px' },
			this.effectDuration,
			'swing',
			function() {
			    ITzone.ui.slideBox.isSliding = 0;
				if (ITzone.ui.slideBox.nextPage != -1) {
        			ITzone.ui.slideBox.slideTo(ITzone.ui.slideBox.nextPage);
        			ITzone.ui.slideBox.nextPage = -1;
        		} else {
        		   ITzone.ui.slideBox.resetAutoSlide();
				}
			});

		// Select new link in navigation
		$('#' + this.containerId + this.linkIdSuffix + this.currentPage).removeClass(this.selectedClass);
		$('#' + this.containerId + this.linkIdSuffix + pageId).addClass(this.selectedClass);
		
		// Switch page number
		this.currentPage = pageId;
	},
	
	slideToNext: function() {
		if (this.nextPage != -1)
		    return;
		
	    if (this.currentPage + 1 == this.pagesCount)
	        this.slideTo(0);
		else
		    this.slideTo(this.currentPage + 1);
	},
	
	resetAutoSlide: function() {
	    if (this.autoSlideTimer != null)
	    	clearInterval(this.autoSlideTimer);;
	    this.autoSlideTimer = setInterval('ITzone.ui.slideBox.slideToNext()', this.autoSlideTime);
	}
}
