// aB Custom JS

addHiddenClass = function(el) {
	el.addClass('hidden');
};

var cookieOptions = {
	'duration': 30*24*60*60
};

// At-A-Glance JS

var ataglanceInit = function() {
	if ($$('div.ataglance').length < 1) return false;
	var labels = $$('div.ataglance td.date');
	var cells = $$('div.ataglance td');
	var cellCount = cells.length/4;
	// Parse the cookie for last visit and update it if necessary
	var strToDate = function(str) {
    	var a = str.split('-');
        return new Date(a[0], a[1]-1, a[2]);
	};
	var dateToStr = function(d) {
    	return d.getFullYear()+'-'+(d.getMonth()+1)+'-'+d.getDate();
	};
	var curDate = strToDate(cells[cells.length-1].getProperty('title'));
	var lastVisit = Cookie.get('lastVisit');
	var neverVisited = false;
	if (lastVisit) {
    	lastVisit = lastVisit.split('.').map(strToDate);
	} else {
    	lastVisit = [new Date(0), curDate];
    	Cookie.set('lastVisit', lastVisit.map(dateToStr).join('.'), cookieOptions);
	}
	if (lastVisit[1].getTime() != curDate.getTime()) {
    	// Update the cookie by shifting and adding today's date
    	lastVisit = [lastVisit[1], curDate];
    	Cookie.set('lastVisit', lastVisit.map(dateToStr).join('.'), cookieOptions);
	}
	var daysSinceLast = Math.ceil((lastVisit[1]-lastVisit[0])/1000/60/60/24);
	if (daysSinceLast > 365) neverVisited = true;
	if (daysSinceLast > 15) daysSinceLast = 15;
	// Add TD eventhandlers
	$each(cells, function(el, i) {
    	if (i%cellCount < 15-daysSinceLast) {
        	el.addClass('old');
    	}
		if (el.hasClass('date')) return;
		if (i%cellCount == 15-daysSinceLast) {
    		if (daysSinceLast < 15) el.addClass('cusp');
    		if (i < cellCount && !neverVisited) {
        		var n = new Element('span', {'class':(daysSinceLast < 15)?('lastvisit'):('oldlastvisit')});
        		n.setText('Your Last Visit');
        		n.injectTop(el.getFirst());
    		}
		}
		el.lookup = {
			list: $($ES('div.itemlist',el)[0]),
			img: $($ES('img',el)[0]),
			label: labels[cells.indexOf(el)%cellCount]
		};
		if(el.lookup.list) el.lookup.list.setOpacity(0);
		el.addEvents({
			'mouseenter': function(e) {
				var el = this;
				if (el.hasClass('old')) {
					el.setOpacity(1);
					if (el.lookup.label) el.lookup.label.setOpacity(1);
				}
				if (el.lookup.img) el.lookup.img.effect('margin-top',{duration: 400, transition: Fx.Transitions.Back.easeIn}).start(-4,0);
				if (el.lookup.list) {
					//el.lookup.list.setOpacity(0);
					var f = function() {
						this.opened = true;
						this.removeClass('hidden');
						this.effect('opacity', {duration: 400, transition: Fx.Transitions.Quad.easeInOut}).start(0.9);
					};
					el.lookup.list.timer = f.delay(500, el.lookup.list);
				}
			}.bindWithEvent(el),
			'mouseleave': function(e) {
				var el = this;
				if (el.hasClass('old')) {
					el.setOpacity(0.5);
					if (el.lookup.label) el.lookup.label.setOpacity(0.5);
				}
				if (el.lookup.list && el.lookup.list.opened) {
					el.lookup.list.opened = false;
					var ef = el.lookup.list.effect('opacity', {duration: 400, transition: Fx.Transitions.Quad.easeInOut, onComplete: function() {
						this.addClass('hidden');
					}.bind(el.lookup.list)});
					ef.start(0);
				} else if (el.lookup.list) {
					$clear(el.lookup.list.timer);
				}
			}.bindWithEvent(el)
		});
	});
	// Add the show/hide link
	var div = $$('div.ataglance')[0];
	var bar = div.getPrevious().getPrevious();
	var linky = new Element('a', {'href':'#', 'class':'showhide'});
	if (Cookie.get('showaag') === false) Cookie.set('showaag', 'yes', cookieOptions);
	if (Cookie.get('showaag') == 'yes') {
		linky.setText('Hide');
	} else {
		linky.setText('Show');
		div.addClass('hidden');
		//div.setStyle('height', 0);
	}
	linky.addEvent('click', function(e) {
		if (this.div.hasClass('hidden')) {
			this.div.setStyle('height',0);
			this.div.setStyle('overflow','hidden');
			this.div.removeClass('hidden');
			var f = function(el) {
				el.setStyle('overflow','visible');
			};
			this.div.effect('height',{duration:300, transition:Fx.Transitions.Quad.easeInOut, onComplete:f}).start(this.div.scrollHeight);
			Cookie.set('showaag', 'yes', cookieOptions);
			this.linky.setText('Hide');
		} else {
			this.div.setStyle('overflow','hidden');
			var f = function(el) {
				el.addClass('hidden');
				el.setStyle('overflow','visible');
			};
			this.div.effect('height',{duration:300, transition:Fx.Transitions.Quad.easeInOut, onComplete:f}).start(0);
			Cookie.set('showaag', 'no', cookieOptions);
			this.linky.setText('Show');
		}
	}.bind({div:div,linky:linky}));
	linky.injectTop(bar);
};
window.addEvent('domready', ataglanceInit);

// Navigation JS

var navigationInit = function() {
	var openTab = Cookie.get('opentab');
	var menu = new Element('div', {'class':'nav-menu clearfix'});
	var boxes = $$('div.nav-box');
	if (boxes.length < 1) return false;
	$each(boxes, function(box) {
		box.setStyle('overflow','hidden');
		box.setStyle('position','relative');
		var menu = this;
		var head = box.getFirst();
		var tab = new Element('a', {'href':'#', 'title':head.getProperty('title')});
		var name = head.getText();
		tab.setText(name);
		if (openTab == name || !openTab) {
			if (!openTab) {
				openTab = name;
				Cookie.set('opentab', name, cookieOptions);
			}
			tab.addClass('active');
			menu.opened = {tab:tab, box:box};
		} else {
			box.addClass('hidden');
		}
		tab.addEvent('click', function(e) {
			e = e || window.event;
			Cookie.set('opentab', this.tab.getText(), cookieOptions);
			this.tab.addClass('active');
			this.menu.opened.tab.removeClass('active');
			this.box.setStyle('height',0);
			this.box.removeClass('hidden');
			this.box.injectAfter(this.menu);
			this.box.effect('height',{duration:300, transition:Fx.Transitions.Quad.easeInOut}).start(this.box.scrollHeight);
			this.menu.opened.box.effect('height',{duration:300, transition:Fx.Transitions.Quad.easeInOut, onComplete:addHiddenClass}).start(0);
			this.menu.opened = {tab:this.tab, box:this.box};
			if (e.stopPropagation) {
				e.stopPropagation();
			} else {
				e.cancelBubble = true;
			}
			return false;
		}.bind({menu:menu, tab:tab, box:box}));
		tab.injectInside(menu);
		head.remove();
	}.bind(menu));
	menu.injectBefore(boxes[0]);
};
window.addEvent('domready', navigationInit);