/****************************************************************
** switcher -- a code switching images for "lifestyle" section 
**
** $Id$
****************************************************************/
var switchers = {};
  function generateNum(to){
      var rand = Math.floor(Math.random() * to*100)
      return rand%to;
    }
var switcher = function()
{
	this.id = 'switcher';
	this.items = [];
	this.cursor = 0; 
	this.phase = 0; // 0 - 1 - 2
	this.slideInterval = 5000; // ms
	this.opacity = 1.0;
	this.opacityStep = 0.05;
	this.opacityInterval = 50; // ms
	this.contenerName = 'switchers';
	this.timer = null;
	this.counterId = '';
	this.totalCounterId = '';
    this.startRand = false;
	this.autoStart = true; 
	this.itemclassName ='switcher_item';
    this.dynamicAbsoulte =false;
	this.run = function() 
	{
        
		t = document.getElementById(this.id)
		.getElementsByTagName('div');
		for(i = 0; i < t.length; i++)
		{
			if(t[i].className == this.itemclassName)
			{                
				this.items.push(t[i]);
			}
		}
		if(this.items.length < 2) this.autoStart = false; 
        
        if(this.startRand)
            
            this.cursor  = generateNum(this.items.length);
		this.getCurrent().style.display = 'block';
		// switchers.switcher = this;
		if(this.autoStart) this.runSlideshow();
		this.displayStatus();
	}
	
	this.runSlideshow = function()
	{
        
		this.timer = setInterval(this.contenerName + '.' + this.id + ".startSlide()", this.slideInterval);
	}
	
	
	this.startSlide = function()
	{
		this.getCurrent().style.zIndex = 100;
		this.getCurrent().style.display = 'block';
		this.getNext().style.display = 'block';
		this.getNext().style.zIndex = 90;
        if (this.dynamicAbsoulte)
        {
        this.getNext().style.position = 'absolute';
        this.getNext().style.top = '435px';
        }
		this.step();
	}
	
	this.stopSlide = function()
	{
        
		this.getCurrent().style.display = 'none';
		this.getCurrent().style.opacity = '1.0';
		this.getCurrent().style.filter = 'alpha(opacity = 100)';
        if (this.dynamicAbsoulte)
        {
            
            this.getNext().style.position = '';    
            }

		this.getNext().style.zIndex = 100;
		this.getNext().style.display = 'block';
        
		this.cursor = (this.cursor + 1) % this.items.length;
		this.opacity = 1.0;  
		this.displayStatus();
	}
		
	this.step = function()
	{
		if(this.opacity <= this.opacityStep)
		{
			this.stopSlide();
		}
		else
		{
			this.opacity -= this.opacityStep;
            
			this.getCurrent().style.opacity = this.opacity;
			this.getCurrent().style.filter = "alpha(opacity = " + this.opacity*100 + ")";
			setTimeout(this.contenerName + '.' + this.id + ".step()", this.opacityInterval);
		}
	}
	
	this.next = function()
	{
		this.flip(1);
	}
	
	this.prev = function()
	{
		this.flip(-1);
	}
	
	this.flip = function(n)
	{
		// czyszczenie
		clearInterval(this.timer);
		this.getCurrent().style.display = 'none';
		this.getCurrent().style.opacity = '1.0';
		this.getCurrent().style.filter = 'alpha(opacity = 100)';
		// zmiana
		this.cursor = ((this.items.length + this.cursor + n) % this.items.length);
		this.getCurrent().style.display = 'block';
		//
		this.displayStatus();
	}	
	
	this.getCurrent = function()
	{
		return this.items[this.cursor];
	}
	
	this.getNext = function()
	{
		return this.items[(this.cursor + 1) % this.items.length];
	}
	
	this.displayStatus = function()
	{
        
		if(this.counterId)
		{
			document.getElementById(this.counterId).innerHTML = this.cursor + 1;
		}
		
		if(this.totalCounterId)
		{
			document.getElementById(this.totalCounterId).innerHTML = this.items.length;
		}
	}
}

