// JavaScript Document

	function isArray(obj) {
	   if (obj.constructor.toString().indexOf("Array") == -1)
		  return false;
	   else
		  return true;
	}

	var ContentManager = Class.create({
		initialize: function(id, options) {
			this.options = {
				
									
			};
			Object.extend(this.options,options || {});
			
			
			this.name = id;
			this.el = $(id);
				
		},
		element: function() {
			
			return (this.el != null) ? this.el : $(this.name);
			
			
		},
		update: function(html, vars) {
			
			if (vars) {
				
				template = new Template(html);
				
				html = template.evaluate(vars);
				
			}
			this.show();
			this.element().update(html);
				
		},
		swap: function(group, options){
			
			if (isArray(group)) {
			
				elements = group;
				
			}
			else {
				
				elements = new Array(group);
									 
			}
			
			/*
			if (this.element().visible()) {
				
				this.element().hide();
				elements.each( function(item) { 
					
					$(item).show();
				
				});
					
			}
			else {
					
				this.element().show();
				elements.each( function(item) { 
					
					$(item).hide();
				
				});
				
			}
			*/
			
			elements.each( function(item) { 
					
				$(item).hide();
				
			});
			this.show();
							
		},
		toggle: function() {
			
			(this.element().visible()) ? this.hide() : this.show();
		
		},
		show: function() {
			
			this.element().show();
			//this.element().appear({ duration: 2.0 });
			//Effect.SlideDown(this.element(), { duration: 3.0 });
			
		},
		load: function(url, parameters) {
			
			new Ajax.Request(url, {
			method:'post',
			onSuccess: function(transport){
			
				this.update(transport.responseText);
	
				}.bind(this),
			onFailure: function(){ alert('Something went wrong...') },
			parameters: parameters
			});
			
		},
		fetch: function(url, parameters) {
			
			new Ajax.Request(url, {
			method:'post',
			onSuccess: function(transport){
				
				this.response = transport.responseText.evalJSON(true) || null;
			
	
				}.bind(this),
			onFailure: function(){ alert('Something went wrong...') },
			parameters: parameters
			});
			
			return this.reponse;
			
		},
		hide: function() {
			
			this.element().hide();
			//Effect.SlideUp(this.element(), { duration: 3.0 });

		
		},
		clear: function() {
		
			var inputs = new Array()
			
			this.element().down('form').descendants().each( 
				function(item) {
					
					if (item.readAttribute('clearable') && item.readAttribute('clearable') != "false") {
						
						this.push(item);
						
					}
					
				}, inputs
			);
			
			inputs.each(function(item){ 
								 
				item.value = '' ;
				item.checked = false;
			
			});
			
		},
		reset: function() {
			
			this.element().down('form').reset();

		
		},
		fill: function(data) {
			
			data.each(function(pair) {
							   
				var input = this.element().down('form').select('[name="' + pair.key + '"]');
							   
				if (input.size() != 0) {
					
					input.first().value = pair.value;
					
					
				}
			
			
			}, this);
		
		}
	});