var iPushboxSlideSpeed = 2000;
var iPushboxAutoSlideDelay = 8000;
var iPushboxTimeout;
var iPushboxSlideCount;
var iPushboxCurrentSlide;
var bPushboxAnimating = false;

var IEVersion = getInternetExplorerVersion();
if ( IEVersion!=-1 && IEVersion<9 ) {
	jQuery('body').addClass('IE8-or-less');
}

function initialise_pushbox() {

	if ( jQuery('#pushbox').length > 0 ) {
		// a pushbox exists on this page...

		// position all of the slides...
		jQuery('#pushbox ul.slides li').css('left', 0);
		jQuery('#pushbox ul.slides li').hide();

		// declare some variables...
		iPushboxSlideCount = jQuery('#pushbox ul.slides li').length;
		iPushboxCurrentSlide = 0;

		// add the list items for the slide navigation...
		var sPushboxNav = '';
		if (iPushboxSlideCount > 1) {
			for (i=1; i<=iPushboxSlideCount; i++) {
				sPushboxNav += '<li><a href="#">' + i + '</a></li>';
			}
		}
		jQuery('.pushbox_nav').html( sPushboxNav );

		// set up the onclick events for the slide navigation...
		var objPushboxNavLinks = jQuery('.pushbox_nav li a').each( function( index ) {

			jQuery(this).click( function(event) {
				pushbox_slide_to( objPushboxNavLinks.index(this)+1 );
				return false;
			});


		})


		// slide the first slide in...
		pushbox_slide_to( 1 );

	}

}

function pushbox_slide_to( iSlideToShow ) {
	
	if (bPushboxAnimating==false) {
		
		bPushboxAnimating = true;
		
		// stop any queued up slide requests...
		if(iPushboxTimeout) {
			clearTimeout(iPushboxTimeout);
		}


		// if the slide to show number is greater than the total number of slides, move in the first one...
		if ( iSlideToShow > iPushboxSlideCount ) {
			iSlideToShow = 1;
		}

		// remove the "active" class from all slide nav...
		jQuery('.pushbox_nav li').removeClass('active');

		// cancel out and finish all previously queued up animations...
		jQuery('#pushbox ul.slides li').stop(true, true);

		// fade out the current slide...
		if ( iPushboxCurrentSlide > 0 ) {
			jQuery('#pushbox ul.slides li:nth-child(' + iPushboxCurrentSlide + ')').fadeOut( iPushboxSlideSpeed );
		}

		// fade in the slide to show...
		jQuery('#pushbox ul.slides li:nth-child(' + iSlideToShow + ')').fadeIn( iPushboxSlideSpeed, function (){

			// set this slide's nav item to active...
			jQuery('.pushbox_nav li:nth-child(' + iSlideToShow + ')').addClass('active');

			// update that this is the current slide...
			iPushboxCurrentSlide = iSlideToShow;
			
			bPushboxAnimating = false;

		});

		// queue up the next slide to show...
		iPushboxTimeout = setTimeout( function() {

			pushbox_slide_to( iPushboxCurrentSlide + 1 );

		}, iPushboxAutoSlideDelay )
	
	}
}

function getInternetExplorerVersion() {
// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
	var rv = -1; // Return value assumes failure.
	if ( navigator.userAgent.indexOf('MSIE') > -1 )
	{
		var ua = navigator.userAgent;
		var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
		if (re.exec(ua) != null)
		  rv = parseFloat( RegExp.$1 );
	}
	return rv;
}
