var last;

function getObjID(objID) {
var obj = document.getElementById(objID);
return obj;
}

function insertAtCursor(myField, myValue) {
  if (myField !== null)
  {
    var space=" ";
    myValue=space + myValue;
    if (document.selection) {
    myField.focus();
    var sel = document.selection.createRange();
    sel.text = myValue;
    }
    else if (myField.selectionStart || myField.selectionStart === '0') {
      var startPos = myField.selectionStart;
      var endPos = myField.selectionEnd;
      myField.value = myField.value.substring(0, startPos) + myValue + myField.value.substring(endPos, myField.value.length);
    } else {
      myField.value += myValue;
    }
  }
}


function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft;
			obj = obj.offsetParent;
		}
	}
	else if (obj.x) 
  {
		curleft += obj.x;
	}
  return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
//	var printstring = '';
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
//			printstring += ' element ' + obj.tagName + ' has ' + obj.offsetTop;
			curtop += obj.offsetTop;
			obj = obj.offsetParent;
		}
	}
	else if (obj.y) 
  {
		curtop += obj.y;
  }
//	window.status = printstring;
	return curtop;
}


function getObj(name)
{
 if (document.getElementById)
 {
	   this.obj = document.getElementById(name);
	   this.style = document.getElementById(name).style;
 }
 else if (document.all)
 {
	   this.obj = document.all[name];
	   this.style = document.all[name].style;
 }
 else if (document.layers)
 {
	   if (document.layers[name])
	   {
	   	this.obj = document.layers[name];
	   	this.style = document.layers[name];
	   }
	   else
	   {
	    this.obj = document.layers.testP.layers[name];
	    this.style = document.layers.testP.layers[name];
	   }
 }
}

function showLinks(lyr, obj) {
	var newX = findPosX(obj) - 7;
	var newY = findPosY(obj) + 12; 
//	newY += 5;
	var x = new getObj(lyr);
	x.style.top = newY + 'px';
	x.style.left = newX + 'px';
	x.style.display='block'; 
}

function hideLinks(lyr) {
document.getElementById(lyr).style.display="None";
}

function holdLinks(lyr) {
	var x = new getObj(lyr);
	x.style.display='block'; 
}


function showReg(lyr, obj) {
	var newX = findPosX(obj) + 0;
	var newY = findPosY(obj) + 12;
//	newY += 5;
	var x = new getObj(lyr);
	x.style.top = newY + 'px';
	x.style.left = newX + 'px';
	x.style.display='block';    
//document.all[lyr].style.height='210px'; 
//document.all[lyr].style.width='83px';
}


function holdReg(lyr) {
	var x = new getObj(lyr);
	x.style.display='block'; 
}


/* small() makes selected layer shorter (height property)*/
function hideReg(lyr) {
	var x = new getObj(lyr);
	x.style.display='none'; 
}


function followup0(lyr, objID) {
	var obj = getObjID(objID);
	var newX = findPosX(obj) + 2;
	var newY = findPosY(obj)+1;
	var x = new getObj(lyr);
	x.style.top = newY + 'px';
	x.style.left = newX + 'px';
}


function followup(lyr, obj) {
	var newX = findPosX(obj) + 2;
	var newY = findPosY(obj)+1;
	var x = new getObj(lyr);
	x.style.top = newY + 'px';
	x.style.left = newX + 'px';
}



function resize ()
{
//document.pinned.style='"height:20px; width:'+divwidth2+'px; overflow:hidden"';
  if ($("#pinned1").length>0 && $("#cell").length>0) {
    $("#pinned1").css('width', '20px');
    $("#pinned2").css('width', '20px');

	var WidthOfCell1=document.getElementById("cell").offsetWidth-20;
//    var WidthOfCell1=$("#cell").offsetWidth-20;
    var WidthOfCell2=WidthOfCell1+8;
    $("#pinned1").css('width', WidthOfCell1+'px');
    $("#pinned2").css('width', WidthOfCell2+'px');
  }
//	document.all.pinned1.style.width=WidthOfCell1+'px';
//	document.all.pinned2.style.width=WidthOfCell2+'px';
}


$(function(){
    var treeOpen = true; 
   
    $(".mbar-link-sortable").sortable({axis: 'x', revert:true, tolerance: 'pointer' });
       
    $('.newPost .treesign').click(function(){
       $(this).parent().find('.followups').slideToggle();
       if ($(this).attr("src") == "images/close-tree.png")
       {
         $(this).attr("src","images/open-tree.png");
       } else {
         $(this).attr("src","images/close-tree.png");    
       }
    });
   
});

 
