$(document).ready(function() {
	if ($('#landing_slideshow').length > 0) landing_slideshow_init();
	if ($('#main_navigation').length > 0) main_nav_hover();
	if ($('a.tooltip').length > 0) tooltips();
	if ($('a.simple').length > 0) simple();
	if ($('a.video_player_button').length > 0) slide_video_player();
	if ($('div.slide').length > 0) startHomeShow();
	if ($('#calendar_content').length > 0) fade_in_calendar();
	if ($('div.b_gray').length > 0) view_date_hover();
	if ($('ul.select').length > 0) ie_select_dropdown();
	if ($('#tabs').length > 0) tabs_toggle();
	if ($('li.season').length > 0) season_menu();
	if ($('input.text').length > 0) clear_form();
	if ($('#media').length > 0) video_pagination();
	if ($('#newsletter_signup').length > 0) newsletter_validation();
	if ($('#date_sort').length > 0) {
		relocate();
		$('#fromdatepicker').datepicker({dateFormat:'yy-mm-dd',beforeShow:function() {
			$('#ui-datepicker-div').mouseover(function() {
				$('#date_sort li').addClass('sfhover');
			});
		}});
		$('#todatepicker').datepicker({dateFormat:'yy-mm-dd',beforeShow:function() {
			$('#ui-datepicker-div').mouseover(function() {
				$('#date_sort li').addClass('sfhover');
			});
		}});
	}
	if ($('a.iframe').length > 0) {
		$("a.iframe").fancybox({
			width:565,
			height:405,
			padding:4
		});
	}
	if ($('#side_content').length > 0) {
		add_height();
	}
	if ($('#tessitura_content').length > 0) {
		tessitura_width();
		$(window).resize(function() { tessitura_width(); });
	}
	if ($('.calendar_month_page').length > 0) equal_heights();
	if ($('a.disabled').length > 0) disable_button();
});
function disable_button() {
	$('a.disabled').fadeTo(0,.5);
	$('a.disabled').click(function() {
		return false;
	})
}
function equal_heights() {
	if ($('#event_image_list').length > 0) {
		tallest = 0;
		$('#event_image_list li').each(function() {
			var myh = $(this).height();
			if (myh > tallest) {
				tallest = myh;
				$('#event_image_list li .white').each(function() {
					$(this).css({'height':(tallest-8)+'px'});
				});
			}
		});
	}
}
function tessitura_width() {
	var w = $(window).width();
	if (w < 1014) {
		$('body').css({'background-position':'-93px 0px'});
	} else {
		$('body').css({'background-position':'top center'});
	}
}
function relocate() {
	$('#submit_datespan').click(function() {
		var f = $('#fromdatepicker').attr('value');
		var t = $('#todatepicker').attr('value');
		if (f != '' && t != '') {
			var url = '/calendar/range/'+f+'-'+t+'/';
			window.location = url;
		} else {
			$('.hasDatepicker').each(function() {
				if ($(this).attr('value') == '') {
					$(this).css({'border-color':'#BA0000'});
				} else {
					$(this).css({'border-color':'#BCBDBC'});
				}
			});
		}
		return false;
	});
}
function add_height() {
	var h = $('#side_content').height();
	$('#main_content').css({'min-height':h+'px'});
}
function newsletter_validation() {
	$('#email_submit_lightbox').click(function() {
		var fn = $('#first_name').attr('value');
		var ln = $('#last_name').attr('value');
		var e  = $('#email_address').attr('value');
		var pw = $('#password').attr('value');
		var error = 0;
		
		$('.required').each(function() {
			if ($(this).attr('value') == '' || $(this).attr('value') == ' ' || $(this).attr('value') == 'Required Field') {
				error++;
				if ($(this).attr('id') != 'password') {
					$(this).attr('value','Required Field');					
				}
				$(this).css({'border-color':'#BA0000','color':'#BA0000'});
			} else {
				$(this).css({'border-color':'#C6C6C6','color':'#000000'});
			}
		});
		
		if (error <= 0) {
			$.ajax({
				url:$('#form_newsletter').attr('action'), // TYLER CHANGED THIS
				data: 'first_name='+fn+'&email_address='+e+'&last_name'+ln+'&pw='+pw,
				type: 'POST',
				success: function(msg) {
					if (msg == 'Success') {
						$('#form_newsletter').html('<p><strong>Thank you, your information has been submitted.</strong></p><p>This window will close in 3 seconds.</p>');
						setTimeout('parent.$.fancybox.close()',3000);
					} else {
						$('#form_newsletter').prepend('<p><strong>'+msg+'</strong></p>');
					}
				}
			});
		}
		
		return false;
	});
	
}
function video_pagination() {
	// initialize
	get_thumbs();
	
	$('#b_previous').click(function() {
		var n = Number($('#page').attr('value'))-1;
		$('#page').attr('value',n);
		get_thumbs();
		return false;
	});
	$('#b_next').click(function() {
		var n = Number($('#page').attr('value'))+1;
		$('#page').attr('value',n);
		get_thumbs();
		return false;
	});

	
}
function get_thumbs() {
	var page = Number($('#page').attr('value'));
	var total = $('#total_pages').attr('value');
	
	set_pagination(page,total,4);
	$('ul#thumbnails').html('<li class="loading"></li>');
	$.getJSON("/ajax/multimedia-pagination/"+page, function(data) { // TYLER CHANGED THIS
		$('ul#thumbnails').html('');
		$.each(data.images, function(i,item) {
			var li = '<a href="'+item.linkto+'"><span></span><img src="'+item.src+'" alt="'+item.title+'" width="120" height="90" /></a><p>'+item.title+'<br />'+item.time+'</p>';
			if (i % 5 == 0) {
				$('<li/>').attr('class','break').appendTo('ul#thumbnails');
				$('<li/>').attr('class','first').html(li).appendTo('ul#thumbnails');
			} else {
				$('<li/>').html(li).appendTo('ul#thumbnails');
			}
		});
	});
	return false;
}
function set_pagination(current_page,total_pages,max_pages_in_a_row) {
	$('#span').html('');
	if (current_page <= 1) {
		$('#b_previous').css('display','none');
	} else {
		$('#b_previous').css('display','block');
	}
	
	if (current_page < max_pages_in_a_row) {
		for (var i = 1;(i <= max_pages_in_a_row && i <= total_pages);i++) {
			if (i == current_page) {
				$('#span').append('<a class="num active" href="#">'+i+'</a>');
			} else {
				$('#span').append('<a class="num" href="#">'+i+'</a>');
			}
		}
		
		if (total_pages > (max_pages_in_a_row+1)) {
			if (total_pages > (max_pages_in_a_row+2)) {
				$('#span').append('...');
			}
			$('#span').append('<a id="total" class="num" href="#">'+total_pages+'</a>');
		}
	} else if (current_page >= (total_pages - max_pages_in_a_row)) {
		$('#span').append('<a class="num" href="#">1</a>');
		
		if (total_pages > max_pages_in_a_row && (total_pages - max_pages_in_a_row) > 2) {
			$('#span').append('...');
		}
		
		for (var i = (total_pages - max_pages_in_a_row);i <= total_pages; i++) {
			if (i > 1) {
				if (i == current_page) {
					$('#span').append('<a class="num active" href="#">'+i+'</a>');
				} else {
					$('#span').append('<a class="num" href="#">'+i+'</a>');
				}
			}
		}
	} else {
		$('#span').append('<a class="num" href="#">1</a>');
		if ((current_page - 2) > 2) {
			$('#span').append('...');
		}
		
		for (var i = (current_page - 2); i <= (current_page + 2); i++) {
			if (i == current_page) {
				$('#span').append('<a class="num active" href="#">'+i+'</a>');
			} else {
				$('#span').append('<a class="num" href="#">'+i+'</a>');
			}
		}
		$('#span').append('...');
		$('#span').append('<a id="total" class="num" href="#">'+total_pages+'</a>');
	}
	
	if (current_page >= total_pages) {
		$('#b_next').css('display','none');
	} else {
		$('#b_next').css('display','block');
	}
	
	$('a.num').each(function() {
		$(this).click(function() {
			var n = $(this).html();
			$('#page').attr('value',n);
			get_thumbs();
			return false;
		});
	});
}
function clear_form() {
	$('input.text').each(function() {
		var v = $(this).attr('value');
		$(this).click(function() { $(this).attr('value',''); });
		$(this).blur(function() { 
			if ($(this).attr('value') == '') $(this).attr('value',v);
		});
	});
}
function tabs_toggle() {
	$('#tabs a').each(function() {
		$(this).click(function() {
			var index = $('#tabs a').index(this);
			$('.tab_copy').each(function() { $(this).removeClass('active'); });
			$('#tabs a').each(function() { $(this).removeClass('shadow'); });
			$('.tab_copy').eq(index).addClass('active');
			$(this).addClass('shadow');
			return false;
		});
	});
}
function view_date_hover() {
	$('div.b_gray').each(function() {
		$(this).mouseover(function() { $(this).css({'background-color':'#E6E9D8','border-color':'#E6E9D8'}); });
		$(this).mouseout(function() { $(this).css({'background-color':'#F5F5F5','border-color':'#F5F5F5'}); });
	});
}
function pause() {
	var swf = document.getElementById('myytplayer');
	swf.pauseVideo();
}

