var swipeTimout;
function slideswiper(name)
{
	var settings = swipeSettings(name);
    var width = $xo('#'+ name).parent().width();
	
    if( /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) )
    {
        $xo('#'+ name).css({ 'max-width': width +'px' });
        
        $xo(window).on('orientationchange',function()
        {
            var rotation = $xo('#'+ name).attr('data-rotated');
            if (rotation == 'true')
            {
                var width = $xo('#'+ name).attr('data-width');
                $xo('#'+ name).css({ 'max-width': width +'px' });
                $xo('#'+ name).attr({ 'data-rotated':'false' });
            }
            else
            {
                var newWidth = $xo('#'+ name).parent().width();
                $xo('#'+ name).css({ 'max-width': newWidth +'px' });
                $xo('#'+ name).attr({ 'data-rotated':'true' });
            }
        });
    }
	if (settings.initialized != 'true')
    {
		$xo('#'+ name).wrapInner('<div class="swipeScrollBox"><div class="swipeDataHolder"></div></div>');
		if (settings.buttons != 'false')
		{
			if (settings.direction == 'horizontal')
			{
				$xo('#'+ name).append('<div class="swipeDataButtons"><div class="swipeBtn" data="left"><i class="fa-light fa-angle-left"></i></div><div class="swipeBtn" data="right"><i class="fa-light fa-angle-right"></i></div></div>');
			}
			if (settings.direction == 'vertical')
			{
				$xo('#'+ name).append('<div class="swipeDataButtons"><div class="swipeBtn" data="up"><i class="fa-light fa-angle-up"></i></div><div class="swipeBtn" data="down"><i class="fa-light fa-angle-down"></i></div></div>');
			}
			$xo(document).off('click','#'+ name +' .swipeBtn').on('click','#'+ name +' .swipeBtn',function()
			{
				var direction = $xo(this).attr('data');
				var name = $xo(this).parents('.swipeBox').attr('id');
				swipeAnimation(direction,name);
			});
		}
    }
	
	autoSwipeTransition(name);
	var scrollTimeout;
	$xo('.swipeScrollBox').on('scroll',function()
	{
		if(scrollTimeout){ clearTimeout(scrollTimeout); }
		scrollTimeout = setTimeout(function(){ checkSwipeBtns(name); },50);
	});

	var height = $xo('#'+ name +' .swipeDataHolder div').eq(0).outerHeight();
	$xo('#'+ name).css({ 'height':'calc('+ height +'px + 10px)' });
    $xo('#'+ name).attr({ 'data-initialized':'true', 'data-width':width, 'data-rotated':'false', 'data-direction':settings.direction });
	
	checkSwipeBtns(name);
	
	
	function swipeSettings(name)
	{
		// Defaults
		var directionParam 	= 'horizontal';
		var verticalParam	= $xo('#'+ name).attr('vertical');
		var buttonsParam    = $xo('#'+ name).attr('buttons');
		var initializedParam= $xo('#'+ name).attr('data-initialized');
		var autoParam    	= $xo('#'+ name).attr('auto');
		var delayParam    	= $xo('#'+ name).attr('delay');
		var movementParam	= $xo('#'+ name).attr('movement');
		var playParam		= $xo('#'+ name).attr('play');
		
		if (isEmpty(verticalParam) != 'true')
		{
			directionParam = 'vertical';
		}
		
		if (isEmpty(buttonsParam) == 'true')
		{
			buttonsParam = 'false';
		}
		else
		{
			buttonsParam = 'true';
		}
		
		if (isEmpty(initializedParam) == 'true' || initializedParam == 'false')
		{
			initializedParam = 'false';
		}
		else
		{
			initializedParam = 'true';
		}
		
		if (isEmpty(playParam) == 'true')
		{
			playParam = 'false';
		}
		else
		{
			playParam = 'true';
		}
		
		if (isEmpty(delayParam) == 'true')
		{
			delayParam = 6000;
		}
		else
		{
			delayParam = parseInt(delayParam) * 1000;
		}
		
		if (isEmpty(movementParam) == 'true')
		{
			movementParam = '90';
		}
		
		var setting =
		{
			direction: directionParam,
			buttons : buttonsParam,
			initialized : initializedParam,
			play : playParam,
			delay: delayParam,
			movement: movementParam
		}
		return setting;
		
		function isEmpty(data)
		{
			if (data === undefined || data === null || data === false)
			{
				return 'true';
			}
			else
			{
				return 'false';
			}
		}
	}
	
	function swipeAnimation(direction,name,reset)
	{
		var settings = swipeSettings(name);
		var percent = parseFloat(settings.movement);
		var width = parseFloat($xo('#'+ name +' .swipeScrollBox').outerWidth());
		var height = parseFloat($xo('#'+ name +' .swipeScrollBox').outerHeight());
		var position1 = width * (percent * .01);
		var position2 = height * (percent * .01);
		
		clearTimeout(swipeTimout);
		
		if (direction == 'right' && reset == true)
		{
			$xo('#'+ name +' .swipeScrollBox').animate({ scrollLeft: '0px' }, 500);
		}
		else if (direction == 'down' && reset == true)
		{
			$xo('#'+ name +' .swipeScrollBox').animate({ scrollTop: '0px' }, 500);
		}
		else if (direction == 'left')
		{
			$xo('#'+ name +' .swipeScrollBox').animate({ scrollLeft: '-='+ position1 +'px' }, 500);
		}
		else if (direction == 'right')
		{
			$xo('#'+ name +' .swipeScrollBox').animate({ scrollLeft: '+='+ position1 +'px' }, 500);
		}
		else if (direction == 'up')
		{
			$xo('#'+ name +' .swipeScrollBox').animate({ scrollTop: '-='+ position2 +'px' }, 500);
		}
		else if (direction == 'down')
		{
			$xo('#'+ name +' .swipeScrollBox').animate({ scrollTop: '+='+ position2 +'px' }, 500);
		}
	}
	function checkSwipeBtns(name)
	{
		var settings = swipeSettings(name);
		if (settings.buttons != 'false')
		{
			var width = Math.floor($xo('#'+ name +' .swipeDataHolder').width()); // Full Width
			var container = $xo('#'+ name +' .swipeScrollBox').width();	// Visible Width
			var endPos = parseFloat($xo('#'+ name +' .swipeScrollBox').scrollLeft());
			var endWidth =  width - container;
			
			if (endPos <= 0)
			{
				$xo('#'+ name +' .swipeBtn[data="left"]').hide();
			}
			if (endPos > 0)
			{
				$xo('#'+ name +' .swipeBtn[data="left"]').fadeIn(250);
			}
			if (endPos < endWidth)
			{
				$xo('#'+ name +' .swipeBtn[data="right"]').fadeIn(250);
			}
			if (endPos >= endWidth)
			{
				$xo('#'+ name +' .swipeBtn[data="right"]').hide();
			}
		}
		
		clearTimeout(swipeTimout);
		if (endPos >= endWidth)
		{
			autoSwipeTransition(name,true);
		}
		else
		{
			autoSwipeTransition(name);
		}
	}

	function autoSwipeTransition(name,reset)
	{
		var settings = swipeSettings(name);
		if (settings.play == 'true')
		{
			clearTimeout(swipeTimout);
			swipeTimout = setTimeout(function()
			{
				 var direction = 'right';
				 if (settings.direction == 'vertical')
				 {
					 direction = 'down';
				 }
				 swipeAnimation(direction,name,reset);
			}, settings.delay);
		}
	}
}
