﻿
var bannerRotation = 5; // in seconds

var banners = new Array();
banners[0] = {src : '../images/head_banner_dc.jpg',
              map : '',
              link: '../TrySonicare/ContactUs.aspx'};
banners[1] = {src : '../images/head_banner_facebook.jpg',
              map : 'facebook',
              link: '../Facebook/Default.aspx'};
banners[2] = {src : '../images/head_banner_featuredpeers.jpg',
              map : 'peers',
              link: '../FeaturedPeers/Default.aspx'};
banners[3] = {src : '../images/head_banner_order.jpg',
              map : 'order',
              link: ''};
banners[4] = {src : '../images/head_banner_5.jpg',
              map : 'webinars',
              link: ''};
banners[5] = {src : '../images/head_banner_afdc.jpg',
              map : '',
              link: '../TrySonicare/Default.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();
	
	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();
}

function bannerForward(isInfinite) {
  if(isInfinite == undefined) {
    isInfinite = false;
  }
    pause();
    
	if (currentBannerRotation + 1 >= banners.length)
	{
	    if(isInfinite) {
	        currentBannerRotation = 0;
        } else {
            return false;
        }
	} else {
	    currentBannerRotation++;
	}
	
	bannerStep();
	
	return true;
}

function bannerStep() {
	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 outerHeight(includeMargin) {
        if (typeof (includeMargin) == 'undefined' || includeMargin == null) {
            includeMargin = false;
        }
        return this.div.outerHeight(includeMargin);
    }

    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.outerHeight = outerHeight;
    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].outerHeight(true);
                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");

        if (fill_height < news_box_height) {
            $('.news_buttons').hide();
        }
    }

    function addStory(story) {
        this.stories[this.stories.length] = story;
    }

    function scrollup(e) {
        if (this.visiblebottom + 1 < this.stories.length && NewsBox.isscrolling == false) {
            var delta = (this.stories[this.visiblebottom + 1].height() + 12) * -1;
            this.movescroller(delta);
            this.visiblebottom++;
            this.visibletop++;
        }
    }

    function scrolldown(e) {
        if (this.visibletop > 0 && NewsBox.isscrolling == false) {
            var delta = this.stories[this.visiblebottom].height() + 12;
            this.movescroller(delta);
            this.visiblebottom--;
            this.visibletop--;
        }
    }

    function movescroller(delta) {
        var position = this.scroller.position();
        NewsBox.isscrolling = true;
        this.scroller.animate({ top: delta + position.top + '' }, 1000, 'swing', function() { NewsBox.isscrolling = false; });
    }

    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;
}

NewsBox.isscrolling = false;

// --- HELPER METHODS --- //
function GetDomObject(obj)
{
    return ((typeof(obj) != 'object') ? document.getElementById(obj) : obj);
}

