CommunitySearch = {
	
	Moving : {
		Up : 1,
		Down : 2
	},
	
	_timerlen : 5,
	_slideAniLen : 250,
	_cachebust : new Date().valueOf().toString(),
	_moving : {},
	_dir : {},
	_loaded : {},
	_obj : {},
	_endHeight : {},
	_startTime : {},
	_timers : {},	
	
	Show : function(c) {
		
		this._SlideDown(String.fromCharCode(c));
		
		for (var i=65;i<91;i++) {
			if (i == c)
				continue;
			this._SlideUp(String.fromCharCode(i));
		}
	},
	
	Toggle : function(c) {
		if (this._IsDown(String.fromCharCode(c)))
			this._SlideUp(String.fromCharCode(c));
		else
			this.Show(c);
	},
	
	_SlideDown : function(c) { 
		if (this._IsDown(c))
			return;
			
		this._dir[c] = CommunitySearch.Moving.Down;
		this._StartSlide(c);
		if (this._loaded[c] === undefined)
		{
			this._loaded[c] = false;
			
			var args = [];
			args.push('letter=' + c);
			args.push(this._cachebust);
			
	        var url = 'api/communities.php?' + args.join('&');
    	    GDownloadUrl(url, this._MakeHandler(c));
		}		
	},
	
	_SlideUp : function(c) { 
		if (this._IsUp(c))
			return;	
		this._dir[c] = CommunitySearch.Moving.Up;
		this._StartSlide(c);	
	},
	
	_IsDown : function(c) {
		return Html.Get("communities" + c).style.display != "none";
	},
	
	_IsUp : function(c) {
		return Html.Get("communities" + c).style.display == "none";		
	},
	
	_MakeHandler : function(id)
	{
		return function(body, status) {
			if (status == 200)
			{
				CommunitySearch._loaded[id] = true;				
				var newnode = Html.NodeFromHtml(body);
				var div = Html.Get('communities' + id);
				div.removeChild(div.firstChild);
				div.appendChild(newnode);
			}
		};
	},
	
	_StartSlide : function(id)
	{
		var eid = 'communities' + id;
	    var element = Html.Get(eid);
	    this._obj[id] = element;
	    this._endHeight[id] = parseInt(element.style.height);
    	this._startTime[id] = (new Date()).getTime();
		if (element)
		{
			if (CommunitySearch.Moving.Down == this._dir[id])
				element.style.height = '1px';
			element.style.display = '';
		}
    	this._timers[id] = window.setInterval(this._MakeSlideTickHandler(id), this._timerlen);		
	},
		
	_MakeSlideTickHandler : function(id)
	{
		return function() { CommunitySearch._SlideTick(id); };
	},
	
	_SlideTick : function(id)
	{
		var elapsed = (new Date()).getTime() - this._startTime[id];
		if (elapsed > this._slideAniLen)
			this._EndSlide(id);
		else 
		{
			var d = Math.round(elapsed / this._slideAniLen * this._endHeight[id]);
			if (CommunitySearch.Moving.Up === this._dir[id])
				d = this._endHeight[id] - d;
			var element = this._obj[id];
			if (element)
				element.style.height = d + 'px';
		}
	},
	
	_EndSlide : function(id) {
		window.clearInterval(this._timers[id]);
	    var element = this._obj[id];
		if (element)
		{
	    	if (CommunitySearch.Moving.Up === this._dir[id])
		        element.style.display = 'none';
		    element.style.height = this._endHeight[id] + 'px';
		}
	    delete this._moving[id];
	    delete this._timers[id];
	    delete this._startTime[id];
	    delete this._endHeight[id];
	    delete this._obj[id];
	    delete this._dir[id];		
	}	
};