/*!
 * Goodar
 * @author    Vyacheslav Voronchuk
 * @date      25.12.2009 
 * Copyright(c) 2009, by Vyacheslav Voronchuk
 */

Goodar.Slider = Ext.extend(Ext.util.Observable, {
    
  constructor: function(config)
  {
    for(value in config)
      this[value] = config[value];
    
    if(!config.template)
    {
      this.template = new Ext.Template
      ([
        '<div class="slider-selector-' + this.id + ' cover" id="slider-' + this.id + '-item-{handler}">',
          '<a href="/film/{id}"><img title="{comment}" alt="{header}" src="' + this.posterPath + '{handler}_small.jpg"/></a><br/>',
          '<a href="/film/{id}">{header}</a>',
        '</div>'
      ].join(''));
    }
    
    Goodar.Slider.superclass.constructor.call(this);
    
    if(!config.container)
      this.container = Ext.get('slider-wrap-' + this.id);
    
    this.container.sliderWidth = this.startX;
    Ext.each(this.data, function(item) {
      this.container.sliderWidth = this.container.sliderWidth + 128;
      this.template.append('slider-wrap-' + this.id, item);
    }, this);
    this.container.setStyle({ width: this.container.sliderWidth + 'px' });
    //удаляем скриптовый тэг
    this.container.child('script:first-child').remove();
     
    
    //управление прокруткой
    this.container.on('mouseover', function(event, el, object)
    {
    	if(this.scroll == true)
    	{
    	  this.scroll = false;
    	  this.direction = null;
    	  this.stopFx();
    	}
    	
    	var iWidth = Ext.getBody().getWidth() / 2;
  	    var iKof = (iWidth - 150) / 20;
    	if(event.xy[0] < (iWidth - 150))
    	{
    	  this.speedL = Math.round(event.xy[0] / iKof) / 10;	
          if(this.direction !== 'left')	
          {
        	this.direction = 'left';
        	this.stopFx();
    	    this.left();
          }
    	}
    	else if(event.xy[0] > (iWidth + 150))
    	{
    	  this.speedR = Math.round(20 - ((event.xy[0] - iWidth - 150) / iKof)) / 10;
    	  if(this.direction !== 'right')	
          {
          	this.direction = 'right';
          	this.stopFx();
      	    this.right();
          }
    	}
    	else
    	{
    	  this.direction = null;
    	}
    });
    this.container.on('mouseleave', function(event, el, object)
    {
        this.direction = null;
    	this.scroll = true;
    	this.slide.defer(2000, this);
    });
    
    Goodar.Manager.addCmp(this.id, this);
    
    if(config.autoStart)
      this.start();
  },

  //старт промотки
  start: function()
  {
    if(this.minLength > this.data.length)
      return false;
    
    this.container.scroll = true;
    this.container.parentWidth = this.container.parent().getWidth();
    this.container.shiftX = this.startX;
    this.container.delay = 0;
    this.container.direction = 'right';
    this.container.slide = function()
    {
      if(!this.scroll)
    	return false;
      /*var oChild = this.child('div:first-child');
      oChild.shift.defer(this.delay, oChild, [{ x: -100, easing: 'elasticIn', duration: 1, scope: this, callback: function() 
      {
        var oChild = this.child('div:first-child');
        oChild.insertAfter(this.child('div:last-child'));
        oChild.setStyle({ position: null, left: null, top: null});
        
        this.shiftX = this.shiftX - 128; 
        this.delay = this.delay + 800;
        this.slide.call(this);
      }}]);*/
            
      this.shift({ x: this.shiftX, duration: 2, scope: this, callback: function() 
      {
    	if(!this.scroll)
    	  return false;
    	  
        if(this.direction === 'right')
          this.shiftX = this.shiftX - 128; 
        else
          this.shiftX = this.shiftX + 128; 
        
        if(this.shiftX < -(this.sliderWidth - this.parentWidth - 128))
          this.direction = 'left';
        if(this.shiftX > 24 - 128)
          this.direction = 'right';
        
        this.slide();
      }});
    };   
    
    //мотаем влево (scope = container)
    this.container.left = function()
    {
      if((this.direction !== 'left') || this.scroll) 	
    	return false;
      
      if(this.shiftX < 24)
		this.shiftX = this.shiftX + 128;   	
	  this.shift({ x: this.shiftX, duration: this.speedL, scope: this, callback: function() 
      {
  		this.left(false);
  	  }});
    };
    //мотаем вправо (scope = container)
    this.container.right = function()
    {
	  if((this.direction !== 'right') || this.scroll) 	
	    return false;
		
	  if(this.shiftX > -(this.sliderWidth - this.parentWidth - 128))
	    this.shiftX = this.shiftX - 128;
	  this.shift({ x: this.shiftX, duration: this.speedR, scope: this, callback: function()
	  {
	  	this.right(false);
	  }});  
    };
    
    
    this.container.slide();
  }  
});
