/** Some ideas for further development:
  *	-option to turn off miniCart, could be stored as a cookie
  * -store state in cookie if you need to refresh the browser
  * -use ajax to add stuff to the cart
  * -hightlight row when a new item is added
*/

var timerObj=null; //allows only one timed action at a time.  open/close whatever..
var objParentDiv = null;
var objParentDiv_ = null;
var timeoutSet= false;
var resizeInProgress = false; //stops all actions if a resize is in progress

//Make sure only one is happening at the same time.
var isGrowing = false;
var isShrinking = false;

var closeScrollParam = 10;

//the higher the slower
var growSpeed = 10;
var shrinkSpeed = 0;


function getGrowthSize(maxX, currentX)
{
  return Math.round((maxX - currentX)/10);  
}

function showMiniCart(containerId){
  try{
	if(resizeInProgress){return;}
	if(objParentDiv != null){
	  if(objParentDiv.style.display == "block"){		
		shrinkDiv();
	  }
	  return;
	}
	objContainerDiv=getElement(containerId);
	var y = findPosY(objContainerDiv);
	var x = findPosX(objContainerDiv);
	objParentDiv=getElement('miniCart');
	objParentDiv_=getElement('innerCart');
	objParentDiv.style.top = (y + 21) + 'px';
	objParentDiv.style.left = (x + 14) + 'px';
	objParentDiv.style.width = objContainerDiv.style.width;
	if(objParentDiv.style.display == "block"){
	  shrinkDiv();
	}else{
	  objParentDiv.style.display= "block";
	  objParentDiv_.style.top = (objParentDiv_.offsetHeight * -1) + 'px';
	  var miniCartHeight = objParentDiv_.offsetHeight;
	  objParentDiv.style.height = '0px';
	  growDivTo(miniCartHeight);
	}
  }
  catch(err){}
}

function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

//unpixilize a px value
function unpixilize(inStr){return inStr.substring(0,inStr.indexOf('px'));}

//set time out for mini Cart to close when idle
function timeMiniCartOut(duration){
  if(timeoutSet){return;}
  objParentDiv=getElement('miniCart');
  objParentDiv_=getElement('innerCart');
  timerObj = setTimeout("shrinkDiv()",duration);
  timeoutSet = true;
}

//clear set timeout
function clearTimeOutMiniCart(){
  timeoutSet = false;
  clearTimeout(timerObj);
  timerObj=null;
}

//recursive grow function through timeout.
function growDivTo(maxHeight){
  try{
	if(isShrinking){return;}
	curHeight = unpixilize(objParentDiv.style.height);
	if(parseInt(curHeight) < parseInt(maxHeight))	{
	  var growthSize = getGrowthSize(parseInt(maxHeight),parseInt(curHeight));
	  if(growthSize <= 0){
		objParentDiv.style.height = maxHeight + 'px';
		objParentDiv_.style.top = -0 + 'px';
		objParentDiv = null;
		objParentDiv_ = null;
		isGrowing = false;
		resizeInProgress = false;
	  }else{
		resizeInProgress = true;
		isGrowing = true;
		newHeight = parseInt(objParentDiv.style.height) + growthSize;
		newTop = parseInt(objParentDiv_.style.top) + growthSize;
		objParentDiv.style.height = newHeight + 'px';
		objParentDiv_.style.top = newTop + 'px';
		setTimeout('growDivTo('+ maxHeight +')',growSpeed);
	  }
	}
  }catch(err){}
}

//recursive shrick call through timeout
function shrinkDiv() {
  try{
	if(isGrowing){return;}
	curHeight = unpixilize(objParentDiv.style.height);
	if(parseInt(curHeight) > closeScrollParam){
	  resizeInProgress = true;
	  isShrinking = true;
	  newHeight = parseInt(objParentDiv.style.height) - closeScrollParam ;
	  newTop = parseInt(objParentDiv_.style.top) - closeScrollParam ;
	  objParentDiv.style.height = newHeight + 'px';
	  objParentDiv_.style.top = newTop + 'px';
	  setTimeout('shrinkDiv()',shrinkSpeed);
	}else{
	  resizeInProgress = false;
	  isShrinking = false;
	  objParentDiv.style.display = "none";
	  objParentDiv = null;
	  objParentDiv_ = null;;
	}
  }catch(err){}
}

//short cut to get element by ID
function getElement(eName){return document.getElementById(eName);}

