/**
 * jQuery :: emoryCrossSlide
 * -------------------------
 */

(function( $ )
{
	var methods =
	{
		// ****************************
		init : function (options)
		{
            var settings =
            {
              'interval' : 2000,
              'fadeSpeed' : 250,
              'width' : null,
              'height' : null,
              'startAutomatically' : true,
              'direction' : 'forward'
            };
            
            if (options) $.extend(settings, options);
            
            if (settings.direction != 'forward' && settings.direction != 'reverse')
            	$.error("emoryCrossSlide error: 'direction' parameter must be 'forward' or 'reverse'.");
            
            settings.fadeSpeed = parseInt(settings.fadeSpeed);
            
            settings.interval = parseInt(settings.interval);
            
            var context = this;
                        
            return context.each(function()
            {        
              
              var _ecsContainer = $(this);
              
              // ************************************************************************
              // set Height and Width of container
              
              var _ecsHeight = 0;
              var _ecsWidth = 0;
              
              // if settings.width is null, and if a width attribute isn't defined in CSS,
              // then after the window loads, set the container's width equal to
              // the width of the largest child

              if ((settings.width == null) && (_ecsContainer.css('width') == ""))
              {
            	  $(window).load(function()
            	  {
            		  _ecsContainer.children().each(function()
                              {                                          
                                  if ($(this).width() > _ecsWidth)
                                      _ecsWidth = $(this).width();
                              });
            		  
            	  });
              }
              else
              {
            	  _ecsWidth = settings.width;
              }
              
              // if settings.height is null, and if a height attribute isn't defined in CSS,
              // then after the window loads, set the container's height equal to
              // the height of the largest child

              if ((settings.height == null) && (_ecsContainer.css('height') == ""))
              {
            	  $(window).load(function()
            	  {
            		  _ecsContainer.children().each(function()
                              {                                          
                                  if ($(this).height() > _ecsHeight)
                                      _ecsHeight = $(this).height();
                              });
            	  });
              }
              else
              {
            	  _ecsHeight = settings.height;
              }
              
    		  _ecsContainer.width(_ecsWidth);
    		  
    		  _ecsContainer.height(_ecsHeight);
    		  
              // ************************************************************************
    		  // Set position value, if it isn't set already.
    		  
    		  if (_ecsContainer.css("position") == "")
    		  {
    			  _ecsContainer.css("position", "relative");
    		  }
    		  
    		  
              // ************************************************************************
    		  // Layer children absolutely.
              
    		  _ecsContainer.children().each(function()
              {
                  $(this).css('position', 'absolute');
                                    
                  if (!$(this).is("*:first-child"))
                  {
                       $(this).css('opacity', '0');
                       $(this).css('z-index', '0');
                  }    
                  else
                  {
                	  $(this).addClass('_ecsCurrentShot');
                	  $(this).css('z-index', '1');
                  }
                      
              });
    		  
              // ************************************************************************
    		  // Initialize data
    		  
    		  _ecsContainer.data("currentIndex", 0);
    		  _ecsContainer.data("fadeSpeed", settings.fadeSpeed);
    		  _ecsContainer.data("interval", settings.interval);
    		  _ecsContainer.data("direction", settings.direction);
    		  _ecsContainer.data("playing", false);
    		  
              // ************************************************************************
    		  // Start?
    		  
    		  if (settings.startAutomatically === true)
    		  {
    			  _ecsContainer.emoryCrossSlide('play');
    		  }
                                             
            });
			
		},
		
		// ****************************
		pause : function ()
		{
			return this.each(function()
			{
				var _ecsContainer = $(this);
				
				var playing = _ecsContainer.data("playing");
								
				if (playing === true)
				{
					var intervalId = _ecsContainer.data("intervalId");
					
					clearInterval(intervalId);
					
					_ecsContainer.data("playing", false);
					
					_ecsContainer.removeData("intervalId");
					
				}
			});
			
		},
		
		// ****************************
		play : function ()
		{
			return this.each(function()
			{
				var _ecsContainer = $(this);
				
				var playing = _ecsContainer.data("playing");
				
				
				if (playing === false)
				{
					 var interval = _ecsContainer.data("interval");
					 
					 var intervalId = setInterval(function()
				     {
	                    _ecsContainer.emoryCrossSlide('advance');   
				     },
				     interval);
					 
					 _ecsContainer.data("intervalId", intervalId);
					 
					 _ecsContainer.data("playing", true);
				}

			});
		},
		
		// ****************************
		advance : function (arg)
		{
			
            var advanceToNext = false;
            
            var advanceToPrev = false;
            
            var myIndex = NaN;
            
            if (arg != undefined && arg != null)
            	myIndex = parseInt(arg);
            
			return this.each(function()
			{

				var _ecsContainer = $(this);
				
				var direction = _ecsContainer.data("direction");
				
				var fadeSpeed = _ecsContainer.data("fadeSpeed");
				
				_ecsContainer.children('._ecsCurrentShot').each(function()
			    {
                    $(this).removeClass('_ecsCurrentShot');
                    $(this).stop();                                   
                    $(this).animate({ opacity : 0 }, fadeSpeed);
                    $(this).css('z-index', 0);
                    
                    var nextOne;
                                        
					if (!isNaN(myIndex))
					{
						nextOne = $(this).parent().children("*:eq("+myIndex+")");							
					}
					else if (direction == 'forward' || direction == 'reverse')
					{
						if (arg == "next")
						{
							advanceToNext = true;
						}
						else if (arg == "prev")
						{
							advanceToPrev = true;
						}
						
						
	                    if (direction == 'forward' && !advanceToPrev || advanceToNext)
	                    {	     
	                    	
                    		if ($(this).next().length > 0)
		                    {
		                       nextOne = $(this).next();
		                    }
		                    else
		                    {
		                       nextOne = $(this).parent().children('*:first-child');
		                    }
	                    
	                    }
	                    else if (direction == "reverse" && !advanceToNext || advanceToPrev)
	                    {
                    		if ($(this).prev().length > 0)
		                    {
		                       nextOne = $(this).prev();
		                    }
		                    else
		                    {
		                       nextOne = $(this).parent().children('*:last-child');
		                    }
	                    }	                    
					}
					
                    if (nextOne == null)
                    {
                    	$.error("emoryCrossSlide error: unable to determine the next slide item to advance to.");
                    }
                    else
                    {
	                    nextOne.addClass('_ecsCurrentShot');
	                    nextOne.stop();
	                    nextOne.animate({ opacity : 1 }, fadeSpeed);
	                    nextOne.css('z-index', 1);
	                    
	                    var index = _ecsContainer.children().index(nextOne);

	                    _ecsContainer.data("currentIndex", index);
	                    
                    }
                    
                    
		          });
			});
		},
		
		stepBack : function()
		{
			return this.each(function()
			{
				var _ecsContainer = $(this);
				
				var i = _ecsContainer.data("currentIndex");
				
				var direction = _ecsContainer.data("direction");
				
				_ecsContainer.emoryCrossSlide("advance", i);
			});
			
		}
		
	};	// end of methods array
	
	
	// ****************************
	
	$.fn.emoryCrossSlide = function(method)
	{  
		if (methods[method])
		{
			return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
		}
		else if (typeof method === 'object' || !method)
		{
			return methods.init.apply(this, arguments);
		}
		else
		{
			$.error('Method '+method+' does not exist in jQuery.emoryCrossSlide.' );
		}    
	};
	
})( jQuery );
