BAB=(typeof BAB=='undefined'?{}:BAB);
BAB.classes=(typeof BAB.classes=='undefined'?{}:BAB.classes);
BAB.classes.panels = function()
{
	var i, c, _this = this;
	this.currentIndex = 0;
	this.intervalTime = 7000;
	this.fadeTime = 500;

	if(!(this.container = $('#main-content .banner_home_page_top').first()).length) return;

	// this.list = $('<ul class="CMSPanelList" />').appendTo(this.container);

	this.banners = [];
	// this.titles = [];
	var i = 0;
	$(this.container)
		.css({
			position: 'relative'
		})
		.find('img')
			.each(function(x, img)
			{
				// if((title = $(img).attr('title')) == '') return true;
			
				var banner = $('<div />')
					.attr('id', 'masthead-banner--' + i)
					.addClass('masthead-banner')
					.css({
						position: 'absolute',
						left: '0',
						top: '0',
						zIndex: 1
					})
					.appendTo(_this.container);
		
				if(i != 0)
				{
					$(banner).css({
							zIndex: 0,
							display: 'none'
						})
				}

				if((link = $(img).parent('a')).length)
				{
					$(link)
						// .attr({'title': title})
						.appendTo(banner);
				}
				else
				{
					$(banner).append(img);
				}
/*			
				var li = $('<li id="masthead-list--' + i + '" />')
					.text(title)
					.bind('click dblclick', {index: i}, function(event)
					{
						_this.cycle(event.data.index);
					})
					.appendTo(_this.list);
*/			
				_this.banners.push(banner);
				// _this.titles.push(li);
				i ++;
			})
		.end()
		.find('p').first().remove();
	
	if(this.banners.length)
	{
		// $(this.container).css({overflow: 'visible'});
		// $(this.titles[0]).addClass('active');
		this.startCycleInterval();
	}
}
BAB.classes.panels.prototype.cycle = function(newIndex)
{
	// console.log('cycle');
	if(typeof newIndex == 'undefined')
	{
		var newIndex = this.currentIndex + 1;
		if(typeof this.banners[newIndex] == 'undefined')
		{
			newIndex = 0;
		}
	}
	else if(typeof newIndex != 'number' || typeof this.banners[newIndex] == 'undefined' || newIndex == this.currentIndex)
	{
		return;
	}
	else
	{
		this.clearCycleInterval();
	}
	
	// $(this.titles[this.currentIndex]).removeClass('active');
	// $(this.titles[newIndex]).addClass('active');
	$(this.banners[this.currentIndex]).fadeOut(600, function() { $(this).css({zIndex: 1}); });
	$(this.banners[newIndex]).fadeIn(600, function() { $(this).css({zIndex: 2}); });
	
	this.currentIndex = newIndex;
}
BAB.classes.panels.prototype.startCycleInterval = function()
{
	// console.log('start interval');
	var _this = this;
	this.interval = setInterval(function() { _this.cycle(); }, this.intervalTime);
}
BAB.classes.panels.prototype.clearCycleInterval = function()
{
	// console.log('clear interval');
	if(this.interval) clearInterval(this.interval);
}
BAB.classes.panels.prototype.restartCycleInterval = function()
{
	// console.log('restart interval');
	this.clearCycleInterval();
	this.startCycleInterval();
}
$(document).ready(function() { new BAB.classes.panels(); });

