var INVALID_ARGUMENT = -2147024809;

dojo.require("dojo.fx");

var myfx = new Object();

myfx.filesIn = function(/*Object*/ args){

	var tr = dojo.byId(args.node);
	var div = dojo.query("div.files", tr)[0];
	args.node = div;
	var s = div.style;

	var anim = dojo.animateProperty(dojo.mixin({
		properties: {
			height: {
				// wrapped in functions so we wait till the last second to query (in case value has changed)
				start: function (){
				    
					s.overflow = "hidden";
					if (!isDisplayed(tr)) {
						s.height = "1px";
						s.display = "block";
						try {
						    tr.style.display = "table-row";
						} catch (ex) {
						    // IE can GDIF
						    // not handling table-row is one thing
						    // but throwing an exception is just annoying
						    if (ex.number == INVALID_ARGUMENT)
						        tr.style.display = "block";
						}
						return 1;
					} else {
						var height = dojo.style(div, "height");
						return Math.max(height, 1);
					}
					
				},
				end: function(){
					return div.scrollHeight;
				}
			}
		}
	}, args));
	dojo.connect(anim, "onEnd", function(){ 
		s.height = "auto";
	});

	return anim; // dojo._Animation
}

myfx.filesOut = function(/*Object*/ args){
	var tr = dojo.byId(args.node);
	var div = dojo.query("div.files", tr)[0];
	args.node = div;
	var s = div.style;

	var anim = dojo.animateProperty(dojo.mixin({
		properties: {
			height: {
				end: 1 // 0 causes IE to display the whole panel
			}
		}
	}, args));

	dojo.connect(anim, "beforeBegin", function(){
		s.overflow = "hidden";
		s.display = "";
	});
	dojo.connect(anim, "onEnd", function(){
		s.height = "auto";
		s.display = "none";
		tr.style.display = "none";
	});

	return anim; // dojo._Animation
}

function isDisplayed(node)
{
	var disp = dojo.style(node, "display");
	if (disp == "none")
		return false;
	return true;
}

function toggleFiles(node, show)
{
	var files = node.nextSibling;
	var forced = typeof show != "undefined";
	var displayed = isDisplayed(files);
	
	if (forced && (show == displayed))
		return;

	if ("anim" in files && files.anim.status() != "stopped")
		files.anim.stop();
	
	if ((forced && show) || !displayed) {
		files.anim = myfx.filesIn({node: files, duration: 650});
		files.anim.play();
	} else {
		files.anim = myfx.filesOut({node: files, duration: 650});
		files.anim.play();
	}
		
}
function displayAllFiles(show)
{
	var tables = dojo.query("table.pluginlist");
	tables.forEach(function (table) {
		var trs = dojo.query("tr.detail");
		trs.forEach(function (tr) {
			toggleFiles(tr, show);
		});
	});
}
function expandAllFiles()
{
	displayAllFiles(true);
}
function collapseAllFiles()
{
	displayAllFiles(false);
}

