(function( $ ){
	
	var settings = {
		url: null,
		noImgPath: '/img/ui/news_noimg.png',
		trim: 75
    };
	
	var methods = {
		init : function (options) {
			if ( options ) { 
				$.extend( settings, options );
			}
			
			return this.each(function(i, el){
		    	var _this = this;
		    	var $this = $(this);
		    	var aEl = $('p a', _this).clone();
		    	$('p a', _this).remove();
		    	
		    	$.ajax({
		    		url: settings.url,
		    		dataType: 'script',
		    		success: jQuery.proxy(renderNews, _this)
	    		});
		    	
		    	function renderNews (res) {
		    		var newsObj = motorrad_news_data;
		    		var title = jQuery.trim(newsObj['title']);
		    		var regex = '/.*[\.?]$/';
	    			if (!title.match('[.?!]$')) title += '.';
		    		var body = jQuery.trim(newsObj['text'].substring(0, settings.trim - title.length)) + '...';
		    		$('span', aEl).html(title + ' ' + body);
		    		
		    		$('img', aEl).attr('src', (newsObj['img'] === undefined ? settings.noImgPath : newsObj['img']));
		    		$($('p', _this).get(0)).append(aEl);
		    	}
		    });
		}
	}
	
	$.fn.bmwmotorradNews = function( method ) {
	    if ( methods[method] ) {
	    	return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
	    } else if ( typeof method === 'object' || ! method ) {
	    	return methods.init.apply( this, arguments );
	    } else {
	        $.error( 'Method ' +  method + ' does not exist on jQuery.bmwmotorradNews' );
	    }
	}	
})( jQuery );
