
var Agenda = Class.create({
	initialize: function(picker) {
		this.picker = picker;
		this.id = this.picker.href.substring(this.picker.href.indexOf('#')+1);
		this.div = $(this.id);
		if (!this.div) return;

		this.div.id = '';
		this.picker.observe('click', this.activate.bindAsEventListener(this));

		Agenda.ALL.push(this);
		Agenda.INDEX[this.id] = this;

		this.deactivate();

		this.sessions = $A();
		this.div.select('td.session').each(function(td) {
			this.sessions.push(new Agenda.Session(td));
		}.bind(this));
	},

	activate: function(evt) {
		if (Agenda.ACTIVE && Agenda.ACTIVE == this) return;

		this.picker.addClassName('active');
		this.div.show();

		if (Agenda.ACTIVE) {
			Agenda.ACTIVE.deactivate();
			Agenda.HUD.deactivate();
		}
		Agenda.ACTIVE = this;
	},

	deactivate: function() {
		Agenda.ACTIVE = null;
		this.picker.removeClassName('active');
		this.div.hide();
	}
});
Agenda.ALL = [];
Agenda.INDEX = {};



Agenda.HUD = {
	initialize: function() {
		this.div = $(Builder.node('div', {'class':'hud'})).hide();
		this.capTop = Builder.node('div', {'class':'cap top'});
		this.capBottom = Builder.node('div', {'class':'cap bottom'});
		this.h3 = Builder.node('h3');
		this.padder = Builder.node('div', {'class':'padder'});
		this.closeBtn = Builder.node('a', {'class':'close'}, 'Close');
		this.closeBtn.onclick = function() {
			this.deactivate();
		}.bind(this);

		$('agendas').appendChild(this.div);
		this.div.appendChild(this.capTop);
		this.capTop.appendChild(this.h3);
		this.div.appendChild(this.padder);
		this.div.appendChild(this.capBottom);
	},

	activate: function(x, y, title, content) {
		this.h3.innerHTML = title;
		this.padder.innerHTML = content;
		this.padder.appendChild(this.closeBtn);

		this.div.setStyle({
			top: (y - (this.div.getHeight() - 8))+'px',
			left: (x - 269)+'px'
		});
		if (!this.active) new Effect.Appear(this.div, { duration:.2 });
		this.active = true;
	},

	deactivate: function() {
		new Effect.Fade(this.div, { duration:.2 });
		this.active = false;
	}
};



Agenda.Session = Class.create({
	initialize: function(td) {
		this.td = td;
		this.h4 = td.down('h4');
		this.title = this.h4.innerHTML;
		this.description = this.h4.next('div.hud-content').innerHTML;

		this.td.addClassName('a');
		this.td.observe('click', this.activate.bind(this));
	},

	activate: function() {
		var po = this.h4.positionedOffset();
		var x = po[0] + (this.td.getWidth() / 2);
		Agenda.HUD.activate(x, po[1], this.title, this.description);
	}
});



document.observe('dom:loaded', function() {
	/* agenda HUD */
	Agenda.HUD.initialize();

	/* regional agendas */
	$$('#agenda-picker > li > a').each(function(picker) {
		new Agenda(picker);
	});

	/* auto select regional agenda */
	if (location.hash) {
		var id = location.hash.substring(1);
		if (id && Agenda.INDEX[id]) {
			Agenda.INDEX[id].activate();
		}
	}
	if (!Agenda.ACTIVE) Agenda.ALL[0].activate();


	/* experts overlay */
	var overlay = new AC.HTMLOverlay($$('#experts'), { overlayShadowSrc:'http://devimages.apple.com/events/iphone/techtalks/images/experts_overlaybg.png' });
	document.observe(overlay.id+':afterPop', function() {
		var active = null;
		var lis = $(overlay.descriptionPanel).down('ul').childElements();
		lis.each(function(li, index) {
			var h4 = li.down('h4');
			h4.onclick = function() {
				if (active) active.removeClassName('active');
				active = this.up();
				active.addClassName('active');
			};
			if (index === 0) h4.onclick();
		});
	});
});

/* registration dropdown links */
function goRegister(){
   if (document.register.registersel.options[document.register.registersel.selectedIndex].value != "none") { 
        document.location = document.register.registersel.options[document.register.registersel.selectedIndex].value;
    }
    else{
        alert("Please select a city");
    }
}