(function ($) {
    function HomeController() {
        // Set default properties.
        this.name = ("homeController-" + (new Date()).getTime());
    }

    // ----------------------------------------------------------------------- //
    // initialize homepage module 
    // ----------------------------------------------------------------------- //
    HomeController.prototype.initializeHomepageModule = function () {
        $("#on_tonight_holder").show();

        homeController.setRowsVisibleToInitialState();


        $('#schedule_list_holder').cycle({
            fx: 'fade',
            speed: '200',
            timeout: 0,
            next: '#scroll_next',
            prev: '#scroll_prev',
            before: homeController.onBeforeChannelChange,
            after: homeController.onAfterChannelChange

        });

        $("#schedule_list_holder").hover(
		  function () {
		      homeController.setRowsVisibleToExpandedState();
		  },
		  function () {
		      homeController.setRowsVisibleToInitialState();
		  }
		);


		// reverse slide order so images display left to right with chiclets 
        var slideList = $(".NewInExtras li").get();
        slideList.reverse();
        $(".NewInExtras ul").empty();
        $(".NewInExtras ul").append(slideList);

        $(".NewInExtras").append('<div id="cycle_nav"></div>');

        $(".NewInExtras ul").cycle({
            pager: "#cycle_nav",
            timeout: 0,
            backwards: true,
            pagerAnchorBuilder: function (idx, slide) {
                return '<li><a href="#"></a></li>';
            }
        });
    };

    // ----------------------------------------------------------------------- //
    // before the channel changes
    // ----------------------------------------------------------------------- //
    HomeController.prototype.onBeforeChannelChange = function () {
        // remove channel logo
        var channels = ["STZ1", "STZ8", "STZ7", "STZ5", "STZ3", "STZ4"];
        $("#scroll_channel").removeClass(channels.join(" "));

        var channel_div = $(this);
        //alert(channel_div.html());
        for (var i = 0; i < channels.length; i++) {
            var channel_class = channels[i];
            if (channel_div.hasClass(channel_class)) {
                $("#scroll_channel").addClass(channel_class);
                $("#scroll_channel").hide();
                $("#scroll_channel").fadeIn();
                $(".on_tonight_text_holder h3").html("");
                if (channel_div.hasClass("etpt")) {
                    $(".on_tonight_text_holder h3").html("All times ET/PT");
                } else {
                    var time = (channel_div).attr("class").split("channel_grouping").join("").split(channel_class).join("").split(" ").join("").split("_").join(" ");
                    $(".on_tonight_text_holder h3").html(time);
                }
            }
        }

    };

    // ----------------------------------------------------------------------- //
    // set rows visible to initial state
    // ----------------------------------------------------------------------- //
    HomeController.prototype.setRowsVisibleToInitialState = function () {
        $('.channel_grouping ul li .title').ThreeDots({
            max_rows: 3,
            ellipsis_string: '...',
            allow_dangle: false,
            whole_word: true

        });


        $("#on_tonight_holder, this, .channel_grouping li").removeClass("channel_grouping_expanded");
        $("#on_tonight_holder, this, .channel_grouping li").addClass("channel_grouping_contracted");
        $(".threedots_ellipsis").show();

    };

    // ----------------------------------------------------------------------- //
    // set rows visible to expanded state
    // ----------------------------------------------------------------------- //
    HomeController.prototype.setRowsVisibleToExpandedState = function () {
        $('.channel_grouping ul li .title').ThreeDots({
            max_rows: 5,
            ellipsis_string: '...',
            allow_dangle: false,
            whole_word: true
        });

        var numberOfVisibleEllipses = $(".threedots_ellipsis:visible").length;
        if (numberOfVisibleEllipses > 0) {
            $("#on_tonight_holder, this, .channel_grouping li").removeClass("channel_grouping_contracted");
            $("#on_tonight_holder, this, .channel_grouping li").addClass("channel_grouping_expanded");
            $(".threedots_ellipsis").hide();
        }

    };

    // ----------------------------------------------------------------------- //
    // show channel 
    // ----------------------------------------------------------------------- //
    HomeController.prototype.onAfterChannelChange = function () {
        this;
        //alert("after");
    };


    // ----------------------------------------------------------------------- //
    // ----------------------------------------------------------------------- //


    // Create a new instance of the homeController and store it in the window.
    window.homeController = new HomeController();

    // Return a new homeController instance.
    return (window.homeController);

})(jQuery);



