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

jQuery(function($) {
	function intenseAnimateSlideImage($el) {
		var currentPosition = $el.data('current-position');
		var direction = $el.data('direction');
		var anchor = $el.data('anchor');

		if (direction === 'slideleft' || direction === 'slideup') {
			// 1 pixel row at a time
			currentPosition -= 1;
		} else {
			currentPosition += 1;
		}

		$el.data('current-position', currentPosition);

		// move the background with backgrond-position css properties
		$el.css("backgroundPosition", (direction === 'slideleft' || direction === 'slideright') ?
			currentPosition + "px " + anchor + "%" : anchor + "% " + currentPosition + "px");
	}

	function intenseSetupAnimatedSlideImage(i, element) {
		var $el = $(element);
		var speed = $el.data('speed');

		$el.data('current-position', 0);

		setInterval(function() {
			intenseAnimateSlideImage($el);
		}, speed);
	}

	function intenseSetupAnimatedScrollImage(i, element) {
		var $el = $(element);
		var $image = $el.find('img');
		var direction = $el.data('direction');		
		var cssAttribute;		

		$el.mouseenter(function() {
			var distance;
			var imageWidth = $image.width();
			var imageHeight = $image.height();	
			var containerWidth = $el.width();
			var containerHeight = $el.height();

			switch(direction) {
				case 'scrollup':
					cssAttribute = 'top';
					distance = (imageHeight - containerHeight);
					break;
				case 'scrolldown':
					cssAttribute = 'bottom';
					distance = (imageHeight - containerHeight);
					break;
				case 'scrollleft':
					cssAttribute = 'left';
					distance = (imageWidth - containerWidth);
					break;
				case 'scrollright':
					cssAttribute = 'right';
					distance = (imageWidth - containerWidth);
					break;
			}

			if (!$image.hasClass('activated')) {
				$image.css(cssAttribute,  "-" + distance + "px");//);
			} else {
				$image.addClass('activated');
			}
		});

		$el.mouseleave(function() {
			$image.removeClass('activated');
			$image.css(cssAttribute, 0);
		});
	}

	$(window).load(function() {
		$('.animated-image.slide').each(intenseSetupAnimatedSlideImage);
		$('.animated-image.scroll').each(intenseSetupAnimatedScrollImage);	
	});	
});