/* Author: CM - MindWorks Marketing http://www.mindworks.co.uk */

(function($){
 
 
	$.fn.newsRoller = function(options){

		var element = this; 

		// options
		var defaults = {
			tag: 'a',
			dwell: 3000,
			duration: 500
		};
		var options = $.extend(defaults, options);	
		
		// get a list of photos to rotate through
		var rollerAnchors = element.children(options.tag);
			
		// if only one element, exit;
		if (rollerAnchors.length < 2) {
			return;
		}
			
		// set up rollers
		
			// js method
//			if (!Modernizr.csstransitions) {
			
//				rollerAnchors.css({ 'opacity':0 });
//				rollerAnchors.first().css({ 'opacity':1 });
			
			// css transition method
//			} else {
//			
//				rollerAnchors.addClass('newsroller');
//				rollerAnchors.first().addClass('newsroller_on');
//				
//			}
			
		// the height of one item
		var itemHeight = rollerAnchors.first(options.tag).height();
		var itemWidth = rollerAnchors.first(options.tag).width();
		
		go();
		
		function go() {
			
			var t = setTimeout ( function() {
				
				// animation
				
					// js method
//					if (!Modernizr.csstransitions) {
					
						rollerAnchors.first(options.tag).animate({ 'opacity':0 , 'margin-left':-20 } , options.duration);
					
					// css transition method
//					} else {
//					
//						rollerAnchors.first(options.tag).addClass('newsroller_off');
//						
//					}
					
				
				// 
				
				var n = setTimeout ( function() {
				
					// clone the now faded out front pic to the back of the stack
					var oldHero = rollerAnchors.first(options.tag);
					element.append(oldHero);
					
					// reinitialize the fader
					rollerAnchors = element.children(options.tag);
				
					// js method
//					if (!Modernizr.csstransitions) { 
					
						rollerAnchors.last(options.tag).css({ 'margin-left':0 });
						rollerAnchors.first(options.tag).animate({ 'opacity':1 } , options.duration);
					
//					// css transition method
//					} else {
//					
//						rollerAnchors.last(options.tag).removeClass('newsroller_off');
//						rollerAnchors.last(options.tag).removeClass('newsroller_on');
//						rollerAnchors.first(options.tag).addClass('newsroller_on');
//						
//					}
					
					// run again
					go();
					
				}, options.duration + 1000)
			
			}, options.dwell ); /* time visible */
		}
			
		
	};
 
 
})(jQuery);



