function _gel(a){return document.getElementById(a)}
function _uc(a){return a.toUpperCase()}
function _esc(a){return escape(a)}
function _uhc(a,c){_gel(a).value=c;_gel(a).disabled=false}
var k=[];
var p=0;
var n=null;
function h(){
   if(!n){
     n=document.createElement("DIV");
     n.style.display="none";
     n.style.position="absolute";
     n.style.cursor="move";
     n.style.border="2px solid #999999";
     document.body.appendChild(n)
   }
  return n
}
var m={"obj":null,"init":function(a){a.onmousedown=m.start;if(isNaN(parseInt(h().style.left)))h().style.left="0px";if(isNaN(parseInt(h().style.top)))h().style.top="0px";a.onDragStart=new Function();a.onDragEnd=new Function();a.onDrag=new Function()}
,"start":function(a){var c=m.obj=this;a=m.fixE(a);var b=parseInt(h().style.top);var e=parseInt(h().style.left);c.onDragStart(e,b,a.clientX,a.clientY);c.lastMouseX=a.clientX;c.lastMouseY=a.clientY;document.onmousemove=m.drag;document.onmouseup=m.end;return false}
,"drag":function(a){a=m.fixE(a);var c=m.obj;var b=a.clientY;var e=a.clientX;var f=parseInt(h().style.top);var i=parseInt(h().style.left);var j,g;j=i+e-c.lastMouseX;g=f+b-c.lastMouseY;h().style.left=j+"px";h().style.top=g+"px";c.lastMouseX=e;c.lastMouseY=b;c.onDrag(j,g,a.clientX,a.clientY);return false}
,"end":function(){document.onmousemove=null;document.onmouseup=null;m.obj.onDragEnd(parseInt(h().style.left),parseInt(h().style.top));m.obj=null}
,"fixE":function(a){if(typeof a=="undefined")a=window.event;if(typeof a.layerX=="undefined")a.layerX=a.offsetX;if(typeof a.layerY=="undefined")a.layerY=a.offsetY;return a}
};

function _initDrag(a){
  k=a;
  for(var c=0;c<k.length;c++){
    for(var b=0;b<k[c].childNodes.length-1;b++){
        var e=k[c].childNodes[b];
        if(e.nodeType == 3) continue;

        var f=document.getElementById(e.id+"_h");
        f.module=e;
        m.init(f);
        f.onDragStart=function(i,j){
             clearInterval(p);
             var g=this.module;
             r(g);

             g.origNextSibling=g.nextSibling;

             var l=h();

             l.style.left=o(g,true)+"px";
             l.style.top=o(g,false)+"px";
             l.style.height=g.offsetHeight+"px";
             l.style.width=g.offsetWidth+"px";
             l.style.display="block";
             this.dragged=false
             return false;
         };
         f.onDrag=function(i,j){
             t(this.module,i,j);
             this.dragged=true
         };
         f.onDragEnd=function(i,j){
             if(this.dragged){
                p=s(this.module,150,15)
             }else{
               q()
             }
             if(this.module.nextSibling!=this.module.origNextSibling){
               if(this.module.parentNode.id == "save" && this.module.origNextSibling.parentNode.id != "save" ){

                  //adjust scroll windows current usage
                  total_height -= this.module.offsetHeight;

                  add_item_to_saved( this.module );

                  //get some content
                  check_messages(this.module.id);


               } else if( (this.module.parentNode.id != "save" &&  this.module.origNextSibling.parentNode.id == "save" ) ) {

                  //beleted
                  this.module.parentNode.removeChild(this.module);

               }
             }
         }
     }
   }
}

function q(){h().style.display="none"}
function s(a,c,b){var e=parseInt(h().style.left);var f=parseInt(h().style.top);var i=(e-o(a,true))/b;var j=(f-o(a,false))/b;return setInterval(function(){if(b<1){clearInterval(p);q();return}b--;e-=i;f-=j;h().style.left=parseInt(e)+"px";h().style.top=parseInt(f)+"px"},c/b)}
function r(a){
         for(var c=0;c<k.length;c++){
           var b=0;
           for(var e=0;e<k[c].childNodes.length;e++){
             var f=k[c].childNodes[e];
             if(f.nodeType == 3) continue;
            // alert(f);
             if(f==a)
                b=f.offsetHeight;
             f.pagePosLeft=o(f,true);
             f.pagePosTop=o(f,false)-b
           }
         }
}
function o(a,c){var b=0;while(a!=null){b+=a["offset"+(c?"Left":"Top")];a=a.offsetParent}return b}
function t(a,c,b){
         var e=null;
         var f=100000000;
         for(var i=0;i<k.length;i++){
           for(var j=0;j<k[i].childNodes.length;j++){
             var g=k[i].childNodes[j];
             if(g.nodeType==3) continue;
             if(g==a) continue;
             var l=Math.sqrt(Math.pow(c-g.pagePosLeft,2)+Math.pow(b-g.pagePosTop,2));
             if(isNaN(l)) continue;
             if(l<f){
               f=l;e=g
             }
           }
         }

         if(e!=null&&a.nextSibling!=e){

           e.parentNode.insertBefore(a,e);
           u(a);
         }
}
function u(a){a.parentNode.style.display="none";a.parentNode.style.display=""}

