/* 
	by Paul@YellowPencil.com and Scott@YellowPencil.com
	feel free to delete all comments except for the above credit
*/
/* Created By:   Yan Yan 
   Date Created: 09/28/2007
   Description:  the javascript functions are used to adjust the height of elements
   Adopted from the columns balance js by Paul@YellowPencil.com and Scott@YellowPencil.com
*/

//tid = theme id, lid = layout id
function setTopOfferHeight(tid, lid)
{
  //  var idArray = new Array('topOffer1','topOffer2');
  //  setSameHeight(idArray); 
  //  idArray = new Array('topOffer3','topOffer4');
  //  setSameHeight(idArray); 

    var idArray = new Array('TOOfferName-1','TOOfferName-2')
    setSameHeight(idArray); 
    idArray = new Array('TOOfferName-3','TOOfferName-4');
    setSameHeight(idArray); 

    idArray = new Array('TOCatName-1','TOCatName-2');
    setSameHeight(idArray); 
    idArray = new Array('TOCatName-3','TOCatName-4');
    setSameHeight(idArray); 
    
    idArray = new Array('TODetail-1','TODetail-2');
    setSameHeight(idArray); 
    idArray = new Array('TODetail-3','TODetail-4');
    setSameHeight(idArray); 
}
function setFCardFArticleHeight(idA, idB, tid, lid)
{
    if (lid==16)
    {
        var idArray = new Array('featureCardContent','featureArticleContent');
        setSameHeight(idArray); 
    }
}
function setCatDirCellHeight(idPrefix, total,numPerRow, tid, lid)
{
    var numRows = total / numPerRow;
  
    for (var i=0; i< numRows ;i++)
    {
        var idArray = new Array(numPerRow );
        for (var j=0; j<numPerRow; j++)
        { 
            idArray[j]=  idPrefix  + "-" + (i*numPerRow + j+1);
        } 
        setSameHeightTagClass(idArray, "span", "text"); 
        setSameHeightTagClass(idArray, "a", "name"); 
    }
   
}

function setSameHeight(idArray)
{
	var maxHeight = 0;
	maxHeight = getMaxHeight(idArray) ;
	
	if (document.getElementById && maxHeight>0) 
	{		
		// Let's set all columns to that maximum height
		for (var i = 0; i < idArray.length; i++) 
		{
			var elmt= document.getElementById(idArray[i]);
			if (elmt)
			{
			    elmt.style.height = maxHeight + 'px';
    	
			    // Now, if the browser's in standards-compliant mode, the height property
			    // sets the height excluding padding, so we figure the padding out by subtracting the
			    // old maxHeight from the new offsetHeight, and compensate!  So it works in Safari AND in IE 5.x
			    if (elmt.offsetHeight > maxHeight) 
			    {
				    elmt.style.height = (maxHeight - (elmt.offsetHeight - maxHeight)) + 'px';
			    }
			}
		}
	}
}

function getMaxHeight(idArray) 
{
	var maxHeight = 0;
	if (document.getElementById) 
	{
		// Let's determine the maximum height out of all columns specified
		for (var i = 0; i < idArray.length; i++) 
		{
			var elmt= document.getElementById(idArray[i]);
			if (elmt)
			{
			    if (elmt.offsetHeight > maxHeight) maxHeight = elmt.offsetHeight;
			}
		}
	}
		
	return maxHeight;	
}

//set the same height given tagName, className
function setSameHeightTagClass(idArray, tagName, className)
{
    var newArray =  new Array(idArray.length);
    var num=0;
	for (var i = 0; i < idArray.length; i++) 
	{	
		var elmt= document.getElementById(idArray[i]);
		
		if (elmt)
		{
		    var childElmts = elmt.getElementsByTagName(tagName)	
		    for (var j=0; j < childElmts.length; j++)
		    {	
		        if (childElmts[j].className==className)
		        {	            
		            newArray[num] = childElmts[j];
		            num = num+1;		            
		        }
		    }
        }
    }

	var maxHeight = 0;
	maxHeight = getMaxHeightByElmt(newArray) ;	
		    
	if (document.getElementById && maxHeight>0) 
	{		
		// Let's set all columns to that maximum height
		for (var i = 0; i < newArray.length; i++) 
		{
			var elmt= newArray[i];
			if (elmt)
			{
			    elmt.style.height = maxHeight + 'px';
    	
			    // Now, if the browser's in standards-compliant mode, the height property
			    // sets the height excluding padding, so we figure the padding out by subtracting the
			    // old maxHeight from the new offsetHeight, and compensate!  So it works in Safari AND in IE 5.x
			    if (elmt.offsetHeight > maxHeight) 
			    {
				    elmt.style.height = (maxHeight - (elmt.offsetHeight - maxHeight)) + 'px';
			    }
			}
		}
	}
}


function getMaxHeightByElmt(elmts) 
{
	var maxHeight = 0;
	if (document.getElementById) 
	{
		// Let's determine the maximum height out of all columns specified
		for (var i = 0; i < elmts.length; i++) 
		{
			var elmt= elmts[i];
			if (elmt)
			{
			    if (elmt.offsetHeight > maxHeight) maxHeight = elmt.offsetHeight;
			}
		}
	}
		
	return maxHeight;	
}




