/*
Pequeño feed de noticias
@autor Fernando López mitridatesATgmailDocCom
require : 	jquery-1.4.2.min.js
- Toma los valores de un array, pero bien podría ser ajax... 
*/
(function($) {

	var options = {},	
		news = {},
		current=0,
		selector='#paginador_rs',
		bttn={
				next : '#next',
				prev: '#prev'
				}, 
		
		methods = {
			 init : function(n){
				 news = false || n;
				 methods.set.call(this,{i:0});
				 },
			set:function(c){
				if(news.length==0){
				$(this).html('<div align="center">No hay noticias disponibles.</div>');	
				$(bttn.next).unbind('click').css({'color': '#ccc','cursor': 'text'});
				$(bttn.prev).unbind('click').css({'color': '#ccc','cursor': 'text'});
				 return false; 	
				}
				methods.set_current(c);
				var titulo = '<div onclick="window.location=\'noticias.html?id='+news[current].id+'\'" style="cursor:pointer;padding-bottom:10px" class="Estilo4">'+news[current].titulo+'</div>';
						
				var imagen = '';
				if(news[current].img_id!=null){
					imagen = '<img align="right" style="margin:0 5px 5px" src="'+news[current].img_url+'" width="'+news[current].img_thumb_w+'" height="'+news[current].img_thumb_h+'"/>';
				}
				var texto = '<span class="Estilo3">'+news[current].intro+'</span>';		
				$(this).html(titulo+imagen+texto);
				$(bttn.next).unbind('click');
				$(bttn.prev).unbind('click');
				
				if(current==0) $(bttn.prev).unbind('click').css({'color': '#ccc','cursor': 'text'});
				else  $(bttn.prev).css({'color': '#000','cursor': 'pointer'}).
									bind('click',function(){$(selector).noticias('set',{i:current-1});});
				
				if(news.length==current+1) $(bttn.next).unbind('click').css({'color': '#ccc','cursor': 'text'});
				else  $(bttn.next).css({'color': '#000','cursor': 'pointer'}).bind('click',function(){ 
													$(selector).noticias('set',{i:'next'});
													});
				
			},
			set_current:function(c){
				
				i = c.i || 0;//index del array
				
				if(typeof i =='number'){
					if(i+1<=news.length && i>=0){
						current = i; 
						return true;
					}else{
						return false;
					}
				}
				if(i=='prev'){
					if(current<0){
						current = current-1; 
						return true;
					}else{
						return false;
					}
				}
							
				if(i=='next'){
					if(current+1<=news.length){
						current = current+1; 
						
						return true;
					}else{
						return false;
					}
				}
			}
		};
  $.fn.noticias = function(method) { 
		if ( methods[method] ) {//llamada al method, puede haber argumentos
		  return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
		} else if ( typeof method === 'object' || ! method ) {//el primer elemento son los argumentos (sin metodo)
		  return methods.init.apply( this, arguments );//aplicamos los arg sobre el elem o lo creamos dinamicamente
		} else {
		  $.error( 'Method ' +  method + ' inexistente en jQuery.noticias' );
		}   
	}
})(jQuery);

