﻿var statePg = 0;
var currentPg = 1;
var nextPg = 0;
var maxPg = 0;
var iMainTime = 8000;
var tOut;
var bHover = false;

function rot_startup() {
    $(".rot_pos").fadeTo(0, 0.0);
    $(".rot_pos").fadeIn();
    $(".rot_pos").fadeTo(1000, 0.2);
    tOut = setTimeout("rot_adv()", iMainTime);
}

function rot_adv(bForce) {
    if (statePg == 1 && nextPg < 1) return;
    if (bHover && !bForce) {
        clearTimeout(tOut);
        tOut = setTimeout("rot_adv()", iMainTime);
        return;
    }
    $("#rot" + currentPg).fadeOut(800);
    $("#rots" + currentPg).attr({
        src: "images/site/squig_gray.png",
        alt: "inactive image"
    }); if (++currentPg > maxPg) currentPg = 1;
    if (nextPg > 0) {
        currentPg = nextPg;
        nextPg = 0;
    }
    if (currentPg == 1) {
        $("#rot" + currentPg).css({ top: '-327px' });
        $("#rot" + currentPg).animate({
            opacity: 'toggle',
            top: '0px'
        }, {
            duration: 1500,
            specialEasing: {
                width: 'linear',
                top: 'swing'
            }
        });
    }
    else if (currentPg == 2) {
        $("#rot" + currentPg).css({ left: '-641px' });
        $("#rot" + currentPg).animate({
            opacity: 'toggle',
            left: '0px'
        }, {
            duration: 1500,
            specialEasing: {
                width: 'linear',
                left: 'swing'
            }
        });
    }
    else $("#rot" + currentPg).fadeIn(800);
    $("#rots" + currentPg).attr({
        src: "images/site/squig_blue.png",
        alt: "inactive image"
    });
    if (statePg == 0) {
        clearTimeout(tOut);
        tOut = setTimeout("rot_adv()", iMainTime);
    }
}

function rot_pause() {
    statePg = 1;
    nextPg = 0;
    clearTimeout(tOut);
    $("#rotp").attr({
        src: "images/site/rot_play.png"
    }).css({
        right: "3px"
    });
}

function rot_play() {
    $("#rotp").attr({
        src: "images/site/rot_pause.png"
    }).css({
        right: "8px"
    });
    statePg = 0;
    rot_adv();
}

$(document).ready(function () {

    $(".rot_pos").each(
    function (intIndex) {
        $(this).bind(
			"click",
			function () {
			    clearTimeout(tOut);
			    nextPg = intIndex + 1;
			    rot_adv(true);
			});
        maxPg = intIndex + 1;
    });

    $("#rotp").toggle(
      function () {
          rot_pause();
      },
      function () {
          rot_play();
      }
    );

    $(".rot").mouseenter(
      function () {
          $(".rot_pos,.rot_ctrl").stop().fadeTo(300, 0.5);
          bHover = true;
      })
      .mouseleave(
      function () {
          $(".rot_pos,.rot_ctrl").stop().fadeTo(800, 0.2);
          bHover = false;
      });

    $(".rot_pos,.rot_ctrl").mouseenter(
      function () {
          $(this).stop().fadeTo(500, 1.0);
      })
      .mouseleave(
      function () {
          $(this).stop().fadeTo(200, 0.5);
      });

    rot_startup()
});

