﻿//Added by Alireza 
function TopNews(topModuleId, objRoot, viewMode, newsCountPerPage, viewSubtitle, Interval, moduleWidth) {
    //Init
    //root element in DOM.module container.used in jquery selector to gain unique objects
    this._topModuleId = topModuleId;
    this._root = objRoot;
    this._currPageIndex = 0;
    this._currNewsIndex = 0;
    this._slideShowInterval = Interval;
    this.nextNewsIntervalId = 0;
    this._viewMode = viewMode;
    this._newsCountPerPage = newsCountPerPage;
    this._viewSubtitle = viewSubtitle;
    this.news = new Array();
    this.baseHeight = 0;
    this._state = 'play';
    this._newsDescState = 'normal';
    this._pagerFlag = true;

    this._moduleWidth = moduleWidth;

    this.newsModules = new Array();

    if (this._viewMode != 'selectModules')
        this.fetchAllNews();
    else
        this.selectModules();

}

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 + "&currentMId=" + that._topModuleId + "&Callback=" + fn,
            contentType: "application/json; charset=utf-8",
            data: paramArray,
            dataType: "json",
            success: function (res) { successFn(res, that) },
            error: errorFn
        });
    },


    usesNewsModules: function (response, that) {
        response.refresh();
    },

    setNewsModules: function () {
        var str = JSON.stringify(this.newsModules)
        var pattern, s;
        pattern = new RegExp('\\[');
        s = str.replace(pattern, '{');
        pattern = new RegExp('\\]');
        s = s.replace(pattern, '}');
        this.PageMethod('setNewsModules', str, this.usesNewsModules, this.FailSetModules);
    },

    //override jquery selector to ensure selected DOM objects be unique in multiple instanse of module
    getElement: function (domElement) {
        return $(this._root).find(domElement);
    },

    ToggleDesc: function (itm) {
        var ht = this.getElement('.main_news').find('.block').height() - $(itm).parent().height();
        if ($(itm).find('div').attr('class') == 'ContractButton') {
            this.getElement('.main_news .block').animate({ marginBottom: -ht }, 250, function () { $(itm).find('div').removeClass('ContractButton').addClass('ExpandButton'); });
            this._newsDescState = 'minimized';
        }
        else {
            this.getElement('.main_news .block').animate({ marginBottom: "0" }, 250, function () { $(itm).find('div').removeClass('ExpandButton').addClass('ContractButton'); });
            this._newsDescState = 'normal';
        }
    },

    initUI: function (firstRun) {
        var that = this;
        $('.TNContainer').parent().parent().css('padding', 0);
        switch (this._viewMode) {
            case 'TextMode':
                var minHeight = this.getElement('.news_thumb > div > ul:first').height(); //this.baseHeight ? this.getElement('.news_thumb > div:first').height() : this.baseHeight;
                this.getElement('.news_thumb').css({ 'height': minHeight + 'px', 'min-height': minHeight + 'px' });
                this.getElement('.news_thumb').width(this._moduleWidth);
                this.getElement('.news_thumb ul li .block p').hide();

                this.getElement('.news_thumb ul li').click(function () {
                    $('.news_thumb ul li.active .block p').cycle('pause').hide();
                    var locale = new String();
                    locale = "fa-ir";
                    if (queryString("Lang") != "false")
                        locale = queryString("Lang")
                    else if (queryString("lang") != "false")
                        locale = queryString("lang")
                    locale = locale.toLowerCase();
                    if (locale == "fa-ir")
                        $(this).find('.block p').show().cycle({ fx: 'scrollRight', cleartype: 0, sync: 1, delay: -10000, continuous: 0, speed: 12000 }); //.hide().cycle('pause')
                    else
                        $(this).find('.block p').show().cycle({ fx: 'scrollLeft', cleartype: 0, sync: 1, delay: -10000, continuous: 0, speed: 12000 }); //.hide().cycle('pause')
                    that.getElement('.news_thumb ul li').removeClass('active');
                    $(this).addClass('active');
                    clearInterval(that.nextNewsIntervalId);
                    that.nextNewsIntervalId = setInterval(function () { that.nextNews(that); }, that._slideShowInterval);
                    that._currNewsIndex = (that._currPageIndex * that._newsCountPerPage) + $(this).prevAll('li').size();
                }).hover(function () {
                    $(this).addClass('hover');
                }, function () {
                    $(this).removeClass('hover');
                });

                this.getElement('.pagerbar ul li').click(function () {
                    if (!that._pagerFlag)
                        return;
                    if ($(this).is('.active')) {
                        return false;
                    }
                    else {
                        that._pagerFlag = false;
                        that.getElement('.pagerbar ul li').removeClass('active');
                        $(this).addClass('active');

                        var selectedPageIndex = $(this).prevAll('li').size();
                        var ht = that.getElement('.news_thumb').height();

                        if (selectedPageIndex > that._currPageIndex) {
                            var upStep = Math.abs(parseInt(that.getElement('.news_thumb > div:first').css('margin-top'))) + ht;
                            that.getElement('.news_thumb > div:first').animate({ marginTop: -upStep }, 1000, function () {
                                that._currPageIndex = selectedPageIndex;
                                that.BindTextNews();

                                that._currNewsIndex = 0;

                                that.initUI('false');

                                that._pagerFlag = true;
                            });
                        }
                        else {
                            var downStep = parseInt(that.getElement('.news_thumb > div:first').css('margin-top')) + ht;
                            that.getElement('.news_thumb > div:first').animate({ marginTop: downStep }, 1000, function () {
                                that._currPageIndex = selectedPageIndex;
                                that.BindTextNews();

                                that._currNewsIndex = 0;

                                that.initUI('false');

                                that._pagerFlag = true;
                            });
                        }
                    }
                }).hover(function () {
                    $(this).addClass('hover');
                }, function () {
                    $(this).removeClass('hover');
                });

                break;
            case 'PictureMode':

                this.getElement('.main_news').width(this._moduleWidth);
                this.getElement('.main_news').height(this.getElement('.main_news').width() / 1.6);

                this.getElement('.main_news').siblings().width(this.getElement('.main_news').width());
                if (this.getElement('.main_news .logo').text() == '')
                    this.getElement('.main_news .logo').animate({ opacity: 0 }, 1);
                else
                    this.getElement('.main_news .logo').animate({ opacity: 0.60 }, 1);

                that.getElement('.toolbar ul').empty();
                for (var i = 0; i < this.news.length; i++)
                    this.getElement('.toolbar ul').append('<li>&nbsp;</li>');

                //add handlers
                if (firstRun == 'true') {
                    this.getElement('#toggleDescIcon').click(function () { that.ToggleDesc(this); });
                    this.getElement('.toolbar #btnPrev').click(function () { that.prevNews(); });
                    this.getElement('.toolbar #btnNext').click(function () { that.nextNews(that); });
                    this.getElement('.toolbar #btnPausePlay').click(function () { that.PausePlay(this); });
                }

                break;
            case 'FullMode':
                this.baseHeight = (this.getElement('.news_thumb div:first ul').height() / this.getElement('.news_thumb div:first ul li').length) * 7;
                
                var minHeight = this.getElement('.news_thumb div:first').height() > this.baseHeight ? this.getElement('.news_thumb div:first').height() : this.baseHeight;
                this.getElement('.news_thumb').css({ 'height': minHeight + 'px', 'min-height': minHeight + 'px' });
                //Mojahedi: 2012-02-06 Comment Out
                //this.getElement('.main_news').height(this.getElement('.news_thumb').height() - 20);
                //this.getElement('.main_news').width(this.getElement('.main_news').height() * 1.6);
                //Mojahedi: 2012-02-06 Comment Out
                //Mojahedi: 2012-02-06 Add                
                this.getElement('.main_news').width(this._moduleWidth);
                //Mojahedi: 2012-02-06 Add

                this.getElement('.main_news').siblings().width(this.getElement('.main_news').width());

                if (this.getElement('.main_news .logo').text() == '')
                    this.getElement('.main_news .logo').animate({ opacity: 0 }, 1);
                else
                    this.getElement('.main_news .logo').animate({ opacity: 0.60 }, 1);

                that.getElement('.toolbar ul').empty();
                for (var i = 0; i < this.getElement('.news_thumb div.active ul li').length; i++)
                    this.getElement('.toolbar ul').append('<li>&nbsp;</li>');

                if (!this._viewSubtitle) {
                    this.getElement('.subtitlebar').parents('tr:first').hide();
                }

                //add handlers
                if (firstRun == 'true') {
                    this.getElement('#toggleDescIcon').click(function () { that.ToggleDesc(this); });
                    this.getElement('.toolbar #btnPrev').click(function () { that.prevNews(); });
                    this.getElement('.toolbar #btnNext').click(function () { that.nextNews(that); });
                    this.getElement('.toolbar #btnPausePlay').click(function () { that.PausePlay(this); });
                }

                this.getElement('.news_thumb ul li').click(function () {
                    if ($(this).is('.active')) {
                        return false;
                    }
                    else {
                        that.getElement('.news_thumb ul li').removeClass('active');
                        $(this).addClass('active');
                        that._currNewsIndex = (that._currPageIndex * that._newsCountPerPage) + $(this).prevAll('li').size();
                        if (that._state == 'play') {
                            clearInterval(that.nextNewsIntervalId);
                            that.nextNewsIntervalId = setInterval(function () { that.nextNews(that); }, that._slideShowInterval);
                        }
                        that.setTopOne();
                    }
                    return false;
                }).hover(function () {
                    $(this).addClass('hover');
                }, function () {
                    $(this).removeClass('hover');
                });

                this.getElement('.pagerbar ul li').click(function () {
                    if (!that._pagerFlag)
                        return;
                    if ($(this).is('.active')) {
                        return false;
                    }
                    else {
                        that._pagerFlag = false;
                        that.getElement('.pagerbar ul li').removeClass('active');
                        $(this).addClass('active');

                        var selectedPageIndex = $(this).prevAll('li').size();

                        var ht = that.getElement('.news_thumb').height();

                        if (selectedPageIndex > that._currPageIndex) {
                            var upStep = Math.abs(parseInt(that.getElement('.news_thumb > div:first').css('margin-top'))) + ht;
                            that.getElement('.news_thumb > div:first').animate({ marginTop: -upStep }, 1000, function () {
                                that._currPageIndex = selectedPageIndex;
                                that.BindTextNews();

                                that.setSubtitle();
                                that.initUI('false');

                                that.getElement('.news_thumb ul li:first').removeClass('active');
                                that.getElement('.news_thumb ul li:first').click();

                                that._pagerFlag = true;
                            });
                        }
                        else {
                            var downStep = parseInt(that.getElement('.news_thumb > div:first').css('margin-top')) + ht;
                            that.getElement('.news_thumb > div:first').animate({ marginTop: downStep }, 1000, function () {
                                that._currPageIndex = selectedPageIndex;
                                that.BindTextNews();

                                that.setSubtitle();
                                that.initUI('false');

                                that.getElement('.news_thumb ul li:first').removeClass('active');
                                that.getElement('.news_thumb ul li:first').click();

                                that._pagerFlag = true;
                            });
                        }
                    }
                }).hover(function () {
                    $(this).addClass('hover');
                }, function () {
                    $(this).removeClass('hover');
                });

                break;
        }
    },

    setTopOne: function () {
        //set variables
        var that = this;
        var imgSrc = this.news[this._currNewsIndex].imgSrc;
        var newsTitle = this.news[this._currNewsIndex].Title;

        //Mojahedi: 2011-07-18 Add
        var newsSubTitle = this.news[this._currNewsIndex].SubTitle;
        //Mojahedi: 2011-07-18 Add

        //Mojahedi: 2011-07-18 Add
        var newsPreTitle = this.news[this._currNewsIndex].PreTitle;
        //Mojahedi: 2011-07-18 Add

        //Mojahedi: 2012-02-06 Add
        var picWidth = this.news[this._currNewsIndex].picWidth;
        //var picHeight = this.news[this._currNewsIndex].picHeight;
        //Mojahedi: 2012-02-06 Add

        var newsDesc = this.news[this._currNewsIndex].Summary;
        var newsDate = this.news[this._currNewsIndex].newsDate;
        var newsUrl = this.news[this._currNewsIndex].newsUrl;
        var newsDescHeight = this.getElement('.main_news').find('.block').height();
        var animateFunc = { opacity: 0, marginBottom: -newsDescHeight };
        if (that._newsDescState != 'normal') {
            animateFunc = { opacity: 0.60 };
        }

        this.getElement('.main_news .desc').show();

        this.getElement('.main_news .block').animate(animateFunc, 250, function () {
            that.getElement('.main_news .block a').attr('href', newsUrl);
            that.getElement('.main_news .block h4').text(newsTitle);
            //Mojahedi: 2011-07-18 Add
            that.getElement('.main_news .block span.SubTitle').text(newsSubTitle);
            //Mojahedi: 2011-07-18 Add

            //Mojahedi: 2011-08-20 Add
            that.getElement('.main_news .block span.PreTitle').text(newsPreTitle);
            //Mojahedi: 2011-08-20 Add

            that.getElement('.main_news .block small').text(newsDate);
            that.getElement('.main_news .block p').text(newsDesc);
            if (that._newsDescState == 'normal')
                that.getElement('.main_news .block').animate({ opacity: 0.60, marginBottom: 0 }, 250, function () { that.getElement('.main_news .block #toggleDescIcon').html('<div class="ContractButton">&nbsp;</div>'); });
            else
                that.getElement('.main_news .block').animate({ opacity: 0.60, marginBottom: -1 * (that.getElement('.main_news .block').height() - that.getElement('.main_news .block h4').parent().height()) }, 0, function () { });
            //Mojahedi: 2012-02-06 Comment Out
            //            that.getElement('.main_news #imgMain').attr('src', imgSrc);
            //            that.getElement('.main_news #imgMain').hide(1, function () {

            //                if ($(this).height() > $(this).width())
            //                    $(this).css({ 'height': that.getElement('.main_news').height() });
            //                else
            //                    $(this).css({ 'width': that.getElement('.main_news').width() });
            //                $(this).show(1, function () { $(this).css('display', 'inline'); });
            //            });
            //Mojahedi: 2012-02-06 Comment Out
            //Mojahedi: 2012-02-06 Add
            that.getElement('.main_news #imgMain').attr('src', imgSrc);
            that.getElement('.main_news #imgMain').hide(1, function () {
                $(this).css({ 'height': $('div.news_thumb').height(),
                    'width': picWidth
                });
                $(this).show(1, function () { $(this).css('display', 'inline'); });
            });
            //Mojahedi: 2012-02-06 Add
        });

        this.getElement('.toolbar ul li').removeClass('active');
        switch (that._viewMode) {
            case 'FullMode':
                this.getElement('.toolbar td ul li:eq(' + (this._currNewsIndex % this._newsCountPerPage) + ')').addClass('active');
                break;
            case 'PictureMode':
                this.getElement('.toolbar td ul li:eq(' + this._currNewsIndex + ')').addClass('active');
                break;
        }
    },

    BindPager: function () {
        this.getElement('.pagerbar td:eq(1) ul').empty();
        for (var index = 0; index < this.news.length; index++) {
            if (index % this._newsCountPerPage == 0) {
                this.getElement('.pagerbar td:eq(1) ul').append($('<li></li>').text(Math.floor(index / this._newsCountPerPage) + 1));
            }
        }
    },

    BindTextNews: function () {
        this.getElement('.news_thumb').empty();

        for (var index = (this._currPageIndex * this._newsCountPerPage); index < (this._currPageIndex + 1) * this._newsCountPerPage; index++) {
            if (index >= this.news.length) break;
            if (index % this._newsCountPerPage == 0) {
                $('<div><ul></ul></div>').appendTo(this.getElement('.news_thumb'));
            }
            switch (this._viewMode) {
                case 'FullMode': //Mojahedi: 2011-07-19 Add .append($('<span class=\'SubTitle\'></span>').text(this.news[index].SubTitle) 08-20 Add .append($('<span class=\'PreTitle\'></span>').text(this.news[index].PreTitle)
                    $('<li></li>').append($('<span class=\'PreTitle\'></span>').text(this.news[index].PreTitle)).append($('<h4></h4>').text(this.news[index].Title)).append($('<span class=\'SubTitle\'></span>').text(this.news[index].SubTitle)).appendTo(this.getElement('.news_thumb div:last ul'));
                    break;
                case 'TextMode':
                    //$('<li></li>').append($('<div></div>').addClass('picndate').append($('<a></a>').attr('href', this.news[index].newsUrl).append($('<img />').attr('alt', ' ').attr('src', this.news[index].imgThumbSrc))).append($('<div></div>').text(this.news[index].newsDate))).append($('<div></div>').addClass('block').append($('<h4></h4>').text(this.news[index].Title)).append($('<p></p>').append($('<span></span>').text(this.news[index].Summary)).append('<span></span>'))).appendTo(this.getElement('.news_thumb ul:last'));
                    $('<li></li>').append(
                        $($('<table></table>').attr('cellpadding', '0').attr('cellspacing', '0').attr('width', '100%').append($('<tr></tr>').append(
                            $('<td></td>').attr('width', '1%')
                                .append($('<a></a>').attr('target', '_blank').attr('href', this.news[index].newsUrl)
                    //Mojahedi: 2011-11-22 Comment Out
                    //.append($('<img />').attr('alt', ' ').attr('src', this.news[index].imgThumbSrc))))
                    //Mojahedi: 2011-11-22 Comment Out
                    //Mojahedi: 2011-11-22 Comment Out
                                    .append($('<img style=\'display:' + this.news[index].Display + ';\' />').attr('alt', ' ').attr('src', this.news[index].imgThumbSrc))))
                    //Mojahedi: 2011-11-22 Comment Out
                                        .append($('<td></td>').attr('width', '100%')
                                            .append($('<div style=\'width:100%;\'></div>').addClass('block')
                                                .append($('<span class=\'PreTitle\'></span>').text(this.news[index].PreTitle))
                                                .append($('<h4></h4>').text(this.news[index].Title))
                    //Mojahedi: 2011-07-18 Add .append($('<span class=\'SubTitle\'></span>').text(this.news[index].SubTitle)) 08-20 Add .append($('<span class=\'PreTitle\'></span>').text(this.news[index].PreTitle))
                                                    .append($('<span class=\'SubTitle\'></span>').text(this.news[index].SubTitle))
                                                        .append($('<div></div>').text(this.news[index].newsDate))
                                                            .append($('<p></p>')
                                                                .append($('<span></span>').text(this.news[index].Summary))
                                                                    .append('<span>&nbsp;</span>')
                                        )
                                    )
                                )
                            )
                        )
                    ).appendTo(this.getElement('.news_thumb ul:last'));
                    break;
            }
        }

        this.getElement('.news_thumb > div:first').addClass('active');
        this.getElement('.news_thumb ul li:first').addClass('active');

        this.getElement('.pagerbar td:eq(' + (this._currPageIndex + 1) + ') ul li:first').addClass('active');
    },

    setSubtitle: function () {
        if (this._viewSubtitle) {
            this.getElement('.subtitlebar').parents('tr:first').show();
            this.getElement('.subtitlebar').empty();

            var startIndex = (this._currPageIndex + 1) * this._newsCountPerPage;
            for (var index = startIndex; index < this.news.length; index++) {
                this.getElement('.subtitlebar')
                        .append($('<span></span>').text(this.news[index].Title))
            }

            //if (this.getElement('.subtitlebar span').length == 1)
            //    this.getElement('.subtitlebar').append('<span>&nbsp;</span>');

            this.getElement('.subtitlebar').cycle({ fx: 'scrollUp', cleartype: 0, timeout: 6000 });
        }
    },

    nextNews: function (that) {
        switch (that._viewMode) {
            case 'TextMode':
            case 'FullMode':
                that.getElement('.news_thumb div.active ul li:eq(' + (((that._currNewsIndex + 1) % that._newsCountPerPage) % that.getElement('.news_thumb div.active ul li').length) + ')').click();
                break;
            case 'PictureMode':
                that._currNewsIndex = that._currNewsIndex + 1;
                if (that._currNewsIndex >= that.news.length)
                    that._currNewsIndex = 0;
                if (that._state == 'play') {
                    clearInterval(that.nextNewsIntervalId);
                    that.nextNewsIntervalId = setInterval(function () { that.nextNews(that); }, that._slideShowInterval);
                }
                that.setTopOne();
                break;
        }
    },

    prevNews: function () {
        switch (this._viewMode) {
            case 'TextMode':
            case 'FullMode':
                if (this._currNewsIndex % this._newsCountPerPage == 0)
                    this.getElement('.news_thumb div.active ul li:eq(' + (this.getElement('.news_thumb div.active ul li').length - 1) + ')').click();
                else
                    this.getElement('.news_thumb div.active ul li:eq(' + ((this._currNewsIndex - 1) % this._newsCountPerPage) % this.getElement('.news_thumb div.active ul li').length + ')').click();
                break;
            case 'PictureMode':
                this._currNewsIndex = this._currNewsIndex - 1;
                if (this._currNewsIndex <= 0)
                    this._currNewsIndex = this.news.length - 1;
                if (this._state == 'play') {
                    clearInterval(this.nextNewsIntervalId);
                    this.nextNewsIntervalId = setInterval(function () { this.prevNews(); }, this._slideShowInterval);
                }
                this.setTopOne();
                break;
        }
    },

    PausePlay: function (itm) {
        var str = new String($(itm).attr('src'));
        var pattern, s;
        if (str.toLowerCase().search('pause.png') != -1) {
            pattern = new RegExp('(Pause.png)', 'gi');
            s = $(itm).attr('src').replace(pattern, 'play.png');
            $(itm).attr('src', s);
            clearInterval(this.nextNewsIntervalId);
            this._state = 'pause';
        }
        else {
            pattern = new RegExp('(play.png)', 'gi');
            s = $(itm).attr('src').replace(pattern, 'Pause.png');
            $(itm).attr('src', s);
            var that = this;
            this.nextNewsIntervalId = setInterval(function () { that.nextNews(that); }, this._slideShowInterval);
            this._state = 'play';
        }
    },

    BindNews: function () {
        var that = this;
        switch (this._viewMode) {
            case 'TextMode':
                this.getElement('.news_thumb').css('margin-left', 'auto');
                this.BindPager();
                this.BindTextNews();
                this.initUI('true');

                this.getElement('.news_thumb ul li:first').removeClass('active');
                this.getElement('.news_thumb ul li:first').click();

                break;
            case 'PictureMode':
                this.initUI('true');
                this.setTopOne();

                this.nextNewsIntervalId = setInterval(function () { that.nextNews(that); }, this._slideShowInterval);

                break;
            case 'FullMode':
                this.BindPager();
                this.BindTextNews();
                this.setSubtitle();
                this.initUI('true');

                this.getElement('.news_thumb ul li:first').removeClass('active');
                this.getElement('.news_thumb ul li:first').click();

                break;
        }
    },

    SuccessFetchNews: function (response, that) {
        if (response.noitem != undefined)
            $(that._root).empty().html(response.noitem);
        $(that._root).find('table:first').show();
        that.news = response;
        if (response.length == undefined)
            that.news = new Array(response);
        that.BindNews();
    },

    FailFetchNews: function (err) {
        //Removed by Alireza
        //alert('Error on Fetch news! ');
        //End Removed by Alireza
    },

    fetchAllNews: function () {
        this.PageMethod('fetchAllNews', '{}', this.SuccessFetchNews, this.FailFetchNews);
    },


    SuccessselectModules: function (response, that) {
        that.newsModules = response;
        if (response.length == undefined)
            that.newsModules = new Array(response);

        var query = new jsinq.Query('\
             from portal in $0 \
             select portal \
            ');
        query.setValue(0, new jsinq.Enumerable(response));
        var result = query.execute();


        var enumerator = result.groupBy(function (a) { return a.portalID }).getEnumerator();
        that.getElement('#portals').empty();
        while (enumerator.moveNext()) {
            that.getElement('#portals').append($('<option></option>').text(enumerator.current().first().portalAlias).val(enumerator.current().first().portalID));
        }

        enumerator = result.where(function (a) { return a.selected == true; }).getEnumerator();
        that.getElement('#SelectedNews').empty();
        while (enumerator.moveNext()) {
            that.getElement('#SelectedNews').append($('<option></option>').text(enumerator.current().moduleTitle).val(enumerator.current().moduleID));
        }

        var bindModules = function (index) {
            that.getElement('#portals option[value="' + index + '"]').attr('selected', true);
            that.getElement('#nominatedNews').empty();
            enumerator = result.where(function (a) { return a.portalID == index; }).getEnumerator();
            while (enumerator.moveNext()) {
                if (!enumerator.current().selected)
                    that.getElement('#nominatedNews').append($('<option></option>').text(enumerator.current().moduleTitle).val(enumerator.current().moduleID));
            }
        };

        bindModules(0);

        that.getElement('#portals').unbind('change');
        that.getElement('#portals').change(function (event) {
            bindModules(event.target.value);
        });

        that.getElement('#modSelect').unbind('click');
        that.getElement('#modSelect').click(function () {
            that.getElement('#nominatedNews option').each(function (i, item) {
                if ($(item).attr('selected') == true) {
                    result.where(function (a) { return a.moduleID == $(item).attr('value') }).first().selected = true;
                    that.getElement('#SelectedNews').append($(item));
                }
            });
        });

        that.getElement('#modUnselect').unbind('click');
        that.getElement('#modUnselect').click(function () {
            that.getElement('#SelectedNews option').each(function (i, item) {
                if ($(item).attr('selected') == true) {
                    enumerator = result.where(function (a) { return a.moduleID == $(item).attr('value') });
                    enumerator.first().selected = false;
                    $(item).remove();
                    bindModules(enumerator.first().portalID);
                }
            });
        });
    },

    FailselectModules: function (err) {
        //Removed by Alireza
        //alert('Error on Fetch newsModules! ');
        //End Removed by Alireza
    },

    FailSetModules: function (err) {
        //Removed by Alireza
        //alert('Error on Storing newsModules! ');
        //End Removed by Alireza
    },

    selectModules: function () {
        this.PageMethod('selectModules', '{}', this.SuccessselectModules, this.FailselectModules);
    },

    pausecomp: function (millis) {
        var date = new Date();
        var curDate = null;

        do { curDate = new Date(); }
        while (curDate - date < millis);
    }
}
//End Added by Alireza

