(function($) {

   $.fn.transictionto = function(options) {
      var settings = $.extend({
   }, options || {});
   //wrap into div if no div is present.
   $(this).each(function() {
      if ($(this).parent('div').size() == 0) {
         $(this).wrap('<div></div>')
      }
      //now swap with background trick
      $(this)
      .parent()
         .css('background-image', 'url(' + settings.destinationImage + ')')
         .css('background-repeat', 'no-repeat')
      .end()
      .fadeOut(500, function() {
         this.src = settings.destinationImage;
         $(this).show();
      });
   });
};
})(jQuery);

(function($) {
    // Default configuration properties.
    var defaults = {
    }, windowLoaded = false;

    $(window).bind('load.jnewsticker', function() { windowLoaded = true; });

    /**
     * The jNewsticker object.
     *
     * @constructor
     * @class jnewsticker
     * @param e {HTMLElement} The element to create the carousel for.
     * @param o {Object} A set of key/value pairs to set as configuration properties.
     * @cat Plugins/jNewsticker
     */
    $.jnewsticker = function(e, o) {
        this.options    = $.extend({}, defaults, o || {});

        this.container = $(e);
        this.selectedContainer = $(e).find('#selected');
        this.selectedImage = $(e).find('#selected_img');
        this.selectedText = $(e).find('#tickertext');
        this.items = $(e).find('.selector');
		this.currentIndex = 0;
		this.timeout = -1;
        

        if (!windowLoaded && $.browser.safari) {
			var self = this;
            $(window).bind('load.jnewsticker', function() { self.setup(); });
        } else {
            this.setup();
        }
        
    };
    
    // Create shortcut for internal use
    var $jn = $.jnewsticker;

    $jn.fn = $jn.prototype = {
    	jnewsticker: '0.1'
    };

    $jn.fn.extend = $jn.extend = $.extend;

    $jn.fn.extend({
        /**
         * Setups the jnewsticker.
         *
         * @method setup
         * @return undefined
         */
        setup: function() {
            
            
            var me = this;
            $(this.items).mouseenter(
            		function(e) {
            			$(me.selectedImage).attr('src', $(e.currentTarget).children('.selector_image').children().first().children().first().attr('src'));
            			$(me.selectedImage).parents('a').first().attr('href', $(e.currentTarget).children('.selector_image').children().first().attr('href'));
            			$(me.selectedText).html($(e.currentTarget).children('.selector_text').html());
            			
						// reset active state
						$(me.items).removeClass('active');
						$(e.currentTarget).addClass('active');

						me.currentIndex = $(me.items).index(e.currentTarget);

						// reset timesequence
						me.resetTimesequence();
		            }
            	);

            $(this.items).mouseleave(
		            function(e) {
						// start timesequence
						me.activateTimesequence();
		            }
	            );

			var item = $(this.items).eq(this.currentIndex);
			$(this.selectedImage).transictionto({ destinationImage: $(item).children('.selector_image').children().first().children().first().attr('src') });
			$(this.selectedImage).parents('a').first().attr('href', $(item).children('.selector_image').children().first().attr('href'));
			$(this.selectedText).html($(item).children('.selector_text').html());
			$(this.items).removeClass('active');
			$(item).addClass('active');

			this.activateTimesequence();
        },

		activateTimesequence: function() {
			var items = this.items;
			//var item = $(this.items).eq(this.currentIndex + 1);
			var currentIndex = this.currentIndex;
			var selectedContainer = this.selectedContainer;
			var selectedImage = this.selectedImage;
			var selectedText = this.selectedText;

			this.timeout = window.setInterval(
				function() {
					currentIndex++;
					if(currentIndex >= $(items).size()) {
						currentIndex = 0;
					}
					var item = $(items).eq(currentIndex);

					$(selectedImage).transictionto({ destinationImage: $(item).children('.selector_image').children().first().children().first().attr('src') });
					$(selectedImage).parents('a').first().attr('href', $(item).children('.selector_image').children().first().attr('href'));
					$(selectedText).html($(item).children('.selector_text').html());
					
					$(items).removeClass('active');
					$(item).addClass('active');
				},
				10500
			);
		},

		resetTimesequence: function() {
			window.clearInterval(this.timeout);
		}
    });
    
    
    $.fn.jnewsticker = function(o) {
        if (typeof o == 'string') {
            var instance = $(this).data('jnewsticker'), args = Array.prototype.slice.call(arguments, 1);
            return instance[o].apply(instance, args);
        } else {
            return this.each(function() {
                $(this).data('jcarousel', new $jn(this, o));
            });
        }
    };
    
})(jQuery);

