// SIMPLE REVEALER
var SimpleReveal = Class.create({
	initialize : function(container, options){
		this.container = $(container);
		this.options = Object.extend({toggleClass:'active', oLinkSel:'.open', cLinkSel:'.close', contentSel:'.content', duration:.5, cb:function(){}}, options || {});
		this.setup();
	},
	setup : function(){
		if (this.options.tLinkSel)
			this.toggles = this.container.select(this.options.tLinkSel).invoke('observe', 'click', this.open.bind(this));
		else{
			this.container.select(this.options.oLinkSel).invoke('observe', 'click', this.open.bind(this));
			this.container.select(this.options.cLinkSel).invoke('observe','click', this.close.bind(this));
		}
		this.content = this.container.select(this.options.contentSel).first().hide();
	},
	open : function(e){
		e.stop();
		if (this.effect != null) return;
		this.effect = new Effect.toggle(this.content, 'blind', {duration:this.options.duration, afterFinish:function(){this.effect=null;}.bind(this)});
		this._cleanUp();
	},
	close : function(e){
		e.stop();
		if (this.effect != null) return;
		this.effect = new Effect.BlindUp(this.content, {afterFinish:this._cleanUp.bind(this, true), duration:this.options.duration});
	},
	_cleanUp : function(reset){
		this.container.toggleClassName(this.options.toggleClass);
		this.options.cb.apply(this);
		if (reset) this.effect = null;
	}
});

// AJAX REVEALER
var Revealer = Class.create({
	initialize : function(container, oLinkSel, cLinkSel, contentSel, options){
		this.options = Object.extend({toggleClass:'active',duration:.7,parameters:{}, cb:function(){}}, options || {});
		this.container = $(container);
		this.openLink = this.container.select(oLinkSel);
		this.closeLinks = this.container.select(cLinkSel);
		this.content = this.container.down(contentSel);
		if (this.content != null)
			this.content.hide();
		else
			return;
		this.url = this.options.url;
		this.setup();
		this.effect = null;
	},
	setup : function(){
		this.container.observe('revealer:open', this.load.bind(this));
		this.openLink.invoke('observe', 'click', this.load.bind(this));
		this.closeLinks.invoke('observe','click', this.toggle.bind(this));
		this.callback = this.options.cb.bind(this);
		this.close = this._close.bindAsEventListener(this);
	},
	load : function(ev){
		ev.stop();
		if (this.effect != null) return;
		if (this.url){
			if (this.options.beforeCall) this.options.beforeCall.apply(this);
			new Ajax.Request(this.url, {
				parameters : this.options.parameters,
				onSuccess: function(r){
				this.content.update(r.responseText);
				this.toggle();
			}.bind(this)
			});
		}
		else
			this.toggle();
	},
	_close : function(ev){
		if (ev && ev.stop) ev.stop();
		if (this.effect != null) return;
			this.callback();
			this.effect = new Effect.BlindUp(this.content, {afterFinish:this.makeStatic.bind(this)});
			this.container.removeClassName(this.options.toggleClass);
			try{this.container.up('table.common').stopObserving('course:opened', this.close);}catch(e){}
	},
	toggle : function(ev){
		if (ev && ev.stop)
			ev.stop();
		if (this.effect != null) return;
		this.content.setStyle({'position':'relative'});
		this.callback();
		this.effect = new Effect.toggle(this.content, 'blind', {duration:this.options.duration, afterFinish:this.makeStatic.bind(this)});
		if (!this.container.hasClassName(this.options.toggleClass)){
			try{this.container.up('table.common').fire('course:opened');
			    this.container.up('table.common').observe('course:opened', this.close);}catch(e){}
		}else
			try{this.container.up('table.common').stopObserving('course:opened', this.close);}catch(e){}
		this.container.toggleClassName(this.options.toggleClass);
	},
	makeStatic : function (){
		this.effect = null;
		this.content.setStyle({'position':'static'});
	}
});

var CourseRevealer = Class.create(Revealer, {
	initialize : function($super, container, oLinkSel, cLinkSel, contentSel, options){
		$super(container, oLinkSel, cLinkSel, contentSel, options);
		this.callback = function(){
			this.container.select('.foot').invoke('toggle');
			if (!this.container.hasClassName('active')){
				new ShowAll(this.container.down('.dataRow'), {viewableCourses:3, toggleSel:'.toggle', heightSelector:'table', rowSelector:'.schedRow'});
				this.content.select('.cal').each(function(el){
					new Calendar(el);
				}.bind(this));
				this.content.select('.lbOn').each(function(el){
					new lightbox(el);
				});
			}
		}.bind(this);
	}
});