//Removed by Alireza
////nox
//function TopNews(topModuleId,objRoot,viewMode,newsCountPerPage,viewSubtitle,Interval,moduleWidth) {
//    //Init
//    //root element in DOM.module container.used in jquery selector to gain unique objects
//    this._topModuleId = topModuleId;
//    this._root=objRoot;
//    this._currPageIndex = 0;
//    this._currNewsIndex = 0;
//    this._slideShowInterval = Interval;
//    this.nextNewsIntervalId = 0;
//    this._viewMode = viewMode;
//    this._newsCountPerPage = newsCountPerPage;
//    this._viewSubtitle = viewSubtitle;
//    this.news = new Array();
//    this.baseHeight = 0;
//    this._state = 'play';
//    this._newsDescState = 'normal';
//    this._pagerFlag = true;

//    this._moduleWidth = moduleWidth;
//    
//    //this._maxNewsTitleLength = 100;  .  
//        
//    this.newsModules = new Array();
//    //this.selectModuleTitle=newsCountPerPage;
//    if(this._viewMode!='selectModules')
//        this.fetchAllNews();
//    else
//        this.selectModules();
//    
//}

//TopNews.prototype = {
//    //call asp.net page method asynchronous (send and recives data in JSON format)
//    PageMethod: function (fn, paramArray, successFn, errorFn) {
//        //alert(fn);
//        var pagePath = window.location; //.pathname;
//        var that = this;
//        //Call the page method  
//        $.ajax({
//            type: "POST",
//            url: pagePath + "&currentMId=" + that._topModuleId + "&Callback=" + fn,
//            contentType: "application/json; charset=utf-8",
//            data: paramArray,
//            dataType: "json",
//            success: function (res) { successFn(res, that) },
//            error: errorFn
//        });
//    },


