/**
 * @author 
 * v. 1.2
 */
(function($) {
	$.fn.frameAnimation = function(settings) {
	var config = {
				'endWithFrameOne': false,
				'delay': 50, 
				'repeat': 1, 
				'mouseOut': false, 
				'hoverMode': false, 
				'reverse': false, 
				'startFrame':1, 
				'looptoStart': true, 
				'animateID': null, 
				'idSymbol': 1,
				'animateTarget': null,
				'targetSymbol': 1,
				'passCount': 1 };
	if (settings) {$.extend(config, settings);}
	settings = config;
	
		if (settings.idSymbol == 1) {
			var idSymbol = '#';
		}
		else { var idSymbol = '.'; }

		if (settings.targetSymbol == 1) {
			var targetSymbol = '#';
		}
		else { var targetSymbol = '.'; }
		
		var aniID = settings.animateTarget;
		var targID = settings.animateID;

		$(idSymbol + targID).each(function() {		
			// Get the DIV height.  This is the STEP to animate each "frame".
			var step =  parseInt($(idSymbol + targID).outerHeight());

			var limit =  parseInt(backgroundPosition($(idSymbol + targID)).split('px')[1]);
			var offset = limit;
		
			if (settings.imageHeight != undefined) 
			{
				var height = settings.imageHeight;				

				// the LAST frame in the image. go no further, thus it's a limit.
				limit = step - height; 

				// start of frame 1 is actually 0, so frame 2 - 1 times step = the end of frame 1, or start of frame 2.
				offset = (settings.startFrame-1)*step; 

				// 0px + 0px for frame 1, 0px + offsetpx for frame 2, etc.
				$(idSymbol + targID).css("backgroundPosition", "0px " + offset + "px");
				
			}
		
			var delay = settings.delay;			
			var numRepeatOver = settings.repeat;
			var numRepeatOut = settings.repeat;
			numRepeatOut = settings.numRepeatOut;
			var repet = 1;
			var t;
			var movingUp = false;
			var movingDown = false;
			var animationComplete = false;
			var passCount = settings.passCount;

/*///////////////////////// FUNCTIONS ///////////////////////////////////////*/	
	
			function backgroundPosition(obj)
			{
				bgPos = obj.css('background-position');
				if(typeof(bgPos) === 'undefined') { bgPos = obj.css('background-position-x') + ' ' + obj.css('background-position-y') };
				return bgPos;
			}	
			
			var moveDown = function(obj)
			{
				offset += step;
				obj.css("backgroundPosition", "0px " + offset + "px");
			}
		
			var moveUp = function(obj)
			{
				offset -= step;
				obj.css("backgroundPosition", "0px " + offset + "px");
			}

	
			var animateDown = function(obj, times){
			if (movingUp == true) return;
			
			offset = parseInt(backgroundPosition(obj).split('px')[1]);
			t = null;
			if (offset  >= 0) {
				
					offset = limit;
				if (repet == times){
					
					if (times < numRepeatOut && settings.hoverMode == true && settings.mouseOut == true) repet = numRepeatOut;
					if (settings.mouseOut == false){
						repet = 1;
						obj.css("backgroundPosition", "0px " + offset + "px");
					}
					if (settings.endWithFrameOne == true && hoverMode == false){
						obj.css("backgroundPosition", "0px " + offset + "px");
						repet = 0;
					}
					movingDown = false;
					
					return;
				}
				repet++;
			
			}
			if (movingUp == false)
				moveDown(obj);

			t = setTimeout(function(){animateDown(obj, times)}, delay);
		}
		
		var animateUp = function(obj, times){
			
			if (repet > times) {   // if numRepeatOver and numRepeatOut are set differently...
				repet = times;
			}
			
			if (movingDown == true) return;
			
			offset = parseInt(backgroundPosition(obj).split('px')[1]);
			t = null;
			if (offset  <= limit ) {
			
					offset = 0;
					

				if (repet <= 1){
					if (settings.mouseOut == false){
						repet = 1;
						obj.css("backgroundPosition", "0px " + offset + "px");
					}
					movingUp = false;
					return;
				}
			
				repet--;
			}
			if (movingDown == false)
				moveUp(obj);
			t = setTimeout(function(){animateUp(obj, times)}, delay);
		}

	
			var animateLoop = function(obj, times)
			{
				if (animationComplete == true) 
				{
					return;
				}	
			

				offset = parseInt(backgroundPosition(obj).split('px')[1]);
				t = null;
				
				if ((offset <= limit) || (movingDown == true))
				{
					movingDown = true;
			
					if ((offset != 0) || (offset < 0))
					{
						moveDown(obj);
						passCount--;
					}
					
					else if ((offset == 0) || (offset > 0)) 
					{
						movingDown = false;						
						animationComplete = true;
						return;
		
					}
						
					else if ((targID == "boxAnimate") && (passCount < 6)) {						
						t = setTimeout(function(){animateLoop(obj, times)}, slower);
					}
					else if ((targID == "boxAnimate") && (passCount > 5)) {
						t = setTimeout(function(){animateLoop(obj, times)}, fast);
					}
					else {
						t = setTimeout(function(){animateLoop(obj, times)}, slower);
					}
				}

				if ((movingDown == false) && (animationComplete == false)) 
				{
					moveUp(obj);
					passCount++;
				}

				if ((targID == "boxAnimate") && (passCount < 6)) {											
					t = setTimeout(function(){animateLoop(obj, times)}, slow);
					
				}
				else if ((targID == "boxAnimate") && (passCount > 5)) {
					t = setTimeout(function(){animateLoop(obj, times)}, fast);
				}
				
				else {
					t = setTimeout(function(){animateLoop(obj, times)}, slow);
				}				
			}
	
/*///////////////////////// LOGIC ///////////////////////////////////////*/	
			if ((settings.hoverMode == true) && (settings.mouseOut != false))
			{				
				$(targetSymbol + aniID).hover(function(evt){
					$('.hello').html(targetSymbol + aniID);
					movingDown = false;
					movingUp = true;
					animateUp($(idSymbol + targID),numRepeatOut);
					return false;
				},function(evt) {
					movingUp = false;
					movingDown = true;
					animateDown($(idSymbol + targID), numRepeatOver);
					return false;
				});
			}						

			else if ((settings.hoverMode == false) && (settings.looptoStart == true))
			{		
				var slow = delay;
				var slower = slow + 100;
				var fast = slow - 70;		
				animateLoop($(idSymbol + targID), numRepeatOver);
			}
		}); 

		return this;

	};

})(jQuery);


