var looping = true;
var animating = false;

function advanceSlide(target, theButtons){
    theButtons.removeClass("active");
    target.addClass("active");
    var newSlide;
    switch (target.attr("id")) {
    case "slideBtn1":
	newSlide = $("#slide1");
	break;
    case "slideBtn2":
	newSlide = $("#slide2");
	break;
    case "slideBtn3":
	newSlide = $("#slide3");
	break;
    case "slideBtn4":
	newSlide = $("#slide4");
	break;
    case "slideBtn5":
	newSlide = $("#slide5");
	break;
    case "slideBtn6":
	newSlide = $("#slide6");
	break;
    }

    // IE really doesnt like to fade transparen pngs
    // so slide it in/out if we're on IE
    // (leadingWhitespace is currently false on IE6, 7, and 8
    if (!jQuery.support.leadingWhitespace) {
	animating = true;
	$(".txt:visible").slideUp("slow", function() {
		newSlide.slideDown("slow", function(){ animating = false; });
	    });
    } else {
	animating = true;
	$(".txt:visible").fadeOut("slow", function() {
		newSlide.fadeIn("slow", function(){ animating = false;});
	    });
    }
}

$(document).ready(function() {
          
	// hide the "slides"
	$(".txt").each(function(index) {
		$(this).css({ "display": "none" });
	    });

	// turn on the first one
	$("#slide1").css({ "display": "block" });

	var theButtons = $(".ad-nav > li > a");
	theButtons.each(function(index) {
		$(this).click(function() {
			looping = false;
			if(!animating)
			    advanceSlide($(this), theButtons);
		    });
	    });
	
	var currentSlideIdx = 1;
	window.setInterval(function() {
		if(!looping)
		    return;
		currentSlideIdx += 1;
		if(currentSlideIdx == 7)
		    currentSlideIdx = 1;
		var theSlide = $("#slideBtn" + currentSlideIdx);
		advanceSlide(theSlide, $(".ad-nav > li > a"));
	    }, 10000);

    });