//    usesNewsModules: function (response, that) {
//        response.refresh();
//    },

//    setNewsModules: function () {
//        var str = JSON.stringify(this.newsModules)
//        var pattern, s;
//        pattern = new RegExp('\\[');
//        s = str.replace(pattern, '{');
//        pattern = new RegExp('\\]');
//        s = s.replace(pattern, '}');
//        //alert(str);
//        this.PageMethod('setNewsModules', str, this.usesNewsModules, this.FailSetModules);
//    },

//    //override jquery selector to ensure selected DOM objects be unique in multiple instanse of module
//    getElement: function (domElement) {
//        return $(this._root).find(domElement);
//    },

//    ToggleDesc: function (itm) {
//        var ht = this.getElement('.main_news').find('.block').height() - $(itm).parent().height();
//        if ($(itm).find('div').attr('class') == 'ContractButton') {
//            this.getElement('.main_news .block').animate({ marginBottom: -ht }, 250, function () { $(itm).find('div').removeClass('ContractButton').addClass('ExpandButton'); });
//            this._newsDescState = 'minimized';
//        }
//        else {
//            this.getElement('.main_news .block').animate({ marginBottom: "0" }, 250, function () { $(itm).find('div').removeClass('ExpandButton').addClass('ContractButton'); });
//            this._newsDescState = 'normal';
//        }
//    },

