$(document).ready(function () {
	
	$("#push_bubble").lwaFlash({parent:".pushCont_Bubble",fadecontainer:true})
	$("#box_Grey_Push_1").lwaFlash({parent:".box_Grey_Push",timeout:6000,nav:true})
	
	$(".tweet").tweet({
		username: "LaiosWebbAnalys",
		join_text: "auto",
		avatar_size: 48,
		count: 2,
		auto_join_text_default: "",
		auto_join_text_ed: "we",
		auto_join_text_ing: "we were",
		auto_join_text_reply: "we replied to",
		auto_join_text_url: "we were checking out",
		loading_text: "Laddar tweets..."
	});
});

(function($){
	$.addFlash = function(id,options) {
		
		var innerTimer,mainTimer
		var s = $.extend({
			parent: ""	,
			timeout: 15000,
			nav: false,
			fadecontainer : false
		},options);
		
		var f = {
			id : $(id).attr("id"),
			counter : 0,
			total : 0,
			innerTimeout : 700,
			parent : $(id).parent(s.parent),
			init : function() {
				var init_counter
				$(id).find("li")
					.hide()
					.each( function() {
						if (init_counter == null) {
							init_counter = 0
						} else {
							init_counter ++
						}
						$(this).attr({"id":f.id+"_li_"+init_counter})
				});
				f.total = init_counter
				
				if (s.nav) {
					var nav_next = $(f.parent).find(".flash_next")
					var nav_prev = $(f.parent).find(".flash_prev")
					
					nav_next.bind("click", function() {
						f.stop()
						f.timer()
					});
					
					nav_prev.bind("click", function() {
						f.stop()
						f.counter = f.counter - 2
						f.timer()
					});
				};
				
				f.parent.bind("click", function() {
					f.stop()
					f.timer()
				});
				
				if (s.fadecontainer) {
					f.innerTimeout = 1000;
					
				};
				
				f.start();
			},
			start : function() {
				f.timer()
			},
			stop : function() {
				clearTimeout(mainTimer)
				clearTimeout(innerTimer)
			},
			timer : function() {
				
				if (f.counter < 0) {
					f.counter = f.total
				};
				if (f.counter > f.total) {
					f.counter = 0
				}
				
				if (s.fadecontainer) {f.parent.fadeOut("slow")}
				else {$(id).find("li").fadeOut("fast")}
				innerTimer = setTimeout(function() {
					if (s.fadecontainer) {
						$(id).find("li").hide()
						$("#"+f.id+"_li_"+f.counter).show()
					} else {
						$("#"+f.id+"_li_"+f.counter).fadeIn("fast")
					}
					f.counter ++
					if (s.fadecontainer) {f.parent.fadeIn("slow")}
				}, 700);
				
				mainTimer = setTimeout(function() {
					f.timer()
				}, s.timeout)
			}
		};
		
		f.init();
		
		$(id).show()
		
		
		
	};
	
	
	
	var docloaded = false;
	$(document).ready(function () {docloaded = true} );
	
	$.fn.lwaFlash = function(options) {
		return this.each( function() {
			if (!docloaded)
			{
				$(this).hide();
				var id = this;
				$(document).ready
				(
					function ()
					{
					$.addFlash(id,options);
					}
				);
			} else {
				$.addFlash(this,options);
			}
		});
	};
})(jQuery);
