jQuery(document).ready(function(){
    /* begin grants table stuff */
    jQuery("table#grants_awarded").before('<div id="filterset"><form><label for="organization_name">Organization Name: <input name="organization_name" type="text" /></label><select name="fund_name"><option value="">Fund Name</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="GCOS">GCOS &ndash; Gulf Coast Oil Spill Fund</option><option value="AC">AC &ndash; IMPACT Arts &amp; Culture</option><option value="CY">CY &ndash; IMPACT Children &amp; Youth</option><option value="CIV">CIV &ndash; IMPACT Civic Engagement</option><option value="ED">ED &ndash; IMPACT Education</option><option value="HEA">HEA &ndash; IMPACT Health</option><option value="SS">SS &ndash; IMPACT Social Services</option><option value="MC">MC &ndash; McClure</option><option value="MET">MET &ndash; Metropolitan Opportunities</option><option value="TRA">TRA &ndash; Transparency Project Planning Fund</option></select><select name="year"></select></form><button id="reset_filter">reset</button></div>');
    /* Create the Year select options. */
    grant_years = [];
    grant_years.push("");
    jQuery('#grants_awarded tr td:nth-child(4)').each(function(){
        value = jQuery(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();
    jQuery.each(grant_years, function(key, value) {
        displayValue = value;
        if (value == '') {
            displayValue = 'Year';
        }
        jQuery('#filterset select[name=year]').append('<option value="' + value + '">' + displayValue + '</option>');
    });
    var grantsTable = null;
    grantsTable = jQuery('#grants_awarded').dataTable({
        "aoColumns": [
            null,
            null,
            null,
            null,
            null
        ],
        "bFilter": false,
        "iDisplayLength": 100
    });
    /* Add search filters. */
    jQuery("#filterset input[name=organization_name]").keyup(function(){
        grantsTable.fnFilter(this.value, 0);
    });
    jQuery("#filterset select[name=fund_name]").change(function(){
        grantsTable.fnFilter(this.value, 2);
    });
    jQuery("#filterset select[name=year]").change(function(){
       grantsTable.fnFilter(this.value, 3); 
    });
    /* Add reset button. */
    jQuery('#reset_filter').click( function() {
        grantsTable.fnFilter('', 0);
        grantsTable.fnFilter('', 2);        
        grantsTable.fnFilter('', 3);
        jQuery("#filterset input[name=organization_name]").val('');
        jQuery("#filterset select[name=fund_name]").val('');        
        jQuery("#filterset select[name=year]").val('');
    } );
    /* end grants table stuff */
	
	/*
	jQuery('#navigationSecondary li ul').each(function(){
		jQuery(this).parent('#navigationSecondary li').children('a').css({
			'background-image':'url(/images/icon_closed.gif)',
			'background-repeat':'no-repeat',
			'background-position':'5px 5px'
		}).click(function(){
			jQuery(this).next('ul').slideToggle(250);
			return false;			
		}).toggle(
			function(){
				jQuery(this).css('background-image','url(/images/icon_open.gif)');
			},
			function(){
				jQuery(this).css('background-image','url(/images/icon_closed.gif)');
			}
		);
	});
	jQuery('.current_page_item').each(function(){
		//alert(jQuery('.current_page_parent>a').html());												
		jQuery('.current_page_ancestor>ul').slideToggle(250);	  
	    jQuery('.current_page_ancestor>a')
	        .css('background-image','url(/images/icon_open.gif)')
	        .unbind().click(function(){return false});
	});
	jQuery('#navigationSecondary ul, #navigationSecondary li, #navigationSecondary a').css({
		'visibility':'hidden'
	}).css({
		'visibility':'visible',
		'z-index':'5000',
		'overflow':'visible',
		'_height':'0',
		'zoom':'1'
	});
	jQuery('.archives li ul').each(function(){
		jQuery(this).parent('#navigationSecondary li').children('a').css({
			'background-image':'url(/images/icon_closed.gif)',
			'background-repeat':'no-repeat',
			'background-position':'0px 6px'
		}).click(function(){
			jQuery(this).next('ul').slideToggle(250);
			return false;			
		}).toggle(
			function(){
				jQuery(this).css('background-image','url(/images/icon_open.gif)');
			},
			function(){
				jQuery(this).css('background-image','url(/images/icon_closed.gif)');
			}
		);
	});
	*/
	jQuery(".comments").each(function(){
		jQuery(this).children('.expand').css({
			'background-image':'url(/images/icon_closed.gif)',
			'background-repeat':'no-repeat'
		}).click(function(){
			jQuery('.commentContainer').slideToggle(300);
			return false;
		}).toggle(
			function(){
				jQuery(this).css('background-image','url(/images/icon_open.gif)');
			},
			function(){
				jQuery(this).css('background-image','url(/images/icon_closed.gif)');
			}
		);
	});
	jQuery('#comments .commentContainer').css('display', 'none');
	jQuery(':header a').css('text-decoration','none');	
	jQuery(':header + .byline').css({'float':'none','margin-bottom':'20px'});
	/* header fade */
	if (jQuery('#gnofheader').len!=0){
	jQuery('#gnofheader').cycle({speed:7000});
	};
	jQuery('#content.blog .comment:even').css({
		'background-color': '#FDEFCE'
	});
	jQuery('#searchBox + blockquote').css('margin-top','-5px');
	fontStep = 0;
	jQuery(".fontSize").click(function(){
		if(fontStep == 0){
			var currentFontSize = jQuery('#content').css('font-size');
			var currentFontSizeNum = parseFloat(currentFontSize, 10);
			var newFontSize = currentFontSizeNum*1.2;
			//jQuery("#content, #content .readmore, #content p").css('font-size',newFontSize);
			//jQuery("#content :header").css('font-size',newFontSize*1.2);
			jQuery("#content").css('font-size',newFontSize);
			fontStep++;
		}else if(fontStep == 1){
			var currentFontSize = jQuery('#content').css('font-size');
			var currentFontSizeNum = parseFloat(currentFontSize, 10);
			var newFontSize = currentFontSizeNum*1.2;
			//jQuery("#content, #content .readmore, #content p").css('font-size',newFontSize);
			//jQuery("#content :header").css('font-size',newFontSize*1.2);
			jQuery("#content").css('font-size',newFontSize);

			fontStep++;
		}else if(fontStep == 2){
			//jQuery("#content, #content .readmore, #content p").css('font-size','12px');
			//jQuery("#content :header").css('font-size','16px');
			jQuery("#content").css('font-size','12px');
			fontStep=0;
		}
		return false;
	});
	/* BB Comment Incremental Search */
	jQuery('#commentSearchBox').submit(function(e) {e.preventDefault();});
	jQuery('#commentSearchBox input[name=commentInput]').keyup(function(){
	    jQuery('p.none_found').remove();
	    var searchString = this.value.toLowerCase();
	    jQuery('div.comment').each(function(){
	        var content = jQuery(this).text().toLowerCase();
	        content = content.replace('posted by', '');
	        content = content.replace('|', '');
	        if (content.indexOf(searchString) == -1) {
	            jQuery(this).css("display", "none");
	        }
	        else {
	            jQuery(this).css("display", "block");
	        }
	    });
	    var numVisible = jQuery("div.comment:visible").length;
	    if (numVisible > 0) {
	        jQuery("div.comment:visible:even").css("background-color", "rgb(253, 239, 206)");
	        jQuery("div.comment:visible:odd").css("background-color", "rgb(255, 255, 255)");
	    }
	    else {
	        //jQuery('div.comments').prepend('<p class="none_found">No comments matched your search.</p>');
	        jQuery('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.
	*/
	jQuery('.toggleButton').parent().next('p,div').hide();
        jQuery('.toggleButton').click(function() {
			jQuery(this).parent().next('p,div').slideToggle('slow');
			    return false;
         });
         
    
    //Give Now
	//In Honor/In Memory modded slide code and input clearing
		jQuery('a.slideClear').parent().next('p,div').hide();
			jQuery('a.slideClear').click(function() {
				if (jQuery(this).parent().next('p,div').is(':visible'))
				{
					return false;
				}
				else
				{
					jQuery('a.slideClear').parent().next('p,div').hide();
					jQuery(this).parent().next('p,div').slideToggle('fast');
					return false;
				}
			});
		
		jQuery('#honor').focus(function(){
			document.getElementById('memory').value = '';
		});
		jQuery('#memory').focus(function(){
			document.getElementById('honor').value = '';
		});
		jQuery('#msg_noaddress').hide();
	//form verification
	jQuery('#form_paypal').submit(function()
		{
		    var donor_info = '';
		    jQuery('[name="fname"],[name="lname"],[name="email"],[name="address"],[name="city"],[name="state"],[name="zip"]').each(
			function(){
			    if(jQuery(this).val()!=''){
				donor_info = donor_info+jQuery(this).attr('name')+': '+jQuery(this).val()+', ';
			    }
			}
		    );
		    jQuery('[name="os1"]').val(donor_info);
		    
		    var donation_info = '';
		    jQuery('[name="anonymous"]:checked,[name="name_fund"],[name="honor"],[name="memory"]').each(
			function(){
			    if(jQuery(this).val()!=''){
				donation_info = donation_info+jQuery(this).attr('name')+': '+jQuery(this).val()+', ';
			    }
			}
		    );
		    jQuery('[name="os0"]').val(donation_info);
		    
		    if(jQuery('[name="acknowledgement"]:checked').val()=='true' && jQuery('[name="os1"]').val()==''){
			jQuery('#msg_noaddress').fadeIn();
			return false;
		    }
			if ( jQuery('input.select_fund:checked').length !=0 || jQuery('#name_fund:checked').length == 1 && jQuery('#fund_name').val() != "") // check that a fund is selected
			{
				jQuery('#msg_noselection').hide();
				jQuery('#msg_specify_named_fund').hide();
				jQuery('#msg_noaddress').hide();
				    if (jQuery('#input_amount').val() < 1) //check donation amount
				    {
					    jQuery('#msg_moreamount').fadeIn();
					    return false; // prevent the form from submitting
				    }
				    else
				    {
					    
					    jQuery('#msg_moreamount').hide();
					    if(jQuery('#input_amount').val() > 249&&jQuery('[name="os1"]').val()==' ,,, , , '){
						jQuery('#msg_noaddress').fadeIn();
						return false;
					    }else{
						jQuery('#msg_noaddress').hide();
						jQuery('#msg_activity').fadeIn();
						return true; // submit the form
					    }
				    }
				}
			else
			{
				jQuery('#msg_moreamount').hide();
				if (jQuery('#name_fund:checked').length == 1 && jQuery('#fund_name').val() == "") // If "name fund" is selected check that a fund is named
				{
					jQuery('#msg_noselection').hide();
					jQuery('#msg_specify_named_fund').fadeIn();
				}
				else
				{
					jQuery('#msg_specify_named_fund').hide();
					jQuery('#msg_noselection').fadeIn();
				}
				return false; // prevent the form from submitting
			}
		});
		
	//form verification
	jQuery('#form_paypal_pip').submit(function()
		{
			jQuery('#msg_activity').hide();
			jQuery('#msg_noselection').hide();
			jQuery('#msg_specify_named_fund').hide();
			jQuery('#msg_noaddress').hide();
		    jQuery('#msg_moreamount').hide();
		    
			if (jQuery('#input_amount').val() < 1) //check donation amount
		    {
			    jQuery('#msg_moreamount').fadeIn();
			    return false; // prevent the form from submitting
		    }
		    
			var is_error = false;
			// #pip_email is to allow for email input in comments form
		    jQuery('[name="first_name"],[name="last_name"],#pip_email,[name="address1"],[name="city"],[name="state"],[name="zip"],[name="night_phone_a"],[name="night_phone_b"],[name="night_phone_c"]').each(
				function(){
				    if(!jQuery(this).val()) {
				    	is_error = true;
				    }
				}
		    );
		    if (is_error) {
		    	jQuery('#msg_noaddress').fadeIn();
				return false;
		    }
		    
			if ( jQuery('input.select_fund:checked').length !=0 || jQuery('#name_fund:checked').length == 1 && jQuery('#fund_name').val() != "") // check that a fund is selected
			{
			    if(jQuery('#input_amount').val() > 249&&jQuery('[name="os1"]').val()==' ,,, , , '){
					jQuery('#msg_noaddress').fadeIn();
					return false;
			    } else {
//			    	var donate_level = jQuery('[name="to_donate"]:checked').attr('id').toLowerCase().replace(/\b[a-z]/g, function(letter) {return letter.toUpperCase();});
//					jQuery('input.item_name').val('');
//					var fund_name = jQuery('input.select_fund').val() + " (2011)";
//					jQuery('input.item_name').val(fund_name);
					
//					var domLastName = jQuery('[name="last_name"]');
//					var domLname = jQuery('[name="lname"]');
//					var company_name = jQuery('[name="company_name"]').val();
//					if (company_name) {
//						company_name = " (" + company_name + ")";
//					}
//					domLastName.val('');
//					domLastName.val(domLname.val() + company_name);
					
					var donor_address = '[';
				    jQuery('[name="company_name"],[name="email"],[name="address1"],[name="address2"],[name="city"],[name="state"],[name="zip"],[name="phone"]').each(
					function(){
					    if(jQuery(this).val()!=''){
						donor_address = donor_address+jQuery(this).attr('title')+': '+jQuery(this).val()+', ';
					    }
					}
				    );
				    donor_address = donor_address.slice(0,-2);
				    donor_address += "]";
				    jQuery('[name="os0"]').val('');
				    jQuery('[name="os0"]').val(donor_address);
				    
				    var first_name = jQuery('[name="first_name"]').val();
				    var last_name = jQuery('[name="last_name"]').val();
				    var donor_name = jQuery('[name="donor_name"]').val();
				    var comment = jQuery('[name="comments"]').val();
				    if(jQuery('[name="anonymous"]').attr('checked')) {
				    	donor_name = "Anonymous";
				    }
				    jQuery('[name="os1"]').val('');
				    jQuery('[name="os1"]').val("[First Name: " + first_name + ", Last Name: " + last_name + ", Annual Report Name: " + donor_name + ", Donor Comment: " + comment + "]");
				    
					jQuery('#msg_noaddress').hide();
					jQuery('#msg_activity').fadeIn();
					return true; // submit the form
			    }
			}
			else
			{
				jQuery('#msg_moreamount').hide();
				if (jQuery('#name_fund:checked').length == 1 && jQuery('#fund_name').val() == "") // If "name fund" is selected check that a fund is named
				{
					jQuery('#msg_noselection').hide();
					jQuery('#msg_specify_named_fund').fadeIn();
				}
				else
				{
					jQuery('#msg_specify_named_fund').hide();
					jQuery('#msg_noselection').fadeIn();
				}
				return false; // prevent the form from submitting
			}
		});
	selection=jQuery('a').not("a[href*='www.gnof.org'],a[href*='/'],a[href*='javascript:window.print();']");
	selection.attr('target','_blank');
	
	
	/* slidedeck dot navigation

    // Script for handling bullet navigation for SlideDeck
    var SlideDeckAssistant = {
    	innerSlideCount: 0,
    	// prevSlide, nextSlide and goToSlide are all custom functions to control progress of the SlideDeck 
    	prevSlide: function(theIndex){
    		SlideDeckAssistant.goToSlide(theIndex - 1);
    	},
    	nextSlide: function(theIndex){
    		SlideDeckAssistant.goToSlide(theIndex + 1);
    	},
    	goToSlide: function(theIndex){        
    		jQuery('ul.galleryNav li').removeClass('active');
    		jQuery('ul.galleryNav li:eq('+ theIndex +')').addClass('active');
    		var myDeck = jQuery('.slidedeck_frame dl').slidedeck();
    		myDeck.goTo(theIndex+1);
    		if((theIndex + 1) == SlideDeckAssistant.innerSlideCount){
    			// disable the next button
    			jQuery('ul.galleryArrows .next').addClass('disabled');
    			jQuery('ul.galleryArrows .prev').removeClass('disabled');
    		}else if((theIndex + 1) == 1){
    			// disable the previous button
    			jQuery('ul.galleryArrows .next').removeClass('disabled');
    			jQuery('ul.galleryArrows .prev').addClass('disabled');
    		}else{
    			// enable both next/previous buttons
    			jQuery('ul.galleryArrows .next, ul.galleryArrows .prev').removeClass('disabled');
    		}
    	},
    	init: function(){
    		SlideDeckAssistant.innerSlideCount = jQuery('.skin-GNOF dl.slidedeck dd').length;
    		// Store number of slides in SlideDeck
    		
    		jQuery('.skin-GNOF').append('<ul class="galleryNav"><\/ul>');
    		// create bullet navigation element
    		
    		for (i=0 ; i < SlideDeckAssistant.innerSlideCount ; i++ ) {
    			jQuery('ul.galleryNav').append('<li><a href="goto#' + (i+1) + '">' + (i+1) + '<\/a><\/li>');
    		}
    		// create navigation bullets based on number of slides
    		
    		var goToDots = jQuery('ul.galleryNav li a');
    		jQuery('ul.galleryNav').css({marginLeft: '-' + (goToDots.outerWidth(true)*goToDots.length / 2) + 'px'});
    		// position navigation bullets
    		
    		jQuery('ul.galleryNav li:first').addClass('active');
    		jQuery('ul.galleryArrows li.prev').addClass('disabled');
    		jQuery('ul.galleryNav li a').click(function(e){
    			e.preventDefault();
    			var theIndex = jQuery('ul.galleryNav li a').index(jQuery(this));
    			SlideDeckAssistant.goToSlide(theIndex);
    			return false;
    		});
    		// assign click function to navigation bullets
    }
    };
    

   	SlideDeckAssistant.init();
	
	var theSlideDeck = jQuery('.slidedeck').slidedeck();
	if (theSlideDeck){
		theSlideDeck.options.complete = function () {
			jQuery('ul.galleryNav li').removeClass('active');
			jQuery('ul.galleryNav li:eq('+ ( theSlideDeck.current - 1 ) +')').addClass('active');
		}
	}
	
	*/
	

	
	    // Script for handling bullet navigation for SlideDeck
    var SlideDeckAssistant = {
    
    	init: function(){
    		
    		jQuery('.skin-GNOF').append('<div class="galleryNav"><a class="slidedeckPrevious" href="#prevSlide">previous slide</a><a class="slidedeckNext" href="#nextSlide">next slide</a></div>');
    		
    		jQuery('a.slidedeckPrevious').click(function(e){
   			 	e.preventDefault();
   			    jQuery('.slidedeck_frame dl').slidedeck().prev();
   			    jQuery('.slidedeck_frame dl').slidedeck().pauseAutoPlay = true;
   			 });
    
    		jQuery('a.slidedeckNext').click(function(e){
    		    e.preventDefault();
    		    jQuery('.slidedeck_frame dl').slidedeck().next();
    		    jQuery('.slidedeck_frame dl').slidedeck().pauseAutoPlay = true;
    		});

    	}
    };
    
    SlideDeckAssistant.init();
	
	// vimeo lightbox
	jQuery('a[href*="player.vimeo.com"]').fancybox({
      type: 'iframe',
      width: 622, 
      height: 350,
      overlayShow: true,
      overlayOpacity: 0.7
    });
	
});
