/*
 * vcs - vioma content slider
 * 
 */
if ( typeof vcs == 'undefined' ) {
	if ( window.console && window.console.info ) {
		console.info( 'vioma content slider' );
		console.info( 'Version 0.1 beta' );
	}
	
	vcs = {
		timer: false,
		settings: {},
		counter: 0,
		animate: function( identifier, backward ) {
			if ( identifier === undefined || !vcs.settings[identifier] ) return false;
			
			var left = vcs.settings[identifier].width,
				left_current = Math.abs( parseInt( vcs.settings[identifier].container_inner.css( 'left' ), 10 ) ),
				left_max = ( vcs.settings[identifier].children.size() - 1 ) * vcs.settings[identifier].width;
			
			left_current = parseInt( left_current / vcs.settings[identifier].width ) * vcs.settings[identifier].width;
			
			if (backward) {
				left = left_current - left;
				
				if (left < 0) {
					left = left_max;
				}
			}
			else {
				if (left_current < left_max) {
					left = left_current + left;
				}
				else {
					left = 0;
				}
			}
			
			jQuery( vcs.settings[identifier].pagina.container )
				.children()
				.removeAttr( 'class' )
				.eq( parseInt( left / vcs.settings[identifier].width ) )
				.attr( 'class', 'active' );
			
			vcs.settings[identifier].container_inner.animate({ left: '-' + left }, { queue: false });
		},
		start: function( identifier ) {
			if ( identifier === undefined || !vcs.settings[identifier] ) return false;
			
			vcs.settings[identifier].timer = window.setInterval(function(){
				vcs.animate( identifier );
			}, vcs.settings[identifier].interval );
		},
		stop: function( identifier ) {
			if ( identifier === undefined || !vcs.settings[identifier] ) return false;
			
			window.clearInterval( vcs.settings[identifier].timer );
		},
		create: function( container, options ) {
			identifier = vcs.counter++;
			var container = jQuery( container ),
				container_inner = container.children().eq( 0 ),
				children = container_inner.children(),
				width = children.eq( 0 ).outerWidth();
				
			vcs.settings[identifier] = jQuery.extend(
				true,
				{
					container: container,
					container_inner: container_inner,
					children: children,
					width: width,
					pagina: {
						container: false,// jQuery selector
						path: false,// path to image
						path_active: false// path to image
					},
					links: {
						prev: false,// jQuery selector
						next: false// jQuery selector
					},
					interval: 5000000
				},
				options
			);
		
			if ( !vcs.settings[identifier].container[0] ) return;
			
			var container = vcs.settings[identifier].container,
				container_inner = vcs.settings[identifier].container_inner,
				children = container_inner.children();
		
			vcs.settings[identifier].container
				.attr( 'vcs_element', identifier )
				.css({
					position: 'relative',
					overflow: 'hidden'
				})
				.bind( 'mouseenter', function( j ){
					vcs.stop( jQuery( this ).attr( 'vcs_element' ) );
				})
				.bind( 'mouseleave', function( j ){
					vcs.start( jQuery( this ).attr( 'vcs_element' ) );
				});
			
			container_inner
				.css({
					position: 'absolute',
					top: 0,
					left: 0,
					height: container.css( 'height' ),
					width: children.size() * vcs.settings[identifier].width
				});
				
			vcs.start( identifier );
			
			if ( vcs.settings[identifier].links ) {
				if (vcs.settings[identifier].links.prev) {
					jQuery(vcs.settings[identifier].links.prev)
						.filter( ':not([vcs_element])' )
						.filter( ':first' )
						.attr('vcs_element', identifier)
						.bind('click', function(){
							var identifier = jQuery( this ).attr( 'vcs_element' );
							vcs.stop(identifier);
							vcs.animate(identifier, true);
							vcs.start(identifier);
							return false;
						});
				}
				
				if (vcs.settings[identifier].links.next) {
					jQuery(vcs.settings[identifier].links.next)
						.filter( ':not([vcs_element])' )
						.filter( ':first' )
						.attr('vcs_element', identifier)
						.bind('click', function(){
							var identifier = jQuery( this ).attr( 'vcs_element' );
							vcs.stop(identifier);
							vcs.animate(identifier);
							vcs.start(identifier);
							return false;
						});
				}
			}
			
			if ( vcs.settings[identifier].pagina.container ) {
				vcs.settings[identifier].children.each(function( i ){
					jQuery( document.createElement( 'a' ) )
						.attr( 'class', ( i == 0 ? 'active' : '' ) )
						.attr( 'href', '#' )
						.attr( 'vcs_element', identifier )
						.html( vcs.settings[identifier].pagina.path ? '<img src="' + vcs.settings[identifier].pagina.path + '">' + ( vcs.settings[identifier].pagina.path_active ? '<img src="' + vcs.settings[identifier].pagina.path_active + '">' : '' ) : ( i + 1 ) )
						.bind( 'click', function(){
							 vcs.stop( jQuery( this ).attr( 'vcs_element' ) );
							 vcs.animate( jQuery( this ).attr( 'vcs_element' ) );
							 return false;
						})
						.appendTo( vcs.settings[identifier].pagina.container );
				});
			}
		}
	};

	(function(jQuery) {
		jQuery.fn.vcs = function( options ) {
			this.each(function() {
				vcs.create( this, options );
			});
			
			return this;
		};
	})(jQuery);
}
