/**
 * Well look at you all curious :)
 * 
 * @link http://twiteye.com
 * 
 * @copyright Ambient Age, 2009 (http://www.ambientage.com)
 * @author  @davkell (www.davidkelly.ie)
 * 
 * @version 2.1
 * 
 */

var ambient = {
	ideas : new Array(),
	count: 0,
	numIdeas: 0,
	intervalId: null,
	since: null,
	token: null
};
	
/**
 * doIt
 * do a wee spot of animation to keep things ticking over
 * 
 */
	function doIt(element){
		
		var id = '#' + $(element).attr('id');
		
		$(id).queue(function(){
			$(this).slideDown('fast')	
			.animate({marginTop: "+=30px"}, {
				duration: 1000,
				easing: 'linear'
			})
			.dequeue();
			++ambient.count;
			ambient.numIdeas; 
			clearSome();
		});
		
	} // doIt
	
	
/**
 * popOne
 * pop an idea off the stack and share it with the world
 */
	function popOne(){
		var anIdea = ambient.ideas.pop();
		if (anIdea) {
			doIt(anIdea);
		}
	}

/**
 * clearSome
 * Do a little clean up
 */
	function clearSome(){

		if (ambient.count == ambient.numIdeas ) {
			var thatsAll = '';
			thatsAll += '<div  id="thats-all" style="display: block; margin-top: 1em; text-align: center" class="idea done">';
			thatsAll += '<div class="message">That is all the ideas I have for you for now....hopefully it will help get you started!</div></div>';
			$('#ideas').prepend(thatsAll).slideDown('slow');
			clearInterval(ambient.intervalId);
			ambient = null;
		}
	} // clearSome
	
	
$(document).ready(function(){

// Give me ideas!!
	inspireMe();
	
/**
 * Nip off and grab some inspiration
 */
 function inspireMe(){
 	
	ambient.token = $('body').attr('class');
 	var url = '/index/givemeideas/'; 
	$.getJSON( 
		url,
		{
			token: ambient.token
		},
		function(data, textStatus){
			
			if (data) {
				switch (textStatus) {
					case 'success':
						var greatIdeas = data.length;
						var out = '';
						for (z = 0; z < greatIdeas; z++) {
							out += '<div  id="' + data[z].id + '" class="idea ' + data[z].type + '">';
							out += '<div class="message">' + data[z].tweet + '<span class="tweet-link">&nbsp; [<a href="' + data[z].permalink + '">...who asked?</a>]&nbsp;</span></div></div>';
						}
						$('#ideas').append(out);
						$('#holding').slideUp('slow').remove();
						$('#ideas .idea').hide();
						dropMe();
						break;
					default: // error msg if not success
						var thatsAll = '';
						thatsAll += '<div  id="thats-all" style="display: block; margin-top: 1em; text-align: center" class="idea done">';
						thatsAll += '<div class="message">Ooops, we seem to have had an error while getting ideas for you. Very sorry!</div></div>';
						$('#ideas').prepend(thatsAll).slideDown('slow');
						clearInterval(ambient.intervalId);
						break;
						
				}
			}
			else{
				var thatsAll = '';
					thatsAll += '<div  id="thats-all" style="display: block; margin-top: 1em; text-align: center" class="idea done">';
					thatsAll += '<div class="message">Oops, we seem to have had an error while getting ideas for you. Very sorry!</div></div>';
					$('#holding').fadeOut('fast');
					$('#ideas').prepend(thatsAll).slideDown('slow');
				clearInterval(ambient.intervalId);
			}

		}	
	);
 } // inspireMe
 
 
 /**
  * dropMe
  * Add them all to something more usable
  */	 
	function dropMe(){
		$('.idea').each( function(){
			ambient.ideas.push($(this));
		});
		ambient.numIdeas = ambient.ideas.length;	
	} // dropMe
 

 // get the ball rolling...
	popOne(); 
});
	
// keep the ball rolling...
	ambient.intervalId = setInterval('popOne();', '4000');
