(function() {
    INTEL("widget", function() {

        var widgetFunctions;
        document.domain = 'intel.com';
        widgetFunctions = {
            callBacks: [],
            readyInitialize: function(widget) {
                var callBacks = this.callBacks,
                getFeedFunction = function(request, callBack) {
                    if (callBacks[request.key]) {
                        delete callBacks[request.key];
                    }
                    callBacks[request.key] = callBack; // Store the callback.
                    INTEL.widget.message(widget, request); // Issue the request
                };

                getISO8601 = function(dateStr) {
                    var regexp = "([0-9]{4})(-([0-9]{2})(-([0-9]{2})" +
                            "(T([0-9]{2}):([0-9]{2})(:([0-9]{2})(\.([0-9]+))?)?" +
                            "(Z|(([-+])([0-9]{2}):([0-9]{2})))?)?)?)?",
                             d = dateStr.match(new RegExp(regexp)),
                            offset = 0,
                            date = new Date(1, 0, 1);

                    if (d) {
                        date = new Date(d[1], 0, 1);
                        if (d[1]) { date.setYear(d[1]); }
                        if (d[3]) { date.setMonth(d[3] - 1); }
                        if (d[5]) { date.setDate(d[5]); }
                        if (d[7]) { date.setHours(d[7]); }
                        if (d[8]) { date.setMinutes(d[8]); }
                        if (d[10]) { date.setSeconds(d[10]); }
                        if (d[12]) { date.setMilliseconds(Number("0." + d[12]) * 1000); }
                        if (d[14]) {
                            offset = (Number(d[16]) * 60) + Number(d[17]);
                            offset *= ((d[15] == '-') ? 1 : -1);
                        }

                        offset -= date.getTimezoneOffset();
                        time = (Number(date) + (offset * 60 * 1000));
                        date = new Date(Number(time));
                    }
                    return date;
                };

                monthAbbreviations = {
                    "$COPY$_en_us_long": ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
                    "$COPY$_en_us_short": ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
                    "$COPY$_de_de": ["Jan", "Feb", "Mrz", "Apr", "Mai", "Jun", "Jul", "Aug", "Sep", "Okt", "Nov", "Dez"],
                    "$COPY$_fr_fr": ["janv.", "févr.", "mars", "avr.", "mai", "juin", "juil.", "août", "sept.", "oct.", "nov.", "déc."],
                    "$COPY$_ru_ru": ["???", "???", "???", "???", "???", "???", "???", "???", "???", "???", "???", "???"]
                };

                jQuery(INTEL.Feeds).filter(function() { return (jQuery(this).attr("widgetName") === widget.name); }).each(function() {
                    jQuery(this.feeds).each(function(i, val) {
                        var url = this.url,
                        target = this.target,
                        filter = this.filter,
                        showdescription = this.showdescription,
                        key = widget.name + '-frq-' + i,
                        maxToShow = (this.maxToShow && (!isNaN(parseInt(this.maxToShow, 10)))) ? this.maxToShow : 4,
                        viewAllLabel = (this.viewAllLabel) ? this.viewAllLabel : "View all",
                        viewAllLink = (this.viewAllLink) ? this.viewAllLink : null,
						markup = this.markup;

                        getFeedFunction({ key: key, url: url, max: maxToShow, filter: filter, showdescription: showdescription, markup: markup },
                            function(response) {
                                var feedMarkup = '',
                                numToShow = maxToShow,
                                numToShow = (response.length < numToShow) ? response.length : numToShow,
                                pageLocale = (jQuery('html').attr('lang')) ? jQuery('html').attr('lang').toLowerCase() : 'en_us',
                                months = (monthAbbreviations['$COPY$_' + pageLocale]) ? monthAbbreviations['$COPY$_' + pageLocale] : monthAbbreviations['$COPY$_en_us_long'];   //see above monthAbbreviations for list of available settings

                                jQuery(response).slice(0, numToShow).each(function() {
                                    var date = getISO8601(this.updated),
                                    link = this.link,
                                    title = this.title,
                                    description = this.description,
                                    tags = this.tags,
                                    dateStr = months[date.getMonth()] + ' ' + date.getDate() + ', ' + date.getFullYear();

                                    if (jQuery.browser.msie && jQuery.browser.version === '6.0') {
                                        link = (link) ? link.replace(/\&amp;/g, '&').replace(/\&quot;/g, '"').replace(/\&\#25;/g, '%') : link;
                                        title = (title) ? title.replace(/\&amp;/g, '&').replace(/\&quot;/g, '"').replace(/\&\#25;/g, '%') : title;
                                        description = (description) ? description.replace(/\&amp;/g, '&').replace(/\&quot;/g, '"').replace(/\&\#25;/g, '%') : description;
                                    }

                                    /*
									feedMarkup += '<a class="rssprtitle" href="' + link + '" title="' + title + '">' + title + '</a><br><span class="rssprdate">' + dateStr + '</span>';
                                    feedMarkup += '<br><p class="rssprdesc">' + description + '</p>';
                                    //feedMarkup += '<p class="rssprcat">Categories: ' + tags + '</p>';
                                    feedMarkup += '<div class="hr-dotted"></div>';
									*/
									
									//insert the values into the provided markup									
									var providedMarkup = markup;
									providedMarkup = providedMarkup.replace(/\[link\]/g, link);
									providedMarkup = providedMarkup.replace(/\[title\]/g, title);
									providedMarkup = providedMarkup.replace(/\[date\]/g, dateStr);
									providedMarkup = providedMarkup.replace(/\[description\]/g, description);

									feedMarkup += providedMarkup;

                                })
                                jQuery(target).html(feedMarkup);
                            });
                    });
                });
            },
            onmessage: function(data, widget) {
                if (widget && this.callBacks && this.callBacks[data.key]) {// When the data is received this handles dispatching the response to the appropriate callback.
                    this.callBacks[data.key](data.response);  // execute the callback.
                    // clear the callback for this request.
                    delete this.callBacks[data.key];
                }
            }
        };

        // Make sure the Feeds were defined for the page.
        if (INTEL.Feeds) {

            jQuery(INTEL.Feeds).each(function() {
                if (jQuery('#' + this.widgetName).length === 0) {
                    jQuery('body').append('<div style="display:none" id="' + this.widgetName + '"></div>');
                }
                jQuery[this.widgetName] = widgetFunctions;
                INTEL.widget({ container: "#" + this.widgetName,
                    src: this.dataFrame,
                    proxy: this.proxy,
                    name: this.widgetName
                });
            })
        }
    });
})();



