/*Global Constants*/
var FXFRAMERATE = 1000 / 40; //40 FPS
var FXMAXHEIGHT = 435;

/*Element aliases*/
var contentTD;
var contentDIV;

/*Saved attribute values for restore purpose*/
var saved_contentTD_height;
var saved_contentTD_paddingTop;
var saved_contentTD_paddingBottom;

/*Working variables*/
var curFXDone=true;
var curFXDir;
var fx_vel;
var fx_accel;

var callback_pullupfx_ended = null;

function pxVal2Css (val)
{
	return val + "px";
}

function pxCss2Val (css)
{
	return parseInt (css);
}

function needsFX(){
  var curUrl = window.location.href;

  var i = curUrl.toLowerCase().indexOf ("blogs/");
  if (i != -1){ //for blogs, only the main page needs FX
    /*var blogUrl = "main/";   
    i = curUrl.indexOf (blogUrl);
    if (i != -1 && i+blogUrl.length-1 < curUrl.length-1) return false;
	
	blogUrl = "java/";   
    i = curUrl.indexOf (blogUrl);
    if (i != -1 && i+blogUrl.length-1 < curUrl.length-1) return false;*/
	return false;
  };
   
  return true;  
}

function initContentFXStuffs(){
  contentTD = document.getElementById ("IDContentTD");
  contentDIV = document.getElementById ("IDContentDIV");
   
  /*Save for restore later*/
  if (contentTD.style.height == "") saved_contentTD_height = -1;
  else saved_contentTD_height = pxCss2Val (contentTD.style.height); 
  saved_contentTD_paddingTop = 20;//contentTD.style.paddingTop;
  saved_contentTD_paddingBottom = 20;//contentTD.style.paddingBottom;
  
  contentTD.style.overflow = "hidden";
  contentDIV.style.overflow = "hidden";  
}

function prepareContentForFX (){ //name a bit ambiguous, meant to prepare before drop down fx only
  if (needsFX()){	
    contentTD.style.height = pxVal2Css (1);      
    contentDIV.style.height = pxVal2Css (1);
//	alert ('breaking');	  
	contentTD.style.paddingTop = pxVal2Css (0);
    contentTD.style.paddingBottom = pxVal2Css (0);
	contentDIV.style.display = "none";
//	alert ('break');
  } else{
    curFXDir = 1;
	if (saved_contentTD_height == -1)
	{
		contentTD.style.height = "";
		contentDIV.style.height = "";
	}
	else
	{
		contentTD.style.height = pxVal2Css (saved_contentTD_height);
		contentDIV.style.height = pxVal2Css (saved_contentTD_height);
	};
  };
};

function startDropDownFX(){  	
  if (!curFXDone && curFXDir == 1) return;
  
  curFXDir = 1;
  fx_vel = 50.0;
  fx_accel = -3.5;  
  curFXDone = false;	  
  setTimeout ('_dropDownFX(1)', 100);
}

function _dropDownFX (i){
  if (curFXDir != 1) return;
  
  var d = new Date();
  var startTime = d.getTime();
  
  var targetHeight = saved_contentTD_height;
  if (saved_contentTD_height != -1)
  {
  	targetHeight -= (saved_contentTD_paddingTop + saved_contentTD_paddingBottom);
					 
    if (i > targetHeight) i = targetHeight;
  };
  
  contentTD.style.height = pxVal2Css (i);
  contentDIV.style.height = pxVal2Css (i);
  
  if (i > saved_contentTD_paddingTop + saved_contentTD_paddingBottom){
    /*Restore after padding does not add space*/
    contentTD.style.paddingTop = pxVal2Css (saved_contentTD_paddingTop);
    contentTD.style.paddingBottom = pxVal2Css (saved_contentTD_paddingBottom);
	contentDIV.style.display = "";
  };  

  if (pxCss2Val (contentTD.style.height)/*+(saved_contentTD_paddingTop + saved_contentTD_paddingBottom)*/ < FXMAXHEIGHT){
    var increment = fx_vel;
    fx_vel += fx_accel; if (fx_vel < 2.5) fx_vel = 2.5;
    if (saved_contentTD_height == -1 || i < targetHeight){
      if (curFXDir == 1){ //continue only when not changed direction	    	  
	    var d = new Date();
	    var duration = d.getTime() - startTime;			
        setTimeout ('_dropDownFX(' + (i+increment) + ')', 
                    (duration < FXFRAMERATE ? FXFRAMERATE-duration+1 : 0));
  	  };
    };
  } else{
	if (saved_contentTD_height == -1)
	{
		contentTD.style.height = ""; //return to "as it is" height
    	contentDIV.style.height = "";
	}
	else
	{
    	contentTD.style.height = pxVal2Css (targetHeight);
    	contentDIV.style.height = pxVal2Css (targetHeight);
	};
	curFXDone = true;
  }; 
};

function startPullUpFX(){
  if (!curFXDone && curFXDir == -1) return;
  
  curFXDir = -1;
  fx_vel = -50.0;
  fx_accel = 3.5;
  curFXDone = false;
  if (saved_contentTD_height == -1)
  	_pullUpFX (-1);
  else
    _pullUpFX (pxCss2Val (contentTD.style.height));
}

function _pullUpFXaction(){
	callback_pullupfx_ended();
}

function _pullUpFX (i){ 
  if (curFXDir != -1) return;

  var d = new Date();
  var startTime = d.getTime();
  
  if (i != -1)
  {
  	if (i < 1) i = 1;  
  	contentTD.style.height = pxVal2Css (i);
  	contentDIV.style.height = pxVal2Css (i);
  
  	if (i < saved_contentTD_paddingTop + saved_contentTD_paddingBottom){
    	/*Restore after padding does add space*/
    	contentTD.style.paddingTop = pxVal2Css (0);
    	contentTD.style.paddingBottom = pxVal2Css (0);
		contentDIV.style.display = "none";
  	};
  };

  if (i != -1 && pxCss2Val (contentTD.style.height) < FXMAXHEIGHT){
    var increment = fx_vel; 
    fx_vel += fx_accel; if (fx_vel > -2.0) fx_vel = -2.0;
    if (i > 1){
      if (curFXDir == -1){ //continue only when not changed direction
	    var d = new Date();
	    var duration = d.getTime() - startTime;		
        setTimeout ('_pullUpFX(' + (i+increment) + ')' , 
	                (duration < FXFRAMERATE ? FXFRAMERATE-duration+1 : 0));
  	  };
    } else{
	  setTimeout ('_pullUpFXaction()', 100);	
	  curFXDone = true;
    };
  } else{    
    var d = new Date();
    var duration = d.getTime() - startTime;			
    contentTD.style.height = pxVal2Css (FXMAXHEIGHT-(saved_contentTD_paddingTop + saved_contentTD_paddingBottom));
    contentDIV.style.height = pxVal2Css (FXMAXHEIGHT-(saved_contentTD_paddingTop + saved_contentTD_paddingBottom));	
	i = pxCss2Val (contentTD.style.height);	
	setTimeout ('_pullUpFX(' + i + ')' , 
                (duration < FXFRAMERATE ? FXFRAMERATE-duration+1 : 0));
  };
};

