// Page-specific widgets
var listofOldValues = [];
var listofFunctions = [];
var listOfCheckBoxes = [];

function resetCheckboxes(){
	for (var i=0; i<listofOldValues.length; i++) {
		if(listofOldValues[i]!=null){
		try{
			listofFunctions[i](!listofOldValues[i]);
		}catch(e){}
		listOfCheckBoxes[i].checked=!listofOldValues[i];
		}
	}
 listofOldValues = new Array();
 listofFunctions = new Array();
 listOfCheckBoxes = new Array();
}

function setCheckboxes(){
	for (var i=0; i<listofOldValues.length; i++) {
		if(listofOldValues[i]!=null){
		try{
			listofFunctions[i](listofOldValues[i]);
		}catch(e){}
		listOfCheckBoxes[i].checked=listofOldValues[i];
		}
	}
 listofOldValues = new Array();
 listofFunctions = new Array();
 listOfCheckBoxes = new Array();
}

//   Compare Scroller
var CompareScroller = Class.create(Scroller, {
	initialize : function($super,opts){
		opts = Object.extend({endPad : 28}, opts || {});
		$super(opts);
		
		this.items.each(function(el){
			new HoverDelay(el, {
				enterCb : function(){if (el.hasClassName("rTucked")) this.shift(-this.options.endPad); else if(el.hasClassName("lTucked"))this.shift(this.options.endPad)}.bind(this),
				leaveCb : function(){if (el.hasClassName("rTucked")) this.shift(this.options.endPad); else if(el.hasClassName("lTucked")) this.shift(-this.options.endPad)}.bind(this)
		});}.bind(this));
		this.slider.observe('compare:reset', this.reset.bind(this));
		// intro motion
		setTimeout(function(){
			if (this.itemcount > this.options.incrementCount){
				this.slider.style.left = -(this.itemwidth)+'px';
				this.slider.morph({left: '0px', mode :'relative'}, {duration: 1});
			}
		}.bind(this), 500);
	},
	setup : function($super){
		$super();
		this.calcLast();
	},
	calcLast : function(){
		this.slider.select('.rTucked, .lTucked').each(function(el){el.removeClassName("rTucked").removeClassName("lTucked");});
		if (this.items.length >= this.options.incrementCount){
			var l = this.current * this.options.incrementCount;
			if (l < this.items.length || (this.atEnd()&&l==this.items.length && this.current!=1))
				this.items[l-1].addClassName("rTucked");
			if (this.atEnd() && this.current>1){
				l=this.itemcount-this.options.incrementCount;
				this.items[l].addClassName("lTucked");
			}
		}
	},
	next : function($super, e){
		$super(e);
		this.calcLast();
	},
	prev : function($super, e){
		$super(e);
		this.calcLast();
	},
	afterRemove : function(item){
		if (this.itemcount > this.options.incrementCount){
			$('courseCompare').addClassName("more6");
		}else
			$('courseCompare').removeClassName("more6");
		if (item)
			item.fire('course:removed');
	},
	beforeRemove : function(item){
		if (item != null){
			if (item.hasClassName("rTucked")) this.shift(this.options.endPad);
			else if (item.hasClassName("lTucked")) this.shift(-this.options.endPad);
		}
	},
	shift : function(amt){
		new Effect.Move(this.slider, {x: amt,duration:.5, transition: bouncePast, queue:'end'});
	},
	removeItem : function($super, ev){
		if (this.itemcount ==3)
			$$('.course .close').invoke('hide');
		$super(ev);
	},
	reset : function($super){
		$super();
		$$('.course .close').invoke(this.itemcount>2?'show':'hide');
	}
});

