﻿(function ($) {
    $.fn.extend({
        vscroller: function (options) {
            var settings = $.extend({ speed: 1000, stay: 2000, tweets: '', 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('tweet-wrapper');
                if (settings.tweets == '') { alert('xml dosyası yokki'); return; }
                $.ajax({
                    url: settings.tweets,
                    type: 'GET',
                    cache: settings.cache,
                    success: function (xml) {
                        //if there are news UserNames then build the html
                        var contentWrapper = $('<div/>').addClass('tweet-contents-wrapper');
                        var tweetHeader = $('<div/>').addClass('tweet-header');
                        var tweetContents = $('<div/>').addClass('tweet-contents');
                        wrapper.append(contentWrapper);
                        contentWrapper.append(tweetHeader);
                        contentWrapper.append(tweetContents);
                        tweetHeader.html($(xml).find('tweets').attr('title'));
                        var i = 0;
                        totalElements = $(xml).find('tweet').length;
                        $(xml).find('tweet').each(function () {
                            var tweet = $('<div/>').addClass('tweet');
                            tweetContents.append(tweet);
                            var username = $('<div/>').addClass('username');
                            var description = $('<div/>').addClass('description');
							var image = $('<div/>').addClass('Image');
                            tweet.append(username);
                            tweet.append(description);
							tweet.append(image);
                            username.append(getCircle($(this).attr('category'), $(this).attr('date')));
                            var url = $(this).attr('url');
                            var htext = $(this).find('UserName').text();
                            description.append($('<h1/>').html("<a href=http://www.twitter.com/" + htext + ">" + htext + "</a><br /><br>"));
                            var tweetText = $(this).find('Text').text();
							var imageText = $(this).find('Image').text();
                            if (tweetText.length > 80) {
                                tweetText = tweetText.substr(0, 80) + "...";
                            }
                            description.append($('<div/>').addClass('Text').html(tweetText));
							//image.append($('<div/>').addClass('Text').html(imageText));
                            image.append($('<div class="TweetImage"/>').html("<img style='width:50px; height:50px;' src='" + imageText + "'>"));
                        });
                        h = parseFloat($('.tweet:eq(0)').outerHeight());
                        $('.tweet', wrapper).each(function () {
                            $(this).css({ top: i++ * h });
                        });
                        t = (totalElements - 1) * h;
                        tweetContents.mouseenter(function () {
                            mouseIn = true;
                            if (!isScrolling) {
                                $('.tweet').stop(true, false);
                                clearTimeout(interval);
                            }
                        });
                        tweetContents.mouseleave(function () {
                            mouseIn = false;
                            interval = setTimeout(scroll, settings.stay);
                        });
                        interval = setTimeout(scroll, 1);
                    }
                });
                //$.get(settings.tweetfeed, );
                function scroll() {
                    if (!mouseIn && !isScrolling) {
                        isScrolling = true;
                        $('.tweet:eq(0)').stop(true, false).animate({ top: -h }, settings.speed, function () {

                            clearTimeout(interval);
                            var current = $('.tweet:eq(0)').clone(true);
                            current.css({ top: t });
                            $('.tweet-contents').append(current);
                            $('.tweet:eq(0)').remove();
                            isScrolling = false;
                            interval = setTimeout(scroll, settings.stay);

                        });
                        $('.tweet: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;

                    }
                  
                }

            });
        }
    });
})(jQuery);

