$(document).ready(function(){
	
	// Initialize gallery
	$('ul.gallery').galleria({
		history : false,
		insert : '#stage',
		onImage : function(image,caption,thumb) {

			// fade in the image & caption
			if(! ($.browser.mozilla && navigator.appVersion.indexOf("Win")!=-1) ) { // FF/Win fades large images terribly slow
				image.css('display','none').fadeIn(1000);
			}
			caption.css('display','none').fadeIn(1000);

			// fetch the thumbnail container
			var _li = thumb.parents('li');

			// fade out inactive thumbnail
			_li.siblings().children('img.selected').fadeTo(500,0.3);

			// fade in active thumbnail
			thumb.fadeTo('fast',1).addClass('selected');

			// add a title for the clickable image
			image.attr('title','Next image >>');
		},
		onThumb : function(thumb) { // thumbnail effects goes here

			// fetch the thumbnail container
			var _li = thumb.parents('li');

			// if thumbnail is active, fade all the way.
			var _fadeTo = _li.is('.active') ? '1' : '0.3';

			// fade in the thumbnail when finnished loading
			thumb.css({display:'none',opacity:_fadeTo}).fadeIn(1500);

			// hover effects
			thumb.hover(
				function() { thumb.fadeTo('fast',1); },
				function() { _li.not('.active').children('img').fadeTo('fast',0.3); } // don't fade out if the parent is active
			)
		}

	});

	// Timer for gallery image changes
	$('ul.gallery').everyTime(5000, function(e) {
		$.galleria.next();
	});
	
	// Show/hide for more info on stats
	$('.map-tip').tooltip({
		effect: 'fade',
		position: 'bottom center',
		offset: [-190, 0],
		events: {
			def: 'click,mouseleave'
			}
	});
	
	// Colorbox script for campaign phases
	$("a[rel='phase1-gallery']").colorbox({
		transition:"elastic",
		current: "Photo {current} of {total}",
		opacity: 0.5
	});
	
	$("a[rel='phase2-gallery']").colorbox({
		transition:"elastic",
		current: "Photo {current} of {total}",
		opacity: 0.5
	});
	
	$("a[rel='phase3-gallery']").colorbox({
		transition:"elastic",
		current: "Photo {current} of {total}",
		opacity: 0.5
	});
	
	// Add classes to H2 on FAQ pages	
	var faqTest = $("#modulePage7109653 #content h2:first > a").length;
	if (faqTest != 0) {
		$("#modulePage7109653 #content h2").addClass("faq-more");
		$("#modulePage7109653 #content h2 a").last().addClass("faq-active");
		$("#modulePage7109653 #content .header").hide();
	}
	
	if (faqTest == 3) {
		$("#modulePage7109653 #content h2 a").last().addClass("faq-active-question");
	}
	
	if ($(".search-results").length) {
		$("#primaryStylesheet").attr('href', '/display/common.css?styleId=793701&amp;RK=1277409525420&amp;CE=21');
		$("#content").addClass("search-results-page");
	}
	
	var matTest = $("#modulePage7109664 #content h2:first > a").length;
	if (matTest != 0) {
		$("#modulePage7109664 #content h2").addClass("faq-more");
		$(".file-item-list").addClass("file-category");
	}
	
	var forumTest = $("#modulePage7109667 #content h2:first > a").length;
	if (forumTest != 0) {
		$("#modulePage7109667 #content h2").addClass("faq-more");
		$(".discussion-comment-thread div:first").addClass("post-main");
	}
	
	// Check if page is password recovery page
	if ($('#recover_email').length) {
		$('body').addClass("password-recovery");
	}
	

	// Check if document-title is present
	if ($('.document-title').length) {
		var h2Test = $('.document-title').html().trim();
		
		// check for 404 page
		if (h2Test == "Page Not Found") {
			$('body').addClass("page-404");
		}
		// Check if page login page being accesses on backend
		if (h2Test == "Website Login") {
			$('body').attr('id', 'modulePage7082969');
			$("#primaryStylesheet").attr('href', '/display/common.css?styleId=788565&amp;RK=1277838045840&amp;CE=21');
		}
		// Check if page is initial Search page
		if (h2Test == "Search") {
			$('.document-title').addClass("search-title");
			$('.body').before('<p class=\"search-p\">Search the Campaign Management Center and the entire get.geekthelibrary.org site.</p>')
		}
	}
	
	// Add class to remember password container
	$("#rememberPassword").next().addClass("remember-pass");
	$("#login-form").append('<div class=\"login-extra\"><p>Geek the Library is now open to all U.S. public libraries. Fill out our short online form and someone will contact you with more information.</p><p><strong><a title=\"\" href=\"/register-for-geek-the-library/\">Start here &raquo;</a></strong></p></div>');
	
	
	// Check if page is profile update page
	if ($('#informationArea').length) {
		$('body').addClass("member-profile");
		$("#primaryStylesheet").attr('href', '/display/common.css?styleId=793701&amp;RK=1277409525420&amp;CE=21');
		
		$('#subscriptionsArea .notice-box > div').addClass('member-subscription');
		$('#subscriptionsArea .notice-box div:first').removeClass('member-subscription');
		
		$('.header input:first').addClass('member-active-panel');
		
		$('.header input').click(function(){
			$('.header input').removeClass('member-active-panel');
			$(this).addClass('member-active-panel');
		});
	}
	
	// Check if page is blog page
	if ($('#modulePage7237431').length) {
		$('#content').before('<div class=\"cmc-heading\"><h2>Campaign Blog</h2><p>Stay informed about the latest news, tips and information regarding the campaign.<p></div>');
	}
	
	// Check if page is campaign material page
	if ($('#modulePage7109664').length) {
		if ($('#directory1309365').length) {
			$('h2.document-title').after('<div class=\"cmc-heading\"><p>Everything You Need to Launch Geek the Library</p></div><p class=\"materials-copy\">Here are all of the art files and supplementary material (kits, presentations, informative guides, etc.) that you need to launch and execute the awareness campaign. As you move through each phase, please stop back to download the appropriate material. In addition to this comprehensive repository, a sidebar—on the right-hand side of each page—directs you to appropriate materials for that particular stage of the campaign. (Please note that access to these files is for your library staff only, and the files must not be passed on to staff from other libraries that have not completed the registration process.)</p>');
		}
		
		$(".icon img[title='File']").parent().parent().addClass('single-file');
		$(".icon img[title='File']").parent().parent().parent().addClass('single-file-list');
	}
	
	// Check if page is contact page
	if ($('#modulePage7197173').length) {
		$('#formField1Subtitle').text('Send us your comments or questions, and someone will get back to you as soon as possible.');
		$('.document-title').text('Get in Touch');
	}
	
	// Check if page is forum page
	if ($('#modulePage7109667').length) {
		if ($('.discussion-comment-thread').length) {
			} else {
				$('h2.document-title').after('<div class=\"cmc-heading\"><p>Use this forum to share ideas with other participating public libraries. Ask questions, answer questions, post success stories, talk about challenges, etc.</p></div>');
			}
	}
	
	if ($('.member-profile ').length) {
		$('.member-profile .header br').remove();
	}
	
	
	// Add syles to search result types 
	if ($('#modulePage7544305').length) { 
		$('.search-result-type').each(function(){
			var resultType = $(this).html();
			if (resultType == "FAQ Entry") {
				$(this).addClass('result-faq');
			}
			if (resultType == "Discussion Post") {
				$(this).addClass('result-forum');
			}
			if (resultType == "Discussion Post Reply") {
				$(this).addClass('result-forum');
			}
			if (resultType == "Page") {
				$(this).addClass('result-page');
			}
			if (resultType == "Journal Entry") {
				$(this).addClass('result-journal');
			}
			if (resultType == "File") {
				$(this).addClass('result-file');
			}
		});
		
	}
	
});
