﻿feed_control = function() { };

feed_control.prototype.service_path = '/ajax/feed_control.ashx?method=';
feed_control.prototype.journalInfo = null;
feed_control.prototype.find_out_more = null;
feed_control.prototype.scopus = null;
feed_control.prototype.scienceDirect = null;

feed_control.prototype.init = function() {
    this.journalInfo = $('#journalInfo');
    this.find_out_more = $('#find_out_more');
    this.scopus = $('#scopus');
    this.scienceDirect = $('#scienceDirect');
    this.sdMostRecent = $('#mostRecent');
};

feed_control.prototype.get_feed = function(journal_id) {
    var s = this;
    $.ajax({
        type: "GET",
        url: this.service_path + 'get&journal_id=' + journal_id,
        success: function() {
            s.get_feed_success.call(s, arguments[0]);
        },
        cache: false
    });
};

feed_control.prototype.get_feed_success = function() {
    if (arguments[0] != '') {
        var obj = eval("(" + arguments[0] + ")");

        var html = [];
        html.push('<div class="impact_content" style="border-right: solid 1px #ffffff;">'
            + '<p class="no_margin bold" style="text-transform: uppercase;">' + obj.journal[0].Title + '</p>'
            + '<p class="no_margin">' + decodeURIComponent(obj.journal[0].Description).split('+').join(' ') + '<a href="' + obj.journal[0].Url + '">MORE</a></p>'
            + '</div><div class="impact_content">'
            + '<p class="no_margin bold">' + obj.journal[0].ImpactFactor + '</p>'
            + '<p class="no_margin">' + obj.journal[0].ImpactFactor5Year + '<br />' + obj.journal[0].IssuesPerYear + '</p></div>');
        this.journalInfo.html(html.join(''));

        var html = [];
        html.push('<a href="/journals/Journal.aspx?JournalId=' + obj.journal[0].JournalId + '">Find out more</a>');
        this.find_out_more.html(html.join(''));

        var html = [];
        html.push('<h2 style="margin: 0 20px 0 0;">Best Cited</h2>');
        html.push('<h2 style="margin: 0 20px 0 0; font-size: 0.7em;">' + obj.journal[0].Title + '</h2>');
        html.push('<ul class="feed_ul">');
        if (obj.scopus.length > 0) {
            for (var i = 0; i < obj.scopus.length; i++) {
                html.push('<li><p><a href="' + obj.scopus[i].InwardUrl + '" target="_blank">', obj.scopus[i].Title, '</a></p><p>Volume '
                + obj.scopus[i].Volume + ', Issue ' + obj.scopus[i].Issue + ', Page ' + obj.scopus[i].Page + ', ' + obj.scopus[i].PubDate
                + ',<br />' + obj.scopus[i].AuthList + '</p></li>');
            }
        } else {
            html.push('<li><p>No results found.</p></li>');
        }
        html.push('</ul>');
        html.push('<a href="/rss/top-cited/' + obj.journal[0].TitleRSS + '/" target="_blank"><img src="/_common/images/logos/rss.gif" alt="Subscribe to RSS Feed" style="float: right; margin-right: 25px;" /></a>');
        this.scopus.html(html.join(''));

        html = [];
        html.push('<h2 style="margin: 0 20px 0 0;">Most Used</h2>');
        html.push('<h2 style="margin: 0 20px 0 0; font-size: 0.7em;">' + obj.journal[0].Title + '</h2>');
        html.push('<ul class="feed_ul">');
        if (obj.scienceDirect.length > 0) {
            for (var i = 0; i < obj.scienceDirect.length; i++) {
                html.push('<li><p><a href="' + obj.scienceDirect[i].Link + '" target="_blank">', obj.scienceDirect[i].Title, '</a></p><p>', obj.scienceDirect[i].Description, '</p></li>');
            }
        }
        else {
            html.push('<li><p>No results found.</p></li>');
        }
        html.push('</ul>');
        html.push('<a href="/rss/most-used/' + obj.journal[0].TitleRSS + '/" target="_blank"><img src="/_common/images/logos/rss.gif" alt="Subscribe to RSS Feed" style="float: right; margin-right: 25px;" /></a>');
        this.scienceDirect.html(html.join(''));

        html = [];
        html.push('<h2 style="margin: 0 20px 0 0;">Just Published</h2>');
        html.push('<h2 style="margin: 0 20px 0 0; font-size: 0.7em;">' + obj.journal[0].Title + '</h2>');
        html.push('<ul class="feed_ul">');
        if (obj.mostRecent.length > 0) {
            for (var i = 0; i < obj.mostRecent.length; i++) {
                html.push('<li><p><a href="' + obj.mostRecent[i].Link + '" target="_blank">', obj.mostRecent[i].Title, '</a></p><p>', obj.mostRecent[i].Description, '</p></li>');
            }
        }
        else {
            html.push('<li><p>No results found.</p></li>');
        }
        html.push('</ul>');
        html.push('<a href="/rss/just-published/' + obj.journal[0].TitleRSS + '/" target="_blank"><img src="/_common/images/logos/rss.gif" alt="Subscribe to RSS Feed" style="float: right; margin-right: 25px;" /></a>');
        this.sdMostRecent.html(html.join(''));

        this.feed_ul_height();
    }
};

feed_control.prototype.feed_ul_height = function(tallest) {
    $(function() {
        $('ul.feed_ul li:last-child').css({ border: 'none' });
    });

    var tallest = 0;
    $('ul.feed_ul li').each(function() {
        $(this).css('height', '');
    }).each(function() {
        if ($(this).height() > tallest) {
            tallest = $(this).height();
        }
    }).each(function() {
        $(this).height(tallest);
    });
};

$(function() {
    feedcontrol = new feed_control();
    feedcontrol.init();
});
