﻿function TopNews(objRoot,newsCountPerPage,viewSubtitle,Interval) {
    //Init
    //root element in DOM.module container.used in jquery selector to gain unique objects
    this._root=objRoot;
    this._currPageIndex = 0;
    this._currNewsIndex = 0;
    this._slideShowInterval = Interval;
    this.nextNewsIntervalId = 0;
    this._newsCountPerPage = newsCountPerPage;
    this._viewSubtitle = viewSubtitle;
    this.news = new Array();
    this.baseHeight = 0;
    this.fetchAllNews();
    
}

TopNews.prototype = {
    //call asp.net page method asynchronous (send and recives data in JSON format)
    PageMethod: function(fn, paramArray, successFn, errorFn) {
        var pagePath = window.location.pathname;
        var that = this;
        //Call the page method  
        $.ajax({
            type: "POST",
            url: pagePath + "?Callback=" + fn,
            contentType: "application/json; charset=utf-8",
            data: paramArray,
            dataType: "json",
            success: function(res) { successFn(res, that) },
            error: errorFn
        });
    },

    //override jquery selector to ensure selected DOM objects be unique in multiple instanse of module
    getElement: function(domElement) {
        return $(this._root).find(domElement);
    },

    initUI: function() {
        var that = this;
        this.getElement('.news_thumb tr td').hover(function() 
        {
            if ($(this).is('.active')) {
                return false;
            }
            else {
                that.getElement('.news_thumb tr td').removeClass('active');
                $(this).addClass('active');
                that._currNewsIndex = (that._currPageIndex * that._newsCountPerPage) + $(this).prevAll('td').size()
               that.setTopOne();
                
            }
       }, function() {

          return false;
          
        });

        this.getElement('.pagerbar ul li').click(function() {
            if ($(this).is('.active')) {
                return false;
            }
            else {
                that.getElement('.pagerbar ul li').removeClass('active');
                $(this).addClass('active');

                var selectedPageIndex = $(this).prevAll('li').size();
                var ht = that.getElement('.news_thumb div.active').height();

                var step = Math.abs(selectedPageIndex - that._currPageIndex);

                if (selectedPageIndex > that._currPageIndex) {
                    var upStep = Math.abs(parseInt(that.getElement('.news_thumb div:first').css('margin-top'))) + (ht * step);
                    that.getElement('.news_thumb div:first').animate({ marginTop: -upStep }, 1000, function() {
                        that.getElement('.news_thumb div').removeClass('active');
                        that.getElement('.news_thumb div:eq(' + selectedPageIndex + ')').addClass('active');

                        that._currPageIndex = selectedPageIndex;

                        that.getElement('.toolbar ul').empty();
                        for (var i = 0; i < that.getElement('.news_thumb div.active tr td').length; i++) {
                            that.getElement('.toolbar ul').append('<td>&#8226</td>');
                        }

                     
                        if (that._viewSubtitle)
                            that.getElement('.subtitlebar').cycle({ fx: 'scrollUp', random: 1, cleartype: 0, timeout: 6000 });

                        that.getElement('.news_thumb div.active tr td:first').click();
                    });
                }
                else {
                    var downStep = parseInt(that.getElement('.news_thumb div:first').css('margin-top')) + (ht * step);
                    that.getElement('.news_thumb div:first').animate({ marginTop: downStep }, 1000, function() {
                        that.getElement('.news_thumb div').removeClass('active');
                        that.getElement('.news_thumb div:eq(' + selectedPageIndex + ')').addClass('active');

                        that._currPageIndex = selectedPageIndex;

                        that.getElement('.toolbar ul').empty();
                        for (var i = 0; i < that.getElement('.news_thumb div.active tr td').length; i++) {
                            that.getElement('.toolbar ul').append('<td>&#8226</td>');
                        }
                        if (that._viewSubtitle)
                            that.getElement('.subtitlebar').cycle({ fx: 'scrollUp', random: 1, cleartype: 0, timeout: 6000 });

                        that.getElement('.news_thumb div.active tr td:first').click();
                    });
                }
            }
        }).hover(function() {
            $(this).addClass('hover');
        }, function() {
            $(this).removeClass('hover');
        });
    },

    setTopOne: function() {
        //set variables
       
        var that = this;
        var imgSrc = this.news[this._currNewsIndex].imgSrc;
        var imgThumbSrc = this.news[this._currNewsIndex].imgThumbSrc;
        var imgSrcLink = this.news[this._currNewsIndex].imgSrcLink;
        var newsTitle = this.news[this._currNewsIndex].Title;
        var newsDesc = this.news[this._currNewsIndex].Summary;
        var newsDate = this.news[this._currNewsIndex].newsDate;

        var newsDescHeight = this.getElement('.main_news').find('.block').height();
        this.getElement('.main_news .block').animate({ opacity: 0, marginBottom: -newsDescHeight }, 250, function() {
         that.getElement('.main_news .block h4').text(newsTitle);
         that.getElement('.main_news .block small').text(newsDate);
         that.getElement('.main_news .block p').text(newsDesc);

         that.getElement('.Header #HeaderLink').attr('href', imgSrcLink);
         that.getElement('#HeaderLink').text(newsTitle);
         that.getElement('.toolbar td').text(newsDesc);
         that.getElement('.main_news #imgMain').attr('src', imgSrc);
         that.getElement('.main_news #imgMainLink').attr('href', imgSrcLink);

        });

        this.getElement('.toolbar ul li').removeClass('active');
        this.getElement('.toolbar td ul li:eq(' + (this._currNewsIndex % this._newsCountPerPage) + ')').addClass('active');
    },

    BindTextNews: function() {
        for (var index = 0; index < this.news.length; index++) {
            if (index % this._newsCountPerPage == 0) {
                $('<div><table><tr></tr></table></div>').appendTo(this.getElement('.news_thumb'));
            }
           
            $('<td align=center><em></em></td>').append($('<img width=70 height=30 />').attr('src',this.news[index].imgThumbSrc)).appendTo(this.getElement('.news_thumb div:last tr'));
            this.getElement('.news_thumb tr td:last').append($('<h4></h4>').text(this.news[index].Title));
        }

        this.getElement('.news_thumb div:first').addClass('active');
        this.getElement('.news_thumb tr td:first').addClass('active');
        this.getElement('.pagerbar td:eq(1) tr td:first').addClass('active');
    },

    BindNews: function() {
        this.BindTextNews();
        this.initUI();
        this.setTopOne()
        var that = this;
      
    },

    SuccessFetchNews: function(response, that) {
        that.news = response;
        if (response.length == undefined)
            that.news = new Array(response);
        that.BindNews();
    },

    FailFetchNews: function(err) {
       alert('Error!  ' + err.responseText);
   },

 fetchAllNews: function() {
      this.PageMethod('fetchAllNews',{},this.SuccessFetchNews, this.FailFetchNews);
    }

}
 
 