document.observe("dom:loaded", function() {
	home_page = new HomePage({script: "/index.php?cmd=load_page"});		
});
var HomePage = Class.create();
HomePage.prototype = {
	initialize: function(options) {
		this.options = options;
		this.loadUser();
	},
	loadUser: function(){
		var col1 = $('col1_content');
		var col3 = $('col3_content');
		
		var buckets = [{title: "News", insert: {element: col3, position: "bottom"}, bucketLoad: this.draw_news},
					   {title: "Meets", insert: {element: col1, position: "bottom"}, bucketLoad: this.draw_meets}];
		buckets.each(function(bucket){
			new Bucket(bucket);		   
		});				
	},
	draw_news: function(bucket){
		var news = _DATA.news;	
		var ul = new Element("ul", {"class": "news_wrapper"});
		bucket.insert(ul);
		news.each(function(n){
			var li = new Element("li", {"class": "news_holder"});
			ul.insert({bottom: li});
			this.title = new Element("div", {"class": "news_title "+n.category}).update(n.title);
			
			this.details = new Element("div", {"class": "details"});
						
			li.insert(this.title).insert(this.details); 
			
			var _body = new Element("div").update(n.body.htmlspecialchars_decode());
						
			this.details.insert(_body);
			
		});
	},
	draw_meets: function(bucket){
		var meets = _DATA.meets;
		
		var ul = new Element("ul", {"class": "meets_wrapper"});
		bucket.insert(ul);
		var _class = "";
		this.g = new k.Growler({location: "tl"});
		meets.each(function(m){
			var li = new Element("li", {"class": "meets_holder clearfix " + _class});
			ul.insert({bottom: li});
			this.title = new Element("div", {"class": "meet_name"}).update(m.meet_name);
			this.date = new Element("div", {"class": "meet_date"}).update(m.meet_start);
			
			this.details = new Element("div", {"class": "meet_details"});
			
			m.files.each(function(f){
				var a = new Element("a", {"target": "_blank", "class": "clearfix", "href": f.file_location}).update(f.file_name);					  				this.details.insert(a);
			}.bind(this));
			if(m.entry){
				var a = new Element("a", {"class": "clearfix", "href": "/meet_signup/index.php?meet_id="+m.meet_id}).update("Signup for Meet("+m.entry+" days left)");					  				
				this.details.insert(a);
				
				if(m.entry < 10){
					var reminder = new Element("a", {"class": "clearfix", "href": "/meet_signup/index.php?meet_id="+m.meet_id}).update(m.entry+" Days Left to Sign Up For "+m.meet_name);
					var n = this.g.growl(reminder, {sticky: true});
				}
			}
						
			li.insert(this.title).insert(this.date).insert(this.details); 
			
			if(_class == ""){
				_class = "odd";	
			}
			else{
				_class = ""	
			}
		}.bind(this));
	}
}
var Bucket = Class.create();
Bucket.prototype = {
	initialize: function(options){
		this.options =Object.extend( {
			className:         "tt_bucket",		//should not have to change - just the class name
			title:          	"title",			//true if you want the tab to be closable, flase if always open
			insert:				{"element": $(document.body), "position": "bottom"},
			bucketLoad:			Prototype.emptyFunction 	//Prototype.emptyFunction does nothing and returns nothing
		}, options || {});
		this.make_bucket()
	},
	make_bucket: function(){
		this.bucket = new Element("div", {"class": this.options.className});
		
		this.handle = new Element("h3", {"class": "handle"}).update(this.options.title);
		this.bucket.insert({bottom: this.handle});
		
		this.toggle = new Element("a", {"class": "toggle", "title": "Show/Hide "+this.options.title});
		this.toggle_img = new Element("img", {"src": "/images/block-slide.gif"});
		this.toggle.insert(this.toggle_img);
		this.handle.insert({bottom: this.toggle});
		
		this.content = new Element("div", {"class": "content"});
		this.bucket.insert({bottom: this.content});
		
		if (typeof this.options.insert.element != 'object'){
			var insert = $(this.options.insert.element);	
		}
		else{
			var insert = this.options.insert.element;	
		}	
		
		switch(this.options.insert.position){
			case "top":
				insert.insert({top: this.bucket});
			break;
			case "bottom":
				insert.insert({bottom: this.bucket});
			break;
			case "before":
				insert.insert({before: this.bucket});
			break;
			case "after":
				insert.insert({after: this.bucket});
			break;
		}
		this.toggle.observe("click", function(){
			this.content.toggle();						  
		}.bind(this));
		if(Object.isFunction(this.options.bucketLoad)){
			this.options.bucketLoad(this.content);
		}
	}
}
var home_page = {};