//    initUI: function () {
//        var that = this;
//        $('.TNContainer').parent().parent().css('padding', 0);
//        switch (this._viewMode) {
//            case 'TextMode':
//                //this.baseHeight = (this.getElement('.news_thumb > div:first').height() / this.getElement('.news_thumb > div:first ul li').length) * 7
//                var minHeight = this.getElement('.news_thumb > div > ul:first').height(); // > this.baseHeight ? this.getElement('.news_thumb > div:first').height() : this.baseHeight;
//                this.getElement('.news_thumb').css({ 'height': minHeight + 'px', 'min-height': minHeight + 'px' });
//                //this.getElement('.news_thumb div ul').css({ 'height': minHeight + 'px', 'min-height': minHeight + 'px' });
//                this.getElement('.news_thumb').width(this._moduleWidth);
//                this.getElement('.news_thumb ul li .block p').hide();

//                //this.nextNewsIntervalId = setInterval(function() { that.nextNews(that); }, this._slideShowInterval);
//                this.getElement('.news_thumb ul li').click(function () {
//                    //                    if ($(this).is('.active')) {
//                    //                        return false;
//                    //                    }
//                    //                    else {
//                    $('.news_thumb ul li.active .block p').cycle('pause').hide();
//                    var locale = new String();
//                    locale = "fa-ir";
//                    if (queryString("Lang") != "false")
//                        locale = queryString("Lang")
//                    else if (queryString("lang") != "false")
//                        locale = queryString("lang")
//                    locale = locale.toLowerCase();
//                    if (locale == "fa-ir")
//                        $(this).find('.block p').show().cycle({ fx: 'scrollRight', cleartype: 0, sync: 1, delay: -10000, continuous: 0, speed: 8000 }); //.hide().cycle('pause')
//                    else
//                        $(this).find('.block p').show().cycle({ fx: 'scrollLeft', cleartype: 0, sync: 1, delay: -10000, continuous: 0, speed: 8000 }); //.hide().cycle('pause')
//                    that.getElement('.news_thumb ul li').removeClass('active');
//                    $(this).addClass('active');
//                    clearInterval(that.nextNewsIntervalId);
//                    that.nextNewsIntervalId = setInterval(function () { that.nextNews(that); }, that._slideShowInterval);
//                    that._currNewsIndex = (that._currPageIndex * that._newsCountPerPage) + $(this).prevAll('li').size();
//                    //                        if (that._state == 'play') {
//                    //                            clearInterval(that.nextNewsIntervalId);
//                    //                            that.nextNewsIntervalId = setInterval(function() { that.nextNews(that); }, that._slideShowInterval);
//                    //                        }
//                    //                        that.setTopOne();
//                    //}
//                    //return false;
//                }).hover(function () {
//                    $(this).addClass('hover');
//                }, function () {
//                    $(this).removeClass('hover');
//                });

