// CLASS Homepage Carousel
var current = 1;
var countdown = 7000;
var timer = setInterval ('carousel.moveNext()', countdown);
var carousel = {
	init: function()
	{
		$.ajax({
			url: '/category/carousel/rss2',
			dataType: 'xml',
			type: 'GET',
			success: function(xml){
				var xml = $(xml).find('item');
				var html = '';
				var nav = '';
				
				xml.each(function(i)
				{
					var title = $(this).find('title').text();
					var img = $(this).find('thumbnail').text();
					var description = (!$.browser.safari) ? $(this).find('content\\:encoded').text() : $(this).find('encoded').text();
					var url = $(this).find('eventURL').text();
					var quote = $(this).find('eventQuote').text();
					
					//console.log('title: ' + title + ' | img: ' + img + ' description: ' + description + ' | Event Link: ' + url + ' | Event Quote: ' + quote);
					
					html += '<div class="featured" rel="' + (i+1) + '">\n';
					html += '  <span class="details"><a href="' + url + '">Details and Tickets</a></span>\n';
					html += '  ' + img + '\n';
					html += '  <h2>' + title +'</h2>\n';
					html += '  ' + description + '\n';
					html += '  <blockquote>' + quote + '</blockquote>\n';
					html += '</div>\n';
					
					if(i==0)
						nav += '<li rel="' + (i+1) + '" class="active"><a href="#">' + (i+1) + '</a></li>';
					else
						nav += '<li rel="' + (i+1) + '"><a href="#">' + (i+1) + '</a></li>';
				});
				
				$('#slideshow').html(html);
				$('.slideshow-nav ul').html(nav);
				$('.featured:eq(0)').fadeTo('fast',1);
			}
		});
	},
	moveNext: function()
	{
		var slideshow = $('#slideshow');
		var nav = $('.slideshow-nav li');

		var next = (current < nav.length) ? parseInt(current)+1 : 1;
		//console.log('current: ' + current + ' | next: ' + next + ' nav: ' + nav.length);
		
		if(current != next)
		{	
			$('.slideshow-nav li').removeClass();	//remove active class
			$('.slideshow-nav li[rel="' + next + '"]').addClass('active');	//make new item active
			
			//fadeIn/Out
			$('#slideshow .featured[rel="' + current + '"]').fadeTo('fast',0, function()
			{					
				$('#slideshow .featured[rel="' + next + '"]').fadeTo('fast',1);
				current = next;
			});
		}
	}
}

$(function(){
	var slideshow = $('#slideshow');
	var nav = $('.slideshow-nav li a');
	var comingSoon = $('.coming-soon .home-body');
	var news = $('.news .home-body');
	
	if(comingSoon.height() < news.height())
		comingSoon.css('height', news.height())
	else if(news.height() < comingSoon.height())
		news.css('height', comingSoon.height());
	
	carousel.init();
		
	// slideshow navigation
	nav.live('click',function() {  
		//console.log('current: ' + current);
		clearTimeout(timer);
	
		var next = parseInt($(this).parent('li').attr('rel'));
		
		if(current != next)
		{	
			$('.slideshow-nav li').removeClass();	//remove active class
			$(this).parent('li').addClass('active');	//make new item active
			
			//fadeIn/Out
			$('#slideshow .featured[rel="'+ current+'"]').fadeTo('fast',0, function()
			{
				$('#slideshow .featured[rel="'+ next + '"]').fadeTo('fast',1);
				current = next;
			});
		}
		
		timer;
		
		return false;
	});  
});
