﻿/* Company Alerts Control */
(function($) {
    var CompanyAlerts = {
        offset: 150,
        sliderDelay: 7000,
        canvasWidth: 10000,
        currentPage: 1,
        playing: 1,
        init: function() {
            this.container = $("#company-alerts");

            this.canvas = this.container.find(".canvas");
            this.canvasWidth = this.offset * companyAlerts_Count;
            this.canvas.css("width", this.canvasWidth);

            this.initEvents();

            this.play();
        },

        initEvents: function() {
            this.container.find(".next").click(function(e) {
                e.preventDefault();
                CompanyAlerts.next(e);
            });

            this.container.find(".back").click(function(e) {
                e.preventDefault();
                CompanyAlerts.back(e);
            });

            this.container.bind("mouseenter", function() {
                CompanyAlerts.stopPlay();
                $(this).addClass("hover");
            }).bind("mouseleave", function() {
                $(this).removeClass("hover");
                CompanyAlerts.play();
            });
        },

        next: function(e) {
            if (this.currentPage == companyAlerts_Count) {
                this.currentPage = 1;
                this.reset();
                return;
            } else {
                ++this.currentPage;
            }
            this.slide();
        },

        play: function() {
            this.playing = setTimeout(function() {
                CompanyAlerts.stopPlay();
                CompanyAlerts.next();
                CompanyAlerts.play();
            }, this.sliderDelay);
        },

        stopPlay: function() {
            if (this.playing) {
                clearTimeout(this.playing);
            }
        },

        back: function(e) {
            if (this.currentPage == 1) {
                //TODO: disable the button
                return;
            } else {
                --this.currentPage;
            }
            this.slide();
        },
        slide: function() {
            /* 
            Just to have a look how it could be animiated
            By the way "Sine" is a optimal easing.
            http://www.lemonsanver.com/jQuery/easingAnimationPlugin.html 
            */
            //this.canvas.animate({ "marginLeft": -1 * (this.currentPage - 1) * this.offset }, { queue: false, duration: 800, easing: "sineEaseIn" });
            if (isRtl) {
                this.canvas.animate({ "marginRight": -1 * (this.currentPage - 1) * this.offset }, 1000);
            } else {
                this.canvas.animate({ "marginLeft": -1 * (this.currentPage - 1) * this.offset }, 1000);
            }
        },
        reset: function() {
            if (isRtl) {
                this.canvas.css("marginRight", 0);
            } else {
                this.canvas.css("marginLeft", 0);
            }
        }
    }
    $(document).ready(function() {
        CompanyAlerts.init();
    });
})(jQuery);
