/**
 * params
 *	elemCarousel
 *	perMove
 */
 

 
function wdm_slide( objParams ) {
	objParams = objParams || {};
	var _iterator;
	for (_iterator in objParams) {
		this[_iterator] = objParams[_iterator];
	}
	//this.perMove = this.perMove * -1;
    this.elemChildCarousel = this.elemCarousel.find('li');
    this.maxMove = this.elemChildCarousel.length - 1;
    //console.log(this.maxMove);
	this.currentMove = 0;
	this.timer = null;
	var _self = this;
	
	this.move = function(moveTo) {
			
		if ( moveTo > this.maxMove) {
            moveTo = 0;
		}
        else if ( moveTo < 0 ) {
            moveTo = this.maxMove;
        }
        
        /*
        // next btn
        if (moveTo <= this.maxMove)
            _self.nextBtn.hide();
        else
            _self.nextBtn.show();
            
        // prev btn
        if (moveTo >= 0)
            _self.prevBtn.hide();
        else
            _self.prevBtn.show(); */
        this.stop();
		this.elemChildCarousel.filter(':visible').fadeOut('normal', function() {
               _self.elemChildCarousel.eq(moveTo).fadeIn('normal');
               _self.currentMove = moveTo;
               _self.start();
        });
        
		//this.currentMove = moveTo;
	};
	
	this.start = function() {
        if (_self.timer ) return;
		_self.timer = setTimeout(function() {
			var _moveTo = _self.currentMove + 1;
            //console.log(_self.currentMove);
			_self.move(_moveTo);
		}, _self.speed);
	};
	
	
	this.stop = function() {
		clearTimeout(_self.timer);
		_self.timer = null;
	};
	
	this.start();
	
	this.next = function() {
		var _moveTo = _self.currentMove + 1;
		_self.move(_moveTo);
	}
	
	this.prev = function() {
		var _moveTo = _self.currentMove - 1;
		_self.move(_moveTo);
	}
    this.nextBtn.click( function () { 
        _self.next();
        return false;
    });
    
    this.prevBtn.click( function () { 
        _self.prev();
        return false;
    }); 
}

$( function() {
    // homepage
       var _pSlider = $('div.header div.portfolio-slider ul');
       
       _pSlider.find('li:not(:first)').hide();
       if ( _pSlider.length > 0 ) {
           new wdm_slide( {
               speed : 5000,
               elemCarousel : _pSlider,
               nextBtn : _pSlider.nextAll('a.sc-right'),
               prevBtn : _pSlider.nextAll('a.sc-left')
           });
       }
       
       
    // portfolio
    if ( $('div.container-wrap ul.pfolio').length < 1 ) return;
    
    $('div.container-wrap ul.pfolio div.foliocont ul').each( function() {
            var _me = $(this);
            
            new wdm_slide( {
               speed : 5000,
               elemCarousel : _me,
               nextBtn : _me.nextAll('a.sc-right'),
               prevBtn : _me.nextAll('a.sc-left')
           });
       
       });
});
