/*
	Copyright Robert Nyman, http://www.robertnyman.com
	Free to use if this text is included
	// modified slightly based on Prototype
*/
function getStyle(oElm, strCssRule){
    var strValue = "";
    if(document.defaultView && document.defaultView.getComputedStyle){
        var css = document.defaultView.getComputedStyle(oElm, null);
        strValue = css ? css.getPropertyValue(strCssRule) : null;
    }
    else if(oElm.currentStyle){
        strCssRule = strCssRule.replace(/\-(\w)/g, function (strMatch, p1){
            return p1.toUpperCase();
        });
        strValue = oElm.currentStyle[strCssRule];
    }
    return strValue;
}


function Draggable(el)
{
  var yDelta = 0;
  var yStart = yStop = 0;
  var elStop = elStart = inertDist= currDist = 0;
  var speed=0;
  var startTime, endTime;

  // remove the events
  function enddrag()
  {
    document.onmouseup = null;
    document.onmousemove = null;
	if(elStop) inertDist=(elStop-elStart)/4;
	currDist=0;
	if(inertDist > 0) inertDist = Math.ceil(inertDist);
	    else inertDist = Math.floor(inertDist);
		
	/*endTime=new Date();
	//$('test').innerHTML+=(endTime-startTime)+"; ";
	dragInt=setInterval(function(){
				if (inertDist!=currDist)
				{
					if (inertDist>0)
					{
						el.style.top=parseInt(el.style.top)+1;
						currDist++;
					}else 
					{
						el.style.top=parseInt(el.style.top)-1;
						currDist--;
					}
					//$('test').innerHTML+=inertDist+", "+currDist+"; ";
				} else clearInterval(dragInt);
			},1);
	*/
  }

  // fire each time it's dragged
  function drag(e)
  {
    e = e || window.event;
    yDelta = yStart - parseInt(e.clientY);
    yStart = parseInt(e.clientY);

	if (yDelta>0)
	{
			if ((parseInt(el.style.top) - yDelta)>30)
			{
				el.style.top = (parseInt(el.style.top) - yDelta) + 'px';
				elStop=parseInt(el.style.top);
			}else
			{
				el.style.top ='0px';
				elStop=0;
			}
	} else
	{
		if (absPosition($('bottomLine')).y<absPosition($('deadLine')).y-50)
		{
			el.style.top = (parseInt(el.style.top) - yDelta) + 'px';
			elStop=parseInt(el.style.top);
		}else
		{
			while (absPosition($('bottomLine')).y<=absPosition($('deadLine')).y-16)
				el.style.top=parseInt(el.style.top)+1+"px";
			elStop=0;
		}
	}
  }

  // initiate the drag
  function md(e)
  {
	var srcEl = window.event ? window.event.srcElement : e.target;
	if (srcEl.getAttribute('dragger')!='true') return false;
	e = e || window.event;
    yStart = parseInt(e.clientY);
    elStart = parseInt(el.style.top);
	startTime = new Date();
	el.style.top = parseInt(getStyle(el,'top')) + 'px';
    document.onmouseup = enddrag;
    document.onmousemove = drag;
    return false;
  }

  // tie it into the element
  el.onmousedown = md;

}
