$(document).ready( function() {
	
	// add event to next buttons
	$('#'+wdmSlide.targetId+', #quick-slide').click( function(e) {
		var _obj = e.target;
		
		if (_obj.nodeName.toLowerCase() != 'a') return true;
		var _class = $(_obj).attr('class');
		if (!_class) return true;
		
		if (_class.indexOf('wdm-slide-') < 0) return true;
		
		_class = _class.split(' ')[1];
		var _move = _class.split('-')[2];
		
		if (!_move) return true;
		
		var _title = $(_obj).attr('title');
		wdmSlide.move(_move, _title);
		return false;
	});
	
	$('ul.selection-work li a, #see-project').fancybox({
		'overlayShow': true
	});
});

var wdmSlide = {
	
	targetId : 'wdm-slide',
		
	maxRotation : 4,
	
	timeRotation : null,
	
	distance : 980,
	
	init : function (options) {
		$.extend(this, options);
		
		if ( this.maxRotation > 1 ) this.startRotation();
	},
	
	move : function(dist, type) {
		this.stopRotation();
		
		dist = parseInt(dist);
		if ( type == 'Next') dist = dist + 1;
		else if ( type == 'Previous' ) dist = dist - 1;
		
		dist = this.compute(dist);
		
		var _self = this;
		$('#'+this.targetId).animate({marginLeft:dist+'px'}, 'slow');
	},
	
	compute : function(dist) {
		var _distance = parseInt('-'+this.distance);
		return dist * _distance;
	},
	
	startRotation : function() {
		var _self = this;
		
		this.timeRotation = window.setInterval(
			function() {
				var _target = '#'+_self.targetId;
				
				var _currentPos = $(_target).css('margin-left');
				_currentPos = parseInt(_currentPos);
				var _moveTo = _currentPos + parseInt('-'+_self.distance);
				
				var _max = _self.maxRotation * _self.distance;
				
				if (_currentPos <= parseInt( '-'+_max) ) _moveTo = 0;
				
				$(_target).animate({marginLeft:_moveTo+'px'}, 'slow');
			}, 20000
		);
	},
	
	stopRotation : function() {
		window.clearInterval(this.timeRotation);
	}
	
};