//   Show more slider
var ShowMoreSlider = Class.create({
	initialize : function(cont, opts){
		this.options = Object.extend({slideFrom : 'slidefrom', linkSelector : '.showMore'}, opts || {});
		this.container = $(cont);
		this.targetRow = $(this.options.slideFrom);
		this.links = $$(this.options.linkSelector);
		this.moreText = "Show More Features";
		this.lessText = "Show Fewer Features";
		
		var height = this.container.getHeight();
		this.initialHeight = (this.targetRow.cumulativeOffset().top + this.targetRow.getHeight()) - this.container.cumulativeOffset().top;
		this.expandedHeight =  height;
		
		$$('.matchHeight').each(function(el){
			el.style.height = height+'px';
		});
		
		this.container.setStyle({height: this.initialHeight+'px'});
		
		this.links.each(function(el){
			el.observe('click', this.expand.bind(this));
		}.bind(this));

	},
	expand : function(ev){
		ev.stop();
		if (ev.element().hasClassName('active')){
			this.container.morph({height: this.initialHeight+'px'}, {duration: 1});		
			this.links.each(function(el){el.removeClassName("active").innerHTML = this.moreText;}.bind(this))	
		}else{
			var doc = $$('html').first();
			this.container.morph({height: this.expandedHeight+'px'}, {duration: 1, afterUpdate:function(){
					window.scrollTo(0, doc.getHeight());
			}});
			this.links.each(function(el){el.addClassName("active").innerHTML = this.lessText;}.bind(this))
		}
		
	}
});

var CourseSelector = Class.create({
    initialize : function(checkPrefix, coursePrefix){
		this.container = $('overlayChoose');
		this.checkPrefix = checkPrefix;
		this.coursePrefix = coursePrefix;
		this.setup();
    },
	setup : function(){
		this.container.select('.commit').first()
			.observe('click', this.commit.bind(this));
		this.items = [];
		this.container.select('.opt').each(function(el){
			el.observe('change', this.enoughChecked.bind(this));
			var course = $(this.coursePrefix+el.readAttribute('name').split(this.checkPrefix)[1]);
			course.observe('course:removed', function(){
				eval(el.readAttribute('name')+'fn(false)');
				el.checked = false;
			}.bind(this));
			this.items.push({checkbox:el,course:course});
		}.bind(this));
	},
	commit : function(ev){
		if(ev!=null) ev.stop();
		this.items.each(function(item){
			if (item.checkbox.checked){
				item.course.removeClassName('inactive');
			}else{
				item.course.addClassName('inactive');
			}
		}.bind(this));
		$('courseScroll').fire('compare:reset');
		this.container.fire('pop:close');
	},
	enoughChecked : function(ev){
		if (this.container.select('.opt:checked').length < 2){
			ev.element().checked = true;
			ev.element().onclick();
		}
	}
});

// load events.
document.observe('dom:loaded', function() {
	new HeightNormalizer('.wrap > table > thead tr th, .matchHeight thead tr th, #scrollRight');
	new HeightNormalizer('tr.special > td');
	new HeightNormalizer('tr.tutoring > td');
	new HeightNormalizer('tr.extrastudy > td');
	new HeightNormalizer('tr.morefeatures > td');
	new HeightNormalizer('tr.availablenear > td');
	new HeightNormalizer('tr.additionaloptions > td');
	new HeightNormalizer('tr.alt > td');
	var s = new CompareScroller();
	
	//Prepare column functionality
	$$('div.course:not(div.empty)').each(function(course){
		new HoverDelay(course, {delay : .3,
			enterCb : function(){course.addClassName('active');},
			leaveCb : function(){course.removeClassName('active');}
		});
	});
	
	new ShowMoreSlider('courseCompare', {slideFrom : 'showMore', linkSelector : '.showMore'});
	new PopUp('choosePrograms', 'overlayChoose', {closeSelector:'.dismiss'});
	
	// tooltips
	var parent = $('courseWrap');
	$$('.tooltip').each(function(el){
		var pop = el.down('.overlayTip');
		if (el && pop)
			new PositionedToolTip(el, pop, {anchor: el.down('a'),yOffset:-22,xOffset:20,parent:parent});
	}); 
	
		//zip codes
	var zipPop = $('setLocationCmp');
	$$('.enterZip').each(function(el){
		new PositionedPopup(el, zipPop, {parent:parent, popParent:'.course', yOffset:-68, xOffset:62, modal:true});
	});

    var courseSelector = new CourseSelector('check_', 'course_');
	courseSelector.commit();
	
	
});