function slide_video_player() {
	var duration = 1000;
	var easing = 'easeOutExpo';
	var pb_top = $('#pseudo_background').position().top;
	var new_top = (pb_top+540)+'px';
	$('a.video_player_button').click(function() {
		$('#header').animate({ top:'540px' },duration,easing);
		$('#pseudo_background').animate({ top:new_top },duration,easing);
		$('#video_holder').animate({ marginTop:'0px' },duration,easing);
		scrollTo(0,0);
		return false;
	});
	$('a#video_player_close').click(function() {
		$('#header').animate({ top:'0px' },duration,easing);
		$('#pseudo_background').animate({ top:pb_top },duration,easing);
		$('#video_holder').animate({ marginTop:'-540px' },duration,easing);
		pause();
		return false;
	});
}

function tooltips() {
	$('a.tooltip').tooltip({
		offset:[8,0],
		relative:true,
		position:'top center'
	});
	//$('a.tooltip').click(function() { return false; });
}
function simple() {
	$('a.simple').tooltip({
		tipClass:'simpletooltip'
	});
}
function main_nav_hover() {
	ie_dropdown();
	$('#main_navigation').mouseover(function() { $('#pseudo_background').addClass('hover'); });
	$('#main_navigation').mouseout(function() { $('#pseudo_background').removeClass('hover'); });
	$('#pseudo_background').mouseover(function() { $('#main_navigation').addClass('hover'); });
	$('#pseudo_background').mouseout(function() { $('#main_navigation').removeClass('hover'); });
}
function ie_select_dropdown() {
	$('ul.select li').each(function(i) {
		$(this).mouseover(function() { $(this).addClass('sfhover'); });
		$(this).mouseout(function() { $(this).removeClass('sfhover'); });
	});
}
function season_menu() {
	// ie
	$('li.season').each(function(i) {
		$(this).mouseover(function() { $(this).addClass('sfhover'); });
		$(this).mouseout(function() { $(this).removeClass('sfhover'); });
		$(this).click(function() {
			var href = $('a.more',this).attr('href');
			window.location = href;
			return false;
		});
	});
}
function ie_dropdown() {
	/* IE 6 :hover Fix adapted for jquery, originally from A List Apart */
	$('#main_navigation li').each(function(i) {
		$(this).mouseover(function() { $(this).addClass('sfhover'); });
		$(this).mouseout(function() { $(this).removeClass('sfhover'); });
	});
	$('#sub_navigation li').each(function(i) {
		$(this).mouseover(function() { $(this).addClass('sfhover'); });
		$(this).mouseout(function() { $(this).removeClass('sfhover'); });
	});
}


