/**
 * @author 
 * v. 1.2
 */
(function($) {
    $.fn.frameAnimation = function(settings) {
    var config = {
                'endWithFrameOne': false,
                'delay': 50, 
                'repeat': 1, 
                'mouseOut': false, 
                'hoverMode': false, 
                'direction': "up", 
                'startFrame':1, 
                'looptoStart': true, 
                'animateID': null, 
                'idSymbol': 1,
                'animateTarget': null,
                'targetSymbol': 1,
				'skipMath': 0,
                '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;

		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;
		var limit, offset, step;

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

    	        limit =  parseInt(backgroundPosition($(idSymbol + targID)).split(' ')[1]);
        	    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");
				}			
		}
        
		else {
			// Get the DIV height.  This is the STEP to animate each "frame".
				var height = settings.imageHeight;      
			    step =  parseInt($(idSymbol + targID).outerHeight());
				limit = step - height; 				
				offset = $(idSymbol + targID).css('background-position');
				offset = offset.split(' ')[1];
			}	


/*///////////////////////// 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 animateUp = function(obj, times) {
			offset = parseInt(backgroundPosition(obj).split(' ')[1]);
			t = null;

			if (offset != limit) {
               	moveUp(obj);
				if (offset != limit) {
               		t = setTimeout(function(){animateUp(obj, 1)}, normal);
				}
				else {
					return;
				}
			}
		}

		var animateDown = function(obj, times) {
			offset = parseInt(backgroundPosition(obj).split(' ')[1]);
			//t = null;

			if (offset != 0) {
               	moveDown(obj);
				if (offset != 0) {
               		t = setTimeout(function(){animateDown(obj, 1)}, normal);
				}
				else {
					return;
				}
			}
		}

    
        var animateLoop = function(obj, times) {
			if (animationComplete == true) {
            	return;
			}    
            
			offset = parseInt(backgroundPosition(obj).split(' ')[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)}, slow);
				}
				else if ((targID == "boxAnimate") && (passCount > 5)) {
					t = setTimeout(function(){animateLoop(obj, times)}, fast);
				}
				else {
					t = setTimeout(function(){animateLoop(obj, times)}, slow);
				}
			}

			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 == true))
            {                
                $(targetSymbol + aniID).hover(function(evt){
				$('#hello').html(offset);
                    movingDown = false;
                    movingUp = true;
                    animateUp($(idSymbol + targID),numRepeatOut);
                },function(evt) {
                    //movingUp = false;
                    //movingDown = true;
                    //animateDown($(idSymbol + targID), numRepeatOver);
                });
            }                        

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

			else if ((settings.hoverMode == false) && (settings.looptoStart == false && (settings.direction == "up")))
            {        
                var normal = delay;
                var slow = normal + 100;
                var fast = normal - 70;
               	animateUp($(idSymbol + targID), numRepeatOver);
            }

			else if ((settings.hoverMode == false) && (settings.looptoStart == false && (settings.direction == "down")))
            {        
                var normal = delay;
                var slow = normal + 100;
                var fast = normal - 70;        
                animateDown($(idSymbol + targID), numRepeatOver);
            }
};
})(jQuery);


