/*
  *	Editor Xtended - Animate It!
  * Copyright (C) 2014 eLEOPARD Design Studios Pvt Ltd. All rights reserved

  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation, either version 3 of the License, or
  * (at your option) any later version.

  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.

  * You should have received a copy of the GNU General Public License
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.

  * For any other query please contact us at contact[at]eleopard[dot]in

*/
var onScrollOffset = {
		scrollOffset: 75
};

onScrollOffset.get = function(){
	return scrollOffset;
};

onScrollOffset.set = function(offset){
	scrollOffset = offset;
};

(function($) {
	$(document).ready(function(){
		var animationStyleClasses = ["animated", "infinite", "bounce", "flash", "pulse", "rubberBand", "shake", "swing", "tada", "wobble", "bounceIn", "bounceInDown", "bounceInLeft", "bounceInRight", "bounceInUp", "bounceOut", "bounceOutDown", "bounceOutLeft", "bounceOutRight", "bounceOutUp", "fadeIn", "fadeInDown", "fadeInDownBig", "fadeInLeft", "fadeInLeftBig", "fadeInRight", "fadeInRightBig", "fadeInUp", "fadeInUpBig", "fadeOut", "fadeOutDown", "fadeOutDownBig", "fadeOutLeft", "fadeOutLeftBig", "fadeOutRight", "fadeOutRightBig", "fadeOutUp", "fadeOutUpBig", "flip", "flipInX", "flipInY", "flipOutX", "flipOutY", "lightSpeedIn", "lightSpeedOut", "rotateIn", "rotateInDownLeft", "rotateInDownRight", "rotateInUpLeft", "rotateInUpRight", "rotateOut", "rotateOutDownLeft", "rotateOutDownRight", "rotateOutUpLeft", "rotateOutUpRight", "hinge", "rollIn", "rollOut", "zoomIn", "zoomInDown", "zoomInLeft", "zoomInRight", "zoomInUp", "zoomOut", "zoomOutDown", "zoomOutLeft", "zoomOutRight", "zoomOutUp"];
		var delayClasses  = ["delay1", "delay2", "delay3", "delay4", "delay5", "delay6", "delay7", "delay8", "delay9", "delay10", "delay11", "delay12"];
		var durationClasses  = ["duration1", "duration2", "duration3", "duration4", "duration5", "duration6", "duration7", "duration8", "duration9", "duration10", "duration11", "duration12", "duration13", "duration14", "duration15", "duration16", "duration17", "duration18", "duration19", "duration20"];
		var scrollOffset = onScrollOffset.get();
		
		$('.eds-on-scroll').each(function(){
			var $module = $(this);
			
			if($module.hasClass('custom'))
			{
				$module.removeClass(function(index, className){
					var removeClassList = "";
					var classList = className.trim().split(/\s+/);
					$.each(classList, function(index, value){
						if((value=='eds-on-scroll') 
							||	($.inArray(value, animationStyleClasses)!==-1) 
							|| ($.inArray(value, delayClasses)!==-1))
							removeClassList += " " + value;							
					});
					
					return removeClassList;					
				});
			}
			else
			{
				var classesToAdd = " eds-scroll-visible ";
						
				$module.removeClass(function(index, className){
					var removeClassList = "";
					var classList = className.trim().split(/\s+/);
					$.each(classList, function(index, value){
						if(($.inArray(value, animationStyleClasses)!==-1) 
							|| ($.inArray(value, delayClasses)!==-1))
							removeClassList += " " + value;							
					});
					
					classesToAdd += removeClassList;			
					return removeClassList;					
				});
				
				$module.addClass("eds-scroll-hidden");
				
				$module.viewportChecker({
			        classToAdd: classesToAdd,
					offset: scrollOffset 
			       });
				
			}		
	
		});	
		
		
		$('.eds-on-click').each(function(){
			var $module = $(this);
			
			if($module.hasClass('custom'))
			{
				$module.removeClass(function(index, className){
					var removeClassList = "";
					var classList = className.trim().split(/\s+/);
					$.each(classList, function(index, value){
						if((value=='eds-on-click') 
							||	($.inArray(value, animationStyleClasses)!==-1) 
							|| ($.inArray(value, delayClasses)!==-1))
							removeClassList += " " + value;							
					});
					
					return removeClassList;					
				});
			}
			else
			{
				var classesToAdd = " ";
					
				$module.removeClass(function(index, className){
					var removeClassList = "";
					var classList = className.trim().split(/\s+/);
					$.each(classList, function(index, value){
						if(($.inArray(value, animationStyleClasses)!==-1) 
							|| ($.inArray(value, delayClasses)!==-1)
							|| ($.inArray(value, durationClasses)!==-1))
							removeClassList += " " + value;							
					});
					
					classesToAdd += removeClassList;			
					return removeClassList;					
				});		
				
				$module.click(function (){
					 $(this).removeClass(classesToAdd).addClass(classesToAdd).one('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend', function(){
					      $(this).removeClass(classesToAdd);
					 });				
				});			
				
			}		
	
		});	
		
		$('.eds-on-hover').each(function(){
			var $module = $(this);
			
			if($module.hasClass('custom'))
			{
				$module.removeClass(function(index, className){
					var removeClassList = "";
					var classList = className.trim().split(/\s+/);
					$.each(classList, function(index, value){
						if((value=='eds-on-hover') 
							||	($.inArray(value, animationStyleClasses)!==-1) 
							|| ($.inArray(value, delayClasses)!==-1))
							removeClassList += " " + value;							
					});
					
					return removeClassList;					
				});
			}
			else
			{
				var classesToAdd = " ";
				
				$module.removeClass(function(index, className){
					var removeClassList = "";
					var classList = className.trim().split(/\s+/);
					$.each(classList, function(index, value){
						if(($.inArray(value, animationStyleClasses)!==-1) 
							|| ($.inArray(value, delayClasses)!==-1)
							|| ($.inArray(value, durationClasses)!==-1))
							removeClassList += " " + value;							
					});
					
					classesToAdd += removeClassList;			
					return removeClassList;					
				});		
				
				var hovered = false;
				$module.hover(					
					function (){
						hovered = true;
						$(this).removeClass(classesToAdd).addClass(classesToAdd).one('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend', function(){
						      $(this).removeClass(classesToAdd);
						});	
						
					},
					function (){			
						hovered = false;
						 $(this).on('webkitAnimationIteration oanimationiteration msAnimationiteration animationiteration', function(e){
							if(!hovered){ 
								$(this).removeClass(classesToAdd);
							}
						 });
					}
				);					
				
			}		
	
		});	
	});	
})(jQuery);