/* // homepage slideshow // */
function startHomeShow() {
	change_color(99);
	$('div.slide').each(function() {
		if (!$(this).hasClass('current')) $(this).fadeOut(0,function() { $(this).css('display','none'); });
	});
	
	var myInt = window.setInterval('homeSlideShow()',6000);
	$('div.slide').mouseenter(function() { 
		window.clearInterval(myInt);
	});
	$('div.slide').mouseleave(function() { 
		myInt = setInterval('homeSlideShow()',6000);
	});
	
	$('a.box').click(function() {
		var index = $('a.box').index(this);
		$('div.current').fadeOut(1000,function() { $(this).css('display','none'); });
		fade(index);
		change_color((index-1));
		window.clearInterval(myInt);
		return false;
	});
	
	$('#b_left').click(function() {
		window.clearInterval(myInt);
		homeSlideShow_reverse();
		return false;
	});
	$('#b_right').click(function() {
		window.clearInterval(myInt);
		homeSlideShow();
		return false;
	});
}

function homeSlideShow_reverse() {
	var total = ($('div.slide').length)-1;
	var i = 0;
	$('div.current').each(function() {
		$(this).fadeOut(1000,function() { $(this).css('display','none'); });
		var index = $('div.slide').index(this);
		i = index;
		if (index == 0) {
			fade(total);
		} else {
			fade(index-1);
		}
	});
	change_color(i);
}
function change_color(i) {	
	var colors = $('#color_array').attr('value');
	var color_array = colors.split(',');
		
	var n = i+1;
	if (n >= color_array.length) n = 0;
	$('a#logo').animate({
		backgroundColor:'#'+color_array[n]
	});
}
function homeSlideShow() {	
	var total = ($('div.slide').length)-1;
	var i = 0;

	$('div.current').each(function() {
		$(this).fadeOut(1000,function() { $(this).css('display','none'); });
		var index = $('div.slide').index(this);
		i = index;
		if (index >= total) {
			fade(0);
		} else {
			fade(index+1);
		}
	});
	change_color(i);
}

