var tflyCurrentPosition = 0;
var tflyMaxCarousel = 4;
var tflyAutoCarousel = 1;
var tflyCarouselTimerTime = 5000;

function tfly_carousel_prev() {
	if ( tflyCurrentPosition > 0 ) {
		tflyCurrentPosition = tflyCurrentPosition - 1;	
	} else {
		tflyCurrentPosition = tflyMaxCarousel - 1;
	}
	
	tfly_carousel_switch( tflyCurrentPosition, 1 );	
}

function tfly_carousel_next() {
	if ( tflyCurrentPosition < (tflyMaxCarousel-1) ) {
		tflyCurrentPosition = tflyCurrentPosition + 1;	
	} else {
		tflyCurrentPosition = 0;
	}

	tfly_carousel_switch( tflyCurrentPosition, 1 );		
}

function tfly_carousel_next_timer() {
	if ( tflyCurrentPosition < (tflyMaxCarousel-1) ) {
		tflyCurrentPosition = tflyCurrentPosition + 1;	
	} else {
		tflyCurrentPosition = 0;
	}

	tfly_carousel_switch( tflyCurrentPosition, 0 );		
}

function tfly_carousel_switch( i, userInput ) {
	if ( userInput ) {
		tflyAutoCarousel = 0;	
	}
	
	$j = jQuery.noConflict();
	
	var c = $j( "#tfly-featured-events ul.images li" );
	c.animate( { opacity: 0 }, 300, function() {
		c.hide().css( "opacity", "1");
		$j( "#tfly-featured-events ul.images li:eq(" + i + ")" ).css( "opacity", "0" ).show().animate( { opacity: 1 }, 300 );
	} );
	
	var overlayContent = $j( "#overlay-content li:eq(" + i + ")" ).html();
	$j( "#overlay .change" ).html( overlayContent );
	
	tflyCurrentPosition = i;
	
	$j( "#tfly-featured-events ul.nav a" ).removeClass( "selected" );
	$j( "#tfly-featured-events ul.nav a#carousel_" + i ).addClass( "selected" );
}

function tfly_carousel_setup() {
	$j = jQuery.noConflict();
	
	// make images full size
	$j( "#tfly-featured-events ul li img" ).css( "width", "100%" );
	
	// only show first event
	$j( "#tfly-featured-events ul li" ).css( "display", "none" );
	$j( "#tfly-featured-events ul li:first" ).css( "display", "block" );
	
	var str = '';
	var i = 0;
	for ( i = 0 ; i < tflyMaxCarousel; i++) {
		str = str + "<li><a id='carousel_" + i + "' href='#' onclick='tfly_carousel_switch(" + i + ", 1 ); return false'>" + ( i + 1 ) + "</a></li>";
	}
	
	str = str + "<li><a id='prev_carousel' href='#' onclick='tfly_carousel_prev(); return false'></a></li>";
	str = str + "<li><a id='next_carousel' href='#' onclick='tfly_carousel_next(); return false'></a></li>";	
	
	$j( "#tfly-featured-events ul.nav" ).html( str );
	
	$j( "#tfly-featured-events ul.nav a#carousel_0" ).addClass( "selected" );
	
	setTimeout( "tfly_carousel_timer()", tflyCarouselTimerTime );
}

function tfly_carousel_timer() {
	if ( 	tflyAutoCarousel ) {
		tfly_carousel_next_timer();		
		setTimeout( "tfly_carousel_timer()", tflyCarouselTimerTime );	
	}
}

function tfly_set_frame_start() {
}

function tfly_set_frame() {
	var w = jQuery( "div#fancy_div ul" ).css("width");
	var h = jQuery( "div#fancy_div ul" ).css("height");
	jQuery( "div#fancy_outer").animate( { width: ( parseInt( w ) + 25 ) + "px", height: ( parseInt( h ) + 25 ) + "px" }, 250 );
}

function tfly_load() {
	tfly_carousel_setup();
	
	// set up fancyBox
	
	jQuery( "a.fancybox").fancybox( {
		'hideOnContentClick': false,
		'overlayOpacity': 0.6
	});
	
	jQuery( "a.fancybox-more" ).fancybox( {
		'frameWidth': 125,
		'frameHeight': 38,
		'overlayOpacity': 0.0,
		'callbackOnStart': tfly_set_frame_start,
		'callbackOnShow': tfly_set_frame
	} );
	
	
//	jQuery('.popups').aqLayer({clone:true,closeBtn:true,attach:'nw',offsetY:0,offsetX:10});	
}

function tfly_fbs_click() {
	u=location.href;t=document.title;window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');
	return false;
}

jQuery( document ).ready( function() { tfly_load(); } );