//                this.getElement('.pagerbar ul li').click(function () {
//                    if (!that._pagerFlag)
//                        return;
//                    if ($(this).is('.active')) {
//                        return false;
//                    }
//                    else {
//                        that._pagerFlag = false;
//                        that.getElement('.pagerbar ul li').removeClass('active');
//                        $(this).addClass('active');

//                        var selectedPageIndex = $(this).prevAll('li').size();
//                        var ht = that.getElement('.news_thumb').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('.news_thumb > div.active ul li:first').click();
//                                that._pagerFlag = true;
//                            });
//                        }
//                        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('.news_thumb > div.active ul li:first').click();
//                                that._pagerFlag = true;
//                            });
//                        }
//                    }
//                }).hover(function () {
//                    $(this).addClass('hover');
//                }, function () {
//                    $(this).removeClass('hover');
//                });
//                break;
//            case 'PictureMode':
//                this.getElement('.main_news').width(this._moduleWidth);
//                this.getElement('.main_news').height(this.getElement('.main_news').width() / 1.6);
//                this.getElement('.main_news').siblings().width(this.getElement('.main_news').width());
//                if (this.getElement('.main_news .logo').text() == '')
//                    this.getElement('.main_news .logo').animate({ opacity: 0 }, 1);
//                else
//                    this.getElement('.main_news .logo').animate({ opacity: 0.60 }, 1);

