function ImageText(img, text) {
  this.img = img;
  this.text = text;
};

var MC = {
  SlideShow: function(imgFrame, textFrame, imgTextGroup, imgTexts) {
    this.imgFrame = imgFrame;
    this.textFrame = textFrame;
    this.imgTextGroup = imgTextGroup;
    this.imgTexts = imgTexts;
    this.images = [];
    this.texts = [];
    this.index = 0;
    
    this.preload = function() {
      for (var i = 0; i < this.imgTexts.length; i++) {
        var image = document.createElement("img");
        image.src = this.imgTexts[i].img;
        $j(image).css({opacity: 1.0});
        this.images.push(image);

        this.texts.push(imgTexts[i].text);
      }
      
      var firstImage = this.images[this.index];
      $j(firstImage).css({opacity:1.0});
      this.imgFrame.append(firstImage);

      var firstText = this.texts[this.index];
      this.textFrame.append(firstText);
    };
    
    this.transitionToImage = function(nextImage, nextText) {
      var imgTextGroup = this.imgTextGroup;

      var image = $j(this.imgFrame).find('img');
      var imgFrame = this.imgFrame;
      var text = $j(this.textFrame).find('p');
      var textFrame = this.textFrame;
      
      imgTextGroup.animate({opacity:0}, 500, "linear", function() {
        image.remove();
        imgFrame.append(nextImage);
        text.remove();
        textFrame.append(nextText);
        $j("#image-text-group").animate({opacity:1.0}, 500);
      });
    };
    
    this.back = function() {
      if (this.index > 0) {
        this.index--;
        this.transitionToImage(this.images[this.index], this.texts[this.index]);
      }
      else if (this.index == 0) {
        this.transitionToImage(this.images[this.images.length-1], this.texts[this.texts.length-1]);
        this.index = this.images.length-1;
      }
    };
    
    this.forward = function() {
      if (this.index + 1 < this.images.length) {
        this.index++;
        this.transitionToImage(this.images[this.index], this.texts[this.index]);
      }
      else if (this.index + 1 == this.images.length) {
        this.transitionToImage(this.images[0], this.texts[0]);
        this.index = 0;
      }
    };
    
    this.preload();
    
    return true;
  }
}

//Image Text Combos
var imgtext6 = new ImageText("/images/screenshot6.png", "<p>Access all processes, documents, and information associated with an employee in a single place.</p>");

var imgtext7 = new ImageText("/images/screenshot7.png", "<p>The Start Process menu allows you to quickly begin any security clearance process for an employee right from the home page.</p>");

var imgtext14 = new ImageText("/images/screenshot14.png", "<p>Automatically generate 4311, 4414, and other mandatory forms in seconds.</p>");

var imgtext9 = new ImageText("/images/screenshot9.png", "<p>The To Do list shows tasks you have left to complete for all outstanding security clearance processes.</p>");

var imgtext10 = new ImageText("/images/screenshot10.png", "<p>Quickly view active security clearance processes, sorted by employee or process type.</p>");

var imgtext11 = new ImageText("/images/screenshot11.png", "<p>Easily see the breakdown of your company based on different criteria through multiple graphs on your home page.</p>");

var imgtext12 = new ImageText("/images/screenshot12.png", "<p>Easily view and edit your company's contracts, employees, cleared personnel and security officers through an easy-to-use interface.</p>");

var imgtext13 = new ImageText("/images/screenshot13.png", "<p>Clearance Track supports multiple file formats; upload documents to the document library of each form group.</p>");

var ss = new MC.SlideShow($j("#pics"), $j("#image-text"), $j("#image-text-group"), [imgtext11, imgtext9, imgtext10, imgtext12, imgtext7, imgtext6, imgtext13, imgtext14]);

var timerID;
var time = 5000; //Length of time between switching pictures (in ms)
var playPause = 1; //0 pause, 1 play

$j(document).ready(function() {
  $j("#goleft").click(function(event) {
    if (playPause == 1)
      clearTimeout(timerID);
    ss.back();
    if (playPause == 1)
      timerID = setTimeout("play()", time);
  });
  
  $j("#goright").click(function(event) {
    if (playPause == 1)
      clearTimeout(timerID);
    ss.forward();
    if (playPause == 1)
      timerID = setTimeout("play()", time);
  });

  $j("#playpause").click(function(event) {
    if (playPause == 1)
    {
      clearTimeout(timerID);
      var pp = document.getElementById('playpause');
      pp.innerHTML = "<img src='/images/play.png'/>";
      playPause = 0;
    }
    else
    {
      var pp = document.getElementById('playpause');
      pp.innerHTML = "<img src='/images/pause.png'/>";
      playPause = 1;
      timerID = setTimeout("play()", time);
    }
  });
  
  timerID = setTimeout("play()", time);
});

function play() {
  ss.forward();
  timerID = setTimeout("play()", time);
};

function playpause() {
  clearTimeout(timerID);
};
