﻿(function ($) {
    $.fn.extend({
        vscroller: function (options) {
            var settings = $.extend({ speed: 1000, stay: 2000, newsfeed: '', cache: true }, options);

            return this.each(function () {
                var interval = null;
                var mouseIn = false;
                var totalElements;
                var isScrolling = false;
                var h;
                var t;
                var wrapper = $(this).addClass('news-wrapper');
                if (settings.newsfeed == '') { alert('No XML file specified'); return; }
                $.ajax({
                    url: settings.newsfeed,
                    type: 'GET',
                    cache: settings.cache,
                    success: function (xml) {
                        //if there are news headlines then build the html
                        var contentWrapper = $('<div/>').addClass('news-contents-wrapper');
                        var newsHeader = $('<div/>').addClass('news-header');
                        var newsContents = $('<div/>').addClass('news-contents');
                        wrapper.append(contentWrapper);
                        contentWrapper.append(newsHeader);
                        contentWrapper.append(newsContents);
                        newsHeader.html($(xml).find('newslist').attr('title'));
                        var i = 0;
                        totalElements = $(xml).find('news').length;
                        $(xml).find('news').each(function () {
                            var news = $('<div/>').addClass('news');
                            newsContents.append(news);
                            var history = $('<div/>').addClass('history');
                            var description = $('<div/>').addClass('description');
                            news.append(history);
                            news.append(description);
                            history.append(getCircle($(this).attr('category'), $(this).attr('date')));
                            var url = $(this).attr('url');
                            var htext = $(this).find('headline').text();
                            description.append($('<h1/>').html("<a target='_blank' href='" + url + "'>" + htext + "</a>"));
                            var newsText = $(this).find('detail').text();
                            if (newsText.length > 80) {
                                newsText = newsText.substr(0, 80) + "...";
                            }
                            description.append($('<div/>').addClass('detail').html(newsText));
                        });
                        h = parseFloat($('.news:eq(0)').outerHeight());
                        $('.news', wrapper).each(function () {
                            $(this).css({ top: i++ * h });
                        });
                        t = (totalElements - 1) * h;
                        newsContents.mouseenter(function () {
                            mouseIn = true;
                            if (!isScrolling) {
                                $('.news').stop(true, false);
                                clearTimeout(interval);
                            }
                        });
                        newsContents.mouseleave(function () {
                            mouseIn = false;
                            interval = setTimeout(scroll, settings.stay);
                        });
                        interval = setTimeout(scroll, 1);
                    }
                });
                //$.get(settings.newsfeed, );
                function scroll() {
                    if (!mouseIn && !isScrolling) {
                        isScrolling = true;
                        $('.news:eq(0)').stop(true, false).animate({ top: -h }, settings.speed, function () {

                            clearTimeout(interval);
                            var current = $('.news:eq(0)').clone(true);
                            current.css({ top: t });
                            $('.news-contents').append(current);
                            $('.news:eq(0)').remove();
                            isScrolling = false;
                            interval = setTimeout(scroll, settings.stay);

                        });
                        $('.news:gt(0)').stop(true, false).animate({ top: '-=' + h }, settings.speed);
                    }
                }
                function getCircle(category, date) {
 					var d = date.toString();
                    var month = '';					
                    var day = '';
 				  switch (d.substring(0, 2)){
                        case '01':
                            month = 'JAN';
                            break;
                        case '02':
                            month = 'FEB';
                            break;
                        case '03':
                            month = 'MAR';
                            break;
                        case '04':
                            month = 'APR';
                            break;
                        case '05':
                            month = 'MAY';
                            break;
                        case '06':
                            month = 'JUN';
                            break;
                        case '07':
                            month = 'JUL';
                            break;
                        case '08':
                            month = 'AUG';
                            break;
                        case '09':
                            month = 'SEP';
                            break;
                        case '10':
                            month = 'OCT';
                            break;
                        case '11':
                            month = 'NOV';
                            break;
                        case '12':
                            month = 'DEC';
                            break;
                    }					
					switch (d.substr(3, 2)){
                        case '01':
                        case '21':
                            day = d.substr(3, 2) + 'st';
                            break;
                        case '02':
                        case '22':
                            day = d.substr(3, 2) + 'nd';
                            break;
                        case '03':
                        case '23':
                            day = d.substr(3, 2) + 'rd';
                            break;
                        default:
                            day = d.substr(3, 2) + 'th';
                            break;
                    }
  
                    return $('<div/>').addClass('circle-outer').append($('<div/>').addClass('circle').addClass(category)
                                                                                                                .append($('<span/>').addClass('day').html(day))
                                                                                                                .append($('<span/>').html('...').addClass('elipses'))
                                                                                                                .append($('<span/>').addClass('month').html(month)));
																																																	
                }

            });
        }
    });
})(jQuery);

