/**
 * Effekte - StartPage
 */

$(window).load(function()
{
	_nexgam.aktuell.load();

	/**
	 * Artikel
	 */
	_nexgam.articles.load();

	// Blätterfunktion
	$("#articles .btn-next").click(function()
	{
		var next = 1;

		if ($(this).attr('next')) {
			next = parseInt($(this).attr('next'));
		};

		_nexgam.loader.show();
		_nexgam.articles.get(next, function(result, Ajax)
		{
			_nexgam.setHtml($('#start-articles'), result);

			_nexgam.articles.loadImages();
			_nexgam.articles.load();

			_nexgam.loader.hide();
		});

		$(this).attr('next', next+1);
		$("#articles .btn-prev").attr('prev', next-1);

		return false;
	});

	$("#articles .btn-prev").click(function()
	{
		var prev = 1;

		if ($(this).attr('prev')) {
			prev = parseInt($(this).attr('prev'));
		};

		_nexgam.loader.show();
		_nexgam.articles.get(prev, function(result, Ajax)
		{
			_nexgam.setHtml($('#start-articles'), result);
			_nexgam.articles.loadImages();
			_nexgam.articles.load();

			_nexgam.loader.hide();
		});

		$(this).attr('prev', prev-1);
		$("#articles .btn-next").attr('next', (prev+1) <= 1 ? 1 : (prev+1));

		return false;
	});


	/**
	 * Tests
	 */
	_nexgam.tests.load();

	// Blätterfunktion
	$("#tests .btn-next").click(function()
	{
		var next = 1;

		if ($(this).attr('next')) {
			next = parseInt($(this).attr('next'));
		};

		_nexgam.loader.show();
		_nexgam.tests.get(next, function(result, Ajax)
		{
			_nexgam.setHtml($('#start-tests'), result);

			_nexgam.tests.loadImages();
			_nexgam.tests.load();

			_nexgam.loader.hide();
		});

		$(this).attr('next', next+1);
		$("#tests .btn-prev").attr('prev', next-1);

		return false;
	});

	$("#tests .btn-prev").click(function()
	{
		var prev = 1;

		if ($(this).attr('prev')) {
			prev = parseInt($(this).attr('prev'));
		};

		_nexgam.loader.show();
		_nexgam.tests.get(prev, function(result, Ajax)
		{
			_nexgam.setHtml($('#start-tests'), result);

			_nexgam.tests.loadImages();
			_nexgam.tests.load();

			_nexgam.loader.hide();
		});

		$(this).attr('prev', prev-1);
		$("#tests .btn-next").attr('next', (prev+1) <= 1 ? 1 : (prev+1));

		return false;
	});
});



if (typeof _nexgam == 'undefined') {
	var _nexgam = {};
};

_nexgam.aktuell =
{
	load : function()
	{
		$("#start-aktuell li").hover(
			function() {
				$(this).stop().animate({"opacity": "0.5"}, "fast");
			},
			function() {
				$(this).stop().animate({"opacity": "1"}, "fast");
			}
		);

		$("#start-aktuell li").click(function()
		{
			window.location = $(this).find('a')[0].href;
			return false;
		});

		$("#start-aktuell li").css('cursor', 'pointer');
	}
};

_nexgam.articles =
{
	load : function()
	{
		$("#start-articles li").hover(
			function() {
				$(this).stop().animate({"opacity": "0.5"}, "fast");
			},
			function() {
				$(this).stop().animate({"opacity": "1"}, "fast");
			}
		);

		$("#start-articles li").click(function()
		{
			window.location = $(this).find('a')[0].href;
			return false;
		});

		$("#start-articles li").css('cursor', 'pointer');
	},

	get : function(start, onfinish)
	{
		_Ajax.asyncPost('ajax_nexgam_articles', function(result, Ajax)
		{
			Ajax.getAttribute('onfinish')(result, Ajax);
		}, {
			project  : _pcsg.Project.name,
			lang     : _pcsg.Project.lang,
			id       : _pcsg.Site.id,
			start    : start,
			onfinish : onfinish,
			onError  : function(Exception)
			{
				_nexgam.report.hide();
			}
		});
	},

	loadImages : function()
	{
		if (_ptools._Browser.isMSIE &&
			!_ptools._Browser.isMSIE9) {
			return;
		};

		$('#start-articles .preview-image').each(function()
		{
			$(this).css('opacity', 0);

			var imagePath = $(this).css('backgroundImage').
				replace('url(', '').
				replace(')', '').
				replace(/"/gi, '');

			var Parent = this;

			$(new Image()).attr("src", imagePath).load(function()
			{
				$(Parent).animate({
					opacity : 1
				});
			});
		});
	}
};

_nexgam.tests =
{
	load : function()
	{
		$("#start-tests li").hover(
			function() {
				$(this).stop().animate({"opacity": "0.5"}, "fast");
			},
			function() {
				$(this).stop().animate({"opacity": "1"}, "fast");
			}
		);

		$("#start-tests li").click(function()
		{
			window.location = $(this).find('a')[0].href;
			return false;
		});

		$("#start-tests li").css('cursor', 'pointer');
	},

	get : function(start, onfinish)
	{
		_Ajax.asyncPost('ajax_nexgam_tests', function(result, Ajax)
		{
			Ajax.getAttribute('onfinish')(result, Ajax);
		}, {
			project  : _pcsg.Project.name,
			lang     : _pcsg.Project.lang,
			id       : _pcsg.Site.id,
			start    : start,
			onfinish : onfinish,
			onError  : function(Exception)
			{
				_nexgam.report.hide();
			}
		});
	},

	loadImages : function()
	{
		if (_ptools._Browser.isMSIE &&
			!_ptools._Browser.isMSIE9) {
			return;
		};

		$('#start-tests .preview-image').each(function()
		{
			$(this).css('opacity', 0);

			var imagePath = $(this).css('backgroundImage').
				replace('url(', '').
				replace(')', '').
				replace(/"/gi, '');

			var Parent = this;

			$(new Image()).attr("src", imagePath).load(function()
			{
				$(Parent).animate({
					opacity : 1
				});
			});
		});
	}
}

