﻿
var bannerRotation = 5; // in seconds

var banners = new Array();
banners[0] = {src : '../images/head_banner_5.jpg',
              map : 'webinars',
              link: ''};
banners[1] = {src : '../images/head_banner_4.jpg',
              map : '',
              link: '../EducationResources/CustomizableLiterature.aspx'};
banners[2] = {src : '../images/head_banner_3.jpg',
              map : '',
              link: '../EducationResources/MentorOfTheYear.aspx'};
banners[3] = {src : '../images/head_banner_2.jpg',
              map : '',
              link: '../ClinicalStudies/Default.aspx'};
banners[4] = {src : '../images/head_banner_6.jpg',
              map : '',
              link: '../TrySonicare/ContactUs.aspx'};


var currentBannerRotation = 0;
var rotateBannerInterval = 0;
var isRotating = false;

function RotateBanner(isInfinite)
{
	if (currentBannerRotation + 1 >= banners.length)
	{
	    if(isInfinite) {
	        currentBannerRotation = 0;
        } else {
            stopRotateBanner();
        }
	} else {
	    currentBannerRotation++;
	}
	
	bannerStep(currentBannerRotation);
	
	return true;
}

function toggleRotateBanner() {
    if(isRotating) {
        pause();
    } else {
        unpause();
    }
}

function bannerBack(isInfinite) {
  if(isInfinite == undefined) {
    isInfinite = false;
  }
    pause();
    
	if (currentBannerRotation - 1 < 0)
	{
	    if(isInfinite) {
	        currentBannerRotation = banners.length - 1;
        } else {
            return false;
        }
	} else {
	    currentBannerRotation--;
	}
	
	bannerStep(currentBannerRotation);
}

function bannerForward(isInfinite) {
  if(isInfinite == undefined) {
    isInfinite = false;
  }
    pause();
    
	if (currentBannerRotation + 1 >= banners.length)
	{
	    if(isInfinite) {
	        currentBannerRotation = 0;
        } else {
            return false;
        }
	} else {
	    currentBannerRotation++;
	}
	
	bannerStep(currentBannerRotation);
	
	return true;
}

function bannerStep(nextstep) {
	var imgBanner = $('#imgBanner');
	
	imgBanner.attr('src', banners[currentBannerRotation].src);
	if(banners[currentBannerRotation].map === '') {
	    imgBanner.removeAttr('useMap');
	    imgBanner.click(function(){
	        window.location.href = banners[currentBannerRotation].link;
	    });
	} else {
	    imgBanner.attr('useMap', '#' + banners[currentBannerRotation].map);
	}
}

function pause() {
    $('#pause a').addClass('paused');
    $('#pause a').attr('title', 'Click to Unpause');
    stopRotateBanner();
}

function unpause() {
    $('#pause a').removeClass('paused');
    $('#pause a').attr('title', 'Click to Pause');
    startRotateBanner();
}

function startRotateBanner(isInfinite) {
  if(isInfinite == undefined) {
    isInfinite = false;
  }
  
  if(!isRotating) {
    rotateBannerInterval = setInterval("RotateBanner(" + isInfinite + ")", bannerRotation * 1000);
    isRotating = true;
  }
}

function stopRotateBanner() {
    if(isRotating) {
        clearInterval(rotateBannerInterval);
        isRotating = false;
    }
}


/**
 * News Story Class
 * describes a news story item
 */
function NewsStory(newstorydiv)
{
    function height() {
        return this.div.height();
    }
    
    function isVisible() {
        return this.visible;
    }
    
    function setVisible(v) {
        this.visible = v;
    }
    
    this.div = $(newstorydiv);  // the display div
    this.visible = false;    // is visible through the scroll display
    this.height = height;
    this.isVisible = isVisible;
    this.setVisible = setVisible;
}

function NewsBox(news_box_id)
{
    function init() {
      var news_stories_divs = $("#" + this.id + " .newsstory");
      var fill_height = 0;
      this.scroller = $("#" + this.id + " .news_box_scroller");
      var news_box_height = $("#" + this.id).height() - 8;
      
      for(var i = 0; i < news_stories_divs.length; i++) {
          this.addStory(new NewsStory(news_stories_divs[i]));
          if(fill_height < news_box_height) {
              fill_height += this.stories[i].height();
              if(fill_height > news_box_height) {
                  fill_height = news_box_height;
              } else {
                  this.stories[i].setVisible(true);
                  this.visiblebottom = i;
              }
          }
      }
      
      this.buttonup = $("#" + this.id + "_buttonup");
      this.buttondown = $("#" + this.id + "_buttondown");
      
    }
    
    function addStory(story) {
        this.stories[this.stories.length] = story;
    }
    
    function scrollup(e) {
      if(this.visiblebottom + 1 < this.stories.length) {
        var delta = (this.stories[this.visiblebottom + 1].height() + 12) * -1;
        this.movescroller(delta);
        this.visiblebottom++;
        this.visibletop++;
      }
    }
    
    function scrolldown(e) {
      if(this.visibletop > 0) {
        var delta = this.stories[this.visiblebottom].height() + 12;
        this.movescroller(delta);
        this.visiblebottom--;
        this.visibletop--;
      }
    }
    
    function movescroller(delta) {
        var position = this.scroller.position();
        this.scroller.animate({top: delta + position.top + ''}, 1000);
    }
    
    this.id = news_box_id;
    this.scroller = null;
    this.position = 0;
    this.stories = [];
    this.addStory = addStory;
    this.init = init;
    this.scrollup = scrollup;
    this.scrolldown = scrolldown;
    this.buttonup = null;
    this.buttondown = null;
    this.visibletop = 0;
    this.visiblebottom = 0;
    this.movescroller = movescroller;
}

// --- HELPER METHODS --- //
function GetDomObject(obj)
{
    return ((typeof(obj) != 'object') ? document.getElementById(obj) : obj);
}
