// JavaScript Document

var timer = 6;
var timer = timer * 1000;
var term;
var time;
//windowing variables
var id = 0;

var left = 100;
var top = 135;



//START CLOUDS//////////////////////////////////////////////////////
function creatediv(html) {

   var newdiv = document.createElement('div');
   newdiv.setAttribute('id', id);
   

  
   
   if ((left || top) || (left && top)) {
       newdiv.style.position = "absolute";
	   newdiv.style.display = "none";
      

       if (left) {
		   lpos = getRand(1 ,70);
           newdiv.style.left =  lpos + "%";
       }
       
       if (top) {
		   tpos = getRand(20, 55);
           newdiv.style.top =   tpos + "%";
       }
   }
   
  
   newdiv.style.paddingTop = "40";
   newdiv.innerHTML = '<small><img src="drag.png" id=drag /></small><div class="speak" id="speak' + id +'" ></div>';
   
  /* if (html) {				//add ajax here to
       newdiv.innerHTML = newdiv.innerHTML+html;
   } else {
       newdiv.innerHTML = "nothing";
   }*/
   
   document.body.appendChild(newdiv);
     $("#" + id).draggable();
         jQuery(function($){		 
        $("#speak" + id).tweet({
            avatar_size: 28,
            count: 1,
			fetch:4,
			query: term,
			template: "{avatar} {time} {text} <br /> {reply_action} - {retweet_action} ",
            loading_text: "Listening..."
        }).bind("empty", function() { $(this).append("<br /> <br />Nothing was found out there. Maybe you should write something!"); });
//alert(term);
		$('#speak'+id).fadeIn('slow');
    });
	 
	 //ajax('theysay.php?term=' + term, 'speak' + id ,'POST');
	 $('#'+id).fadeIn('slow');
  id++;
}
///////////////END CLOUDS////////////////////////////////////////////////////////////
 function getRand(min, max)
			{		
				var randomNum = Math.random() * (max-min); 

				// Round to the closest integer and return it
				return(Math.round(randomNum) + min); 
			}
//posting object for a form. Something is wrong with this, it double submits. 
function post(obj) {
      var poststr = "user=" + encodeURI( document.getElementById("user").value ) +
                    "&pass=" + encodeURI( document.getElementById("pass").value ) +
					"&name=" + encodeURI( document.getElementById("name").value ) +
					 "&id=" + encodeURI( document.getElementById("id").value ) +
					"&twitter_msg=" + encodeURI( document.getElementById("twitter_msg").value );
      ajax('auth.php?' + poststr,'reply','POST');
   } 
   
 //the loop  
function start(){

	
	if(term != ""){
		get();
		disapate();
		time = setTimeout('start()', timer);		
	}
	else{
		term = document.getElementById('term').value = "#OWS";
		time = setTimeout('start()', timer);
	}
}
function colisionDetect(){
		
	
	
}
//grabs the search field and submits it
function get() {
	
      term = document.getElementById('term').value
      creatediv();
   }
   
function disapate()
{
	
	if(id >= 8){
		dis = id - 8;
		closeIt(dis);	
		
	}
}

//closes open clouds
function closeIt(DivId){
	
	if(isNaN(DivId)){$('#'+DivId).fadeOut('slow');}
	
	else{
	Div = document.getElementById(DivId);
	Div.innerHTML = '';
	wrap = document.getElementById("wrap");
	document.body.removeChild(Div);
	}
	
}


