$(document).ready(function() {
	if ($('#people_nav').length > 0) sfHover();
	if ($('#social').length > 0) socialHovers();
	if ($('ul.tab_navigation').length > 0) tabs();
	if ($('a.thumbnail').length > 0) blue_rollovers();
	if ($('#drop_down_select')) drop_down();
	if ($('.drop_down_select')) drop_down_class();
	if ($('#tabs').length > 0) toggle_tabs();
	if ($('ul#faq').length > 0) toggle_faqs();
	if ($('a.links').length > 0) toggle_copy();
	if ($('.faculty_list').length > 0) sort_faculty_staff();
	if ($('a.tooltip').length > 0) tooltips();
	if ($('ul#mycarousel').length > 0) carousel_scroll();
	if ($('#email_signup').length > 0) email_submit();
	if ($('#image_container').length > 0) student_slideshow();
	if ($('#comment_comment').length > 0) blog_validation();
});

var addthis_config = {
	services_compact: 'facebook, twitter, delicious, linkedin, google, email',
	services_exclude: 'print',
	data_use_flash: false
}

function blog_validation() {
	$('#comment_submit').click(function() {
		var n = $('#comment_name').val();
		var e = $('#comment_email').val();
		var c = $('#comment_comment').val();
		var exp = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		var error = 0;

		if (n == '' || n == ' ') {
			$('#comment_name').animate({
				backgroundColor:'#DA998E'
			},{
				duration:500,
				easing:'swing',
				complete:function() {
					$(this).animate({backgroundColor:'#FFFFFF'},{duration:500,easing:'swing'});
				}
			});
			error++;
		}
		
		if (e == '' || e == ' ' || !exp.test(e)) {
			$('#comment_email').animate({
				backgroundColor:'#DA998E'
			},{
				duration:500,
				easing:'swing',
				complete:function() {
					$(this).animate({backgroundColor:'#FFFFFF'},{duration:500,easing:'swing'});
				}
			});
			error++;
		}
		
		if (c == '' || c == ' ') {
			$('#comment_comment').animate({
				backgroundColor:'#DA998E'
			},{
				duration:500,
				easing:'swing',
				complete:function() {
					$(this).animate({backgroundColor:'#FFFFFF'},{duration:500,easing:'swing'});
				}
			});
			error++;
		}
		
		if (error > 0) {
			return false;
		}
	});
}

function student_slideshow() {	
	$('ul#image_container li').each(function() {
		$(this).fadeOut(0);
		if ($(this).attr('class') == 'active') $(this).fadeIn(0);
	});
	
	// start slideshow
	var my_int = window.setInterval('auto_student_slideshow()',5000);
	
	
	$('a.b_circle').each(function() {
		$(this).click(function() {			
			var index =  $('a.b_circle').index(this);
				
			// find corresponding index of tables, turn all others off, turn corresponding on
			$('ul#image_container li').attr('class','');
			$('ul#image_container li').fadeOut(250);
			$('ul#image_container li').eq(index).attr('class','active');
			$('ul#image_container li').eq(index).fadeIn(250);
		
			// turn all tabs off, turn me on
			$('a.b_circle').attr('class','b_circle');
			$(this).attr('class','b_circle active');
			
			// stop auto slideshow once this is clicked
			window.clearInterval(my_int);
			$('a.b_pause').css('background-position','0px -7px');
			$('a.b_pause').attr('class','b_pause paused');
		
			return false;
		});
	});
	
	
	$('a.b_pause').click(function() {
		$(this).toggleClass('paused');
		if ($(this).hasClass('paused')) {
			window.clearInterval(my_int);
			$(this).css('background-position','0px -7px');
		} else {
			my_int = window.setInterval('auto_student_slideshow()',5000);
			$(this).css('background-position','0px 0px');
		}
		
		return false;
	});
}


