/**
* Javascipt // JQuery
* animation script
* for: 'Lanit'
* Webway company - 2011
*/

function makeWordsFly(contSelector, wordArray, globalTime){

	var 
		cont = $(contSelector).eq(0),
		contHeight = cont.height(),
		minTop = contHeight * .1,
		maxTop = contHeight * .8,
		contWidth = cont.width(),
		words = wordArray,
		wordCount = words.length,
		globalTime = globalTime || 5000,
		elements = $();
	
	$(window).bind('resize', function(){
		contWidth = cont.width();
	});
	
	if(!cont || !$.isArray(wordArray)) return;
	
	for(var i=0; i<words.length; i++){
		var word = $('<span class="anim_element anim_element_' + (i+1) + '" />').text(words[i]);
		elements = $.merge(elements, word);
	};
	cont.append(elements);
	
	var firstElement = elements.first();
	
	function getReady(element){
		element
			.css({
					'position' : 'absolute',
					'left' : 0,
					'top' : Math.random() * (maxTop - minTop-element.height()) + minTop
				})
			.animate({'opacity' : 0}, -1)
	}
	
	function animateWord(element){
		element	
			.animate(
				{
					'left' : contWidth/2,
					'opacity' : 1
				}, 
				{
					duration: globalTime/2,
					specialEasing: {
						'left': 'easeInQuad',
						'opacity': 'easeInSine'
					}
				}
			)
			.animate(
				{
					'left' : contWidth,
					'opacity' : 0
				}, 
				{
					duration: globalTime/2,
					complete: function(){
						getReady(element);
						animateWord(element.next().length ? element.next() : firstElement);
					},
					specialEasing: {
						'left': 'easeOutSine',
						'opacity': 'easeOutQuint'
					}
				}
			);
	}

	elements.each(function(){
		var $word = $(this);
		$word
			.css({
				'position' : 'absolute',
				'display' : 'block',
				'left' : 0,
				'white-space' : 'nowrap',
				'top' : Math.random() * (maxTop - minTop-$word.height()) + minTop
			})
			.animate({'opacity' : 0}, -1)
	});
	
	animateWord(firstElement);
	
}


