;(function($) {
	// Enable strict mode
	"use strict";
	
	$.fn.ikTicker = function(options)
	{
		// ticker
		var ticker = function(container, config) {
			
			var app = {};
			app.el = {};
			app.timeout = null;
			
			// init options
			app.cfg = config;
			app.cfg.startDelay = parseInt(config.startDelay) > 0 ? parseInt(config.startDelay) : app.cfg.changeDelay;
			
			app.el.container = $(container);
			app.el.items = app.el.container.find(app.cfg.itemSelector);
			
			app.change = function() {
				if (app.el.container.data("ikTicker-Paused")) return;
				var active = app.el.items.filter("." + app.cfg.activeClass);
				var nextActive = active.next();
				
				if (!nextActive.length) nextActive = app.el.items.first();
				// effect chain
				active.fadeOut(app.cfg.effectTimeHide, function() {
					$(this).removeClass(app.cfg.activeClass).show();
					nextActive.hide(0).addClass(app.cfg.activeClass).delay(app.cfg.inbetweenDelay).fadeIn(app.cfg.effectTimeShow);
				});
				if (app.el.container.data("ikTicker-FirstRun")) {
					app.el.container.data("ikTicker-FirstRun", false);
				}
				app.timeout = setTimeout(function(){ app.change(); }, app.cfg.startDelay);
			};
			
			app.pause = function() {
				var active = app.el.items.filter("." + app.cfg.activeClass);
				active.stop(true).fadeIn(0);
				app.el.container.data("ikTicker-Paused", true);
				if (app.timeout) clearTimeout(app.timeout);
			};
			
			app.resume = function() {
				if (app.el.container.data("ikTicker-Mobile")) return;
				
				var active = app.el.items.filter("." + app.cfg.activeClass);
				if (!active.length || active.length > 1) {
					app.el.items.removeClass(app.cfg.activeClass);
					active = app.el.items.first().hide().addClass(app.cfg.activeClass).delay(400).fadeIn(app.cfg.effectTimeHide);
				}
				
				app.el.container.data("ikTicker-Paused", false);
				if (app.timeout) clearTimeout(app.timeout); // just to be sure
				var delay = (app.el.container.data("ikTicker-FirstRun")) ? app.cfg.changeDelay : app.cfg.startDelay;
				app.timeout = setTimeout(function(){ app.change(); }, delay);
			};
			
			app.switchListModus = function(bMobile) {
				if (bMobile && (app.el.container.data("ikTicker-Mobile") === undefined || app.el.container.data("ikTicker-Mobile") === false))
				{
					app.el.container.data("ikTicker-Mobile", true);
					app.pause();
					app.el.items.show();
					app.el.container.addClass("static");
				}
				if (!bMobile &&  (app.el.container.data("ikTicker-Mobile") === undefined || app.el.container.data("ikTicker-Mobile") === true))
				{
					app.el.container.data("ikTicker-Mobile", false);
					app.el.container.removeClass("static");
					app.el.items.hide();
//					app.el.items.filter("." + app.cfg.activeClass).show();
					app.resume();
				}
			};
			
			if (!app.el.items.length) { return; }
			
			app.el.container.data("ikTicker-Initialized", true);
			app.el.container.data("ikTicker-Paused", false);
			app.el.container.data("ikTicker-FirstRun", true);
			
			// bind hover events, to pause & resume ticker
			app.el.items.hover( function(){app.pause()}, function(){app.resume()} );
			
			// event is also fired on first load
			$(window).on("dynscale-change", function(event, modus, previousModus) {
				if (modus >= app.cfg.dynscaleMobileModus && (!previousModus || previousModus < app.cfg.dynscaleMobileModus)) {
					app.switchListModus(true);
				}
				else if (modus < app.cfg.dynscaleMobileModus && (!previousModus || previousModus >= app.cfg.dynscaleMobileModus)) {
					app.switchListModus(false);
				}

			});
			
		};
		
		// Default options.
        var config = $.extend({
        	effectTimeShow: 0,
        	effectTimeHide: 1000,
        	changeDelay: 5500,
        	startDelay: null,
        	inbetweenDelay: 100,
        	itemSelector: "> *",
        	activeClass: "active",
        	dynscaleMobileModus: 5
        }, options );
		
		return this.each(function() {
	        ticker(this, config);
	    });
		
	};

})(jQuery);
