jQuery(function($)
{
	if( typeof( window.artworks ) == "undefined" )
	{
		window.artworks = {};
	}
	if( typeof( window.artworks.Feeder ) == "undefined" )
	{
		window.artworks.Feeder = function( id, settings )
		{
			if( typeof( window.artworks.FeederInstance ) == "undefined" )
			{
				window.artworks.FeederInstance = {};
			}
			var default_settings = {
				updateInterval:  10000,	
				sourceurl: "testimonial.php",
	 			loadingBox: "p.testi-main",
	  			loadingTitle:  "p.testi-name",
	  			loadedBox:  "div#testimonials",
				timeout: 100000
			};
			if( typeof( settings ) != "undefined" )
			{
				$.each( settings , function( name, fn )
				{ 
					default_settings[name]=fn;	
				} );
			}
			var me = {
				settings: default_settings,
				loadHook:false,
				fallbackData: null,			
				setLoading: function()
				{
					me.fallbackData = $(me.settings.loadedBox).html();
					$(me.settings.loadingBox).html("<img src=\"img/YellowLoad.gif\" class=\"loadingimg\" />");
					$(me.settings.loadingTitle).html("");
					
				},
				loadingDone: function( content )
				{
					$(me.settings.loadedBox).replaceWith( content );
				},
				loadNewContent:function()
				{
					if( !me.loadHook )
					{
						$.ajax({ 
							async: true,
							cache: false,
							beforeSend: function( request )
							{
								me.setLoading();	
								me.loadHook = true;
							},
							complete: function ( request, status )
							{
								me.loadHook = false;						
							},
							data: {},
							dataType: "html",
							error: function( request, status, error )
							{
								$(me.settings.loadedBox).html(me.fallbackData);
							},
							success: function( data, status )
							{
								me.loadingDone( data );
							},
							timeout: 100000,
							type: "get",
							url: me.settings.sourceurl
						});	
					}
				}
			}
			window.artworks.FeederInstance[id] = me;
			me.loadNewContent();
			setInterval( me.loadNewContent, me.settings.updateInterval );
			return me;
		}
	}
});
