/*jslint white: true, browser: true, onevar: true, undef: true, nomen: true, eqeqeq: true, plusplus: true, bitwise: true, regexp: true, newcap: true, immed: true, indent: 2 */

//Ad Rotater JavaScript Document For FLS

/*global $, window, PHEAA */

var flsSlides = (function () {
  
  // Declaring Private Variables
  var aSlides,
  oTimeout = null, 
  oSlideshowList = null,
  oSlideshowController = null,
  iCurrentSlide = 0,
  bSlideBlocker = false,
  
  // Declaring Private Methods
  fnSlideShow = function (e) {
    // Declare variables;
    var oNewSlide,
    // Transform "this" into jQuery object;
    oThis = $(this),
    // Speed interval in milliseconds;
    iSpeed = 400,
    // Extract slide index from this;
    iNewSlide = oThis.data("slideIndex"),
    // Get DOM object of current slide;
    oCurrentSlide = $(aSlides[iCurrentSlide]);
    
    /*
      End function if there is no new slide index, 
      if this is the current slide, 
      or if the animation is already in progress;
    */
    if (isNaN(iNewSlide) || 
        iNewSlide === iCurrentSlide || 
        bSlideBlocker) {
      return false;
    } else {
      // Get DOM object of new slide;
      oNewSlide = aSlides[iNewSlide];
      // Set blocking variable;
      bSlideBlocker = true;
    }
    
    // Fade out old slide and "508" hide it at end;
    oCurrentSlide.data("nextSlide", oNewSlide).fadeOut(iSpeed, function () {
      // Transform this into a jQuery object;
      var oThis = $(this);
      
      // Hide last slide for 508;
      oThis.addClass("hide").css("display", "block");
      // Fade in new slide;
      $(oThis.data("nextSlide")).css("display", "none").removeClass("hide").fadeIn(iSpeed, function () {
        // Discativate blocking variable;
        bSlideBlocker = false;
      }).find("p.storyButton").css("display", "none").fadeIn(iSpeed).end();
    }).find("p.storyButton").fadeOut(iSpeed, function () {
      $(this).css("display", "block");
    }).end();
    
    //Fade for controller;
    $("ul#slideShowController").fadeOut(iSpeed, function () {
      $(this).fadeIn(iSpeed);
    });
    
    // Turn all images to off source image;
    oSlideshowController.find("img").attr("src", "\/images\/homepage\/controller.gif").end();
    // Turn initiating image to on source image;
    oThis.find("img").attr("src", "\/images\/homepage\/controller-active.gif").end().blur();
    // Set current slide index to new slide index;
    iCurrentSlide = iNewSlide;
    
    // End function;
    if (e && e.preventDefault) {
      if (oTimeout) {
        clearTimeout(oTimeout);
        oTimeout = null;
      }
      e.preventDefault();
    }
    return false;
  },
  
  fnSlideTimer = function () {
    var oNewSlideLink,
    iNewIndex = (iCurrentSlide + 1) >= aSlides.length ? 0 : iCurrentSlide + 1;
     
    if (oTimeout) {
      clearTimeout(oTimeout);
      oTimeout = null;
    }
     
    $("ul#slideShowController a").each(function () {
      var oThis = $(this);	
	
      if (oThis.data("slideIndex") === iNewIndex && 
          typeof oNewSlideLink === "undefined") {
        oNewSlideLink = this;
      }
    });
     
    if (typeof oNewSlideLink !== "undefined") {
      fnSlideShow.call(oNewSlideLink);
       
      oTimeout = setTimeout(fnSlideTimer, 7000);
    }
  };
  
  return function () {
    
    // Launch New Window Script
    var oDom = $(document);
          
    // Write in controller for slideshow;
    
    // Get Slidshow list element;
    oDom.find("ul#slideShow").each(function () {
      var i, iLength, sImgSrc;
      
      oSlideshowList = $(this);
      
      // Save a reference to each slide instance as an array;
      aSlides = oSlideshowList.children("li");
      
      // Build the slideshow controller;
      oSlideshowController = $("<ul id=\"slideShowController\" \/>").insertAfter(oSlideshowList);
      
      // Insert a control selector for each slide in the list;
      for (i = 0, iLength = aSlides.length; i < iLength; i += 1) {
        // Determine image source variable value;
        sImgSrc = i > 0 ? 
          "\/images\/homepage\/controller.gif" : 
          "\/images\/homepage\/controller-active.gif";
        
        // Construct slide selecter and append to list;
        $("<li><a href=\"#\"><img src=\"" + sImgSrc + "\" alt=\"Goto Slide #" + (i + 1) + "\" height=\"15\" width=\"15\" \/></a></li>").find("a").data("slideIndex", i).click(fnSlideShow).end().appendTo(oSlideshowController);
      }
      
      // Start slideshow timer;
      $(window).bind("load", function () {
        oTimeout = setTimeout(fnSlideTimer, 7000);
      });
      
    }).end();
  };
  
}());

$("document").ready(flsSlides);
