var Newspaper = Class.create({
  initialize: function(length) {
  	if (length) this.length = length;
  },
  
  index: 0,
  
  length: 0,
  
  select: function(id, select){
	  	var target = $(id);
	  	
	  	this.deselect(id);
	  	target = $("newspaper-content").update("");
	  	
	  	//select = article ID
  		this.apiCall(select);
  },
  
  deselect: function(id){
  	var target;
  	
  	var n = id.split("item-").join("");
  	
  	for (i = 0; i < this.length; i++){
	  	target = $( 'item-' + (i+1) );
  		if ($( 'item-' + (i+1) ).hasClassName('selected')) $( 'item-' + (i+1) ).toggleClassName('selected');
  	}
  	$( 'item-' + n ).addClassName('selected');
  },
  
  apiCall: function(action, params){
    var options = { method : 'post', parameters : 'r=feed/newspaper&id='+action, onSuccess: this.onComplete.bindAsEventListener(this) };
    new Ajax.Request(Loader.AJAX_PATH + 'index.php', options);
  },
  
  onComplete: function(transport){
  	var json = transport.responseJSON;
  	
  	this.generateHTML( this.index, json );
  	
  	sIFR.replace(dbozone, {
	  selector: '#fold-1 h3',
	  wmode: 'transparent',
	  css: [
	    '.sIFR-root { text-align: left;  font-size: 22px; leading:0; letter-spacing: -0.2; color: #336600; padding-right: 10px; line-height:-4px;  }'
	    ,'a { text-decoration: none; }'
	    ,'a:link { color: #336600; }'
	    ,'a:hover { color: #003300; text-decoration: none; }'
	  ]
	});
	
 
  },
  
  generateHTML: function(index, json){
  	this.data = json.items;
  	
  	var target = $("newspaper-content");
  	target.setStyle( { 'backgroundImage': 'none' } );
	
	var header = new Element('h3').update('<a href="' + this.data[0].link + '">' + this.data[0].title + '</a>');
	var description = new Element('div').update( this.data[0].description );
	
	target.appendChild(header);
	target.appendChild(description);
  }
  
});