var CourseList = Class.create({
	initialize : function(revealSel, url){
		this.revealSel = revealSel;
		this.url = url;
		this.setup(true);
	},
	setup : function(skipPops){
		$$(this.revealSel).each(function(el){
			new CourseRevealer(el, 'a.time', 'a.close', 'div.detailedView', {
				url : this.url,
				parameters : {id:el.readAttribute('id').split('course_')[1]}
			});
			el.select('.foot').invoke('hide');
			new BasicHover(el, 'hover');
		});
		
		$$('#widgetListing .dataRow').each(function(table){
			new ShowAll(table);
		});
		
		$$('td.col2').each(function(el){
			new PositionedToolTip(el, el.down('.overlayTip'), {xOffset:100});
		});
		
		if (! skipPops)
			$$('#courseList .lbOn').each(function(el){
				//if (! el.hasClassName('lbInited'))
					new lightbox(el);
			});
		
	}
});

var ShowAll = Class.create({
	initialize : function(container, options){
		this.options = Object.extend(
				{viewableCourses:5, 
				 toggleSel:'.show', 
				 rowSelector:'tr.classRow', 
				 heightSelector:'table.common'}, options || options);
		var rows = container.select(this.options.rowSelector);
		this.limit = rows[this.options.viewableCourses];
		this.container = container;
		this.parent = container.down('.overflow');
		this.trigger = container.down(this.options.toggleSel);
		if (this.limit){
			this.parent.style.height = this.closedHeight();
			this.hideable = this.limit.previous().nextSiblings();
			this.hideable.invoke('setStyle', {display:'none'});
			this.parent.style.height = "100%";
			if (this.trigger){
				var t = this.trigger.innerHTML.replace(/\$COUNT/g, rows.length);
				this.trigger.update(t);
				this.triggerText = this.trigger.observe('click', this.toggle.bind(this)).innerHTML;
			}
		}else
			if (this.trigger)
				this.trigger.hide();
			
		
	},
	openHeight : function(){
		return this.container.down(this.options.heightSelector).getHeight() +'px'
	},
	closedHeight : function(){
		return Math.max(0,this.limit.positionedOffset().top -this.parent.positionedOffset().top) + 'px';
	},
	toggle : function(ev){
		ev.stop();
		if (this.open){
			this.parent.style.height = this.openHeight();
			this.parent.morph({'height':this.closedHeight()},{afterFinish:function(){
				this.hideable.invoke('setStyle',{display:'none'});
				this.parent.style.height = "100%";
			}.bind(this)});
		}else{
			this.limit.style.display = '';
			this.parent.style.height = this.closedHeight();
			this.hideable.invoke('setStyle',{'display':''});
			this.parent.morph({'height':this.openHeight()}, {afterFinish:function(){
				this.parent.style.height='100%';
			}.bind(this)});
			
		}
		this.trigger.toggleClassName('active');
		this.open = this.trigger.hasClassName('active');
		this.trigger.update(this.open?"Show Less":this.triggerText);
	}	
	
});


var SearchFilter = Class.create({
	initialize : function(formId, resultId, options){
		this.form = $(formId);
		this.results = $(resultId);
		this.options = Object.extend({loaderSel:'.loader'}, options || {});
		this.loader = $$(this.options.loaderSel);
		this.setup();
	},
	setup : function(){
		this.timer = new Form.Observer(this.form, 1, this.submit.bind(this));
		if (this.options.reset)
			this.options.reset.observe('click', function(e){e.stop(); this.form.reset();}.bind(this));
	},
	submit : function(){
		this.pause();
		if (this.options.beforeSubmit) this.options.beforeSubmit.apply(this);
		this.resume();
		this.form.request({
			onLoading: this.loading.bind(this), 
			onSuccess : function(r){
				setTimeout(function(){
					this.loader.invoke('hide');
					this.results.update(r.responseText)
					this.results.up('.main').removeClassName('opaque');
					this.options.callback.apply(this);
					
				}.bind(this), 3000);
			}.bind(this)});
	},
	loading : function(){
		if (this.loader){
			this.loader.invoke('show');
			this.results.up('.main').addClassName('opaque');
		}
	},
	pause : function(){
		this.timer.stop();
	},
	resume : function(){
		this.timer.registerCallback();
	}
	
});

