// *********** zip functions ***********
//zip function warnings and notes:
//  target div must have a parent div with id="p_"+targetdivid.
//  target div must have overflow:hidden;
//  target div must not have padding style or mozilla will add this to the height each iteration. Put padding in child div
//  ignores the first occurrances of display: and height:
var zip_dofading=false;
var motion_multiply_default=.33;
var motion_add_default=.01;
var motion_max_default=.1;
var motion_multiply=motion_multiply_default;
var motion_add=motion_add_default;
var motion_max=motion_max_default;
var dosmoothanimation=true;

var fade_multiply=1.2;
var fade_add=.03;


function setzipnormal(){
 motion_multiply=motion_multiply_default;
 motion_add=motion_add_default;
 motion_max=motion_max_default;
}

function setzipfast(){
 motion_multiply=.55;
 motion_add=.04;
 motion_max=.2;
}

function dozipopen(divname,endheight,callbacktext){
 dozip(divname,false,endheight,null,callbacktext);
}
function dozipclose(divname,endheight,callbacktext){
 endheight=(endheight==null)?0:endheight;
 dozip(divname,false,endheight,null,callbacktext);
}
function dohzipopen(divname){
 dozip(divname,true);
}
function dohzipclose(divname,endwidth,callbacktext){
 endwidth=(endwidth==null)?0:endwidth;
 dozip(divname,true,endwidth,null,callbacktext);
}
//Leave endlength null to use full rendered length
//Leave beginlength null to use currentlength
function dozip(divname,iswidth,endlength,beginlength,callbacktext,iterationcallbacktext){
 dofading=zip_dofading;
 var obj=getobj_cache(divname);
 var renderedlength=(iswidth)?getrenderedwidth(divname):getrenderedheight(divname);
 if (endlength==null) endlength=renderedlength;
 if (beginlength==null) beginlength=(iswidth)?getcurrentwidth(obj):getcurrentheight(obj);
 if (beginlength<=1) beginlength=1;
 if (endlength<=1) endlength=1;
 var thislength=beginlength;//used with iterationcallbacktext
 var perc=0;//used with iterationcallbacktext

 if (dosmoothanimation){
  if (endlength>beginlength) { //expanding
   if (dofading) setopacity(obj,0);
  }
  if (iswidth)
   obj.style.width=Math.round(beginlength)+'px';
  else
   obj.style.height=Math.round(beginlength)+'px';
  showobj(obj);
  if (iterationcallbacktext != '') eval(iterationcallbacktext);
  setTimeout('dozipsub("'+divname+'",'+iswidth+','+beginlength+','+beginlength+','+endlength+',"'+callbacktext+'",'+dofading+','+motion_multiply+','+motion_add+','+motion_max+',"'+iterationcallbacktext+'")',30);
		setzipnormal();
 }else{
  if (endlength==1) {
   hideobj(obj);
  } else {
   if (iswidth)
    obj.style.width=Math.round(endlength)+'px';
   else
    obj.style.height=Math.round(endlength)+'px';
  }
  safeeval(callbacktext);
 }
}
function dozipsub(divname,iswidth,thislength,beginlength,endlength,callbacktext,dofading,this_motion_multiply,this_motion_add,this_motion_max,iterationcallbacktext){
 var obj=getobj_cache(divname);
 var additional=2;
 var dif=endlength-beginlength;
 if (dif==0){
  perc=1;
 } else {
  var perc=(thislength-beginlength)/dif;
  var percadd=perc*this_motion_multiply+this_motion_add;
  if (percadd>this_motion_max) percadd=this_motion_max;
  perc+=percadd;
  if (perc>1) perc=1;
  thislength=beginlength+dif*perc;
  if (iswidth)
   obj.style.width=Math.round(thislength)+'px';
  else
   obj.style.height=Math.round(thislength)+'px';
  if (dofading)
   setopacity(obj,(thislength/Math.max(beginlength,endlength))*100);
 }
 if (iterationcallbacktext != '') eval(iterationcallbacktext);
 if (perc==1){
  if (endlength==1) {
   hideobj(obj);
  } else {
   if (iswidth)
    obj.style.width=Math.round(endlength)+'px';
   else
    obj.style.height=Math.round(endlength)+'px';
  }
  safeeval(callbacktext);
  return;
 }
 setTimeout('dozipsub("'+divname+'",'+iswidth+','+thislength+','+beginlength+','+endlength+',"'+callbacktext+'",'+dofading+','+this_motion_multiply+','+this_motion_add+','+this_motion_max+',"'+iterationcallbacktext+'")',30);
}

function getcurrentheight(obj){
 var h=obj.style.height;
 if (h=='') return -1;
 return qfix(h.replace(/[^\d]/g,''));
}
function getcurrentwidth(obj){
 var h=obj.style.width;
 if (h=='') return -1;
 return qfix(h.replace(/[^\d]/g,''));
}

addloadcommand('grh_outputdivs()');

function grh_outputdivs(){
 var grhhtml='<div id="grhdiv" style="display:none;position:absolute;"></div>';
 appendhtml(grhhtml);
}

