// fdrlib.js - version 1.0
// FadeDropRaise Library for Javascript
// Nick O'Neill 2005

function fadeElementIn(id,time,begin){
 // <a href="javascript:fadeElementIn(myelementid,0.3)">fade in!</a>
 // ---- SET THESE IN YOUR SCRIPTS
 // id is the id of the element you're fading
 // time is the amount of time you'd like your element to spend fading in (try 0.3)

 // ---- DON'T SET THESE IN YOUR SCRIPTS
 // begin is the start time of your fade - it will be set automatically when the function is called

 var elem = getByID(id);

 var current = new Date().getTime();
 if (typeof(begin) == "number"){}
 else {begin = new Date().getTime()}

 var timeDiff = (current - begin) / 1000; // /

 if (timeDiff < time){

 var boxOpacity = (timeDiff/time)*(timeDiff/time);

 setOpacity(elem.id,boxOpacity);

 setTimeout("fadeElementIn(\'"+id+"\',"+time+","+begin+")",16);
 } else {setOpacity(elem.id,.99);return}
}

function fadeElementOut(id,time,begin){
 // <a href="javascript:fadeElementOut(myelementid,0.3)">fade out!</a>

 // ---- SET THESE IN YOUR SCRIPTS
 // id is the id of the element you're fading
 // time is the amount of time you'd like your element to spend fading out (try 0.3)

 // ---- DON'T SET THESE IN YOUR SCRIPTS
 // begin is the start time of your fade - it will be set automatically when the function is called

 var elem = getByID(id);

 var current = new Date().getTime();
 if (typeof(begin) == "number"){}
 else {begin = new Date().getTime()}

 var timeDiff = (current - begin) / 1000; // /

 if (timeDiff < time) {

 var boxOpacity = .99-((timeDiff/time)*(timeDiff/time));

 setOpacity(id,boxOpacity);

 setTimeout("fadeElementOut(\'"+id+"\',"+time+","+begin+")",16);
 } else {setOpacity(id,0);return}
}

function raiseElementUp(id,ascent,time,begin,modAscent,topOrig){
 // <a href="javascript:raiseElementUp(myelementid,-100,0.4)">raise up!</a>

 // ---- SET THESE IN YOUR SCRIPTS
 // id is the id of the element you're raising
 // ascent is the amount of pixels the element will lift
 // time is the amount of time in seconds that you'd like your element to spend getting to it's final position (try 0.4)

 // ---- DON'T SET THESE IN YOUR SCRIPTS
 // begin is the start time of your raise - it will be set automatically when the function is called
 // modAscent is a used in calculating distance and should not be set when calling
 // topOrig is the original 'top' value of the element as set by setTop() or your own code

 var elem = getByID(id);

 if (elem.style.top == ""){setTop(elem.id,0)}
 if (typeof(topOrig) == "undefined"){topOrig = antipixel(elem.style.top)}
 var current = new Date().getTime();

 if (typeof(begin) != "number"){begin = new Date().getTime()}

 var timeDiff = (current - begin) / 1000; // /

 if (timeDiff < time) {
 var topPx = topOrig+(ascent*((timeDiff/time)*(timeDiff/time)));

 elem.style.top = pixel(topPx);
 setTimeout("raiseElementUp(\'"+id+"\',"+ascent+","+time+","+begin+","+modAscent+","+topOrig+")",16);

 } else {setTop(elem.id,topOrig+ascent);return}
}

function dropElementDown(id,decent,time,begin,modDecent,topOrig){
 // <a href="javascript:dropElementDown(myelementid,100,0.4)">drop down!</a>

 // ---- SET THESE IN YOUR SCRIPTS
 // id is the id of the element you're dropping
 // decent is the amount of pixels the element will drop
 // time is the amount of time in seconds that you'd like your element to spend getting to it's final position (try 0.4)

 // ---- DON'T SET THESE IN YOUR SCRIPTS
 // begin is the start time of your drop - it will be set automatically when the function is called
 // modDecent is a used in calculating distance and should not be set when calling
 // topOrig is the original 'top' value of the element as set by setTop() or your own code

 var elem = getByID(id);

 if (elem.style.top == ""){setTop(elem.id,0)}
 if (typeof(topOrig) == "undefined"){topOrig = antipixel(elem.style.top)}
 var current = new Date().getTime();

 if (typeof(begin) != "number"){begin = new Date().getTime()}

 var timeDiff = (current - begin) / 1000; // /

 if (timeDiff < time) {
 var topPx = topOrig-(decent*((timeDiff/time)*(timeDiff/time)));

 elem.style.top = pixel(topPx);
 setTimeout("raiseElementUp(\'"+id+"\',"+decent+","+time+","+begin+","+modDecent+","+topOrig+")",16);

 } else {setTop(elem.id,topOrig-decent);return}
}

/*
function setOpacity(id,opacity){
 var toSet = getByID(id);

 if (toSet.style.MozOpacity != null){toSet.style.MozOpacity = opacity;}
 if (toSet.style.opacity != null){toSet.style.opacity = opacity;}
 if (toSet.style.filter != null){toSet.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity="+(opacity*100)+")";}
}
*/

function setTop(id,top){
 getByID(id).style.position = 'relative';
 getByID(id).style.top = pixel(top);
}

function getByID(n){
 var d = window.document;
 if (d.getElementById)
 return d.getElementById(n);
 else if (d.all)
 return d.all[n];
}
function pixel(px){
 return px+"px";
}
function antipixel(px){
 return parseInt(px.replace(/px/,''));
}
