/**
 * ViewHome
 */
var ViewHome = Class.create({
  /**
   * Init ViewHome
   */	
  initialize: function() {
	this.options = Object.extend({
	  elemContent : null
	}, arguments[0] || { });		

	this.nTimerID = null;
	this.arrItems = new Array();
	this.nCurrItem = -1;
	this.bLocked = true;	
	this.nSlideTimer = 5;
	
	// set slide hold
    if (this.options.elemContent.readAttribute('datafld')) {
   	  this.nSlideTimer = this.options.elemContent.readAttribute('datafld');
    }
    
    var elemItems = this.options.elemContent.select('.item');
    var item;	
    for (var nItem = 0; nItem < elemItems.length; nItem++) {
      item = new ViewHomeItem({elemContent:elemItems[nItem]});        
      this.arrItems.push(item);
    }		        
	this.enableTimer(true);
  },
    
  show: function() {
	this.bLocked = false;
	  
	this.options.elemContent.style.visibility = "visible";	        
	this.enableTimer(true);
	this.autoPlay();
  },

  hide: function() {
	this.enableTimer(false);	  
  },
  
  showPrevious: function() {
	if (this.bLocked) {
	  return;
	}
	this.bLocked = true;
	  
	this.enableTimer(false);

	var nSlide = this.arrItems.length-1;
	  
   	if (this.nCurrItem-1 >= 0) {
   	  nSlide = this.nCurrItem - 1;
   	}
   	if (this.nCurrItem >= 0) {
  	  this.arrItems[this.nCurrItem].hide();
    }
	this.nCurrItem = nSlide;
	  
    this.arrItems[this.nCurrItem].show();
    
	this.unlock.bind(this).delay(1);    
  },
  
  showNext: function() {
	if (this.bLocked) {
	  return;
	}
	this.bLocked = true;
	  
	this.enableTimer(false);
		
	var nSlide = 0;
	  
   	if (this.nCurrItem+1 < this.arrItems.length) {
   	  nSlide = this.nCurrItem + 1;
   	}
   	if (this.nCurrItem >= 0) {
  	  this.arrItems[this.nCurrItem].hide();
    }
	this.nCurrItem = nSlide;
	  
    this.arrItems[this.nCurrItem].show();
    
	this.unlock.bind(this).delay(1);    
  },
  
  enableTimer: function(bEnable) {
	this.bAutoPlay = bEnable;
	if (!bEnable && this.nTimerID) {
	  window.clearTimeout(this.nTimerID);
	}
  },
  
  autoPlay: function() {
    if (this.bAutoPlay) {
 	  var nSlide = 0;
	    	  	
   	  if (this.nCurrItem+1 < this.arrItems.length) {
   	    nSlide = this.nCurrItem + 1;
   	  }
   	  if (this.nCurrItem >= 0) {
  	    this.arrItems[this.nCurrItem].hide();
      }
	  this.nCurrItem = nSlide;
	  
	  this.arrItems[this.nCurrItem].show();
   	  
      // start timer
   	  this.nTimerID = this.autoPlay.bind(this).delay(this.nSlideTimer);
    }
  },
  
  unlock: function() {
	this.bLocked = false;
  }  
  
});
