/*!
 * Animated Initializer
 * Copyright (c) 2014 Intense Visions, Inc.
 */

var $scrollAnimatedElements = null;
var intenseClickAnimationTimeout = null;

function intenseClickAnimation($el) {
	var animation = $el.data('animation');
	var delay = parseInt($el.data('delay'), 10);
	var reset = parseInt($el.data('reset'), 10);

	$el.addClass(animation);

	if ($el.data('reset')) {
		$el.removeClass('animated');
	}

	clearTimeout(intenseClickAnimationTimeout);

	intenseClickAnimationTimeout = setTimeout(function() {
		$el.addClass('animated');

		if ($el.data('reset')) {
			setTimeout(function() {
				$el.removeClass('animated');
			}, reset);
		}
	}, delay);
}

function intenseHoverAnimation($el, $triggerEl) {
	$el.not('.instantiated').mouseenter(function() {
		var animation = $triggerEl.data('animation');
		var delay = parseInt($triggerEl.data('delay'), 10);

		var timeoutid = setTimeout(function() {
			$triggerEl.addClass('animated').addClass(animation);
		}, delay);

		$triggerEl.data('timeoutid', timeoutid);
	});

	$el.not('.instantiated').mouseleave(function() {
		var animation = $triggerEl.data('animation');

		if (!$triggerEl.data('endhidden')) {
			$triggerEl.removeClass('animated').removeClass(animation);
		}

		if ($triggerEl.data('timeoutid')) {
			window.clearTimeout($triggerEl.data('timeoutid'));
		}
	});
}

function intenseAnimateElement($el) {
	var $ = jQuery;
	var animation = $el.data('animation');
	var overflow = $('body').css('overflow-x');

	$('body').css('overflow-x', 'hidden');
	$el.show().addClass('animated').addClass(animation).removeClass('start-hidden');	

	setTimeout(function() {
		$('body').css('overflow-x', overflow);
	}, 2000);
}

function intenseSetupAnimations() {
	var $ = jQuery;
	var isIE9 = $("#intense-browser-check").hasClass('ie9');
	var isOldIE = $("#intense-browser-check").hasClass('oldie') && !isIE9;
	var iOS = /(iPad|iPhone|iPod)/g.test(navigator.userAgent);

	$scrollAnimatedElements = $('.scroll-animated');

	// show items that are already visible on page
	$scrollAnimatedElements.each(function(index, el) {
		var $el = $(el);
		var scrollPercent = (parseInt($el.data('scrollpercent'), 10) / 100);
		var height = $el.outerHeight();		

		$el.addClass('instantiated');

		$el.appear(function() {
			var animation = $el.data('animation');
			var delay = parseInt($el.data('delay'), 10);

			setTimeout(function() {
				intenseAnimateElement($el);
				if (isIE9 && $el.data('endhidden')) {
					$el.hide();
				}

				$(window).trigger('resize'); //hack for flexslider to show resize itself
			}, delay);
		}, {
			accX: 0,
			accY: height * -scrollPercent
		});
	});

	// disable scroll animations for old versions of IE and iOS
	if (isOldIE || iOS) {
		$scrollAnimatedElements.each(function(index, el) {
			var $el = $(el);

			if ($el.data('starthidden')) {
				$('.animation-placeholder').hide();
				$el.show();
			} else {
				$el.hide();
			}
		});
	}

	$('.click-animated').not('.instantiated').each(function(index, el) {
		var $el = $(el);

		if ($el.data('starthidden')) {
			$el.show();
		}

		if ($el.data('client-id')) {
			$('#' + $el.data('client-id')).click(function() {
				intenseClickAnimation($el);
			});
		}
	});

	$('.hover-animated').not('.instantiated').each(function(index, el) {
		var $el = $(el);

		if ($el.data('starthidden')) {
			$el.show();
		}

		if ($el.data('client-id')) {
			intenseHoverAnimation($('#' + $el.data('client-id')), $el);
		} else {
			intenseHoverAnimation($el, $el);
		}

	});

	$('.click-animated').not('.instantiated').click(function() {
		var $el = $(this);

		if (!$el.data('client-id')) {
			intenseClickAnimation($el);
		}
	});

	$('.click-animated').not('.instantiated').addClass('instantiated');

	$('.hover-animated').not('.instantiated').addClass('instantiated');

	$('.delay-animated').not('.instantiated').each(function() {
		var animation = $(this).data('animation');
		var delay = parseInt($(this).data('delay'), 10);
		var $el = $(this);

		setTimeout(function() {
			intenseAnimateElement($el);
		}, delay);

		$el.addClass('instantiated');
	});
}

jQuery(function($) {
	$(window).load(function() {
		intenseSetupAnimations();
	});
});