var tfly = {};
/*tfly.util = function() {
    function privateMethod() {
    }
    return {
        publicMethod: function() {
        }
    };
}();*/

tfly.carousel = function() {
    // private members
    var tflyCurrentPosition = 0,
    tflyMaxCarousel = 4,
    tflyAutoCarousel = 1,
    tflyCarouselTimerTime = 5000,
    $j = jQuery.noConflict();

    // private methods
    function timer() {
        if (tflyAutoCarousel) {
            nextTimer();
            setTimeout(timer, tflyCarouselTimerTime );
        }
    }

    function nextTimer() {
        if ( tflyCurrentPosition < (tflyMaxCarousel-1) ) {
            tflyCurrentPosition = tflyCurrentPosition + 1;
        } else {
            tflyCurrentPosition = 0;
        }

        doSwitch( tflyCurrentPosition, 0 );
    }

   function doSwitch( 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" );
    }


    return (function() {
        // Public members

        // Public methods
        this.prev = function() {
            if ( tflyCurrentPosition > 0 ) {
                tflyCurrentPosition = tflyCurrentPosition - 1;
            } else {
                tflyCurrentPosition = tflyMaxCarousel - 1;
            }

            doSwitch( tflyCurrentPosition, 1 );
        }

        this.next = function() {
            if ( tflyCurrentPosition < (tflyMaxCarousel-1) ) {
                tflyCurrentPosition = tflyCurrentPosition + 1;
            } else {
                tflyCurrentPosition = 0;
            }

            doSwitch( tflyCurrentPosition, 1 );
        }

        this.switchTo = function(i, userInput) {
            doSwitch(i, userInput);
        }

        // Setup
        var newMax = $j("#tfly-featured-events ul.images").children().size();
        tflyMaxCarousel = Math.min(tflyMaxCarousel, newMax);

        // 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='carousel.switchTo(" + i + ", 1 ); return false'>" + ( i + 1 ) + "</a></li>";
        }

        str = str + "<li><a id='prev_carousel' href='#' onclick='carousel.prev(); return false'></a></li>";
        str = str + "<li><a id='next_carousel' href='#' onclick='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(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() {
	// set up fancyBox

	////
	// TMH 25 March 2010: Nobody seems to be using fancybox in the SVN so remove it.
  ////

  // 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(); } );
