var menu = {
  len: 6, // menu length 
  scroll_len: 6,
  
  list: '',
  begin: 0,
  up_id :'#aup',
  down_id: '#adown',
  
  init: function() {
	  this.list = $('div.item').each(function(index){
		if($(this).is('.actual'))
			menu.begin = index;
      });
	  
	  if(this.begin != 0) {
		  this.begin = Math.floor(this.begin/this.len)*this.len;
	  }
	  
	  if (this.begin >= (this.list.length-this.len)) {
		  this.begin = this.list.length - this.len;
	  }
	  
	  $(this.up_id).click(function() {
		  menu.up();
	  });
	  
	  $(this.down_id).click(function() {
		  menu.down();
	  });

	  this.refresh();
	},
	
	up: function(direction) {		
		  if(this.begin>0) {
				this.begin -= this.scroll_len;
				if(this.begin<0) this.begin = 0;
				this.refresh();
		  }	
	},
	
	down: function() {
			if(this.begin+this.scroll_len < this.list.length) {
				this.begin += this.scroll_len;
				this.refresh();				
			}
	},
	
	refresh: function() {		
		for(var i=0; i<this.list.length; i++) {
			if(this.begin > i || this.begin+this.len <= i )
				$(this.list[i]).hide(0);
			else
				$(this.list[i]).show(0);
		}
		
		if(this.list.length > this.len) {
		  if(this.begin == 0) {
			  $(this.up_id).addClass('disabled');
		  	  $(this.down_id).removeClass('disabled');
		  } else if((this.begin+this.len) >= this.list.length){
			  $(this.up_id).removeClass('disabled');
			  $(this.down_id).addClass('disabled');
		  } else {
			  $(this.up_id).removeClass('disabled');
			  $(this.down_id).removeClass('disabled');
		  }
		  
		} else {
			$(this.up_id).addClass('disabled');
			$(this.down_id).addClass('disabled');
		}		
	}
}


$(document).ready(function(){
	menu.init();
});

var old_search_value = '';

function search_focus(x){
	old_search_value = x.value;
	x.value = '';
};

function search_blur(x){
	patt = /^\W+$/;

	if (patt.test(x.value) || x.value == ''){
		x.value = old_search_value;
	}
};
