﻿
    /**
    * Elsevier Commercial Sales Magazines
    * Public
    * Homepage Scripts
    *
    * @requires /cp/cp.js
    * @namespace cp.homepage
    * @version 1
    */

    cp.namespace('homepage');
    cp.namespace('weekly');

    cp.homepage = {

        SERVICE_PATH: '/_common/service/_homepagemanager.aspx',
        LIMIT: 5,
        CATEGORY_ID: null,

        N_CLICK: 0,
        N_START: 5,
        N_CURRENT: '0',
        N_REDIRECT: '/news/',
        N_CONTAINER: 'latest-news-container',
        N_PREFIX: 'news-pod-new',
        N_IMG_CONTAINER: 'latest-news-image',

        F_CLICK: 0,
        F_START: 4,
        F_CURRENT: '0',
        F_REDIRECT: '/features/',
        F_CONTAINER: 'features-container',
        F_PREFIX: 'features-pod-new',
        F_IMG_CONTAINER: 'features-container-img',

        get: function() {
            var url = this.SERVICE_PATH;
            var type = arguments[0];
            var current = null;
            var doReq = false;

            if (arguments[0] == 'N') { /* latest news */
                url += '?service=latest_news&method=get&current=' + this.getCurrent(arguments[0]);
                url += (this.CATEGORY_ID != null ? '&catId=' + this.CATEGORY_ID : '');
                this.N_CLICK = (this.N_CLICK + 1);

                doReq = (this.N_CLICK <= this.LIMIT);
            }

            if (arguments[0] == 'F') { /* features */
                url += '?service=features&method=get&current=' + this.getCurrent(arguments[0]);
                url += (this.CATEGORY_ID != null ? '&catId=' + this.CATEGORY_ID : '');
                this.F_CLICK = (this.F_CLICK + 1);

                doReq = (this.F_CLICK <= this.LIMIT);
            }

            if (doReq) {
                this.load(type);

                var ajaxobject = {};
                ajaxobject.url = url;

                ajaxobject.success = function() {
                    var _res = eval("(" + arguments[0].responseText + ")");
                    if (_res.ArticleId != null)
                        cp.homepage.build(arguments[0].responseText, type);
                    else
                        window.location = _res.redirect;
                };

                ajaxobject.error = function() {
                    window.location = eval("cp.homepage." + type + "_REDIRECT");
                };

                cp.ajax.post(ajaxobject);
            }
            else {
                window.location = eval("this." + type + "_REDIRECT");
            }

            return false;
        },

        getCurrent: function() {
            return eval("cp.homepage." + arguments[0] + "_CURRENT");
        },

        load: function() {
            var container = $(eval("cp.homepage." + arguments[0] + "_IMG_CONTAINER"));
            /*container.innerHTML = '<div style="margin-bottom:6px;">loading...</div>';*/
        },

        build: function() {
            var container = $(eval("cp.homepage." + arguments[1] + "_CONTAINER"));
            var icontainer = $(eval("cp.homepage." + arguments[1] + "_IMG_CONTAINER"));

            var ohtml = container.innerHTML; /* old html        */
            var nhtml = '';                  /* new html        */
            var ihtml = '';                  /* new image html  */

            if (arguments[1] == 'N') {
                var obj = eval("(" + arguments[0] + ")");

                nhtml += ohtml;
                nhtml += '<ul class="pod-snippets" id="' + cp.homepage.N_PREFIX + '' + obj.ArticleId + '">';
                nhtml += '<li><a href="' + obj.ArticleLink + '">' + obj.ArticleTitle + '</a><br />';
                nhtml += obj.ArticleIntro + '</li></ul>';

                if (obj.ArticleImage != null && true == false) {
                    ihtml = '<a href="' + obj.ArticleLink + '" class="pod-feature-img" ';
                    ihtml += 'style="background-image:url(\'/_virtual/article-images/';
                    ihtml += obj.ArticleImage + '\');"><span style="display:none;">' + obj.ArticleTitle + '</span></a>';
                    $('latest-news-original').innerHTML = '';
                }

                cp.homepage.N_CURRENT = obj.Current;
            }

            if (arguments[1] == 'F') {
                var obj = eval("(" + arguments[0] + ")");

                nhtml += ohtml;
                nhtml += '<ul class="pod-snippets" id="' + cp.homepage.F_PREFIX + '' + obj.ArticleId + '">';
                nhtml += '<li>';

                if (obj.ArticleImage != null) {
                    nhtml += '<a href="' + obj.ArticleLink + '" style="background-image:url(';
                    nhtml += '/_virtual/article-images/' + obj.ArticleImage + ')" class="pod-feature-img"';
                    nhtml += '><span style="display:none;">' + obj.ArticleTitle + '</span></a>';
                }

                nhtml += '<a href="' + obj.ArticleLink + '">' + obj.ArticleTitle + '</a><br />';
                nhtml += obj.ArticleIntro + '</li></ul>';

                cp.homepage.F_CURRENT = obj.Current;
            }

            container.innerHTML = nhtml;
            icontainer.innerHTML = ihtml;
        },

        rem: function() {
            var type = arguments[0];

            var current = (eval("cp.homepage." + type + "_CURRENT")).split(',');
            var prefix = (eval("cp.homepage." + type + "_PREFIX"));
            var icontainer = $(eval("cp.homepage." + type + "_IMG_CONTAINER"));

            if (current.length > (eval("cp.homepage." + type + "_START"))) {
                var iRemove = current[current.length - 1];
                var div = $(prefix + iRemove);

                if (div != null) {
                    Dom.setStyle(div, 'display', 'none');
                    icontainer.innerHTML = '';
                }

                var _currents = '';
                for (var i = 0; i < current.length; i++) {
                    if (current[i] != iRemove)
                        _currents += current[i] + ',';
                }

                if (_currents != '') {
                    _currents = _currents.substring(0, (_currents.length - 1));

                    if (type == 'N') {
                        cp.homepage.N_CURRENT = _currents;
                        cp.homepage.N_CLICK = (cp.homepage.N_CLICK - 1);
                    }

                    if (type == 'F') {
                        cp.homepage.F_CURRENT = _currents;
                        cp.homepage.F_CLICK = (cp.homepage.F_CLICK - 1);
                    }
                }
            }

            return false;
        }
    };

    cp.homepage.weekly = {
        AREA1_CURRENT: -1,
        AREA2_CURRENT: -1,

        AREA1_INTERVAL: null,
        AREA2_INTERVAL: null,

        LIMIT: 10000,

        start_area1: function() {
            var area = 1;
            var list = window['weeklyBroadcast_area' + area].split(',');

            if (list.length > 1) {
                var current = $(list[cp.homepage.weekly.AREA1_CURRENT]);

                if (current != null) {
                    current.style.display = 'none';
                }

                cp.homepage.weekly.AREA1_CURRENT = cp.homepage.weekly.AREA1_CURRENT + 1;
                var item = $(list[cp.homepage.weekly.AREA1_CURRENT]);

                if (item != null) {
                    item.style.display = 'block';
                }

                if (cp.homepage.weekly.AREA1_CURRENT == (list.length)) {
                    cp.homepage.weekly.AREA1_CURRENT = -1;
                    cp.homepage.weekly.start_area1();
                }
                else {
                    setTimeout(function() {
                        cp.homepage.weekly.start_area1();
                    }, cp.homepage.weekly.LIMIT);
                }
            }
            else {
                /*window.clearInterval(cp.homepage.weekly.AREA1_INTERVAL);*/
                $(list[0]).style.display = 'block';
            }
        },

        start_area2: function() {
            var area = 2;
            var list = window['weeklyBroadcast_area' + area].split(',');

            if (list.length > 1) {
                var current = $(list[cp.homepage.weekly.AREA2_CURRENT]);

                if (current != null) {
                    current.style.display = 'none';
                }

                cp.homepage.weekly.AREA2_CURRENT = cp.homepage.weekly.AREA2_CURRENT + 1;
                var item = $(list[cp.homepage.weekly.AREA2_CURRENT]);

                if (item != null) {
                    item.style.display = 'block';
                }

                if (cp.homepage.weekly.AREA2_CURRENT == (list.length)) {
                    cp.homepage.weekly.AREA2_CURRENT = -1;
                    cp.homepage.weekly.start_area2();
                }
                else {
                    setTimeout(function() {
                        cp.homepage.weekly.start_area2();
                    }, cp.homepage.weekly.LIMIT);
                }
            }
            else {
                /*window.clearInterval(cp.homepage.weekly.AREA2_INTERVAL);*/
                $(list[0]).style.display = 'block';
            }
        }
    };
