$(document).ready(function(){
    /* begin grants table stuff */
    $("table#grants_awarded").before('<div id="filterset"><form><label for="organization_name">Organization Name: <input name="organization_name" type="text" /></label><select name="program_area"></select><select name="fund_name"><option value="">Fund Name</option><option value="AC">AC &ndash; Arts &amp; Culture - Field of Interest</option><option value="CY">CY &ndash; Children &amp; Youth - Field of Interest</option><option value="CEO">CEO &ndash; CEO Discretionary</option><option value="CR">CR &ndash; Community Revitalization</option><option value="EN">EN &ndash; Environment</option><option value="GIA">GIA &ndash; Grants Into Action</option><option value="HH">HH &ndash; Health &amp; Human Services - Field of Interest</option><option value="MC">MC &ndash; McClure</option><option value="NW">NW &ndash; Noah&#39;s Wish</option><option value="UN">UN &ndash; Unrestricted</option></select><select name="year"></select></form><button id="reset_filter">reset</button></div>');
    /* Create the Program Areas select options. */
    grant_program_areas = [];
    grant_program_areas.push("");
    $('#grants_awarded tr td:nth-child(6)').each(function(){
        value = $(this).text();
        value = value.replace(/^\s+/, '');
        var n = grant_program_areas.length;
        var found = false;
        while(n--) {
            if (value == grant_program_areas[n]) {
                found = true; 
                break;
            }
        }
        if (!found) {
            grant_program_areas.push(value);
        }
    });
    grant_program_areas.sort();
    $.each(grant_program_areas, function(key, value) {
        displayValue = value;
        if (value == '') {
            displayValue = 'Program Area';
        }
        $('#filterset select[name=program_area]').append('<option value="' + value + '">' + displayValue + '</option>');
    });
    /* Create the Year select options. */
    grant_years = [];
    grant_years.push("");
    $('#grants_awarded tr td:nth-child(4)').each(function(){
        value = $(this).text();
        value = value.replace(/^\s+/, '');
        var n = grant_years.length;
        var found = false;
        while(n--) {
            if (value == grant_years[n]) {
                found = true; 
                break;
            }
        }
        if (!found) {
            grant_years.push(value);
        }
    });
    grant_years.sort();
    $.each(grant_years, function(key, value) {
        displayValue = value;
        if (value == '') {
            displayValue = 'Year';
        }
        $('#filterset select[name=year]').append('<option value="' + value + '">' + displayValue + '</option>');
    });
    var grantsTable = null;
    grantsTable = $('#grants_awarded').dataTable({
        "aoColumns": [
            null,
            null,
            null,
            null,
            null,
            { "bVisible": false }
        ],
        "bFilter": false,
        "iDisplayLength": 100
    });
    /* Add search filters. */
    $("#filterset input[name=organization_name]").keyup(function(){
        grantsTable.fnFilter(this.value, 0);
    });
    $("#filterset select[name=fund_name]").change(function(){
        grantsTable.fnFilter(this.value, 2);
    });
    $("#filterset select[name=year]").change(function(){
       grantsTable.fnFilter(this.value, 3); 
    });
    $("#filterset select[name=program_area]").change(function(){
        grantsTable.fnFilter(this.value, 5);
    });
    /* Add reset button. */
    $('#reset_filter').click( function() {
        grantsTable.fnFilter('', 0);
        grantsTable.fnFilter('', 2);        
        grantsTable.fnFilter('', 3);
        grantsTable.fnFilter('', 5);
        $("#filterset input[name=organization_name]").val('');
        $("#filterset select[name=program_area]").val('');
        $("#filterset select[name=fund_name]").val('');        
        $("#filterset select[name=year]").val('');
    } );
    /* end grants table stuff */

	$('#navigationSecondary li ul').each(function(){
		$(this).parent('#navigationSecondary li').children('a').css({
			'background-image':'url(/images/icon_closed.gif)',
			'background-repeat':'no-repeat',
			'background-position':'5px 5px'
		}).click(function(){
			$(this).next('ul').slideToggle(250);
			return false;			
		}).toggle(
			function(){
				$(this).css('background-image','url(/images/icon_open.gif)');
			},
			function(){
				$(this).css('background-image','url(/images/icon_closed.gif)');
			}
		);
	});
	$('.current_page_item').each(function(){
		//alert($('.current_page_parent>a').html());												
		$('.current_page_ancestor>ul').slideToggle(250);	  
	    $('.current_page_ancestor>a')
	        .css('background-image','url(/images/icon_open.gif)')
	        .unbind().click(function(){return false});
	});
	$('#navigationSecondary ul, #navigationSecondary li, #navigationSecondary a').css({
		'visibility':'hidden'
	}).css({
		'visibility':'visible',
		'z-index':'5000',
		'overflow':'visible',
		'_height':'0',
		'zoom':'1'
	});
	$('.archives li ul').each(function(){
		$(this).parent('#navigationSecondary li').children('a').css({
			'background-image':'url(/images/icon_closed.gif)',
			'background-repeat':'no-repeat',
			'background-position':'0px 6px'
		}).click(function(){
			$(this).next('ul').slideToggle(250);
			return false;			
		}).toggle(
			function(){
				$(this).css('background-image','url(/images/icon_open.gif)');
			},
			function(){
				$(this).css('background-image','url(/images/icon_closed.gif)');
			}
		);
	});
	$(".comments").each(function(){
		$(this).children('.expand').css({
			'background-image':'url(/images/icon_closed.gif)',
			'background-repeat':'no-repeat'
		}).click(function(){
			$('.commentContainer').slideToggle(300);
			return false;
		}).toggle(
			function(){
				$(this).css('background-image','url(/images/icon_open.gif)');
			},
			function(){
				$(this).css('background-image','url(/images/icon_closed.gif)');
			}
		);
	});
	$('#comments .commentContainer').css('display', 'none');
	$(':header a').css('text-decoration','none');	
	$(':header + .byline').css({'float':'none','margin-bottom':'20px'});
	/* header fade */
	$('#gnofheader').cycle({speed:7000});
	
	$('#content.blog .comment:even').css({
		'background-color': '#FDEFCE'
	});
	$('#searchBox + blockquote').css('margin-top','-5px');
	fontStep = 0;
	$(".fontSize").click(function(){
		if(fontStep == 0){
			var currentFontSize = $('#content').css('font-size');
			var currentFontSizeNum = parseFloat(currentFontSize, 10);
			var newFontSize = currentFontSizeNum*1.2;
			//$("#content, #content .readmore, #content p").css('font-size',newFontSize);
			//$("#content :header").css('font-size',newFontSize*1.2);
			$("#content").css('font-size',newFontSize);
			fontStep++;
		}else if(fontStep == 1){
			var currentFontSize = $('#content').css('font-size');
			var currentFontSizeNum = parseFloat(currentFontSize, 10);
			var newFontSize = currentFontSizeNum*1.2;
			//$("#content, #content .readmore, #content p").css('font-size',newFontSize);
			//$("#content :header").css('font-size',newFontSize*1.2);
			$("#content").css('font-size',newFontSize);

			fontStep++;
		}else if(fontStep == 2){
			//$("#content, #content .readmore, #content p").css('font-size','12px');
			//$("#content :header").css('font-size','16px');
			$("#content").css('font-size','12px');
			fontStep=0;
		}
		return false;
	});
	/* BB Comment Incremental Search */
	$('#commentSearchBox').submit(function(e) {e.preventDefault();});
	$('#commentSearchBox input[name=commentInput]').keyup(function(){
	    $('p.none_found').remove();
	    var searchString = this.value.toLowerCase();
	    $('div.comment').each(function(){
	        var content = $(this).text().toLowerCase();
	        content = content.replace('posted by', '');
	        content = content.replace('|', '');
	        if (content.indexOf(searchString) == -1) {
	            $(this).css("display", "none");
	        }
	        else {
	            $(this).css("display", "block");
	        }
	    });
	    var numVisible = $("div.comment:visible").length;
	    if (numVisible > 0) {
	        $("div.comment:visible:even").css("background-color", "rgb(253, 239, 206)");
	        $("div.comment:visible:odd").css("background-color", "rgb(255, 255, 255)");
	    }
	    else {
	        //$('div.comments').prepend('<p class="none_found">No comments matched your search.</p>');
	        $('div#comments').prepend('<p class="none_found">No comments matched your search.</p>');
	    }
	});
		
	// jQuery slideToggle
	/* 
	this script works with the code that Wordpress automatically outputs and only requires a small amount of hand coding on the link.
	To use this script make a link with href='#' and add class='toggleButton' in the html view, now back on the WYSIWYG editor make a new 
	paragraph under the link containing the text you want to toggle and the script will take care of the rest. If you have multiple paragraphs 
	for the slide down then you need to wrap them all in a div.
	*/
	$('.toggleButton').parent().next('p,div').hide();
        $('.toggleButton').click(function() {
			$(this).parent().next('p,div').slideToggle('slow');
			    return false;
         });
         
    
    //Give Now
	//In Honor/In Memory modded slide code and input clearing
		$('a.slideClear').parent().next('p,div').hide();
			$('a.slideClear').click(function() {
				if ($(this).parent().next('p,div').is(':visible'))
				{
					return false;
				}
				else
				{
					$('a.slideClear').parent().next('p,div').hide();
					$(this).parent().next('p,div').slideToggle('fast');
					return false;
				}
			});
		
		$('#honor').focus(function(){
			document.getElementById('memory').value = '';
		});
		$('#memory').focus(function(){
			document.getElementById('honor').value = '';
		});
	
	//form verification
	$('#form_paypal').submit(function()
		{
			if ( $('input.select_fund:checked').length !=0 || $('#name_fund:checked').length == 1 && $('#fund_name').val() != "") // check that a fund is selected
			{
				$('#msg_noselection').hide();
				$('#msg_specify_named_fund').hide();
				if ($('#input_amount').val() < 1) //check donation amount
				{
					$('#msg_moreamount').fadeIn();
					return false; // prevent the form from submitting
				}
				else
				{
					$('#msg_moreamount').hide();
					$('#msg_activity').fadeIn();
					return true; // submit the form
				}
			}
			else
			{
				$('#msg_moreamount').hide();
				if ($('#name_fund:checked').length == 1 && $('#fund_name').val() == "") // If "name fund" is selected check that a fund is named
				{
					$('#msg_noselection').hide();
					$('#msg_specify_named_fund').fadeIn();
				}
				else
				{
					$('#msg_specify_named_fund').hide();
					$('#msg_noselection').fadeIn();
				}
				return false; // prevent the form from submitting
			}
		});
	$("a").attr('target','_blank');
	$("a[href*='www.gnof.org']").attr('target','');
});