function getrenderedheight(objname){
 var grhobj=getobj_cache('grhdiv');
 var obj=getobj_cache('p_'+objname);
 changetext(grhobj,obj.innerHTML.replace(/display\:/i,'z:').replace(/height\:/i,'zh:'));
 setopacity(grhobj,0);
 showobj('grhdiv');
 var h=getheight(grhobj);
 hideobj('grhdiv');
 changetext(grhobj,'');
 return h;
}
function getrenderedwidth(objname){
 var grhobj=getobj_cache('grhdiv');
 var obj=getobj_cache('p_'+objname);
 changetext(grhobj,obj.innerHTML.replace(/display\:/i,'z:').replace(/width\:/i,''));
 setopacity(grhobj,0);
 showobj('grhdiv');
 var h=getwidth(grhobj);
 hideobj('grhdiv');
 changetext(grhobj,'');
 return h;
}
// *********** zip functions end ***********




var objcache_name=Array();
var objcache_obj=Array();
function getobj_cache(objname){
 for (var i=0;i<objcache_name.length;i++){
  if (objcache_name[i]==objname)
   return objcache_obj[i];
 }
 var obj=getobj(objname);
 objcache_name[objcache_name.length]=objname;
 objcache_obj[objcache_obj.length]=obj;
 return obj;
}
function getobj_dropcache(objname){
 if (mt(objname)){//just drop one
  for (var i=0;i<objcache_name.length;i++){
   if (objcache_name[i]==objname){
    objcache_name[i]=null;
    objcache_obj[i]=null;
    return;
   }
  }
  return;
 }
 objcache_name=null;
 objcache_obj=null;
 objcache_name=Array();
 objcache_obj=Array();
}


// *********** fade functions ***********
//fade function warnings and notes:
//div must have height set for it to work properly in IE. height:1px works too.
//maxperc, minperc are optional
function setfadeslow(){
	fade_multiply=1.1;
	fade_add=.001;
}
function setfadefast(){
 fade_multiply=1.2;
 fade_add=.03;
}
function dofadein(divname,beginperc,endperc,callbackstring){
 beginperc=(beginperc==null)?1:beginperc;
 endperc=(endperc==null)?100:endperc;
 var obj=getobj_cache(divname);
 if (dosmoothanimation){
  setTimeout('dofadesub("'+divname+'",'+beginperc+','+endperc+',\''+prepjs(prepjs(callbackstring))+'\')',30);
  getspeeddiff();
 }else{
  showobj(obj);
  safeeval(callbackstring);
 }
}

function dofadeout(divname,callbackstring){
 var obj=getobj_cache(divname);
 if (dosmoothanimation){
  setTimeout('dofadesub("'+divname+'",100,1,\''+prepjs(prepjs(callbackstring))+'\')',30);
 }else{
  hideobj(obj);
		safeeval(callbackstring);
 }
}

function dofadesub(divname,beginperc100,endperc100,callbackstring,doneperc){
 doneperc=((doneperc==null)?0:doneperc)*fade_multiply+fade_add;
 doneperc=(doneperc>1)?1:doneperc;
 var obj=getobj_cache(divname);
 var dif100=endperc100-beginperc100;
 var thisperc100=doneperc*dif100+(beginperc100);
 if (dif100<0){
  dif100=beginperc100-endperc100;
  thisperc100=beginperc100-doneperc*dif100;
 }
 setopacity(obj,thisperc100);
 if (!isvisible(obj))
  showobj(obj);
 if (doneperc>=1){
//  handlespeeddidff(2000,0);
  if (endperc100<=1) hideobj(obj);
  safeeval(callbackstring);
  return;
 }
 setTimeout('dofadesub("'+divname+'",'+beginperc100+','+endperc100+',\''+prepjs(prepjs(callbackstring))+'\','+doneperc+')',30);
}
// *********** fade functions end ***********



function clr_mix(clr1hex,clr2hex,perc){//perc as decimal
 var clr1=clr_HexToInt(clr1hex);
 var clr2=clr_HexToInt(clr2hex);
 var r1=Math.floor(clr1/65536);
 var g1=Math.floor((clr1-r1*65536)/256);
 var b1=(clr1-r1*65536-g1*256);
 var r2=Math.floor(clr2/65536);
 var g2=Math.floor((clr2-r2*65536)/256);
 var b2=(clr2-r2*65536-g2*256);
 return clr_RGBToHex(Math.floor(r1+(r2-r1)*perc),Math.floor(g1+(g2-g1)*perc),Math.floor(b1+(b2-b1)*perc));
}

function clr_HexToInt(xu){
 var x=xu;
 if (x.substring(0,1)=="#")
  return parseInt(x.substring(1),16);
 return parseInt(x,16);
}

function clr_RGBToHex(r,g,b){
 return "#"+clr_h2(r.toString(16))+clr_h2(g.toString(16))+clr_h2(b.toString(16));
}

function clr_h2(x){
 if (x.length==1)
  return '0'+x;
 return x;
}
