//////////////////////////////////////////////////////
//	Collapsing grid constants
var CG_Default = 0;
var CG_Large = 1;

//////////////////////////////////////////////////////
//	Collapsing grid script by Nevron

var cgSampleGuid = "84a16036-3ac5-4952-9cc1-ffce3c6a6d66";
var cgRowPrefix = "gridTr_";
var cgSectionPrefix = "section_";
var cgSectionLinePostfix = "_sectionLine";
var cgGridLinePostfix = "_gridLine";
var cgGlyphPrefix = "cgGlyph-";
var cgAnimationStepTimeout = 27;
var cgAnimationTimespan = 135;

var cgGlyphsArray = new Array();
var cgDefaultGlyphSet = new Object();
var cgLargeGlyphSet = new Object();

var cgAnimationsArray = new Array();
var cgRowsCache = new Array();
var cgSectionLineRowsCache = new Array();
var cgSectionRowIdsCache = new Array();
var cgAllRowsCache = null;

CgPrecacheImages();
CgMozillaEmbeddedTablesCheat();

function CgPrecacheImages()
{
	cgDefaultGlyphSet.CollapsedImg = new Image(5);
	cgDefaultGlyphSet.ExpandedImg = new Image(5);
	cgDefaultGlyphSet.CollapsedImg.src = "images/gridSectionPlus.gif";
	cgDefaultGlyphSet.ExpandedImg.src = "images/gridSectionMinus.gif";
	cgGlyphsArray[CG_Default] = cgDefaultGlyphSet;

	cgLargeGlyphSet.CollapsedImg = new Image(5);
	cgLargeGlyphSet.ExpandedImg = new Image(5);
	cgLargeGlyphSet.CollapsedImg.src = "images/gridSectionPlusLarge.gif";
	cgLargeGlyphSet.ExpandedImg.src = "images/gridSectionMinusLarge.gif";
	cgGlyphsArray[CG_Large] = cgLargeGlyphSet;
}


function CgMozillaEmbeddedTablesCheat()
{
	var mozillaEmbeddedTablesCheatSpan = document.getElementById("spanMozillaEmbeddedTablesCheat");
	if(typeof(mozillaEmbeddedTablesCheatSpan) == "undefined" || mozillaEmbeddedTablesCheatSpan == null)
		return;

	mozillaEmbeddedTablesCheatSpan.className = "cbShortContentSpanHidden";
}

function CgSwapState(sectionGuid, glyphsetIndex, enforceVisisble)
{
	var existingAnimationObject = cgAnimationsArray[sectionGuid];
	if(typeof(existingAnimationObject) != "undefined" && existingAnimationObject != null)
	{
		existingAnimationObject.scheduleInverse = true;
		return;
	}
	
	var glyphImg = document.getElementById(cgGlyphPrefix + sectionGuid);
	
	if(typeof(glyphImg) == "undefined" || glyphImg == null)
		return;
		
	if(toString(enforceVisisble) != "undefined")
	{
		if(glyphImg.src == cgGlyphsArray[glyphsetIndex].CollapsedImg.src && enforceVisisble == false)
			return;
		if(glyphImg.src == cgGlyphsArray[glyphsetIndex].ExpandedImg.src && enforceVisisble == true)
			return;
	}

	var existingSelectedRows = cgRowsCache[sectionGuid];
	var existingSectionLineRows = cgSectionLineRowsCache[sectionGuid];
	if(existingSelectedRows == null)
	{
		var rows = cgAllRowsCache;
		if(rows == null)
			rows = document.getElementsByTagName("tr");
		existingSelectedRows = new Array();
		existingSectionLineRows = new Array();
		for(var i = 0; i < rows.length; i++)
		{
			if(rows[i].id.indexOf(sectionGuid) == -1)
				continue;
				
			if(rows[i].id.indexOf(cgSectionPrefix) == -1)
			{
				existingSelectedRows.push(rows[i]);
				continue;
			}
			
			if(rows[i].id.indexOf(cgSectionLinePostfix) != -1)
				existingSectionLineRows.push(rows[i]);
		}
		cgRowsCache[sectionGuid] = existingSelectedRows;
		cgSectionLineRowsCache[sectionGuid] = existingSectionLineRows;
	}
	
	if(existingSelectedRows.length == 0)
		return;

	var selectedRows = new Array();
	for(var i = 0; i < existingSelectedRows.length; i++)
		selectedRows[i] = existingSelectedRows[i];
	
	var animationObject = new Object();
	if(glyphImg.src == cgGlyphsArray[glyphsetIndex].ExpandedImg.src)
	{
		glyphImg.src = cgGlyphsArray[glyphsetIndex].CollapsedImg.src;
		animationObject.makeVisible = false;
	}
	else
	{
		glyphImg.src = cgGlyphsArray[glyphsetIndex].ExpandedImg.src;
		animationObject.makeVisible = true;
	}
	
	var animationStepsCount = Math.ceil(cgAnimationTimespan / cgAnimationStepTimeout);
	var rowsPerStep = Math.ceil(selectedRows.length / animationStepsCount);

	animationObject.sectionGuid = sectionGuid;
	animationObject.selectedRows = selectedRows;
	animationObject.processedRows = new Array();
	animationObject.rowsPerStep = rowsPerStep;
	animationObject.scheduleInverse = false;
	animationObject.glyphImg = glyphImg;
	animationObject.glyphsetIndex = glyphsetIndex;
	animationObject.sectionLineRows = existingSectionLineRows;
	animationObject.firstPass = true;
	cgAnimationsArray[sectionGuid] = animationObject;

	var methodString = "CgSwapStateStep('" + sectionGuid + "')";
	setTimeout(methodString, cgAnimationStepTimeout);
}