//                that.getElement('.toolbar ul').empty();
//                for (var i = 0; i < this.news.length; i++)
//                    this.getElement('.toolbar ul').append('<li>&nbsp;</li>');

//                //add handlers
//                this.getElement('#toggleDescIcon').click(function () { that.ToggleDesc(this); });
//                this.getElement('.toolbar #btnPrev').click(function () { that.prevNews(); });
//                this.getElement('.toolbar #btnNext').click(function () { that.nextNews(that); });
//                this.getElement('.toolbar #btnPausePlay').click(function () { that.PausePlay(this); });

//                break;
//            case 'FullMode':
//                this.baseHeight = (this.getElement('.news_thumb div:first ul').height() / this.getElement('.news_thumb div:first ul li').length) * 7
//                var minHeight = this.getElement('.news_thumb div:first').height() > this.baseHeight ? this.getElement('.news_thumb div:first').height() : this.baseHeight;
//                this.getElement('.news_thumb').css({ 'height': minHeight + 'px', 'min-height': minHeight + 'px' });
//                //this.getElement('.news_thumb div').css({ 'height': minHeight + 'px', 'min-height': minHeight + 'px' });

//                this.getElement('.main_news').height(this.getElement('.news_thumb').height() - 20);
//                this.getElement('.main_news').width(this.getElement('.main_news').height() * 1.6);
//                this.getElement('.main_news').siblings().width(this.getElement('.main_news').width());

//                if (this.getElement('.main_news .logo').text() == '')
//                    this.getElement('.main_news .logo').animate({ opacity: 0 }, 1);
//                else
//                    this.getElement('.main_news .logo').animate({ opacity: 0.60 }, 1);

//                that.getElement('.toolbar ul').empty();
//                for (var i = 0; i < this.getElement('.news_thumb div.active ul li').length; i++)
//                    this.getElement('.toolbar ul').append('<li>&nbsp;</li>');

//                //this.getElement('.toolbar td:eq(0)').width(this.getElement('.toolbar').width() - this.getElement('.toolbar td:eq(1)').width());

//                if (!this._viewSubtitle) {
//                    this.getElement('.subtitlebar').parents('tr:first').hide();
//                }

//                //add handlers
//                this.getElement('#toggleDescIcon').click(function () { that.ToggleDesc(this); });
//                this.getElement('.toolbar #btnPrev').click(function () { that.prevNews(); });
//                this.getElement('.toolbar #btnNext').click(function () { that.nextNews(that); });
//                this.getElement('.toolbar #btnPausePlay').click(function () { that.PausePlay(this); });
//                this.getElement('.news_thumb ul li').click(function () {
//                    if ($(this).is('.active')) {
//                        return false;
//                    }
//                    else {
//                        that.getElement('.news_thumb ul li').removeClass('active');
//                        $(this).addClass('active');
//                        that._currNewsIndex = (that._currPageIndex * that._newsCountPerPage) + $(this).prevAll('li').size();
//                        if (that._state == 'play') {
//                            clearInterval(that.nextNewsIntervalId);
//                            that.nextNewsIntervalId = setInterval(function () { that.nextNews(that); }, that._slideShowInterval);
//                        }
//                        that.setTopOne();
//                    }
//                    return false;
//                }).hover(function () {
//                    $(this).addClass('hover');
//                }, function () {
//                    $(this).removeClass('hover');
//                });

//                this.getElement('.pagerbar ul li').click(function () {
//                    if (!that._pagerFlag)
//                        return;
//                    if ($(this).is('.active')) {
//                        return false;
//                    }
//                    else {
//                        that._pagerFlag = false;
//                        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 ht = that.getElement('.news_thumb').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 ul li').length; i++) {
//                                    that.getElement('.toolbar ul').append('<li>&nbsp;</li>');
//                                }

//                                that.setSubtitle();
//                                if (that._viewSubtitle) {
//                                    // that.getElement('.subtitlebar').width(that._maxNewsTitleLength);
//                                    //if (this._viewMode == 'FullMode')
//                                    that.getElement('.subtitlebar').cycle({ fx: 'scrollUp', random: 1, cleartype: 0, timeout: 6000 });
//                                }

//                                that.getElement('.news_thumb div.active ul li:first').click();
//                                that._pagerFlag = true;
//                            });
//                        }
//                        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 ul li').length; i++) {
//                                    that.getElement('.toolbar ul').append('<li>&nbsp;</li>');
//                                }

//                                that.setSubtitle();
//                                if (that._viewSubtitle) {
//                                    //that.getElement('.subtitlebar').width(that._maxNewsTitleLength);
//                                    //if (this._viewMode == 'FullMode')
//                                    that.getElement('.subtitlebar').cycle({ fx: 'scrollUp', random: 1, cleartype: 0, timeout: 6000 });
//                                }

//                                that.getElement('.news_thumb div.active ul li:first').click();
//                                that._pagerFlag = true;
//                            });
//                        }
//                    }
//                }).hover(function () {
//                    $(this).addClass('hover');
//                }, function () {
//                    $(this).removeClass('hover');
//                });

//                //                if (this._viewMode == 'PictureMode') {
//                //                    $(that._root).find('.trfooter').empty();
//                //                    $(that._root).find('.trcontent td:eq(2)').hide();
//                //                }
//                break;
//        }
//    },

//    setTopOne: function () {
//        //set variables
//        var that = this;
//        var imgSrc = this.news[this._currNewsIndex].imgSrc;
//        var newsTitle = this.news[this._currNewsIndex].Title;

//        //Mojahedi: 2011-07-18 Add
//        var newsSubTitle = this.news[this._currNewsIndex].SubTitle;
//        //Mojahedi: 2011-07-18 Add

//        var newsDesc = this.news[this._currNewsIndex].Summary;
//        var newsDate = this.news[this._currNewsIndex].newsDate;
//        var newsUrl = this.news[this._currNewsIndex].newsUrl;
//        var newsDescHeight = this.getElement('.main_news').find('.block').height();
//        var animateFunc = { opacity: 0, marginBottom: -newsDescHeight };
//        if (that._newsDescState != 'normal') {
//            animateFunc = { opacity: 0.60 };
//        }

