﻿(function ($) {

    $.fn.extend({
        vscroller: function (options) {
            var settings = $.extend({ speed: 1000, stay: 4000, 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',
					async : false,
                    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');
						var eventLink = $('<div/>').addClass('event-contents');
						
                        wrapper.append(contentWrapper);
                        contentWrapper.append(newsHeader);
                        contentWrapper.append(newsContents);
						contentWrapper.append(eventLink);
						eventLink.html('<span>For a full list of the Army Distaff Foundation/Knollwood\'s community events, please visit <a href="index.php?file=event&page_id=40">Events</a></span>');
                        newsHeader.html(xml.title);
                        var i = 0;
                        totalElements = $(xml.news).length;
					
                        $(xml.news).each(function (l) {
							
                            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('<img src="'+xml.news[l].image+'" />');
                            var url = xml.news[l].url;
                            var htext = xml.news[l].title;
                            description.append($('<h1/>').html("<a class='various2' href='" + url + "'>" + htext + "</a>"));
                            var newsText = xml.news[l].detail;
                            if (newsText.length > 70) {
                                newsText = newsText.substr(0, 70) + "...";
								
                            }
                            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 = new Date(date);
                    var day = '';
                    var month = '';
                    switch (d.getDay()) {
                        case 1:
                        case 21:
                            day = d.getDay() + ' st';
                            break;
                        case 2:
                        case 22:
                            day = d.getDay() + 'nd';
                            break;
                        case 3:
                        case 23:
                            day = d.getDay() + 'rd';
                            break;
                        default:
                            day = d.getDay() + 'th';
                            break;
                    }
                    switch (d.getMonth()) {
                        case 0:
                            month = 'JAN';
                            break;
                        case 1:
                            month = 'FEB';
                            break;
                        case 2:
                            month = 'MAR';
                            break;
                        case 3:
                            month = 'APR';
                            break;
                        case 4:
                            month = 'MAY';
                            break;
                        case 5:
                            month = 'JUN';
                            break;
                        case 6:
                            month = 'JUL';
                            break;
                        case 7:
                            month = 'AUG';
                            break;
                        case 8:
                            month = 'SEP';
                            break;
                        case 9:
                            month = 'OCT';
                            break;
                        case 10:
                            month = 'NOV';
                            break;
                        case 11:
                            month = 'DEC';
                            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);

