// jan 26 2007 Andrew Leslie

//this function is to have a dynamic animated dropdown for a layer from a link

//usage: imageLinkSlidedown("[the layer you want to change]",[the amount in pixels it grows or shrinks by],[how many steps in the animation],[how many milliseconds between steps],"[initial link text]","[link text once activated]")

//       a css class should be on the link with a "_on" at the end.  the script will change it to "_off" once activated.  This means you should make two classes.

//       the link id should be the same name as the layerID but with a "_opener" appended to the end

//start dynamic animated dropdown

function imageLinkSlideFormCookie(layerID,growBy,steps,ms,aText,bText){
  var cookie = readCookie(layerID);
    if (cookie)
    {
        if (cookie == 1)
        {
            linktochange = layerID + "_opener";
            var linkref = document.getElementById(linktochange);
            var currentclass = document.getElementById(linktochange).className;
            var newclass
            var position
            var position=new Boolean();
            document.getElementById(linktochange).innerHTML = bText;
            
            
            if (growBy > 0){
                newclass = currentclass.replace("_off","_on"); 
                position = 1;
            }
            else{
                newclass = currentclass.replace("_on","_off");
                position = 0;
            }
            linkref.setAttribute("className", newclass);
            linkref.setAttribute("class", newclass);
            
            closingStr = "document.getElementById('" + linktochange + "').href = 'javascript: imageLinkSlidedown(\"" + layerID + "\"," + -(growBy) + "," + steps + "," + ms + ",\"" + bText.replace("'", "\\\\'") + "\",\"" + aText.replace("'", "\\\\'") + "\")'";
            createCookie(layerID,position,100);
            slidedown(layerID,growBy,1,ms,closingStr);
            }
       }     
}

function imageLinkSlidedown(layerID,growBy,steps,ms,aText,bText){
imageLinkSlidedownCookie(layerID,growBy,steps,ms,aText,bText,false)
}

function imageLinkSlidedownCookie(layerID,growBy,steps,ms,aText,bText,mCookie) {
    linktochange = layerID + "_opener";
    var linkref = document.getElementById(linktochange);
    var currentclass = document.getElementById(linktochange).className;
    var newclass
    var position
    var position=new Boolean();
    document.getElementById(linktochange).innerHTML = bText;
    
    
    if (growBy > 0){
        newclass = currentclass.replace("_off","_on"); 
        position = 1;
    }
    else{
        newclass = currentclass.replace("_on","_off");
        position = 0;
    }
    linkref.setAttribute("className", newclass);
    linkref.setAttribute("class", newclass);
    
    document.getElementById(linktochange).href = "javascript: void(0)";
    if (mCookie == false)
    {
    closingStr = "document.getElementById('" + linktochange + "').href = 'javascript: imageLinkSlidedown(\"" + layerID + "\"," + -(growBy) + "," + steps + "," + ms + ",\"" + bText.replace("'", "\\\\'") + "\",\"" + aText.replace("'", "\\\\'") + "\")'";
    
    }
    else
    {
    closingStr = "document.imageLinkSlidedownCookie('" + linktochange + "').href = 'javascript: imageLinkSlidedown(\"" + layerID + "\"," + -(growBy) + "," + steps + "," + ms + ",\"" + bText.replace("'", "\\\\'") + "\",\"" + aText.replace("'", "\\\\'") + "\"true\")'";
    createCookie(layerID,position,100);
    }
    slidedown(layerID,growBy,steps,ms,closingStr);
    
}


function linkSlidedown(layerID,growBy,steps,ms) {
    linktochange = layerID + "_opener";
    document.getElementById(linktochange).href = "javascript: void(0)";
    closingStr = "document.getElementById('" + linktochange + "').href = 'javascript: linkSlidedown(\"" + layerID + "\"," + -(growBy) + "," + steps + "," + ms + ")'";
    slidedown(layerID,growBy,steps,ms,closingStr);
}

function slidedown(layerID,growBy,steps,ms,closingStr) {
	startHeight = parseInt(document.getElementById(layerID).style.height);
	stepValue = 0;
	var i=0;
	animationStr = "";
	animationStr = animationStr + "setTimeout ( \"document.getElementById('" + layerID + "').style.visibility = 'visible'\" , 0 ); "
	if (steps == 1)
	{
	animationStr = animationStr + "setTimeout ( \"document.getElementById('" + layerID + "').style.height = '" + Math.round(growBy+startHeight) + "' + 'px'\" , " + i*ms + " ); "
	}
	else
	{
	    for (i=0;i<steps; i++) {
		    animationStr = animationStr + "setTimeout ( \"document.getElementById('" + layerID + "').style.height = '" + Math.round(stepValue*growBy+startHeight) + "' + 'px'\" , " + i*ms + " ); "
		    stepValue = Math.pow(i/steps,.4);
		    }
		    if (Math.round(stepValue*growBy+startHeight) < .5) {
		        animationStr = animationStr + "setTimeout ( \"document.getElementById('" + layerID + "').style.visibility = 'hidden'\" , " + i*ms + " ); ";
		    }
    }
	closingStr = closingStr.replace(/\"/g,"\\" + "\"" );
	animationStr = animationStr + "setTimeout ( \"" + closingStr +"\" , " + steps*ms + " ); ";
	changeheight(animationStr);
	animationStr=""
}

function changeheight(animationStr){
	eval(animationStr);
	animationStr=""
	}
	
//end dynamic animated dropdown


// cookie functions

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}