// SET THIS VARIABLE FOR DELAY, 1000 = 1 SECOND
var delayLength = 10000;
var height = 272;
var numPanels = 4;

function doMove(panelHeight, tooFar) {
//get our active and next slides
    var $active = jQuery('#mover DIV.visible');
    var $next = $active.next().length ? $active.next()
        : jQuery('#mover DIV').first();

    //fade out our current slide
    $active.css({ opacity: 1.0 })
        .animate({ opacity: 0.0 }, 1000, function () {
        //hide our current slide
            $active.removeClass('visible');
            $active.addClass('hidden');

            var topValue = jQuery("#mover").css("top");
            // Fix for IE
            if (topValue == "auto") { topValue = 0; };
            //figure out how far we have to move our big div that contains all the slides
            var movement = parseFloat(topValue, 10) - panelHeight;
            //move to the new slide
            if (movement == tooFar) {
                $next = jQuery('#mover DIV').first();
                jQuery("#mover").css('top', 0);
            }
            else {
                jQuery("#mover").css('top', movement);
            }

            //manage our 1..2..3..4 button states
            var activeID = parseID($active.attr('id'));
            var $activeSlider = jQuery('#Slider' + activeID);
            $activeSlider.removeClass('s2-sliderButton-Selected');
            $activeSlider.addClass('s2-sliderButton');

            var nextID = parseID($next.attr('id'));
            var $nextSlider = jQuery('#Slider' + nextID);
            $nextSlider.removeClass('s2-sliderButton');
            $nextSlider.addClass('s2-sliderButton-Selected');

            //fade in our new slide
            $next.css({ opacity: 0.0 })
				.addClass('visible')
				.removeClass('hidden')
				.animate({ opacity: 1.0 }, 1000, function () {
			        //fix for anti-aliasing bug in IE
            	    if ($.browser.msie) {
				        this.style.removeAttribute('filter');
				    }
				});
        });
}

function parseID(input){

    var result = input.split('-');
    return result[1];
}


jQuery(function () {

    var tooFar = -(height * numPanels);

    sliderIntervalID = setInterval(function () {
        doMove(height, tooFar);
    }, delayLength);

});

function SetPanelByClick(pos) {
    var $next;
    //
    if (pos == 1) { $next = jQuery('#mover DIV.slide').first(); }
    if (pos == 2) { $next = jQuery('#mover DIV.slide').first().next(); }
    if (pos == 3) { $next = jQuery('#mover DIV.slide').first().next().next(); }
    if (pos == 4) { $next = jQuery('#mover DIV.slide').last(); }

    var $active = jQuery('#mover DIV.visible');

    clearInterval(sliderIntervalID);

    var scrollPos = (pos - 1) * height * -1;

    $active.css({ opacity: 0.0 })
        .animate({ opacity: 0.0 }, 100, function () {
            $active.removeClass('visible');
            $active.addClass('hidden');

            var topValue = jQuery("#mover").css("top");
            // Fix for IE
            if (topValue == "auto") { topValue = 0; };

            jQuery("#mover").css('top', scrollPos);


            var activeID = parseID($active.attr('id'));
            var $activeSlider = jQuery('#Slider' + activeID);

            $activeSlider.removeClass('s2-sliderButton-Selected');
            $activeSlider.addClass('s2-sliderButton');


            $next.css({ opacity: 1.0 })
				.addClass('visible')
				.removeClass('hidden')
				.animate({ opacity: 1.0 }, 100, function () {
				    if ($.browser.msie) {
				        this.style.removeAttribute('filter');
				    }
				    var nextID = parseID($next.attr('id'));
				    var $nextSlider = jQuery('#Slider' + nextID);

				    $nextSlider.removeClass('s2-sliderButton');
				    $nextSlider.addClass('s2-sliderButton-Selected');

				});
        });


}

// 
//  jQuery IE Fade Fix 
// 
//  Adapted from code found at http://jquery.malsup.com/fadetest.html. 
// 
//  This is only needed for IE 7 and earlier, so this is best added to your page using IE's conditional comments 
//  (http://msdn.microsoft.com/en-us/library/ms537512%28VS.85%29.aspx) as follows: 
//      <!--[if lt IE 8]><script type="text/javascript" src="jquery-ie-fade-fix.js"></script><![endif]--> 
//
 (function($) {     $.fn.fadeIn = function(speed, callback) {         return this.animate({opacity: 'show'}, speed, function() {                 if ( $.browser.msie )                 {                         this.style.removeAttribute('filter');                 }                 if ( $.isFunction(callback) )                 {                         callback.call(this);                 }         });     };      $.fn.fadeOut = function(speed, callback) {         return this.animate({opacity: 'hide'}, speed, function() {                 if ( $.browser.msie )                 {                         this.style.removeAttribute('filter');                 }                 if ( $.isFunction(callback) )                 {                         callback.call(this);                 }         });     };      $.fn.fadeTo = function(speed, to, callback) {         return this.animate({opacity: to}, speed, function() {                 if ( to == 1 && $.browser.msie )                 {                         this.style.removeAttribute('filter');                 }                 if ( $.isFunction(callback) )                 {                         callback.call(this);                 }         });     }; })(jQuery); 