//        this.getElement('.main_news .desc').show();

//        this.getElement('.main_news .block').animate(animateFunc, 250, function () {
//            that.getElement('.main_news .block a').attr('href', newsUrl);
//            that.getElement('.main_news .block h4').text(newsTitle);
//            //Mojahedi: 2011-07-18 Add
//            that.getElement('.main_news .block span.SubTitle').text(newsSubTitle);
//            //Mojahedi: 2011-07-18 Add
//            that.getElement('.main_news .block small').text(newsDate);
//            that.getElement('.main_news .block p').text(newsDesc);
//            if (that._newsDescState == 'normal')
//                that.getElement('.main_news .block').animate({ opacity: 0.60, marginBottom: 0 }, 250, function () { that.getElement('.main_news .block #toggleDescIcon').html('<div class="ContractButton">&nbsp;</div>'); });
//            else
//                that.getElement('.main_news .block').animate({ opacity: 0.60, marginBottom: -1 * (that.getElement('.main_news .block').height() - that.getElement('.main_news .block h4').parent().height()) }, 0, function () { });

//            that.getElement('.main_news #imgMain').attr('src', imgSrc);
//            that.getElement('.main_news #imgMain').hide(1, function () {
//                if ($(this).height() > $(this).width())
//                    $(this).css({ 'height': that.getElement('.main_news').height() });
//                else
//                    $(this).css({ 'width': that.getElement('.main_news').width() });
//                $(this).show(1, function () { $(this).css('display', 'inline'); });
//            });
//        });

//        this.getElement('.toolbar ul li').removeClass('active');
//        switch (that._viewMode) {
//            case 'FullMode':
//                this.getElement('.toolbar td ul li:eq(' + (this._currNewsIndex % this._newsCountPerPage) + ')').addClass('active');
//                break;
//            case 'PictureMode':
//                this.getElement('.toolbar td ul li:eq(' + this._currNewsIndex + ')').addClass('active');
//                break;
//        }
//    },

//    BindTextNews: function () {
//        this.getElement('.news_thumb').empty();
//        this.getElement('.pagerbar td:eq(1) ul').empty();
//        for (var index = 0; index < this.news.length; index++) {
//            if (index % this._newsCountPerPage == 0) {
//                $('<div><ul></ul></div>').appendTo(this.getElement('.news_thumb'));
//                this.getElement('.pagerbar td:eq(1) ul').append($('<li></li>').text(Math.floor(index / this._newsCountPerPage) + 1));
//            }
//            switch (this._viewMode) {
//                case 'FullMode': //Mojahedi: 2011-07-19 Add .append($('<span class=\'SubTitle\'></span>')
//                    $('<li></li>').append($('<h4></h4>').text(this.news[index].Title)).append($('<span class=\'SubTitle\'></span>').text(this.news[index].SubTitle)).appendTo(this.getElement('.news_thumb div:last ul'));
//                    break;
//                case 'TextMode':
//                    //$('<li></li>').append($('<div></div>').addClass('picndate').append($('<a></a>').attr('href', this.news[index].newsUrl).append($('<img />').attr('alt', ' ').attr('src', this.news[index].imgThumbSrc))).append($('<div></div>').text(this.news[index].newsDate))).append($('<div></div>').addClass('block').append($('<h4></h4>').text(this.news[index].Title)).append($('<p></p>').append($('<span></span>').text(this.news[index].Summary)).append('<span></span>'))).appendTo(this.getElement('.news_thumb ul:last'));
//                    $('<li></li>').append(
//                        $($('<table></table>').attr('cellpadding', '0').attr('cellspacing', '0').append($('<tr></tr>').append(
//                            $('<td></td>').attr('width', '1%').append(
//                                $('<a></a>').attr('target', '_blank').attr('href', this.news[index].newsUrl)
//                    /*.append(
//                    $('<img />').attr('alt', ' ').attr('src', this.news[index].imgThumbSrc)
//                    )*/
//                    //Mojahedi: 2011-07-18 Add .append($('<span class=\'SubTitle\'></span>').text(this.news[index].SubTitle))
//                            )).append(
//                            $('<td></td>').attr('width', '99%').append(
//                                $('<div></div>').addClass('block').append(
//                                    $('<h4></h4>').text(this.news[index].Title)).append($('<span class=\'SubTitle\'></span>').text(this.news[index].SubTitle))
//                                .append($('<div></div>').text(this.news[index].newsDate)).append(
//                                    $('<p></p>').append(
//                                        $('<span></span>').text(this.news[index].Summary)
//                                    ).append('<span></span>')
//                                )
//                            )
//                        )
//                    ))).appendTo(this.getElement('.news_thumb ul:last'));
//                    break;
//            }

//            //            if (this.getElement('.news_thumb h4').width() > this._maxNewsTitleLength)
//            //                this._maxNewsTitleLength = this.getElement('.news_thumb h4').width();
//        }

//        this.getElement('.news_thumb > div:first').addClass('active');
//        this.getElement('.news_thumb ul li:first').addClass('active');

//        this.getElement('.pagerbar td:eq(1) ul li:first').addClass('active');
//    },

//    setSubtitle: function () {
//        if (this._viewSubtitle) {
//            this.getElement('.subtitlebar').parents('tr:first').show();
//            this.getElement('.subtitlebar').empty();

//            var startIndex = this._currPageIndex * this._newsCountPerPage;
//            var endIndex = startIndex + this.getElement('.news_thumb div.active ul li').length;
//            for (var index = 0; index < this.news.length; index++) {
//                if (index < startIndex || index >= endIndex) {
//                    this.getElement('.subtitlebar').append($('<span></span>').text(this.news[index].Title));
//                }
//            }
//            if (this.getElement('.subtitlebar span').length == 1)
//                this.getElement('.subtitlebar').append('<span></span>');
//        }

//    },

//    nextNews: function (that) {
//        switch (that._viewMode) {
//            case 'TextMode':
//            case 'FullMode':
//                that.getElement('.news_thumb div.active ul li:eq(' + ((that._currNewsIndex + 1) % that._newsCountPerPage) % that.getElement('.news_thumb div.active ul li').length + ')').click();
//                break;
//            case 'PictureMode':
//                that._currNewsIndex = that._currNewsIndex + 1;
//                if (that._currNewsIndex >= that.news.length)
//                    that._currNewsIndex = 0;
//                if (that._state == 'play') {
//                    clearInterval(that.nextNewsIntervalId);
//                    that.nextNewsIntervalId = setInterval(function () { that.nextNews(that); }, that._slideShowInterval);
//                }
//                that.setTopOne();
//                break;
//        }
//    },

