var tabbedbanner_interval = 0;
var current_tab = null;
var justStarted = true;
var playFx = null;

function createTab(id, title, image, banners, tabs, dir)
{
	if (tabs > 0)
		var tab_class = 'tab page_' + Math.floor($('tabbedbanner_menu').getChildren().length / tabs);
	else
		var tab_class = 'tab';
	var tab = new Element('div', {'id': 'tab_' + id, 'class': tab_class});
	
	var tab_title = new Element('div', {'class': 'tab_title'}).inject(tab);
	tab_title.innerHTML = title;
	
	var tab_image_bg = new Element('div', {'class': 'tab_image_bg'}).inject(tab);
	var tab_image = new Element('div', {'class': 'tab_image'}).inject(tab_image_bg);
	tab_image.setStyles({'background-image': 'url(' + image + ')', 'background-repeat': 'no-repeat'});
	
	if (dir == 'horizontal')
		tab.setStyle('text-align', 'center');
	var tabFx = new Fx.Styles(tab, {wait: false, duration: 200});
	tab.addEvents(
	{
		'mouseenter': function()
		{
			tab.addClass('tab_hover');
		},
		'mouseleave': function()
		{
			tab.removeClass('tab_hover');
		},
		'click': function()
		{
			if (!tab.hasClass('active_tab'))
			{
				//playFx.start('0px 30px', '0px 0px');
				
				if (justStarted)
				{
					justStarted = false;
					new Fx.Style($('banner_' + id), 'opacity', {duration: 300}).start(1);
					current_tab = $('tabbedbanner_menu').getFirst();
				}else{
					new Fx.Style($('banner_content').getElement('.banner_wrap'), 'opacity',
					{
						duration: 2000,
						onStart: function()
						{
							$('banner_' + id).injectTop($('banner_content'));
							new Fx.Style($('banner_' + id), 'opacity', {duration: 1750}).start(1);
						}
					}).start(0);
				}
							
				//update active tab
				$$('.tab').removeClass('active_tab');
				tab.addClass('active_tab');
				$$('.tab').fireEvent('mouseleave');
			}
		},
		'mousedown': function()
		{
			pauseBanner();
		},
		'mouseup': function()
		{
			current_tab = tab;
		}
	});
	
	tab.injectInside('tabbedbanner_menu');
}

function rotateBanner(tabs)
{
	//select new tab
	if (current_tab != null && current_tab.getNext() != null)
	{
		current_tab.getNext().fireEvent('mouseenter');
		current_tab.getNext().fireEvent('click');
		current_tab = current_tab.getNext();
	}else{
		$('tabbedbanner_menu').getFirst().fireEvent('mouseenter');
		$('tabbedbanner_menu').getFirst().fireEvent('click');
		current_tab = $('tabbedbanner_menu').getFirst();
	}
	if (tabs > 0 ){
		//if new page change it
		if (!current_tab.hasClass('page_' + current_page)) changeTabPage(current_page + 1);
	}
}

function pauseBanner()
{
	clearInterval(tabbedbanner_interval);
	if ($defined($('tabbedbanner_innertoggle')))
		$('tabbedbanner_innertoggle').set('class', 'play');
}

function startBanner(tabs, timer)
{
	$$('.tabbedbanner_banner').each(function(tab){ tab.setStyle('display', 'block'); });
	if ($defined($('tabbedbanner_innertoggle')))
		$('tabbedbanner_innertoggle').set('class', 'pause');
	
	current_tab.fireEvent('click');
	var rotation = function(){rotateBanner(tabs);};
	tabbedbanner_interval = rotation.periodical(timer);
}

function changeTabPage(page)
{
	var tabs_chain = new Chain();
	
	var tabsOut = function()
	{
		//make tabs disappear from bottom to top
		var active_tabs = $('tabbedbanner_menu').getElements('.page_' + current_page);
		
		active_tabs.each(function(tab, i)
		{
			var fxFn = function()
			{
				new Fx.Style(tab, 'opacity',
				{
					wait: false,
					duration: 400,
					onComplete: function()
					{
						tab.setStyle('display', 'none');
						
						if (i == 0) tabs_chain.callChain();
					}
				}).start(1, 0);
			};
			
			fxFn.delay((active_tabs.length - 1 - i) * 300);
		});
	}
	
	var tabsIn = function()
	{
		if (current_page != null) $('tabbedbanner_page_' + current_page).removeClass('active_page');
		
		//update current page
		current_page = (page >= pages ? 0 : page);
		
		$('tabbedbanner_page_' + current_page).addClass('active_page');
		
		//make new tabs appear from top to bottom
		active_tabs = $('tabbedbanner_menu').getElements('.page_' + current_page);
		
		active_tabs.each(function(tab, i)
		{
			var fxFn = function()
			{
				new Fx.Style(tab, 'opacity',
				{
					wait: false,
					duration: 400,
					onStart: function()
					{
						tab.setStyles({'display': 'block', 'opacity': 0});
					}
				}).start(0, 1);
			}
			
			fxFn.delay(i * 300);
		});
	}
	
	if (current_page != null) tabs_chain.chain(tabsOut);
	tabs_chain.chain(tabsIn);
	
	tabs_chain.callChain();
}

function pages(tabs, timer){

	//positionBanner();
	
	$$('.tabbedbanner_banner').setOpacity(0);

	if (tabs > 0){
		//total number of pages
		pages = Math.ceil($('tabbedbanner_menu').getChildren().length / tabs);
		
		//add pages controls
		for (i = 0; i < pages; i++)
		{
			var page = new Element('span', {'id': 'tabbedbanner_page_' + i, 'class': 'tabbedbanner_page'});
			page.inject($('tabbedbanner_pages'));
			page.innerHTML = (i + 1);
			page.addEvent('click', function()
			{
				var tab_page = parseInt(this.innerHTML) - 1;
				changeTabPage(tab_page);
				pauseBanner();
				if ($defined($('tabbedbanner_menu').getElement('.page_' + tab_page).getPrevious()))
					current_tab = $('tabbedbanner_menu').getElement('.page_' + tab_page).getPrevious();
				else
					current_tab = $('tabbedbanner_menu').getLast();
				startBanner(tabs, timer);
			});
		}

		//display first page
		changeTabPage(0);
	}
	
	//previous/next
	$('tabbedbanner_previous').addEvent('click', function()
	{
		if ($defined(current_tab.getPrevious()))
			current_tab = current_tab.getPrevious();
		else
			current_tab = $('tabbedbanner_menu').getLast();
		
		current_tab.fireEvent('mousedown').fireEvent('click');
	});
	
	$('tabbedbanner_next').addEvent('click', function()
	{
		if ($defined(current_tab.getNext()))
			current_tab = current_tab.getNext();
		else
			current_tab = $('tabbedbanner_menu').getFirst();
		
		current_tab.fireEvent('mousedown').fireEvent('click');
	});
	
	//show first article
	$('tabbedbanner_menu').getFirst().fireEvent('click');
	startBanner(tabs, timer);
}

function positionBanner(height, tabsheight, tabswidth, dir)
{
	var banner_coords = $('tabbedbanner').getCoordinates();
	
	if (dir == 'vertical')
	{
		//position readmore links
		$$('.tabbedbanner_readmore').setStyles({'position': 'absolute', 'margin-top': height - 30});
	}else{
		//position readmore links
		$$('.tabbedbanner_readmore').setStyles({'position': 'absolute', 'margin-top': height - tabsheight - 30});
		
		//position tabbed menu
		$('tabbedbanner_menu');
	}
}