function auto_student_slideshow(timeint) {
	var total = ($('ul#image_container li').length)-1;

	$('ul#image_container li.active').each(function() {
		$(this).fadeOut(250);
		var index = $('ul#image_container li').index(this);
		if (index >= total) {
			fade(0);
		} else {
			fade(index+1);
		}
	});
}

function fade(index) {
	$('ul#image_container li').attr('class','');
	$('ul#image_container li').eq(index).fadeIn(250);
	$('ul#image_container li').eq(index).attr('class','active')
	
	$('a.b_circle').attr('class','b_circle');
	$('a.b_circle').eq(index).attr('class','b_circle active');
}

function email_submit() {
	$('#email_submit').click(function() {
		var email = $('#email').val();
		var first_name = $('#first_name').val();
		var last_name = $('#last_name').val();
		
		$.ajax({
			type:"POST",
			url:"/library/ajax/email-signup-script.php",
			data:"email="+email+"&first_name="+first_name+"&last_name="+last_name,
			success:function(results) {
				if (results == 'Success') {
					var thankyou = '<div id="thankyou"><h1>Thank You</h1><p class="gray">You have been added to our email list.</p></div>';
					$('#email_signup').html(thankyou);
				} else {
					// report error message
					var error = '<div id="thankyou"><h1>Your request could not be completed.</h1><p class="gray">The following error occured while trying to process your request: '+results+'</p></div>';
					$('#email_signup').html(error);
				}
			}
		});
		
		return false;
	});
}

function mycarousel_itemVisibleInCallback(carousel, item, i, state, evt) {
    // The index() method calculates the index from a
    // given index who is out of the actual item range.
    var idx = carousel.index(i, mycarousel_itemList.length);
    carousel.add(i, mycarousel_getItemHTML(mycarousel_itemList[idx - 1]));
}

function mycarousel_itemVisibleOutCallback(carousel, item, i, state, evt) {
    carousel.remove(i);
}

function mycarousel_getItemHTML(item) {
    return '<a class="video_link" onclick="Javascript:play_video(\''+item.flv+'\',\''+item.poster+'\'); return false;" href="' + item.link + '"><span class="video_rollover"></span><img src="' + item.url + '" alt="' + item.title + '" /></a><p><a onclick="Javascript:play_video(\''+item.flv+'\',\''+item.poster+'\'); return false;" href="' + item.link + '">' + item.title + '</a> (' + item.length + ')</p>';
}


function play_video(flvpath, poster) {
	newMovie(poster,flvpath);
	return false;
}

function carousel_scroll() {
	jQuery('#mycarousel').jcarousel({
        // Configuration goes here
		scroll:4,
		wrap:'circular',
		buttonNextHTML:'<a id="c_right" href="#">Next</a>',
		buttonPrevHTML:'<a id="c_left" href="#">Previous</a>',
		itemVisibleInCallback: {onBeforeAnimation: mycarousel_itemVisibleInCallback},
		itemVisibleOutCallback: {onAfterAnimation: mycarousel_itemVisibleOutCallback}
    });
	$('a#c_left').click(function() { return false; });
	$('a#c_right').click(function() { return false; });
}

function tooltips() {
	$('a.tooltip').tooltip({
		track: true,
		delay: 0,
		showURL: false,
		fade: 250
	});
}

function sort_faculty_staff() {
	$('a.sort').each(function() {
		$(this).click(function() {
			var index =  $('a.sort').index(this);
				
			// find corresponding index of tables, turn all others off, turn corresponding on
			$('div.faculty_list').attr('class','faculty_list hide');
			$('div.faculty_list').eq(index).attr('class','faculty_list show');
		
			// turn all tabs off, turn me on
			$('a.sort').attr('class','bordered sort');
			$(this).attr('class','bordered sort active');
		
			return false;
			
		});
	});
}

