/* Rotate carousel javascript module
   Daniel Ceballos
   dceballos@quinstreet.com
   2009
*/

/* Rotate module */
var Rotate = (function() {
  /* when adding new images into carousel
     insert image path here.  make sure
     hash index is same value as href text/outerText 
     in rotation_menu ul.
  */
  var images = {
	  1:{img:$hostname+"img/DM_SurveyImage_Slider.jpg", url:"http://www.surveymonkey.com/s/diabetesmonitor_survey"}, 
	  2:{img:$hostname+"img/10 Steps_Slider.jpg", url:$hostname+"healthy-living/living-with-diabetes-ten-steps-to-a-healthier-lifestyle.htm"},
	  3:{img:$hostname+"img/E-Patient_Slider.jpg", url:$hostname+"learning-center/technology/5-tips-for-being-a-good-e-patient.htm"},
	  4:{img:$hostname+"img/Hope_Slider.jpg", url:$hostname+"healthy-living/five-things-you-should-know-about-diabetes.htm"}

    
  };

  /* public initializer method */
  function init() {
    /* preload all images into DOM */
    jQuery.each(images, function(key,val) {
      var src = val.img;
      images[key].img     = new Image();
      images[key].img.src = src;
    });

    /* select first menu element */
    swap($("#rotation_menu a")[0]);
  }

  /* public method to swap image and 
     set css class */
  function swap(target) {
    var index = target.text;
    /* ie bug - ie method is outerText */
    if (target.outerText) {
      index = target.outerText;
    }
    document.rotation_image.src   = images[index].img.src;
    document.rotation_image.title = images[index].url;

    $("#rotation_menu a").each(function() {
      this.className = "deselected";
    });
    target.className = "selected";
  }

  var carousel = (function() {
    var rotate   = true;
    var interval = 3;    /* seconds */
    var index    = 0;
    var items    = null;
    var timer    = null;
    var length   = null;

    function start() {
      timer  = setInterval(encircle, (interval*1000));
      items  = jQuery.makeArray($("#rotation_menu a")).reverse();
      length = items.length;
      if (!rotate) encircle();
    }

    function stop() {
      clearInterval(timer);
      reset();
    }
    
    /* private */
    function reset() {
      index  = 0;
      items  = null;
      timer  = null;
      length = null;
    }

    function encircle() {
      var item = items.pop();
      swap(item);
      index ++;
      if (index > (length - 1)){
        stop();
        if (rotate) start();
      } 
    }

    return {
      start:start,
      stop:stop
    };
  })();

  return {
    init:init,
    swap:swap,
    carousel:carousel
  };
})();

$(document).ready( function() { 
  
  /* initialize object on document load */
  Rotate.init();
  Rotate.carousel.start();

  /* listen to menu click event and swap */
  $("#rotation_menu a").click(function(e){
    var target_object = null;

    /* ie, moz browser detection */
    if (!e) {
      e = window.event;
      target_object = e.srcElement;
    }else{
      target_object = e.target;
    }

    Rotate.carousel.stop();
    Rotate.swap(target_object);
  });

  /* listen to image click event and redirect */
  $(".rotation img:first").click(function(e){
    if (!e) {
      e = window.event;
      target_object = e.srcElement;
    }else{
      target_object = e.target;
    }
    document.location.href = target_object.title;
  });

  /* news feed nav listeners */
  $("#news_feed .nav li a").click(function(e){
    $("#news_feed .nav li").each(function() {
      this.className = "";
    });

    $("#news_feed .content").each(function() {
      this.style.display = "none";
    });

    this.parentNode.className = "selected";

    if($("#news_" + e.target.parentNode.id)[0]) {
      $("#news_" + e.target.parentNode.id)[0].style.display = "block";
    }
  });

});

