/**
 * Animacion generica
 * Prerequisitos
 * 		
 * 	Estructura del HTML
 * 
 * 	<div class="containerSlideAnimation" style="overflow:hidden; position:relative; ">
 * 		<div class="innerSlideAnimation" style="width:<ancho_del_elemento*cantidad_elementos>px;" id="NewsAnimationNode">
 * 			<div class="slideElement" style="float:left; position:relative; width:<ancho_del_elemento>px;">
 * 				Elementos a mover
 * 			</div>
 * 		</div>
 * 	</div>
 * 	
 * 	El div slideElement se repite la cantidad de veces que se necesite
 * 
 * 	Carga
 * 
 * 	Parametros
 * 		id: "moveableNode"
 * 
 * 	Resultado
 * 
 */

/**
 * Requermientos : 
 * dojo.require("dijit._Widget");
 * dojo.require('dojo.NodeList-fx');
 * dojo.require('dojox.timing');
 */ 
 /*
dojo.provide("widgets.XNG.SlideEngine");
dojo.declare("widgets.XNG.SlideEngine", 
                [dijit._Widget], {
  
  _node :  "" ,
  
  constructor : function(_node){
    this._node = _node ;
    
    var node        = '#' + this._node.node ;
    var node_ul     =  node + ' ul';
    var node_ul_li  =  node_ul + ' li';
    
    
    
  }
  
});

dojo.provide("widgets.XNG.Slide");
dojo.declare("widgets.XNG.Slide", 
                [dijit._Widget, widgets.XNG.SlideEngine], {
  
  node :  "xng_slide" ,
  list :  null ,
  items : null, 
  
  constructor : function(opt){
    this.node               = opt.node || this.node ;
    this.showDuration       = opt.showDuration || this.showDuration ;
    this.scrollTopDuration  = opt.scrollTopDuration || this.scrollTopDuration ;
  }
  
  
  
});

*/

dojo.provide("widgets.XNG_Slide");
dojo.declare("widgets.XNG_Slide", [dijit._Widget], {
    
  node : "xng_slide" ,
    
  list : null ,
  
  items : null, 
  
  showDuration : 3000,
  
  scrollTopDuration : 200,

  index : 0,
  
  interval : null,
  
  init : function(opt){
    
    var self = this ;
    
    this.initializeProperties(opt);
    
    var node        = '#' + this.node ;
    var node_ul     =  node + ' ul';
    var node_ul_li  =  node_ul + ' li';
    
    this.list   = dojo.query(node_ul); 
    this.items  = this.list.query("li");
    
    var itemsWidth = this.items.length * this.list.style("width");
    
    dojo.query(node_ul).style({
        "width": itemsWidth + "px"
    });
    
    this.start();
  },
  
  initializeProperties : function(opt)
  {
    this.node               = opt.node || this.node ;
    this.showDuration       = opt.showDuration || this.showDuration ;
    this.scrollTopDuration  = opt.scrollTopDuration || this.scrollTopDuration ;
    this.effect             = opt.effect || this.effect ;
  },

  start : function()
  {
    var self = this ;
    this.interval          = new dojox.timing.Timer(this.showDuration);
    this.interval.onTick   = dojo.hitch(self, "move");
    this.interval.start();
  },
  
  stop : function()
  {
   if(this.interval) 
    clearInterval(this.interval);
  },
  
  reset : function()
  {
    var self = this ;
    
    var node        = '#' + this.node ;
    var node_ul     =  node + ' ul';
    var node_ul_li  =  node_ul + ' li';
    
    this.list   = dojo.query(node_ul); 
    this.items  = this.list.query("li"); 
    
    this.list.anim({ 
      left: 0
    }, self.scrollTopDuration, null, self.start);
  },
  
  move : function()
  {
    var self = this ;
    dojo.hitch(this, "init");
    
    this.list.anim({ left: (0 - (dojo.coords(self.items[++self.index]).l)) }, self.scrollDuration, null, function(){
        if(self.index == self.items.length - 1) {
          self.index = 0-1;
          dojo.hitch(self, "stop");
          setTimeout(self.reset,self.showDuration);
        }
      }, null);
    
  }
  
  
});

/*
dojo.declare("SlideAnimationClass", null, {
	//acts like a java constructor
	initializer: function(node_id)
	{
		this.start = 0; // offset de comienzo
		this.end = -295; // offset de fin
		this.step = 295; // cantidad de unidades a avanzar
		this.unit = "px"; // unidad
		this.current_start = 0; // posicion actaul de comienzo de la animacion
		this.current_end = this.end; // posicio actual de fin de la animacion
		this.total = 2; // Cantidad de elementos a animar
		this.reset= false; // ???
		this.node_id = node_id;
		this.duration = 2000;
		this.steps = 0;
	},
	

	setProperty: function(property, value)
	{
		eval('this.'+property+' = value');
	},

	startAnimation: function()
	{
		var easing = "linear";
		
		var ef = dojo.fx.easing[easing];
		
		if (ef) {
	       var moveArgs = {
	           node: this.node_id,
	           properties: {
	               marginLeft: {
	                   start: this.current_start,
	                   end: this.current_end,
	                   unit: this.unit
	               }
	           },
	           easing: ef,
	           duration: this.duration
	       };
	       
	       dojo.animateProperty(moveArgs).play();
	       
	       this.steps++;
	        
	       if(this.reset){
    	   	this.reset = false;
    	   	this.current_start = 0;
    	   	this.current_end = this.end;
       } 
	       this.current_start 	= this.current_start - this.step; 
	       this.current_end 	= this.current_end - this.step ;
	       this.steps++;
	       
	       if(this.total - 1<= this.steps){ // reseteo la animacion
	    	   	this.current_start = this.current_end;
	    	   	this.current_end = 0;
				this.reset = true;
				this.steps= 0;
	       }
	   }
	}
});*/