//    prevNews: function () {
//        switch (this._viewMode) {
//            case 'FullMode':
//                if (this._currNewsIndex % this._newsCountPerPage == 0)
//                    this.getElement('.news_thumb div.active ul li:eq(' + (this.getElement('.news_thumb div.active ul li').length - 1) + ')').click();
//                else
//                    this.getElement('.news_thumb div.active ul li:eq(' + ((this._currNewsIndex - 1) % this._newsCountPerPage) % this.getElement('.news_thumb div.active ul li').length + ')').click();
//                break;
//            case 'PictureMode':
//                this._currNewsIndex = this._currNewsIndex - 1;
//                if (this._currNewsIndex <= 0)
//                    this._currNewsIndex = this.news.length - 1;
//                if (this._state == 'play') {
//                    clearInterval(this.nextNewsIntervalId);
//                    this.nextNewsIntervalId = setInterval(function () { this.prevNews(); }, this._slideShowInterval);
//                }
//                this.setTopOne();
//                break;
//        }
//    },

//    PausePlay: function (itm) {
//        var str = new String($(itm).attr('src'));
//        var pattern, s;
//        if (str.toLowerCase().search('pause.png') != -1) {
//            pattern = new RegExp('(Pause.png)', 'gi');
//            s = $(itm).attr('src').replace(pattern, 'play.png');
//            $(itm).attr('src', s);
//            clearInterval(this.nextNewsIntervalId);
//            this._state = 'pause';
//        }
//        else {
//            pattern = new RegExp('(play.png)', 'gi');
//            s = $(itm).attr('src').replace(pattern, 'Pause.png');
//            $(itm).attr('src', s);
//            var that = this;
//            this.nextNewsIntervalId = setInterval(function () { that.nextNews(that); }, this._slideShowInterval);
//            this._state = 'play';
//        }
//    },

//    BindNews: function () {
//        var that = this;
//        //if (this._viewMode == 'PictureMode')
//        //    this._viewSubtitle = false;
//        switch (this._viewMode) {
//            case 'TextMode':
//                this.getElement('.news_thumb').css('margin-left', 'auto');
//                this.BindTextNews();
//                this.initUI();
//                this.getElement('.news_thumb ul li:first').click();
//                break;
//            case 'PictureMode':
//                this.initUI();
//                this.setTopOne();
//                break;
//            case 'FullMode':
//                //this._newsTitleWidth = this.getElement('.news_thumb').width();
//                this.BindTextNews();
//                this.setSubtitle();
//                this.initUI();
//                this.setTopOne();

//                //                this.getElement('.subtitlebar span').each(function() {
//                //                    $(this).truncate();
//                //                });

//                if (this._viewSubtitle) {
//                    //this.getElement('.subtitlebar').width(this._maxNewsTitleLength);
//                    //if (this._viewMode == 'FullMode')
//                    this.getElement('.subtitlebar').cycle({ fx: 'scrollUp', random: 1, cleartype: 0, timeout: 6000 });
//                }
//                break;
//        }
//        this.nextNewsIntervalId = setInterval(function () { that.nextNews(that); }, this._slideShowInterval);
//    },

//    SuccessFetchNews: function (response, that) {
//        if (response.noitem != undefined)
//            $(that._root).empty().html(response.noitem);
//        $(that._root).find('table:first').show();
//        that.news = response;
//        if (response.length == undefined)
//            that.news = new Array(response);
//        that.BindNews();
//    },

//    FailFetchNews: function (err) {
//        //Removed by Alireza
//        //alert('Error on Fetch news! ');
//        //End Removed by Alireza
//    },

//    fetchAllNews: function () {
//        this.PageMethod('fetchAllNews', '{}', this.SuccessFetchNews, this.FailFetchNews);
//    },


//    SuccessselectModules: function (response, that) {
//        that.newsModules = response;
//        if (response.length == undefined)
//            that.newsModules = new Array(response);

//        var query = new jsinq.Query('\
//             from portal in $0 \
//             select portal \
//            ');
//        query.setValue(0, new jsinq.Enumerable(response));
//        var result = query.execute();


//        var enumerator = result.groupBy(function (a) { return a.portalID }).getEnumerator();
//        that.getElement('#portals').empty();
//        while (enumerator.moveNext()) {
//            that.getElement('#portals').append($('<option></option>').text(enumerator.current().first().portalAlias).val(enumerator.current().first().portalID));
//        }

//        enumerator = result.where(function (a) { return a.selected == true; }).getEnumerator();
//        that.getElement('#SelectedNews').empty();
//        while (enumerator.moveNext()) {
//            that.getElement('#SelectedNews').append($('<option></option>').text(enumerator.current().moduleTitle).val(enumerator.current().moduleID));
//        }

//        var bindModules = function (index) {
//            that.getElement('#portals option[value="' + index + '"]').attr('selected', true);
//            that.getElement('#nominatedNews').empty();
//            enumerator = result.where(function (a) { return a.portalID == index; }).getEnumerator();
//            while (enumerator.moveNext()) {
//                if (!enumerator.current().selected)
//                    that.getElement('#nominatedNews').append($('<option></option>').text(enumerator.current().moduleTitle).val(enumerator.current().moduleID));
//            }
//        };

//        bindModules(0);

//        that.getElement('#portals').unbind('change');
//        that.getElement('#portals').change(function (event) {
//            bindModules(event.target.value);
//        });

//        that.getElement('#modSelect').unbind('click');
//        that.getElement('#modSelect').click(function () {
//            that.getElement('#nominatedNews option').each(function (i, item) {
//                if ($(item).attr('selected') == true) {
//                    result.where(function (a) { return a.moduleID == $(item).attr('value') }).first().selected = true;
//                    that.getElement('#SelectedNews').append($(item));
//                }
//            });
//        });

//        that.getElement('#modUnselect').unbind('click');
//        that.getElement('#modUnselect').click(function () {
//            that.getElement('#SelectedNews option').each(function (i, item) {
//                if ($(item).attr('selected') == true) {
//                    enumerator = result.where(function (a) { return a.moduleID == $(item).attr('value') });
//                    enumerator.first().selected = false;
//                    $(item).remove();
//                    bindModules(enumerator.first().portalID);
//                }
//            });
//        });
//    },

//    FailselectModules: function (err) {
//        alert('Error on Fetch newsModules! ');
//    },

//    FailSetModules: function (err) {
//        alert('Error on Storing newsModules! ');
//    },

//    selectModules: function () {
//        this.PageMethod('selectModules', '{}', this.SuccessselectModules, this.FailselectModules);
//    },

//    pausecomp: function (millis) {
//        var date = new Date();
//        var curDate = null;

//        do { curDate = new Date(); }
//        while (curDate - date < millis);
//    }


//}
//End Removed by Alireza 