function CgSwitchAll(gridGuid, glyphsetIndex, smallGlyphsetIndex)
{
	var glyphImg = document.getElementById(cgGlyphPrefix + gridGuid);
	
	if(typeof(glyphImg) == "undefined")
		return;

	var enforceVisisble = true;
	if(glyphImg.src == cgGlyphsArray[glyphsetIndex].ExpandedImg.src)
		enforceVisisble	= false;
		
	var existingSectionRowIds = cgSectionRowIdsCache[gridGuid];
	if(existingSectionRowIds == null)
	{
		var rows = cgAllRowsCache;
		if(rows == null)
			rows = document.getElementsByTagName("tr");
		existingSectionRowIds = new Array();
		for(var i = 0; i < rows.length; i++)
		{
			if(rows[i].id.indexOf(gridGuid) == -1)
				continue;
				
			if(rows[i].id.indexOf(cgSectionPrefix) == -1)
				continue;
			
			if(rows[i].id.indexOf(cgSectionLinePostfix) != -1)
				continue;
				
			if(rows[i].id.indexOf(cgGridLinePostfix) != -1)
				continue;
				
			var underscore01FoundAt = rows[i].id.indexOf("_");
			if(underscore01FoundAt == -1)
				continue;
			var underscore02FoundAt = rows[i].id.indexOf("_", underscore01FoundAt + 1);
			if(underscore02FoundAt == -1)
				continue;
			var underscore03FoundAt = rows[i].id.indexOf("_", underscore02FoundAt + 1);
			if(underscore03FoundAt == -1)
				continue;
			var underscore04FoundAt = rows[i].id.indexOf("_", underscore03FoundAt + 1);
			if(underscore04FoundAt == -1)
				continue;
			var sectionId = rows[i].id.substring(underscore03FoundAt + 1, underscore04FoundAt);
			existingSectionRowIds.push(sectionId);
		}
		cgSectionRowIdsCache[gridGuid] = existingSectionRowIds;
	}
	
	if(existingSectionRowIds.length == 0)
		return;

	if(glyphImg.src == cgGlyphsArray[glyphsetIndex].ExpandedImg.src)
		glyphImg.src = cgGlyphsArray[glyphsetIndex].CollapsedImg.src;
	else
		glyphImg.src = cgGlyphsArray[glyphsetIndex].ExpandedImg.src;
	
	for(var i = 0; i < existingSectionRowIds.length; i++)
		CgSwapState(existingSectionRowIds[i], smallGlyphsetIndex, enforceVisisble);
}

function CgSwapStateStep(sectionGuid)
{
	var animationObject = cgAnimationsArray[sectionGuid];
	if(animationObject == null)
		return;
	
	if(animationObject.firstPass)
	{
		if(animationObject.makeVisible)
		{
			for(var i = 0; i < animationObject.sectionLineRows.length; i++)
				animationObject.sectionLineRows[i].className = "cgHidden";
		}
		animationObject.firstPass = false;
	}
	
	if(animationObject.scheduleInverse)
	{
		animationObject.scheduleInverse = false;
		var rows = animationObject.selectedRows;
		animationObject.selectedRows = animationObject.processedRows;
		animationObject.processedRows = rows;
		animationObject.makeVisible = !animationObject.makeVisible;
		if(animationObject.makeVisible)
			animationObject.glyphImg.src = cgGlyphsArray[animationObject.glyphsetIndex].ExpandedImg.src;
		else
			animationObject.glyphImg.src = cgGlyphsArray[animationObject.glyphsetIndex].CollapsedImg.src;
	}
	
	var newCssClass = "";
	if(animationObject.makeVisible)
	{
		newCssClass = "cgVisible";
		for(var i = 0; i < animationObject.rowsPerStep && animationObject.selectedRows.length != 0; i++)
		{
			var row = animationObject.selectedRows[0];
			row.className = newCssClass;
			animationObject.processedRows.push(animationObject.selectedRows.shift());
		}
	}
	else
	{
		newCssClass = "cgHidden";
		for(var i = 0; i < animationObject.rowsPerStep && animationObject.selectedRows.length != 0; i++)
		{
			var row = animationObject.selectedRows[animationObject.selectedRows.length - 1];
			row.className = newCssClass;
			animationObject.processedRows.push(animationObject.selectedRows.pop());
		}
	}

	if(animationObject.selectedRows.length == 0)
	{
		cgAnimationsArray[animationObject.sectionGuid] = null;
		if(!animationObject.makeVisible)
		{
			for(var i = 0; i < animationObject.sectionLineRows.length; i++)
				animationObject.sectionLineRows[i].className = "cgVisible";
		}
		return;
	}
	
	var methodString = "CgSwapStateStep('" + sectionGuid + "')";
	setTimeout(methodString, cgAnimationStepTimeout);
}
