document.observe('dom:loaded', init);
var events;
function init(){
	col1 = $('banners');
	col2 = $('resident_companies');
	col3 = $('hult_presents');
	col4 = $('upcoming_events');
	col5 = $('content_inner');
	maxHeight = col1.getHeight();
	if (col2) {
		if (maxHeight < col2.getHeight()) maxHeight = col2.getHeight();
		if (maxHeight < col3.getHeight()) maxHeight = col3.getHeight();
		if (maxHeight < col4.getHeight()) maxHeight = col4.getHeight();
		col1.setStyle({height: maxHeight + 'px'});
		col2.setStyle({height: maxHeight + 'px'});
		col3.setStyle({height: maxHeight + 'px'});
		col4.setStyle({height: maxHeight + 'px'});
	} else if(col5) {
		col5Height = col5.getHeight()
		col5Height12 = col5Height-12;
		if (maxHeight < col5Height12){
			//col5 is taller
			col1.setStyle({height: (col5Height12) + 'px'});
		} else {
			//else col1 is taller
			col5.setStyle({height: (maxHeight-3) + 'px'});
		}
		event_datesObj = $('event_dates');
		if (event_datesObj) event_datesObj.setStyle({height: (col5Height-50) + 'px'});
	}
	//active all subnavigations
	init_subnav();


	events = $$('div.event')
	//alert(events.size());
	events.each(function(item) {
							//item.identify();
							$$('div#' + item.identify() + ' div.event_list a').each(function(n) { preview(n);});
//							item.observe('onclick',switchEvents);

							});
	init_slideshow();
//	$('slideshowbg').clonePosition($('event1')).setStyle({'background':'#000'});
}

/*start slideshow*/
var currentSlide = 1;
var slidesArr;
var thisSlideObj;
function init_slideshow(){
	//get all slides
	slidesArr = $$('div.event')
	//alert($(slidesArr.first()).identify());
	//setInterval(findNextSlide,10000);
	setTimeout(findNextSlide,4000);
	setTimeout("setInterval(findNextSlide,10000)",4000);
}
function findNextSlide(){
	slidesArr.each(function(s){
							if (s.visible())
							{
								showNextSlide(s);
								throw $break;
							}
							});
}
function showNextSlide(thisSlide){
	//setup current slide
	thisSlide = $(thisSlide);
	//setup next slide
	thisSlideObj = (slidesArr.last() == thisSlide) ? slidesArr.first() : slidesArr[slidesArr.indexOf(thisSlide)+1];
	//hide current slide
	new Effect.SlideUp(thisSlide,{duration:2});
	//show next slide
	setTimeout("new Effect.SlideDown(thisSlideObj,{duration:2}); ",2000);
}
/*end slideshow*/

function preview(el){
	//add event listener
	this.init = function(){
		el.observe('click',this.switchEvents.bindAsEventListener(this));
	}
	this.init = this.init.bind(this);
	
	this.switchEvents = function(event){
		//stop event so we can handle it - sometimes this gets called manually (not by an event) so lets make sure its an event
		if (event) Event.stop(event);
		showObj = $('event' + el.innerHTML)
		events.each(function(n) {n.hide();});
		showObj.show();
	}
	this.switchEvents = this.switchEvents.bind(this);
	
	this.init();
}

/*start sub nav*/
var lastSubNavObj = null;
var subNavIFrame = null;
function init_subnav() {
		subNavArr = $$('ul.subnav');
		//loop through list of sub navs
		subNavArr.each(function(s) {
			subNavInstance = new subNav(s);
			//fix width
		});
}

function fixWidth(obj) {
	navParts = obj.immediateDescendants();
	var maxWidth = 0;
	navParts.each(function(s) {
		//alert(s.getWidth() + 'px' + s.firstDescendant().getWidth() + 'px' + s.innerHTML);
		if (s.firstDescendant().getWidth() > maxWidth) maxWidth = s.firstDescendant().getWidth();
	});
	navParts.each(function(s) {
		s.firstDescendant().setStyle({
			width: maxWidth + 'px'
		});
	});
}
function subNav(obj) {
	this.hideStatus = false;
	this.shown = false;
	
	this.doShow = function(){
		if (lastSubNavObj) lastSubNavObj.hide();
		this.hideStatus = false;
		obj.show();
		if (!this.shown) {
			fixWidthInstance = new fixWidth(obj);
			this.shown = true;
		}
		lastSubNavObj = obj;
	}
	this.doShow = this.doShow.bind(this);
	
	this.startHide = function(){
		this.hideStatus = true;
		setTimeout(this.doHide,600);
	}
	this.startHide = this.startHide.bind(this);
	
	this.doHide = function(){
		if (this.hideStatus) obj.hide();
	}
	this.doHide = this.doHide.bind(this);

	this.init = function() {
		ancestors = obj.ancestors();
		ancestors[0].observe('mouseover',this.doShow);
		ancestors[0].observe('mouseout',this.startHide);
	}
	this.init = this.init.bind(this);


	this.init();
}/*end sub nav*/