/**
*
*	Midnight Auto Concepts Javascript (Main Page)
*
**/
(function($){
	$.fn.extend( {
		simpleSlider: function(options){
			/**
			 * Set default options
			 */
			var defTitleStyle		=  { color: '#fff', fontSize: '14px', fontFamily: 'Arial', fontWeight: 'bold', margin: '0 10px 0 10px', textAlign: 'left' };
			var defSubtitleStyle	=  { color: '#fff', fontSize: '10px', fontFamily: 'Verdana', margin: '0 10px 0 10px', textAlign: 'left' };
			var defaults = {
								alpha			: '0.6',
								bgColor			: '#000',
								bgSpeed			: 1000,
								captionSpeed	: 'slow',
								height			: '35px',
								timeOut			: 7500,
								titleStyle		: defTitleStyle,
								subtitleStyle	: defSubtitleStyle
						   };
			/**
			 * Merge defaults options with params options
			 */
			var options = $.extend(defaults, options);
			
			return this.each(function(){
				var element = $(this);
				
				/**
				 * Get Width and Height of the first image
				 */
				var w	= element.find('img').eq(0).width();
				var h	= element.find('img').eq(0).height();
				
				/**
				 * Margin for show caption
				 */
				var ms	=  '-'+options.height;
				
				/**
				 * half size of caption
				 */
				var hsc	= String( parseInt( options.height ) / 2 )+'px';
				
				/**
				 * List of all slide
				 */
				var items	= [];
				var index	= 0;
				
				/**
				 * get speed
				 */
				var bgSpeed			= isNaN( parseInt( options.bgSpeed ) ) ? options.bgSpeed : parseInt( options.bgSpeed );
				var captionSpeed	= isNaN( parseInt( options.captionSpeed ) ) ? options.captionSpeed : parseInt( options.captionSpeed );
				
				var start	= function() {
					$( items[ index ] ).fadeIn( bgSpeed,
						function() { 
							var timeOut = ( $(items[ index ]).data('timeOut') == undefined ) ? parseInt( options.timeOut ) : $(items[ index ]).data('timeOut');
							$( items[ index ] ).children('div').animate( { marginTop: ms }, captionSpeed, function() { setTimeout( nextItem, timeOut ); } );
						} 
					);
				}
				
				var nextItem	= function() {
					$( items[ index ] ).children('div').animate( { marginTop: '0' }, captionSpeed, function() { $(this).parent().fadeOut( bgSpeed, start ); } );
					index++; if( index > ( items.length-1) ) index = 0;
				}
				
				/**
				 * Avoid ul standard style
				 */
				element.css( { 
							listStyle	: 'none',
							padding		: '0px',
							width		: w,
							height		: h,
							overflow	: 'hidden'
						  } )
					.children('li')
					.css( {	
							margin		: '0', 
							display		: 'block',
							width		: w+'px',
							height		: h+'px',
							overflow	: 'hidden'
						  } )
					.append( '<div><h1></h1><p><p></div>' )
					.children('div')
					.css({ 
							position		: 'relative',
							backgroundColor	: options.bgColor,
							opacity			: options.alpha,
							width			: w+'px',
							height			: options.height,
							overflow		: 'hidden'
						 });
						 
				element.find('li')
					.each(
						function(i,e) {
							/**
							 * + Add $Rel.1.1.0
							 * Check rel attribute for custom timeout in each single slide
							 */
							var r = $(this).attr('rel');
							if( r != undefined ) {
								if( r.indexOf( 'simpleSlider' ) != -1 ) {
									eval ( 'var o = ' + ( r.substr( r.indexOf( '{' ), ( r.length - 1 ) ) ) );
									$(e).data ( 'timeOut', o.timeOut );
								}
							}
							 
							var t = $(this).attr('title');
							var s = $(this).find('img').eq(0).attr('title');
							// collection
							items[ i ]	= e;
							$(e).hide();

							$(this).find('h1')
								.css( defTitleStyle )
								.css( options.titleStyle )
								.html( t )
								.next('p')
								.css( defSubtitleStyle )
								.css( options.subtitleStyle )
								.html( s );
						}
					);
				start();	// play the game						 
			});
		}
	});
})(jQuery);