/**
 * @author:  Ivan Andonov
 * @email:   ivan.andonov[at]design[dot]bg
 * 
 * @require: dbg.Delegate, dbg.Cookies, jquery
 * @use:     dbg.Debug
 **/

dbg.custom.SampleSlideshow = function(selectors, props, className) {
	
	this.className = className || dbg.custom.SampleSlideshow.className + ++dbg.custom.SampleSlideshow.s_counter;
	
	// selectors
	this.selectors = {
		links : 'div.links a',
		conts : 'div.offers:eq(0) div.offer_holder',
		next : null,
		prev : null
	};
	for (var i in selectors) {
		this.selectors[i] = selectors[i];
	}
	
	// PRIVATE PROPS
	this.timeoutId = null;
	this.lastCont = null;
	this.hiding = false;
	this.totalConts = 0;
	this.currentCont = 0;
	
	// PUBLIC PROPS
	this.currentLinkClass = 'current';
	this.hiddenContClass = 'seo_hidden';
	this.cookieName = '';
	this.fadeSpeed = 1000;
	this.changeTime = 5;
	this.paused = false;
	
	for (var prop in props) {
		this[prop] = props[prop];
	}
	
	// methods
	this.toString = function() {
		return this.className;
	};
	
	this.init = function() {
		$log(this+'.init');
		
		var cls = this;
		
		this.links = jQuery(this.selectors.links).each(function(index, obj) {
			jQuery(this).bind('click', $delegate(cls, 'onLinkClick', index, this));
		});
		
		this.conts = jQuery(this.selectors.conts);
		
		if (this.selectors.next) {
			this.nextBnt = jQuery(this.selectors.next).bind('click', $delegate(cls, 'next'));
		}
		if (this.selectors.prev) {
			this.prevBnt = jQuery(this.selectors.prev).bind('click', $delegate(cls, 'prev'));
		}
		
		this.totalConts = this.links.length;
		
		var lastCont = Number($cookie(this.cookieName));
		this.currentCont = isNaN(lastCont) ? 0 : (lastCont >= this.totalConts ? 0 : lastCont);
		$cookie('lastCont', this.currentCont);
		
		this.lastCont = this.conts.addClass(this.hiddenContClass).eq(this.currentCont).removeClass(this.hiddenContClass);
		
		//$log('currentCont='+this.currentCont);
		//$log('totalConts='+this.totalConts);
		
		this.swapConts(this.currentCont, true);
	};
	
	this.onLinkClick = function(index, link) {
		//$log(this+'.onLinkClick index='+index+' link='+link);
		if (index != this.currentCont) {
			this.swapConts(index);
		}
		return false;
	};
	
	this.swapConts = function(index, instant) {
		//$log(this+'.swapConts index='+index+' instant='+instant);
		
		this.clearTimeout();
		
		$cookie(this.cookieName, index);
		this.currentCont = index;
		
		this.links.removeClass(this.currentLinkClass).eq(index).addClass(this.currentLinkClass);
		if (instant) {
			this.showCont(instant);
		} else if (!this.hiding) {
			this.hideCont();
			//this.showCont();
		}
	};
	
	this.hideCont = function() {
		//$log(this+'.hideCont');
		
		this.hiding = true;
		if (this.lastCont && this.lastCont.length) {
			//this.lastCont.stop().fadeOut(this.fadeSpeed, $delegate(this, 'showCont'));
			this.lastCont.stop().animate({ opacity:0 }, this.fadeSpeed, 'linear', $delegate(this, 'showCont'));
		} else {
			this.showCont();
		}
	};
	
	this.showCont = function(instant) {
		//$log(this+'.showCont instant='+instant+' currentCont='+this.currentCont);
		
		this.hiding = false;
		if (this.lastCont) {
			this.lastCont.addClass(this.hiddenContClass);
		}
		this.lastCont = this.conts.eq(this.currentCont);
		if (this.lastCont && this.lastCont.length) {
			this.lastCont.removeClass(this.hiddenContClass); //.stop();
			if (instant) {
				this.onShowCont();
			} else {
				//this.lastCont.stop().hide().fadeIn(this.fadeSpeed, $delegate(this, 'onShowCont'));
				this.lastCont.stop().animate({ opacity:1 }, this.fadeSpeed, 'linear', $delegate(this, 'onShowCont'));
			}
		}
	};
	
	this.onShowCont = function() {
		//$log(this+'.onShowCont');
		if (!this.paused) {
			this.start();
		}
	};
	
	this.clearTimeout = function() {
		//$log(this+'.clearTimeout');
		clearTimeout(this.timeoutId);
	};
	
	// navigation
	this.next = function() {
		//$log(this+'.next');
		this.swapConts(++this.currentCont%this.totalConts);
		return false;
	};
	
	this.prev = function() {
		//$log(this+'.prev');
		if (--this.currentCont < 0) {
			this.currentCont = this.totalConts-1;
		}
		this.swapConts(this.currentCont);
		return false;
	};
	
	this.start = function() {
		//$log(this+'.start');
		this.paused = false;
		this.timeoutId = setTimeout($delegate(this, 'next'), this.changeTime*1000);
	};
	
	this.stop = function() {
		//$log(this+'.stop');
		this.paused = true;
		this.clearTimeout();
	};
	
};

dbg.custom.SampleSlideshow.s_counter = 0;
dbg.custom.SampleSlideshow.className = 'SampleSlideshow';