var T_INDEX=10;

function add_t(text, lifetime) {
  colors = new Array("#797B94","#77C43D","#5E6E60","#AD2919","#547345","#C2D85B","#2CC2BA","#EA34D9","#F04A08","#60BDA1","#1B739A","#D1C03D","#E6BA4E","#D12AA6","#311B74","#0E92DE","#8C21CF","#747692","#752A65","#580589","#D5CF1E","#6D3D29","#1F24F5","#696195","#8403FD","#2EF797","#615BE5","#3BB98E","#90182F","#379E52","#7260FC","#59B6C9","#EE0965","#70F726","#FACCA1","#023BFB","#ABE408","#38FC7F","#EE799E","#47DF5B","#4F25FA","#6DCE4A","#36AF61","#D06ABA","#BFC0DC","#EBD269","#BB3FD9","#3917CB","#37D6BD","#76CE6F","#76934D","#1B6DE3","#958911","#80460C","#A1B6C3","#51727B","#692C03","#3BA4F4","#D9A0AC","#46A1F5","#474691","#BDAAD0","#38389C","#59F0AA","#C92B65","#D22B5D","#0D3A6F","#DD19CD","#970C8C","#E34809","#412910","#55022F","#3245FC","#858A28","#75E4BB","#6FC6D0","#EF0E5B","#7D5DBA","#783CD6","#73BD71","#1E6FE3","#F244EF","#AD7E7E","#DC241A","#8FEE94","#B753FF","#C6DF1F","#90B60B","#D5CA9C","#C33D63","#4E4B3E","#9D6E84","#602648","#AB85D3","#6EDEDE","#EFCFB6","#509418","#5DFFF3","#AA452D","#D3895C");
  
  x = rand(0,600);
  y = rand(30,500);
  
  color = colors[rand(0,colors.length-1)];

  if (lifetime) {
    newobj = "<div id='t_"+T_INDEX+"' onmouseout='this.style.zIndex="+T_INDEX+"' onmouseover='bring_to_front(this)' style='z-index: "+T_INDEX+"; cursor: default; width: 150px; background-color: #fafafa; border: 1px solid #EAEAEA; padding: 3px; color: "+color+"; margin-left: "+x+"; margin-top: "+y+"; position: absolute;'>"+text+"</div>";
  }
  else {
    newobj = "<div id='t_"+T_INDEX+"' onmouseout='this.style.zIndex="+T_INDEX+"' onmouseover='bring_to_front(this)' style='z-index: "+T_INDEX+"; cursor: default; width: 150px; background-color: #fafafa; border: 1px solid #EAEAEA; padding: 3px; color: "+color+"; margin-left: "+x+"; margin-top: "+y+"; position: absolute; opacity: 0; alpha(opacity=0); '>"+text+"</div>";
  }
 
  document.getElementById('thoughts_div').innerHTML += newobj;
  
  if (!lifetime) {
    changeOpac(0, "t_"+T_INDEX);
    opacity ("t_"+T_INDEX, 0, 100, 2000);
    setTimeout ("clear_t('t_"+T_INDEX+"')", 100000);
  }
  else {
    setTimeout ("clear_t('t_"+T_INDEX+"')", lifetime);
  }
  
  T_INDEX++;
}

function rand(a,b) {
  n = Math.random();
  n = a + n * b;
  n = Math.round(n);
  return n;
}

function bring_to_front (e) {
  e.style.zIndex = T_INDEX;
}

function clear_t (id) {
  opacity(id, 100, 0, 2500);
}

function opacity(id, opacStart, opacEnd, millisec) { 
    //speed for each frame 
    var speed = Math.round(millisec / 100); 
    var timer = 0; 

    //determine the direction for the blending, if start and end are the same nothing happens 
    if(opacStart > opacEnd) { 
        for(i = opacStart; i >= opacEnd; i--) { 
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed)); 
            timer++; 
        } 
    } else if(opacStart < opacEnd) { 
        for(i = opacStart; i <= opacEnd; i++) 
            { 
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed)); 
            timer++; 
        } 
    } 
} 

//change the opacity for different browsers 
function changeOpac(opacity, id) { 
    var object = document.getElementById(id).style; 
    object.opacity = (opacity / 100); 
    object.MozOpacity = (opacity / 100); 
    object.KhtmlOpacity = (opacity / 100); 
    object.filter = "alpha(opacity=" + opacity + ")"; 
} 

function get_new_t () {
  makeGetRequest (SITE_URL + "scripts/thoughts.php?doc=1&t="+unixTime(), 'get_new_t2');
}

function get_new_t2 (r) {
  if (r != " ") {
    add_t(r, 0);
  }
  setTimeout ("get_new_t()", 5000);
}


function submit_thought(f) {
  makeGetRequest(this_page(false, "t="+f.thought.value), "msg");
  f.thought.value = '';
  return false;
}