function check_messages( id )
{

        var pollSpeed = 5000;
        if( $(id+"_q") ) {

          setTimeout("check_messages('"+id+"')", pollSpeed);

          if( $(id+"_r").style.display == "")
            ATTAP.ajax({url:'/ajax/discuss.cgi',section:'get_qa',skip_auth:1,extra_params:'id='+id+'&lastmod='+$(id+'_lmod').value,handler:handle_get_qa});
        }
}

function add_item_to_saved(el)
{

                  //change the colors of this question
                  // el.firstChild.className='open-chat';


                  //add the content divs
                  var ic = $(el.id+"_c");
                  var html = "<div class='enter-chat'><input type='text' size=40 id='"+el.id+"_q' onKeyPress=\"javascript:if( getkey(event) == 13){ send_chat('"+el.id+"'); return false; }\" >&nbsp;<input type='hidden' id='"+el.id+"_lmod' value=0><img align='center' src='/images/buttons/send.gif' value='chat' onclick=\"send_chat('"+el.id+"')\"></div>";
                      html += "<div id='"+el.id+"_v' class='discussion'></div>";
                  ic.innerHTML = html;

                  //show the rollup icon and the content duvs
                  $(el.id+"_r").style.display="";
                  $(el.id+"_c").style.display="";
                  //new Effect.Appear(el.id+"_c");
}

function send_chat(id, item_id)
{
      if($(id+'_q').value == "") return;		
      ATTAP.ajax({url:'/ajax/discuss.cgi',section:'send_chat',elname:id+'_q',extra_params:'id='+id+'&item_id='+(item_id ? item_id : ''),handler:handle_send_chat});

}

var internal_chat_id = 0;

function handle_send_chat(rs)
{
   switch(rs.response){
   case 700:
     var id = rs.data.id;
     var str = "<a href='/user/"+rs.data.username+"'>"+rs.data.username+"</a>:&nbsp;"+$(id+"_q").value;
     var el  = $(id+"_v");

     add_response(el,str);

     $(id+"_q").value = "";
     //new Effect.Highlight(item.id);

     break;
   default:
     ATTAP.default_handler(rs);
     break;
   };
}


function handle_ask_question(rs)
{
   switch(rs.response){
   case 700:
     var ques = new Array();
     ques.topic_id  = rs.data.id;
     ques.name      = rs.data.name;
     var item = createItem(ques);
     var save = $('save');

     if(save.hasChildNodes() )
           save.insertBefore(item,save.firstChild);
        else
           save.appendChild(item);

     add_item_to_saved(item);
     $('question').value = "";

     //show it
     item.style.display="";

     //get some content
     check_messages(item.id);
     break;
   default:
     ATTAP.default_handler(rs);
     break;
   };
}

function add_response(container,str)
{
     var item = document.createElement("div");
     item.id = container.id + "_" +internal_chat_id++;
     item.innerHTML = "<div>"+str+"</div>";

     if(container.hasChildNodes() )
           container.insertBefore(item,container.firstChild);
        else
           container.appendChild(item);

}

function handle_get_qa(rs)
{
   switch(rs.response){
   case 700:
      var ic   = $(rs.data.id+"_v");
      var lmod = $(rs.data.id+"_lmod");

      //update lastmod time
      lmod.value = rs.data.lastmod;

      var resp = rs.data.responses;
      for(var i=0; i<resp.length; i++){
         add_response(ic,"<a href='/user/"+resp[i].username+"'>"+resp[i].username+"</a>:&nbsp;"+resp[i].msg );
      }

      break;
   default:
      ATTAP.default_handler(rs);
   };
}

function expand_box(id)
{
   var self_id = id+"_r";
   var con_id  = id+"_c";

   if( $(self_id) ){

     new Effect.Appear(con_id);
     $(self_id).innerHTML = "<img border='0' src='/images/min.gif' onclick='collapse_box(\""+id+"\"); return false;'>";
     //get some content
     // check_messages(id);
   }
}

function collapse_box(id)
{
   var self_id = id+"_r";
   var con_id  = id+"_c";

   var self    = $( self_id );

   if( $(self_id) ){
     new Effect.Fade(con_id);
     $(self_id).innerHTML = "<img border='0' src='/images/max.gif' onclick='expand_box(\""+id+"\"); return false;'>";
   }
}
