function addloadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload != 'function') {
	window.onload = func;
} else {
	window.onload = function() {
	oldonload();
	func();
}
}
}

// *****************************  main function
window.onload = function() {



declareVariables();
assignLiWidth();
while (x < imgArraySize) {
calculateSpace();
assignSpace();

}
}
// **************************************************************************************************


// *********************** Declarevariables


function declareVariables() {
DIV_TRUE_SIZE = 886;
MAINDIVPADDING = 35;
DIV_SIZE = DIV_TRUE_SIZE - 70;

IMG_MIN_SPACE = 20; // gets image right padding

imgList = document.getElementById("content").getElementsByTagName("li");
liList = document.getElementById("content").getElementsByTagName("li");


imgRowSize = new Array();
imgArraySize = imgList.length;
loopCount = 0;
tempx = 0;
innerloop = 0;
orig = 0;
assingSpaceCont = 0;
thisorig = 0;
pHeight = 65;
LargestImageHeight = 0;
liHeight = 0;

x=0;
imgSize = 0;
imgMarginRight = 0;
}
 
// **************************************************************************

// ************************ assign li width

function assignLiWidth()
{
	var i = 0;
	browserType = " ";
	if(imgList[x].firstChild.tagName == "IMG")
	{
		browserType = "ie";
	}
	else if (imgList[x].firstChild.nextSibling.tagName == "IMG")
	{
		browserType = "firef";
	}

	while (i < imgArraySize)
	{
	
	if (browserType == "ie")
	{
	liList[i].style.width = imgList[i].firstChild.width;
	}

	else
	{
	liList[i].style.width = imgList[i].firstChild.nextSibling.width + "px";
	}
	
	i++;

	}

}






// ************** calculate space function

function calculateSpace() {
numOfImg = 0;
totalRowWidth = 0;
tempTotalRowWidth = 0;
loopContSize = 0;
orig = x;
browserType = "ie";

while (loopContSize < DIV_SIZE) {
if(imgList[x].firstChild.tagName == "IMG")
{
imgRowSize[x] = imgList[x].firstChild.width;
broswerType = "ie";
}
else if (imgList[x].firstChild.nextSibling.tagName == "IMG")
{
imgRowSize[x] = imgList[x].firstChild.nextSibling.width;
browserType = "firef";

}


imgRowSize[x] = imgRowSize[x] + IMG_MIN_SPACE; // get img width + padding
totalRowWidth = totalRowWidth + imgRowSize[x]; // calculates the total width so far
if ((x+1) < imgArraySize) {
if (browserType == "ie")
{
loopContSize = imgList[x].firstChild.width + totalRowWidth;
}

else if(browserType == "firef"){
loopContSize = imgList[x].firstChild.nextSibling.width + totalRowWidth;
}

numOfImg = numOfImg + 1;

}

else
{
if(browserType == "ie")
{
loopContSize = imgList[x].firstChild.width + 1000;
}

else if (browserType == "firef")
{
loopContSize = imgList[x].firstChild.nextSibling.width + 1000;
}

}

x = x + 1;
}
if (x < imgArraySize) {
lastImage = imgList[x].width + IMG_MIN_SPACE;
imgSpace = (DIV_SIZE - (totalRowWidth - IMG_MIN_SPACE));
imgMarginRight = IMG_MIN_SPACE + Math.floor((imgSpace / (numOfImg-1)));
}

if (x >= imgArraySize) {
lastImage = imgList[x-1].width + IMG_MIN_SPACE;
imgSpace = (DIV_SIZE - (totalRowWidth - IMG_MIN_SPACE));
imgMarginRight = IMG_MIN_SPACE + Math.floor((imgSpace / (numOfImg-1)));
}

}
// ********************************************************************


// ***************** assignSpace() function

function assignSpace() {

flagit = 0;
thisorig = orig;
thisorigtemp = thisorig;
assingSpaceCont = orig + (numOfImg - 1);


	while (thisorig < assingSpaceCont) {
		greatestHeight();
		flagit = 1;
		liList[thisorig].style.marginRight = imgMarginRight + "px";
		liList[thisorig].style.height = liHeight + "px";
		thisorig = thisorig + 1;
		
	}
if (thisorig < imgArraySize) {
// (liList[thisorig].style.marginRight);
liList[thisorig].style.marginRight = 15 + "px";
liList[thisorig].style.height = liHeight + "px";

}
}



// ************** greatestheight fucntion



function greatestHeight() {
// alert("height working");
if (x < imgArraySize) {

if(browserType == "ie")
{
imgHeight = imgList[x].firstChild.height;
}

else if (browserType == "firef")
{
imgHeight = imgList[x].firstChild.nextSibling.height;
}


if (flagit == 0) {
	while (thisorigtemp < assingSpaceCont) {
		if(browserType == "ie")
		{
			imgHeight = imgList[thisorigtemp].firstChild.height;
		}

		else if (browserType == "firef")
		{
			imgHeight = imgList[thisorigtemp].firstChild.nextSibling.height;
		}
		
		if (imgHeight > LargestImageHeight) {
			LargestImageHeight = imgHeight;
		}
		thisorigtemp = thisorigtemp + 1;
	}
liHeight = LargestImageHeight + pHeight;
}
}
}

