
var ShowDiv = {
	divs: undefined,
	divsList: undefined,
	speed: 15,
	closeSpeed: 15,
	init: function () {

		ShowDiv.divs = new Array();
		ShowDiv.divsList = new Array();
		
	 	$$("a").each(function (a,e) {
	 		
	 		if (a.rel.substr(0,7)=="showDiv") {
	 			
	 			var args = ShowDiv.parseRel(a.rel);
	 		
	 			var id = args[0];
	 			var group = args[1];
	 			
	 			ShowDiv.divsList[ShowDiv.divsList.length] = id;			
	 			var backDis = $(id).getStyle("display");
	 		
	 			$(id).style.display="block";
	 	
	 			
	 			
	 			ShowDiv.divs[id] = new Array();
	 			ShowDiv.divs[id]["height"] =  $(id).offsetHeight;;
	 			ShowDiv.divs[id]["currentlyExecuting"] = 0;
	 			
	 			if (backDis=="block") {
	 				ShowDiv.divs[id]["currentHeight"] =$(id).offsetHeight;
	 				ShowDiv.divs[id]["visible"] = true;
	 			} else {
	 				ShowDiv.divs[id]["currentHeight"] =0;
	 				ShowDiv.divs[id]["visible"] = false;
	 			}
	 			ShowDiv.divs[id]["pe"] = 0;	
	 		
	 			$(id).setStyle({display: backDis});
	 			
	 			
	 		}
	 		
	 		
	 	});
	 	$$("a").invoke("observe", "click", function() {
	 		
	 		if (this.rel.substr(0,7)=="showDiv") {
	 			
	 			var args = ShowDiv.parseRel(this.rel);
	 			var id = args[0];
	 			var group = args[1];
	 
	 			ShowDiv.showHide(id,ShowDiv.speed);
	 			
	 		}
	 		
	 		
	 	});
	},
	parseRel: function (rel) {
		
		var args  = rel.split("[")[1].split("]")[0].split(",");		
		for(i=0;i<args.length;i++) args[i] = args[i].replace(/^\s+|\s+$/g, '');	/*trim()*/
		return args;
		
	},
	showHide: function (id, speed) {
		

		if (!ShowDiv.divs[id]["visible"]) {
			var dir = 1;
		} else {
			var dir = -1;
			speed = ShowDiv.closeSpeed;
		}
		
		
		/*close other*/
		ShowDiv.divsList.each(function(v) {
			if (ShowDiv.divs[v]["visible"] && v !=id && dir ==1) {
				
				ShowDiv.showHide(v, ShowDiv.closeSpeed);
			}
			
		});
		
		
	
	
		if (!ShowDiv.divs[id]["currentlyExecuting"]) {
	


			
			ShowDiv.divs[id]["currentlyExecuting"] = true;
			ShowDiv.divs[id]["pe"] = new PeriodicalExecuter(function(pe) {
				
				$(id).style.display="block";
				$(id).style.overflow="hidden";
				ShowDiv.divs[id]["currentHeight"] = ShowDiv.divs[id]["currentHeight"] + dir*speed;
			 
				if (dir == -1) {
				  if (ShowDiv.divs[id]["currentHeight"]<=0) {
					 
				     pe.stop();
				    
				
				     ShowDiv.divs[id]["currentlyExecuting"]=0;
				     ShowDiv.divs[id]["currentHeight"] = 0;
				     ShowDiv.divs[id]["visible"] = false;
				  	 
				  }		  
				} else {
				  if (ShowDiv.divs[id]["height"]<=ShowDiv.divs[id]["currentHeight"]) {
					pe.stop();
					
					ShowDiv.divs[id]["currentlyExecuting"]=0;
					ShowDiv.divs[id]["currentHeight"] = ShowDiv.divs[id]["height"];
				  	
				  	$(id).style.overflow = "visible";
				  	ShowDiv.divs[id]["visible"] = true;
				  	
				  }
				}
				$(id).style.height = ShowDiv.divs[id]["currentHeight"]+"px";
				$(id).style.opacity = (ShowDiv.divs[id]["currentHeight"]/ShowDiv.divs[id]["height"]);
			 
			
			}, 0.01);
			return false;
		}
		
	}
}

document.observe('dom:loaded', function () { ShowDiv.init() });
