/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
	Author		: Ch.Raja Shekar
	Description : Javascript for more info row toggle
	Date		: January-30-2010
	Updates		:
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 

var isIE = (document.all ? true : false); // Is IE browser?
var appVersion = navigator.appVersion.split("MSIE")
var version = parseFloat(appVersion[1])

var VIEW_STATE = 'view_state';
var CAPTURE_SET = 'capture_set';

var images_pm = { plus: new Image(16, 16), 
				  plus_hover: new Image(16, 16), 
				  minus: new Image(16, 16), 
				  minus_hover: new Image(16, 16),
				  clear: new Image(1, 1)
				};
	
images_pm.plus.src = 'images/plus.png';
images_pm.plus_hover.src = 'images/plus_hover.png';
images_pm.minus.src = 'images/minus.png';
images_pm.minus_hover.src = 'images/minus_hover.png';
images_pm.clear.src = 'images/clear.gif';
	
// function sets mouseover, mouseout and click events for given element(elImage)
capture_events = function( elImage )
{
	// Check the given element is valid IMG element
	if(!elImage) return ;				
	if(!nonNull(elImage.tagName)) return ;
	if(elImage.tagName.toLowerCase() != 'img') return ;

	// Check if events have been set
	var capture = elImage.getAttribute(CAPTURE_SET);		
	capture = (nonNull(capture) ? parseInt(capture) > 0 : false);
	if(capture) return ;
	
	elImage.onmouseover = function() { image_over(elImage, true);	}
	elImage.onmouseout = function() { image_over(elImage, false);	}
	elImage.onclick = function()	{ toggle_more_info(elImage);	}
	
	image_over(elImage, true);

	elImage.setAttribute(CAPTURE_SET, '1');
}

toggle_more_info = function( elImage )
{
	if(!elImage) return ;	// Exit if elImage is not an element

	var parent = get_parent_node( elImage, 'td' );	//Get the TD(parent) element that displays the image
	if(!parent) return ;							// Exit if no parent element is found
	parent = get_parent_node( parent );				// Get TD's parent TR element
	if(!parent) return ;							// Exit if no parent is found
	var info = next_node(parent);					// Find the next TR row(that contains more info) that is next to current TR element
	if(!info) return ;

	var hide = elImage.getAttribute(VIEW_STATE);			// Get the display state of the info TR
	hide = (nonNull(hide) ? parseInt(hide) > 0 : false);	// Find new display state

	elImage.src = (hide ? images_pm.plus.src : images_pm.minus.src);	// Set appropriate sign image
	info.style.display = (hide ? 'none' : '');				// Hide/display infor row
	elImage.setAttribute(VIEW_STATE, (hide ? '0' : '1'));	// Save display state

	image_over( elImage );									// Display hover image
}

// Display hover image
image_over = function( elImage, hover )
{
	if(!elImage) return ;	// Exit if elImage is not an element

	var hide = elImage.getAttribute(VIEW_STATE);		// Get the display state of the info TR
	hide = (nonNull(hide) ? parseInt(hide) > 0 : false);// Find current display state
	
	if(hide)
		elImage.src = (hover ? images_pm.minus_hover.src : images_pm.minus.src);
	else
		elImage.src = (hover ? images_pm.plus_hover.src : images_pm.plus.src);

	ie_png_fix( elImage );
}

// Fixes PNG transaprency bug in IE < 7
ie_png_fix = function( elImage )
{
	if(isIE)
	{
		if ((version >= 5.5) && (version < 7) && (document.body.filters)) 
		{
			elImage.style.width = elImage.width;
			elImage.style.height = elImage.height;
			elImage.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod='fixed', src=\"" + elImage.src +"\")";
			elImage.src = 'images/clear.gif';
		}
	}
}

get_parent_node = function( el, parentTag)
{
	var curr = el;
	var tag = (nonNull(parentTag) ? parentTag.toLowerCase() : 'tr');

	while(curr && curr.tagName.toLowerCase() !='body')
	{
		if(curr.tagName.toLowerCase() == tag) return curr;
		curr = el.parentNode;
	}

	return null;
}

next_node = function( el )
{
	var parent = el.parentNode;
	if(!parent) return null;

	var match = false;
	for(var index = 0; index < parent.childNodes.length; index++)
	{
		if(match)
		{
			if( nonNull(parent.childNodes[index].tagName) )
				if(parent.childNodes[index].tagName.toLowerCase() == 'tr')
					return parent.childNodes[index];
		}
		else
			match = (parent.childNodes[index] == el);
	}

	return null;
}

function nonNull( text)
{
	return !( text == "" || text == null  || text == 'undefined' )
}

window.onload = function()
{
	var fix = isIE;
	if(fix) fix = ((version >= 5.5) && (version < 7) && (document.body.filters));

	var el = document.getElementsByName('img_expand'), parent;

	for(var index=0; index<el.length-1; index++)
	{
		if(fix) ie_png_fix( el[index] );

		parent = get_parent_node(el[index], 'td' );	// Get the TD(parent) element that displays the image
		if(parent) parent.style.borderWidth = '0px';	// Exit if no parent is found
	}

}

