'remmah.widget'.namespace();
Function.requires('remmah.Morph');

remmah.widget.Rotater = function(element, duration)
{
	this.element = (typeof element == 'string') ? document.getElementById(element) : element;
	this.duration = (duration) ? parseInt(duration) : 1500;
	this.pageDelay = 3000;

	this.morph = new remmah.Morph();
	this.currentPage;
	this.nextPage;
	this.jumpTo = null;
	this.pages = [];
	this.pagerItems = [];
	
	if (this.element)
	{
		this.getPages();
		if (this.pages.length > 1) {
      if (this.pages[0])
      {
        this.createPager(this.pages[0].element.parentNode);
        this.start();
      }
    }
    if (this.pages[0])
    {
      remmah.DOM.setOpacity(1, this.pages[0].element);
    }
	}

	this.iteration = null;
	return this;
}
remmah.widget.Rotater.testedIn = [
	'Firefox v.3.5.8'
];
var proto = remmah.widget.Rotater.prototype;
proto.toString = function()
{
	return 'remmah.widget.Rotater';
};
proto.createPager = function(parent)
{
	var pager = document.getElementById('pagerContainer');
	if (pager)
	{
		var span = this.createPagerItem();
		pager.appendChild(span);
		this.pagerItems.push(span);
	}
	else
	{
		var div = document.createElement('div');
	
		var pdiv = document.createElement('div');
		pdiv.className = 'pagerContainer';
		//pdiv.className = 'pagerContainer clear';
		for (var i=0; i<this.pages.length; i++)
		{
			var page = this.pages[i];
			var span = this.createPagerItem();
			pdiv.appendChild(span);
			this.pagerItems.push(span);
		}
		div.onclick = this.pager_onClick.bind(this);
		div.appendChild(pdiv);
		parent.appendChild(div);
	}
};
proto.createPagerItem = function()
{
	var span = document.createElement('span');
	span.name = this.pagerItems.length+1;
	span.className = 'pager';
	span.innerHTML = span.name;
	span.style.cursor = 'pointer';
	
	return span;
};
proto.setPagerItem = function(page)
{
	this.resetPagerItems();
	var no = page.element.id.split('_')[1];
	this.pagerItems[no].className = 'pager pagerActive';
};
proto.resetPagerItems = function()
{
	for (var i=0; i<this.pagerItems.length; i++)
	{
		this.pagerItems[i].className = 'pager';
	}
};
proto.setPages = function()
{
	if (this.currentPage)
	{
		var next = this.pages[this.pages.indexOf(this.currentPage)+1];			
		this.nextPage = (next) ? next : this.pages[0];
	}
};
proto.getPages = function()
{
	//var pages = remmah.DOM.getElementsByClassName('rotater', this.element);
	//alert(document.getElementById(this.element.id).childNodes.length);
	//alert(document.getElementById(this.element.id).childNodes.length);
  var pages = document.getElementById(this.element.id).childNodes;
  
	var opacity = 0;
	for (var i=0; i<pages.length; i++)
	{
		var page = pages[i];
		if (page.nodeType != 3)
		{
      //page.parentNode.style.height = page.offsetHeight + 'px';
      page.style.width = page.parentNode.offsetWidth + 'px';
      var diaCssObj = this.newPage(page);
    }
	}
	this.currentPage = this.pages[0];
};
proto.newPage = function(page)
{
  //alert(page.innerHTML);
	page.id = 'rotater_'+this.pages.length;
	page.style.position = 'absolute';
	page.onclick = this.page_onClick.bind(this);
	remmah.DOM.setOpacity(0, page);

	var diaCssObj = new remmah.Morph.Object(page, 'opacity:1', this.duration, remmah.EffectTransitions.linear, 'wait')
	diaCssObj.onEnd = this.onEnd.bind(this);
	this.morph.add(diaCssObj);
	this.pages.push(diaCssObj);

	return diaCssObj;
};
proto.addPage = function(page)
{
	var diaCssObj = this.newPage(page);
	this.createPager(this.pages[0].element.parentNode);
	this.pages[0].element.parentNode.appendChild(page);
	
	diaCssObj.reset();
	this.setPages();
}
proto.pager_onClick = function(e)
{
	if (!e) var e = window.event;
	var target = e.target || e.srcElement;
	var number = parseInt(target.name);
	if (!isNaN(parseInt(number * 1)))
		this.jumpTo = number-1;
};
proto.page_onClick = function(e)
{
	if (!e) var e = window.event;
	var target = e.target || e.srcElement;
	//alert(target.id);
	//this.morph.pause();
};
proto.start = function()
{
	//if (this.pages.length < 2)
	//	return false;

	this.setPages();
	this.currentPage.state = 'run';
	this.iteration = 0;
	this.pause = new Date().getTime();
	this.morph.start();

	this.setPagerItem(this.currentPage);
};
proto.onEnd = function()
{
	if (((new Date().getTime() - this.pause)) < this.pageDelay)
		return false;

	this.morph.begin = new Date().getTime();
	this.currentPage.styles[0].finished = false;
	this.currentPage.state = 'run';
	if (this.iteration % 2 == 0)
	{
		this.nextPage.state = 'run';
		this.setPagerItem(this.nextPage);
	}
	else
	{
		this.currentPage.state = 'wait';
		this.currentPage = this.nextPage;
		this.setPages();
		
		if (this.jumpTo != null)
		{
			this.nextPage = this.pages[this.jumpTo];
			this.jumpTo = null;
		}
	}

	this.pause = new Date().getTime();
	this.iteration++;
};
