

function SliderClass(id)
{
	this.id = id;
	this.index = 0;
	this.slideTime = 4500;
	this.slideTo = -1;
	this.pages = $(id+' .dots');
	this.items = $(id+' .items');
	this.itemHeight = $(id).height();
	this.itemCount = $(id+' .item').length;
	
	var dotPos = 132 - ($(id+' .dots').height()/2);
	$(id+' .dots').css('top', dotPos);
	
	var index = 0;
	for(var i=0; i<this.itemCount; i++)
	{
		var data = { slider: this, index: i };
		var citem = this.items.find('.item:eq('+i+')');
		citem.data('sData', data);
		citem.attr('rel', i);
		
		this.pages.find('.dot:eq('+i+')').data('sData', data);
	}
	
	this.pages.find('.dot').click(function(){
		var data = $(this).data('sData');
		data.slider.jumpTo(data.index);
		return false;
	});
	
	return this;
}

SliderClass.prototype.slide = function() {
	this.index++;
	if (this.index >= this.itemCount) 
		this.index = 0;
	
	// slide images to the left
	var thisSlider = this;
	this.items.animate({'top':-this.itemHeight}, function(){
		// shuffle first image to the end
		var img = $(this).find('.item:first').detach();
		$(this).append(img);
		$(this).css('top', 0);
		
		setTimeout('slider.slide()', slider.slideTime);
		
		thisSlider.pages.find('.current').removeClass('current');
		thisSlider.pages.find('.dot:eq('+thisSlider.index+')').addClass('current');
	});
};

SliderClass.prototype.jumpTo = function(index) {
	if (index == this.index) return;
	
	// stop the current animation and jump to the end of it so this function can work properly
	this.items.stop(true,true);
	
	var eq = ':eq('+index+')';
	this.pages.find('.current').removeClass('current');
	this.pages.find('.dot'+eq).addClass('current');
	
	var img = this.items.find('.item[rel='+index+']');
	var left = img.prevAll().detach();
	this.items.append(left);
	this.index = index;
};

function closeMenu()
{
	$('#hover-links a').hide();
	$('#header-content div.menu:visible').animate({ top: 0 }, 'fast', function(){ $(this).hide(); });
}

function updateMonthName()
{
	// change calendar name text to image
	var imgs = {
		'January': '<img src="/images/cal-jan.png"/>',
		'February': '<img src="/images/cal-feb.png"/>',
		'March': '<img src="/images/cal-mar.png"/>',
		'April': '<img src="/images/cal-apr.png"/>',
		'May': '<img src="/images/cal-may.png"/>',
		'June': '<img src="/images/cal-june.png"/>',
		'July': '<img src="/images/cal-july.png"/>',
		'August': '<img src="/images/cal-aug.png"/>',
		'September': '<img src="/images/cal-sep.png"/>',
		'October': '<img src="/images/cal-oct.png"/>',
		'November': '<img src="/images/cal-nov.png"/>',
		'December':	'<img src="/images/cal-dec.png"/>'
	};
	
	var name = $('.hhCalMonthName');
	if (name.length > 0)
	{
		name.html(imgs[name.text()]);
	}
}

var slider;
var count = 0;
var hideTimeout = 0;
var pageName = null;

function slideMenu(index)
{
	$('#hover-links a').hide();
	$('#hover-links a:eq('+index+')').show();
	
	$('#header-content div.menu').hide();
	$('#header-content div.menu:eq('+index+')')
		.css('top', 0)
		.show()
		.animate({ top: -264 },'fast');
}

$(function(){
	
	slider = new SliderClass('#slider');
	setTimeout('slider.slide();', slider.slideTime);

	// get current page name
	var pos = location.href.lastIndexOf('/');
	pageName = location.href.slice(pos+1);
	

	// show menu if the current page name is in the list of names below
	var pageNames = ['visitor', 'meeting-planner', 'sports', 'about'];
	var index = $.inArray(pageName, pageNames);

	if (index >= 0)
	{
		slideMenu(index);
	}
	// otherwise find this page's URL in the sliding menus and show the top level menu
	else
	{
		var url = location.href;
		
		if (url.indexOf('?view') >= 0)
			url = url.slice(0, url.indexOf('?view'));
		
		var link = $('a[href='+url+']');
		if (link.length > 0)
		{
			// show main slideup menu
			var index = link.parents('div.menu').index();
			if (index >= 0)
			{
				slideMenu(index);
				
				// show inner sub menu box if this link is a third level page
				if (link.parents('div.sublevel').length > 0)
				{
					$('div.sublevel').hide();
					link.parents('div.sublevel').show();
				}
			}
		}
			
	}

	// go to the desired page, unless the link clicked is for the current page
	$('#header-links a').click(function(){
		if ('/'+pageName == $(this).attr('href'))
		{
			slideMenu($(this).index());
			return false;
		}
		else
		{
			return true;
		}
	});

	$('div.menu a.sublink').click(function(){
		var menu = $(this).parents('div.menu');
		var id = $(this).attr('data-id');
		var submenu = $('#submenu-'+id);
		menu.find('div.sublevel:visible').slideUp();
		submenu.slideDown();
		return false;
	});

	// update month name to an image
	updateMonthName();
	
	// do it after it changes dynamically
	$(document).ajaxSuccess(function(evt, xhr, ajax){
		if (ajax.data.indexOf('modFunction=displayCalendarView') >= 0)
			updateMonthName();
	});


});










