$(document).ready( function() {
	//init actionviewer
	$('#NewsItems').Actions(
						{
							activeImg: AbsPath + 'images/Navigatie_Actief.jpg',
							inactiveImg: AbsPath + 'images/Navigatie_Normaal.jpg',
							requestUrl: AbsPath + 'includes/SpecialPages/News/Ajax.php',
							navDiv: '.NewsNavigatie'
						}
	);
});

(function($) {
	$.fn.Actions = function(options) {
		options = $.extend({
					activeImg: '',
					inactiveImg: '',
					requestUrl: '',
					navDiv: '',
					wrapper: null,
					imageWrapper: '',
					Actions: new Array()
		}, options);
		
		options.wrapper = $(this);
		
		$.ajax({
			   type: "POST",
			   url: options.requestUrl,
			   dataType: 'json',
			   async: false,
			   error: function(xhr, status, errorThrown) {
					$(this).css('display', 'none');
			   },  
			   success: function(msg) {
					$.each(msg, function(i , s) {
						options.Actions[i] = s;
					});

					init();
			   }
		});
		
		function init() {
			if(options.Actions.length <= 0) {
				options.wrapper.css('display', 'none');
			} else {				
				options.wrapper.children('a.Item').attr('href', options.Actions[0].Location);
				options.wrapper.children('a.Item').children('.Teaser').html(options.Actions[0].Teaser);
				navigation();				
			}
		};
		
		function navigation () {
			$.each(options.Actions, function(i) {
				var navImg = ( i == 0 ) ? options.activeImg : options.inactiveImg ;
				$(options.navDiv).html($(options.navDiv).html() + '<a href="javascript:void(0);"><img src="' + navImg + '" /></a>');
			});
			
			bindNavEvent();
		};
		
		function bindNavEvent() {
			$.each($(options.navDiv).children('a'), function(i) {
				$(this).bind('click', function(e) {
					setAction($(this), i);
				});
			});
		};
		
		function setAction( oNav, Action ) {
			$.each($(options.navDiv).children('a').children('img'), function(x){
				$(this).attr('src', options.inactiveImg);
			});
			
			oNav.children('img').attr('src', options.activeImg);
			
				options.wrapper.children('.Item').fadeOut(300, function() {
					options.wrapper.children('.Item').children('.Teaser').html(options.Actions[Action].Teaser);
					options.wrapper.children('.Item').attr('href', options.Actions[Action].Location);
					
					options.wrapper.children('.Item').delay(300).fadeIn();
				});
		};
	}
})(jQuery);
