$(document).ready(function(){
	
	$.ajaxSetup({ 
	    headers: {"X-Requested-With":"XmlHttpRequest"} 
	});
	
	if(navigator.userAgent.indexOf('MSIE 6') != -1)
	{
		DD_belatedPNG.fix('h1');
	}

	if(navigator.userAgent.indexOf('Windows') != -1)
	{
	//	$('#contents a').css('letter-spacing', '-1px');
	}
	
	$('ul.months').hide();
	$('a.year').click(function () {
		$('a.year').next().slideToggle("fast");
		return false;
	});
	
	$('#searchBox > form').submit(function(){
		if($('#search_str').val() == '')	
		{
			if(!$('#searchBox > p.error').is('p'))
			{
				$('#searchBox').append('<p class="error">Please enter a search keyword</p>');
			}
			return false;
		}
	});
	
	$('#search_str').focus(function(){
		$(this).val('');
	});
	
	$('#search_str').keyup(function(){
		if($('#search_str').val().match(/^'|^"/))
		{
			$('input#phrase').attr('checked', 'checked');
		}
		else
		{
			$('input#any').attr('checked', 'checked');
		}
	});
	
	$('div.childPages > h4').click(function(){
		$('div.childPages > ol').toggle();
	});
	
	$('div.childPages > ol').hide();
	
	$('ul.events').hide();
	
	$('#monthevents').click(function(){
		$(this).next().toggle('fast');
		return false;
	});
	
	$('#textsize').click(function(){

		
		$.ajax({
			type: "GET",
			url: "/textsize",
			data: "textsize=" + $('#textsize').attr('title'),
			dataType: "html",
			success: function(html){
				if($('#textsize').attr('title').match(/small/i))
				{
					$('body').css('font-size', '1em');
				}
				else
				{
					$('body').css('font-size', '1.2em');
				}

				$('#textsize').text(html);
				$('#textsize').attr('title', html);
			}
		});
		
		
		
		return false;
	});
	
	/**
	 *	Bulletin
	 */
	
	$('a.downloadbulletin').click(function(){
		location.href = $(this).attr('href');
		return false;
	});
	
	function searchBulletin(keyword)
	{
		if(!keyword)
		{
			alert('Please enter a keyword.')
			return;
		}
		
		$('#loadingSearch').addClass('loading');
		
		$.ajax({
			type: "GET",
			url: "/bulletin/search",
			data: 'keyword=' + keyword,
			dataType: "html",
			success: function(html){
				$('#loadingSearch').removeClass('loading');
				
				$('#bulletin').replaceWith(html);
			}
		});
	}
	
	$('#bulletinFilterForm').submit(function()
	{
		return false;
	});
	
	$("input[name='searchBulletin']").keypress(function (e){
		if (e.which == 13)
		{
			searchBulletin($("input[name='searchBulletin']").val());
		}
	});
	
	$('input#searchBulletin').click(function(){
		searchBulletin($("input[name='searchBulletin']").val());
		return false;
	});
	
	$('input#filterBulletin').click(function(){
		if(!$("select[name='year']").val() && !$("select[name='issue']").val())
		{
			alert('Please select a Year or an Issue number.')
			return;
		}
		$('#loading').addClass('loading');
		
		data = '';
	//	data = 'year=' + $("select[name='year']").val();
		if($("select[name='year']").val())
		{
			data += '&year=' + $("select[name='year']").val();
		}
		
		if($("select[name='issue']").val())
		{
			data += '&issue=' + $("select[name='issue']").val();
		}
		
		$.ajax({
			type: "GET",
			url: "/bulletin/filter",
			data: data,
			dataType: "html",
			success: function(html){
				$('#loading').removeClass('loading');
				
				$('#bulletin').replaceWith(html);
			}
		});
		return false;
	});

	/**
	 *	Conference
	 */
	
	$('a.downloadconference').click(function(){
		location.href = $(this).attr('href');
		return false;
	});
	
	function searchConference(keyword)
	{
		if(!keyword)
		{
			alert('Please enter a keyword.')
			return;
		}
		
		$('#loadingSearch').addClass('loading');
		
		$.ajax({
			type: "GET",
			url: "/conference/search",
			data: 'keyword=' + keyword,
			dataType: "html",
			success: function(html){
				$('#loadingSearch').removeClass('loading');
				$('#conference').replaceWith(html);
			}
		});
	}
	
	$('#conferenceFilterForm').submit(function()
	{
		return false;
	});
	
	$("input[name='searchConference']").keypress(function (e){
		if (e.which == 13)
		{
			searchConference($("input[name='searchConference']").val());
		}
	});
	
	$('input#searchConference').click(function(){
		searchConference($("input[name='searchConference']").val());
		return false;
	});
	
	$('input#filterConference').click(function(){
		if(!$("select[name='year']").val())
		{
			alert('Please select a Year.')
			return;
		}
		$('#loading').addClass('loading');
		
		data = '';
	//	data = 'year=' + $("select[name='year']").val();
		if($("select[name='year']").val())
		{
			data += '&year=' + $("select[name='year']").val();
		}
		
		$.ajax({
			type: "GET",
			url: "/conference/filter",
			data: data,
			dataType: "html",
			success: function(html){
				$('#loading').removeClass('loading');
				
				$('#conference').replaceWith(html);
			}
		});
		return false;
	});
	
	$('.extract').hover(function(){
		$(".extract_caption", this).stop().animate({top:'0px'},{queue:false,duration:300});
	}, function() {
		$(".extract_caption", this).stop().animate({top:'-400px'},{queue:false,duration:300});
	});
	
	$('div.extract_caption').click(function(){
		location.href = $(this).find('a').attr('href');
	});
});
