(function($){
  
  $.fn.goodTicker = function(options){
    var o = $.extend($.fn.goodTicker.def, options||{});
    var _feed = 'wtf';
    var _timer;
    var _liveItem;
    var _tickerIndex = o.startItem-1||0;
    var _this;
    var _navigation;
    
    $.ajax({
      url: o.feed,
      type: "GET",
      dataType: "xml",
      async: false,
      success: function(xml) {
        //called when successful
        _feed = $(xml);
      },  
      error: function(resp) {
        //called when there is an error
        _feed = {};
      }
    });
    
    if(typeof _feed == 'object' && _feed.length > 0){
      this.each(function(){
        _this = $(this);
        var ticker = _this.wrap($('<div class="good-ui_ticker"></div>').css({
          background: '#f00', 
          position: 'relative',
          overflow: 'hidden',
          height: _this.height()
        }));
        
        if(o.navigation == true){
         
          var _navigation = $('<div class="good-ui_ticker-navigation"></div>').css({
            height: 35, 
            width: 85,
            'padding-right': 15,
            position: 'absolute',
            right: 0,
            top: 0,
            'line-height': '35px',
            'text-align': 'center',
            'font-size': '11px',
            'z-index': 1000
            
          });
          
          
          var prev = $('<a class="good-ui_ticker-button-next" href="#">&lt;</a>').css({
            'margin-right': 10,
            'font-weight': 'bold',
            'text-decoration': 'none',
            'vertical-align': 'baseline'
            }).click(function(){
            tick(_tickerIndex-1, o.repeat);
            clearInterval(_timer);
            setTimeout(function(){
              _timer = setInterval(function(){tick(_tickerIndex, o.repeat||false); clearTimeout();}, o.speed||5000);
            }, o.speed||5000);
          }).appendTo(_navigation);
          
          var counter = $('&nbsp;<span class="good-ui_ticker-current-item">'+(_tickerIndex+1)+'</span> / <span class="good-ui_ticker-total-items">'+$('item', _feed).length+'</span>&nbsp;').appendTo(_navigation);
          
          var next = $('<a class="good-ui_ticker-button-next" href="#">&gt;</a>').css({
            'margin-left': 10,
            'font-weight': 'bold',
            'text-decoration': 'none'
            }).click(function(){
            tick(_tickerIndex, o.repeat);
            clearInterval(_timer);
            setTimeout(function(){
              _timer = setInterval(function(){tick(_tickerIndex, o.repeat||false); clearTimeout();}, o.speed||5000);
            }, o.speed||5000);
          }).appendTo(_navigation);
          
          
          $('<div></div>').css({
            opacity: 0.85, 
            height: '100%', 
            width: '100%', 
            position: 'absolute', 
            top: 0, 
            left:0,
            background: '#fff',
            'z-index': -1
          }).appendTo(_navigation);

          ticker.append(_navigation);
        }
        
        tick(_tickerIndex);
        _timer = setInterval(function(){tick(_tickerIndex, o.repeat||false);}, o.speed||5000);
        
      });
    }
    
    return this;
   
    
    function tick(index, repeat){
      var item = $('item', _feed).get(index);
      
      if($('.good-ui_ticker-item', _this).length > 0){
        $('.good-ui_ticker-item', _this).animate({
          left: _this.outerWidth()+1,
          opacicty: 0
        }, 1000, 'swing', function(){$(this).remove();});
      }
      
      var link = $('<a></a>').attr({
        target: $('window', item).text(),
        href: $('link', item).text()
      }).text($('text', item).text()).css({
        'line-height': _this.outerHeight()+'px',
        position: 'relative',
        'z-index': 1000
      });
      
      
      _liveItem = $('<div class="good-ui_ticker-item"></div>').css({
        position: 'absolute', 
        left: _this.outerWidth()+1,
        height: _this.height(),
        width: _this.width()-15, 
        'z-index': 999,
        'overflow': 'hidden'
      }).append(link).appendTo(_this).animate({
        left: 15,
        opacity: 1
      }, 2500, 'swing');
      
      $('.good-ui_ticker-current-item', _navigation).text(_tickerIndex+1); 
      _tickerIndex++;
      
      if(repeat && _tickerIndex >= $('item', _feed).length){
        _tickerIndex = 0;
      }
    };
    
    
  };
  
  $.fn.goodTicker.def = {
    feed: '/text/news.xml',
    item: {'element': '<span></span>', css: {}},
    startItem: 1,
    repeat: true,
    opacity: 0.3,
    speed: 10000,
    navigation: true
  };
  
})(jQuery);