﻿/*
    ELEMENT ROTATOR
    - this function is used on the home page in order to rotate the news items in the news pods  
*/
function ElementRotator() {

    if (typeof ElementRotator._initialized == 'undefined') {//If prototype for ElementRotator has not been declared do so

        ElementRotator.prototype.init = function(obj) {//Initialize script
            //If object has not been passed in
            if (typeof obj != 'object') throw new Error('ElementRotator expects one argument of type \'object\'');
            this.params = obj;
            this.params.interval = (!this.params.interval || isNaN(this.params.interval)) ? 5000 : this.params.interval; //Interval between news items
            this.fadeDuration = 400; //Duration of the fade effect
            this.effect = 'easeInQuad';
            this.highestHeight = this.getHighestHeight();
            this.setHighestHeight();
            if (this.ok()) {
                this.prepareMarkup(); //Set up mark up before starting the rotation
                var er = this; //Keep reference to current object so that setInterval has a hold on it
               var _interval =  setInterval(function() { er.rotate(); }, this.params.interval); //Rotate news items
            }
        };

        ElementRotator.prototype.getHighestHeight = function () {//Returns the height of the heighest element
            var height = 0;
            $(this.params.parent).children().each(function ($key, $value) {//Loop through each news item
                if (height < $(this).outerHeight()) {
                    height = $(this).outerHeight();
                } //If height of this news item is higher than what has been assigned, swap it
            });
            return (height);
        };

        ElementRotator.prototype.setHighestHeight = function () {//Returns the height of the heighest element
            $(this.params.parent).children().css('height', this.highestHeight);
            
        };

        ElementRotator.prototype.rotate = function () {//Manages the rotating effect of the elements
            if ($(this.params.parent).children().length == 1) {
                return;
            }
            var top = parseInt($(this.params.parent).position().top) - (this.highestHeight);
            // console.log($(this.params.parent));
            $(this.params.parent).animate({
                top: top
            }, this.fadeDuration, this.effect, function () {
                //console.log($(this));
                var li = $(this).find('li:first').detach();
                $(this).css('top', 0);
                li.appendTo($(this));
            });
        };

        ElementRotator.prototype.next = function() {//Manages the rotating effect of the elements

        };

        ElementRotator.prototype.ok = function() {//Manages the rotating effect of the elements
            return ($(this.params.parent).children().length >= 1);
        };

        ElementRotator.prototype.prepareMarkup = function () {//Formats the markup prior to rotation
            var er = this;
            var _ht = "";
            if ($(this.params.parent).children().length == 1) {
                _ht = (1 * this.highestHeight) + 10;
            } else {
                _ht = (2 * this.highestHeight) + 10;
            }
            $(this.params.parent).parent().css({//Set the height of the parent element
                height: _ht,
                overflow: 'hidden'
            });

        };
        
    }
    ElementRotator._initialized = true;//Set _initialized to true in order to avoid multiple prototype declarations
    this.init(arguments[0]);//Initialize
}

$(document).ready(function() {//when document is ready
       
    var obj = {
        parent: '.news-items',//clas name of news items parent
        bkgColor: '#CFE3D8',//background color of news items used for the fade effect
        interval: 10000//Time interval for news items rotation
    };
   var rotator = new ElementRotator(obj); //Start rotation script

    var obj2 = {
        parent: '.news-items2', //clas name of news items parent
        bkgColor: '#CFE3D8', //background color of news items used for the fade effect
        interval: 10000//Time interval for news items rotation
    };
    if ($('.news-items2').length > 0) {   
     var rotator = new ElementRotator(obj2); //Start rotation script
    }

  
    

});