function toggle_copy() {
	$('a.links').each(function() {
		$(this).click(function() {
			var index =  $('a.links').index(this);
				
			// find corresponding index of tables, turn all others off, turn corresponding on
			$('div.senior_copy').attr('class','senior_copy hide');
			$('div.senior_copy').eq(index).attr('class','senior_copy show');
		
			// turn all tabs off, turn me on
			$('a.links').attr('class','links');
			$(this).attr('class','links active');
		
			return false;
			
		});
	});
}

function toggle_faqs() {
	$('a.faq').each(function() {
		$(this).click(function() {
			var index =  $('a.faq').index(this);
			
			// find corresponding index of tables, turn all others off, turn corresponding on
			$('div.toggler').eq(index).toggleClass('hide');
		
			return false;			
		});
	});
}


function toggle_tabs() {
	$('a.tab').each(function() {
		$(this).click(function() {
			var index =  $('a.tab').index(this);
				
			// find corresponding index of tables, turn all others off, turn corresponding on
			$('div.tab_content').attr('class','tab_content hide');
			$('div.tab_content').eq(index).attr('class','tab_content show');
		
			// turn all tabs off, turn me on
			$('a.tab').attr('class','tab');
			$(this).attr('class','tab active');
		
			return false;
			
		});
	});
}

function blue_rollovers() {
	$('td.person').each(function() {
		$('a.thumbnail',this).each(function() { 
			$(this).prepend('<div class="blue"></div>');
			$('.blue',this).fadeTo(0,.44);
		});
		$(this).mouseover(function() {
			$('.blue',this).fadeTo(0,0);
		});
		$(this).mouseout(function() {
			$('.blue',this).fadeTo(0,.44);
		});
	});
}



function tabs() {
	$('ul.tab_navigation li a').each(function() {
		$(this).click(function(i,e) {			
			// get my index
			var index =  $('ul.tab_navigation li a').index(this);
				
			// find corresponding index of tables, turn all others off, turn corresponding on
			$('.tab_inner_content').attr('class','tab_inner_content hide');
			$('.tab_inner_content').eq(index).attr('class','tab_inner_content show');
		
			// turn all tabs off, turn me on
			$('ul.tab_navigation li a').attr('class','box_link');
			$(this).attr('class','box_link active');
		
			return false;
		});
	});
}

function drop_down() {
	/* IE 6 :hover Fix adapted for jquery, originally from A List Apart */
	$('#drop_down_select li').each(function(i) {
		$(this).mouseover(function() { $(this).addClass('sfhover'); });
		$(this).mouseout(function() { $(this).removeClass('sfhover'); });
	});
	$('#drop_down_select').each(function(i) {
		$(this).mouseover(function() { $(this).addClass('sfhover_ul'); });
		$(this).mouseout(function() { $(this).removeClass('sfhover_ul'); });
	});
}

function sfHover() {
	/* IE 6 :hover Fix adapted for jquery, originally from A List Apart */
	$('#main_nav li').each(function(i) {
		$(this).mouseover(function() { $(this).addClass('sfhover'); });
		$(this).mouseout(function() { $(this).removeClass('sfhover'); });
	});
	$('#people_nav li').each(function(i) {
		$(this).mouseover(function() { $(this).addClass('sfhover'); });
		$(this).mouseout(function() { $(this).removeClass('sfhover'); });
	});
}

function drop_down_class() {
	/* IE 6 :hover Fix adapted for jquery, originally from A List Apart */
	$('.drop_down_select li').each(function(i) {
		$(this).mouseover(function() { $(this).addClass('sfhover'); });
		$(this).mouseout(function() { $(this).removeClass('sfhover'); });
	});
	$('.drop_down_select').each(function(i) {
		$(this).mouseover(function() { $(this).addClass('sfhover_ul'); });
		$(this).mouseout(function() { $(this).removeClass('sfhover_ul'); });
	});
}

function socialHovers() {
	$('#social ul li a').each(function(i) {
		$(this).fadeTo(0,.5);
		$(this).mouseover(function() { $(this).fadeTo(0,1); });
		$(this).mouseout(function() { $(this).fadeTo(0,.5); });
	});
}
