var news = function (){
	var nrOfItemsVisible = 5;
    function init(){
		bubbleFountain.bubble($('#bubbleContainer'));

		$(window).scroll(onWindowScroll);

		$('#showMore').live('click', onShowMore);

		$('li.listNewsItem').each(function(i){
			var el = $('li.listNewsItem').eq(i);

			var img;
			var size;
			var max = 8;

			for(var i = 0, l = Math.floor(Math.random()*max)+1; i < l; i++){
				img = $('<img src="/img/bel.png" class="bell">');
				size = (0.3 + (Math.random() * 0.7)) * 54;
				img.width(size);
				img.height(size);
				img.css({top : Math.round(Math.random()*el.height()), left: Math.round(Math.random()*el.width())});
				el.append(img);
			}

			// set random position
			var r = Math.floor(Math.random()*150);
			el.css({
				'position' : 'relative',
				'left' : r + 'px',
				'top' : 0
			});

			el.attr('py', 0);
			el.attr('px', r);

			var newpos = calcNewPosition(el);

			el.animate(
				{
					'top' : newpos.top,
					'left' : newpos.left
				},
				500, 'swing', animate
			);
	
			// hide dialogue links surplus
			if(i >= nrOfItemsVisible && $('li.listNewsItem').length > nrOfItemsVisible){
				el.hide();
			}
		});
		if($('li.listNewsItem').length > nrOfItemsVisible){
			//$('<a href="" id="showMore" style="font-size:0.75em;">Toon meer</a>').insertAfter($('.listNews'));
		}
    }

	function animate(e){
		var el = $(this);
		var newpos = calcNewPosition(el);
		el.animate(
			{
				'top' : newpos.top,
				'left' : newpos.left
			},
			500, 'swing', animate
		);
	}

	function calcNewPosition(el){
		var max = 5;
		var posx = parseInt(el.css('left'));
		var posy = parseInt(el.css('top'));

		if(posx > el.attr('px')){
			posx = parseInt(el.attr('px')) - Math.floor(Math.random()* max);
		} else {
			posx = parseInt(el.attr('px')) + Math.floor(Math.random()* max);
		}

		if(posy > el.attr('py')){
			posy = parseInt(el.attr('py')) - Math.floor(Math.random()* max);
		} else {
			posy = parseInt(el.attr('py')) + Math.floor(Math.random()* max);
		}

		return {left: posx, top: posy};
	}

    function onWindowScroll(){
        // is the scrollbar on the bottom?
        var root = document.documentElement ? document.documentElement : document.body;
        var h = root.scrollHeight;
        var p = root.scrollTop;
        var ch = root.clientHeight;
        if((h - p) == ch){
            onShowMore();
        }
    }

	function onShowMore(e){
		if(e){
			e.preventDefault();
		}
		var t = $('li.listNewsItem:visible').length;
		$('li.listNewsItem').each(function(i){
			if(i >= t && i < t+nrOfItemsVisible){
				$('li.listNewsItem').eq(i).show();
			}
		});
		if(t + nrOfItemsVisible >= $('li.listNewsItem').length){
			$('#showMore').hide('normal');
		}
	}

    return {
		'init' : init
	}
}();

var bubbleFountain = function(){
	var bubbleContainer = null;
	var bubbleInterval = null;
	var bubbleSpeed = 100; // pixels / seconde

	function bubble(container){
		bubbleContainer = container;

		if(bubbleInterval){
			clearInterval(bubbleInterval);
		}

		bubbleInterval = setInterval(addBubbles, 500);
	}

	function addBubbles(){
		var img = $('<img src="/img/bel.png" class="bell">');
		size = (Math.random() * 0.2) * 54;
		img.width(size);
		img.height(size);
		img.css({left:Math.floor(Math.random()*500)});

		var h = $(window).height() + 200;

		img.animate({
				top: -1 * h,
				width: 40,
				height: 40
			},{
				duration : (h / bubbleSpeed) * 1000,
				easing : 'easeInQuad',
				complete : function() {
					bubbleContainer.remove($(this));
				}
			}
		);

		bubbleContainer.append(img);
	}

	return {
		bubble : bubble
	}
}();

$(document).ready(function(){
    news.init();
});
