(function($)
{

	$.fn.imageSlider = function(json)
	{
		var $json = jQuery.parseJSON(json);
		var $count = $json.length;

		var $thisInt = 1;

        var $bottom = $('<img src="/img/layout/slides/' + $json[1]['image'] + '" />').hide();
        var $top = $('<img src="/img/layout/slides/' + $json[0]['image'] + '" />').hide();

        $(this).append($bottom);
		$(this).append($top);
		$(this).children('img:first').remove();

		$top.fadeIn(function()
		{
			$bottom.show();
		});

		var $v = {
			'count'		: $count,
			'bottom'	: $bottom,
			'top'		: $top,
			'thisInt'	: $thisInt,
			'json'		: $json
		};

		setInterval(function()
		{
			$(this).loadNext($v)
		}, 5000);
	};
	$.fn.loadNext = function($v)
	{
		if($v.thisInt == $v.count)
		{
			$v.thisInt = 0;
		}
		
		$v.nextInt = $v.thisInt + 1;
		if($v.nextInt > ($v.count - 1))
		{
			$v.nextInt = 0;
		}

		var $thisSrc = '/img/layout/slides/' + $v.json[$v.thisInt]['image'];
		var $nextSrc = '/img/layout/slides/' + $v.json[$v.nextInt]['image'];

		$v.top.fadeOut(800, function()
		{
			$v.top.attr('src', $thisSrc);
			$v.top.show();
			$v.bottom.attr('src', $nextSrc);
			$v.thisInt++;
		});


	
	}
})
(jQuery);

