/**
 * Copyright (c) 2010 iCurrent
 */
var IC = window.IC||{};

(function(){
if (IC && IC.Widget) {
    return;
}
IC.Widget = function(o) {
  this.initialize(o);
};

IC.Widget.prototype=function(){
  var baseUrl = 'http://widgets.icurrent.com';
  var resourceBase = baseUrl + '/v1';
  return {
    initialize: function(o) {
        this.options = this._extend({
            title: '',
            refreshRate: 300,
            header: true,
            scrollable: false,
            appearance: {
                style: 'inline',
                headline: {
                    byline: '#808080',
                    visited: '#656595'
                }
            }
        }, o);

        var self = this;
        if (this.options.refreshRate > 0) setInterval(function() {self.update()}, this.options.refreshRate * 1000);
        this._widgetId = ++IC.Widget.WIDGET_COUNT;
        this._id = 'ic-widget-' + this._widgetId;
        this._footerUrl = 'http://www.icurrent.com';

        if (this.options.type == 'channel') {
            this.resultUrl = baseUrl + '/results?type=channel&cid=' + this.options.cid;
        } else if (this.options.type == 'starred') {
            this.resultUrl = baseUrl + '/results?type=starred&uid=' + this.options.uid;
        }
        if (this.options.token) {
            this.resultUrl += '&token=' + this.options.token;
        }
        if (this.options.headlines) {
            this.resultUrl += '&headlines=' + this.options.headlines;
        }

        this.appearance = this.options.appearance;
        this.latest = 0;

        document.write(
            '<div id="' + this._id + '" class="ic-widget"></div>'
        );
        this.widgetElem = document.getElementById(this._id);
        if (this.appearance.style == 'widget') {
            this.widgetElem.className += ' ic-style-widget';
        }

        // insert stylesheet
        if (!IC.Widget.hasLoadedStylesheet) {
            this._loadStylesheet(resourceBase + '/widget.css', this.widgetElem);
        }

        // register JSON callback function
        var self = this;
        IC.Widget['resultCallback_' + this._widgetId] = function(results) {
            self._updateResults(results);
            if (self._dynScriptElem) {
                self._dynScriptElem.parentNode.removeChild(self._dynScriptElem);
                self._dynScriptElem = null;
            }
        }

    },

    update: function() {
        var updateUrl = this.resultUrl + '&callback=IC.Widget.resultCallback_' + this._widgetId;
        var head = document.getElementsByTagName('head')[0];
        var script = document.createElement('script');
        script.type = 'text/javascript';
        script.src = updateUrl;
        head.appendChild(script);

        this._dynScriptElem = script;
    },

    _updateResults: function(result) {
        // check if we have more recent documents
        if (result.latest <= this.latest) return;
                
        this.latest = result.latest;

        // clear existing headlines and insert new ones
        var divs = this.headlinesElem.getElementsByTagName('div');
        for (var i = divs.length - 1; i >= 0; i--) {
            if (divs[i].className == 'ic-article') {
                this.headlinesElem.removeChild(divs[i]);
            }
        }

        var headlines = result.headlines;
        var headline;
        var pubdate;
        for (var i = 0; i < headlines.length; i++) {
            headline = headlines[i];
            pubdate = new Date(headline.pubdate * 1000)
            var articleElem = document.createElement('div');
            articleElem.className = 'ic-article';
            this.headlinesElem.appendChild(articleElem);
            articleElem.innerHTML += '<div class="ic-headline"><a target="_blank" href="' + headline.url + '">' + headline.title + '</a></div><div class="ic-byline"><span class="ic-headline-src">' + headline.source + '</span> <span class="ic-headline-pubdate">' + this._formatDate(pubdate) + '</span></div>';

            // clear out the last article
            var top = articleElem.offsetTop;
            var height = articleElem.offsetHeight;
            if (!this.options.scrollable && this.options.height && (top + height > this.options.height)) {
                articleElem.parentNode.removeChild(articleElem);
                break;
            }
        }

        if (result.url != this._footerUrl) {
            this._setFooterLink(result.url, result.title);
        }
    },

    render: function() {
        var self = this;
        if (!IC.Widget.hasLoadedStylesheet) {
            window.setTimeout(function(){self.render.call(self)}, 50);
            return this;
        }

        var widgetHTML = '<div class="ic-widget-container" style="width: ' + this.options.width + 'px;">';
        if (this.options.header) {
            widgetHTML += '<div class="ic-widget-header"><h3>' + this.options.title + '</h3></div>';
        }
        widgetHTML += '<div class="ic-widget-body"><div class="ic-widget-results"></div></div>' +
                      '<div class="ic-widget-footer"><a target="_blank" href="http://www.icurrent.com"><img src="' + resourceBase + '/logo.png"/></a><a class="ic-channel" href="http://www.icurrent.com">' + this.options.title + '&nbsp;&raquo;</a></div>' +
                      '</div>';
        this.widgetElem.innerHTML = widgetHTML;

        var containerElem = this._down(this.widgetElem, 'ic-widget-container', 'div');
        this.headlinesElem = this._down(this.widgetElem, 'ic-widget-results', 'div');
        if (this.options.height) {
            this._setStyle(this.headlinesElem, 'height', this.options.height + 'px');
        }
        if (this.options.scrollable) {
            this._setStyle(this.headlinesElem, 'overflowY', 'auto');
        }

        // appearance settings
        if (this.appearance.font) {
            if (this.appearance.font == 'arial' || this.appearance.font ==  'verdana' || this.appearance.font == 'lucida grande') {
                this._setStyle(this.widgetElem, 'fontFamily', this.appearance.font + ', sans-serif');
            } else if (this.appearance.font == 'georgia' || this.appearance.font == 'times new roman') {
                this._setStyle(this.widgetElem, 'fontfamily', this.appearance.font + ', serif');
            }
        }
        if (this.appearance.container) {
            if (this.appearance.container.background) {
                this._setStyle(containerElem, 'backgroundColor', this.appearance.container.background);
            }
            if (this.appearance.container.text) {
                this._insertStyle('#' + this._id + ' .ic-widget-header,' + 
                                  '#' + this._id + ' .ic-widget-footer,' + 
                                  '#' + this._id + ' .ic-widget-footer a' + 
                                  '{color: ' + this.appearance.container.text + '}');
            }
        }
        if (this.appearance.headlines) {
            if (this.appearance.headlines.background) {
                this._setStyle(this.headlinesElem, 'backgroundColor', this.appearance.headlines.background);
            }
            if (this.appearance.headlines.link) this._insertStyle('#' + this._id + ' .ic-headline a { color: ' + this.appearance.headlines.link + '; }');
            if (this.appearance.headlines.visited) this._insertStyle('#' + this._id + ' .ic-headline a:visited { color: ' + this.appearance.headlines.visited + '; }');
            if (this.appearance.headlines.byline) {
                this._insertStyle('#' + this._id + ' .ic-byline { color: ' + this.appearance.headlines.byline + '; }');
                if (this.appearance.style != 'widget') this._insertStyle('#' + this._id + ' .ic-widget-footer span { color: ' + this.appearance.headlines.byline + '; }');
            }
        }

        this.update();
    },

    _loadStylesheet: function(url, elem) {
        if (IC.Widget.loadingStylesheet) return;

        IC.Widget.loadingStylesheet = true;
        var link = document.createElement('link');
        link.rel = 'stylesheet';
        link.type = 'text/css';
        link.href = url;
        document.getElementsByTagName('head')[0].appendChild(link);

        // check if loaded
        var self = this;
        var interval = setInterval(function() {
            if (self._getStyle(elem, 'position') == 'relative') {
                clearInterval(interval);
                IC.Widget.hasLoadedStylesheet = true;
            }
        }, 50);
    },

    _insertStyle: function(style) {
        var head = document.getElementsByTagName('head')[0];
        var styleElem = document.createElement('style');
        styleElem.type = 'text/css';

        if (styleElem.styleSheet) {
            styleElem.styleSheet.cssText = style;
        } else {
            var rule = document.createTextNode(style);
            styleElem.appendChild(rule);
        }
        head.appendChild(styleElem);
    },

    _getStyle: function(elem, name) {
        if (elem.style[name]) {
            return elem.style[name];
        } else if (elem.currentStyle) { 
            return elem.currentStyle[name];
        } else if (document.defaultView && document.defaultView.getComputedStyle) {
            name = name.replace(/([A-Z])/g, "-$1");
            name = name.toLowerCase();
            s = document.defaultView.getComputedStyle(elem, "");
            return s && s.getPropertyValue(name);
        } else {
            return null;
        }
    },

    _formatDate: function(date) {
        var now = new Date();
        var elapsedSecs = (now - date) / 1000;

        if (elapsedSecs <= 60) {
            return '1 min ago';
        } else if (elapsedSecs <= 3600) {
            return Math.round(elapsedSecs / 60) + ' mins ago';
        } else if (elapsedSecs <= 7200) {
            return '1 hour ago';
        } else if (elapsedSecs <= (3600 * 18)) {
            return Math.round(elapsedSecs / 3600) + ' hours ago';
        } else if (elapsedSecs <= (24 * 3600)) {
            return '1 day ago';
        } else {
            return Math.round(elapsedSecs / (24 * 3600)) + ' days ago';
        }
    },

    _down: function(element, className, tagName) {
        var candidates = element.getElementsByTagName(tagName);
        for (var i = 0; i < candidates.length; i++) {
            if (candidates[i].className == className) return candidates[i];
        }
        return null;
    },

    _setStyle: function(element, style, value) {
        element.style[style] = value;
    },

    setTitle: function(title) {
        this.widgetElem.getElementsByTagName('h3')[0].innerHTML = title;
    },

    _setFooterLink: function(url, title) {
        this._footerUrl = url;
        var footer = this._down(this.widgetElem, 'ic-widget-footer', 'div');
        var link = footer.getElementsByTagName('a')[1];
        link.href = this._footerUrl;
        link.innerHTML = title + '&nbsp;&raquo;';
    },

    _extend: function(destination, source) {
        for (var property in source) {
            destination[property] = source[property];
        }
        return destination;
    }

  }
}();

IC.Widget.WIDGET_COUNT = 0;
})();