function fade(index) {
	$('a.box').each(function() { $(this).removeClass('active'); });
	$('a.box').eq(index).addClass('active');
	$('div.slide').removeClass('current');
	$('div.slide').eq(index).fadeIn(1000,function() { $(this).css('display','block'); });
	$('div.slide').eq(index).addClass('current');
}
function landing_slideshow_init() {
	$('#landing_slideshow img').each(function() {
		if (!$(this).hasClass('active')) $(this).fadeOut(0);
	});
	if ($('#landing_slideshow img').length > 1) l_int = setInterval('landing_slideshow()',5000);
}
function landing_slideshow() {
	var total = ($('#landing_slideshow img').length)-1;
	var next = 0;
	$('#landing_slideshow img').each(function() {
		if ($(this).hasClass('active')) {
			var c = $('#landing_slideshow img').index(this);
			next = (c >= total) ? 0 : (c+1);
		}
	});
	//
	
	$('#landing_slideshow img.active').fadeOut(2000);
	$('#landing_slideshow img.active').removeClass('active');
	$('#landing_slideshow img').eq(next).fadeIn(2000);
	$('#landing_slideshow img').eq(next).addClass('active');
}

function fade_in_calendar() {
	$('#calendar_content').animate({
		opacity:0
	},0,'easeOutExpo',function() { $('#calendar_content').animate({
		marginLeft:'60px',
		opacity:1
	},2000,'easeInOutExpo'); });
}