var forms = {
	init : function() {
		// this.initSiteSearch();
		this.initTextFields();
	},
	initTextFields : function() {
		$('input[type=text].autoSelect').focus(function() {
			$(this).select();
		});
	}
};

var hero = {
	autoAdvance: true,
	interval: 6000,
	showing: 0,
	rItems: new Array(),
	rControls: new Array(),
	pause: false,
	count: 0,
	checkInit: function() {
		if($('#hero').length > 0) return true;
		return false;
	},
	init: function() {
		if(this.checkInit()) {
			this.initPersonas();
			this.initRotator();
		}
	},
	initRotator: function() {
		// opacity fix for FF2 Mac
		jQuery.each(jQuery.browser, function(i, val) {
		  if(i=="mozilla" && jQuery.browser.version.substr(0,3)=="1.8")
			$('ul#nav').css('opacity', 0.9999);	// fix for opacity bug in mac FF		
		});
		this.rItems = $('#rotator .rItem');
		this.rControls = $('#rotatorControls li');
		this.count = this.rItems.size();
		this.setTriggers();
		setTimeout(function() {hero.change('advance',true)},hero.interval);
	},
	setActiveItem: function(pos) {
		this.rControls.each(function() {
			if($(this).hasClass('active')) $(this).removeClass('active');
		})
		$(this.rControls[pos]).addClass('active');
	},
	setTriggers: function() {
		this.rItems.each(function(index) {
			$(this).hover(function(){
				hero.pause = true;
			},function(){
				hero.pause = false;
			});
		});
		this.rControls.each(function(index) {
			var control = $(this);
			control.heroIndex = index;
			control.click(function() {
				if(control.heroIndex != hero.showing) {
					hero.rBreak();
					var goTo = index;
					hero.change(goTo);
					return false;
				} else {
					return false;
				}
			});					
		});
	},
	rBreak: function() {
		this.autoAdvance = false;
	},
	change: function(action,isAuto) {
		if(!isNaN(parseInt(action))) {
			show = parseInt(action);
		} else {
			if(action == 'advance') {
				var show = this.showing + 1;
			} else if(action == 'retreat') {
				var show = this.showing - 1;
			}				
		}
		if(show < 0) show = this.count - 1;
		if(show == this.count) show = 0;
		if((isAuto != true) || (isAuto == true && this.autoAdvance == true)) {
			// do it
			if(this.pause == false) {
				$(this.rItems[this.showing]).fadeOut();
				$(this.rItems[show]).fadeIn();
				this.setActiveItem(show);
				this.showing = show;
			}
			if(this.autoAdvance == true) {
				setTimeout(function() {hero.change(action,true)},hero.interval);				
			}
		} 
	},
	initPersonas: function() {		
		$('.person-slide').each(function(i) {
			var el = $(this);
			var trigger = el.find('div');
			el.hover(function(){
				// pageTracker._trackPageview("/events/home/persona_callout/"+i);
				trigger.animate({left: '-145px'},{queue: false,duration: 'fast',easing: 'swing'});				
			},function(){
				trigger.animate({left: '0px'},{queue: false,duration: 'fast',easing: 'swing'});				
			});
		});
		$(window).load(function () {
			$('#personaLinks').css({visibility: 'visible'});
		});	
	}
};


var navs = {
	init : function() {
		// center the dropdowns on the parent elenebt
		$('#nav ul:not(.oneColWide)').each(function() {
			el = $(this);
			var pW = el.parent().width() / 2 - (el.width() / 2);
			var pos = pW + 'px';
			el.css({'left':pos});
		})
		
		$('#nav').superfish({
			hoverClass: false,
			delay: 600,
			pathClass: false,
			autoArrows: false,
			dropShadows: false,
			speed: jQuery.browser.msie && jQuery.browser.version > 6 ? 1 : 'fast',
			onBeforeShow: function() {
				this.parent().find('a:first').addClass('hov');
				
			},
			onShow: function() {
				// if(jQuery.browser.msie && jQuery.browser.version == 6) {
				// 	// alert('called before');
				// 	$('select').css({visibility: 'hidden'});
				// }
				
			},
			onHide: function() {
				this.parent().find('a:first').removeClass('hov');
				// if(jQuery.browser.msie && jQuery.browser.version == 6) {
				// 					// alert('called after');
				// 					$('select').css({visibility: 'visible'});
				// 				}
			}
		});
		
		$('#nav,#nav ul,#nav li,.overlay').bgIframe({});
		$('#subnav .nav ul').superfish({
			hoverClass: false,
			delay: 600,
			pathClass: false,
			autoArrows: false,
			dropShadows: false,
			speed: jQuery.browser.msie && jQuery.browser.version > 6 ? 1 : 'fast',
			onBeforeShow: function() {
				this.parent().find('a:first').addClass('hov');
			},
			onHide: function() {
				this.parent().find('a:first').removeClass('hov');
			}
		});
		$('#issuesToggle').click(function(){
			if($(this).next('ul').is(':visible')) {
				$(this).next('ul').fadeOut();
			} else {
				$(this).next('ul').fadeIn();
			}
			return false;
		});
		$(':not(#issuesToggle, #issuesWrap ul.oneColWide *)').click(function() {
			$('#issuesToggle').next('ul').fadeOut();
		});
	}
};

function baseDb() {
	var dbApp = this;
	this.label = 'grant';
	this.formId = 'grantsDb';
	// CHANGE
	this.filterPanels = ['year','program','legacy'];
	this.loadUrl = '/index.php?type=238&handler=tx_ccnygrants_pi1&tx_ccnygrants_pi1[action]=JSONResultslist';
	this.pageRange = {
		start: 1,
		stop: false
	}
	this.otherYear = false;
	this.queryArgs = {
		page: 1,
		programs: [],
		years: [],
		word: false,
		sortField: false,
		sortDirection: false,
		resultsPerPage: 25,
		resultCount: 0
	}

	this.displayQueryError = function() {
		var el = $('</span>We\'re sorry, but the search term you entered is invalid. Please try a different search word.</span>');
		$('#searchOutput-description').empty();
		$('#searchOutput-description').append(el);
		$('#searchOutput-description').slideDown('slow');
	}
	this.focusQuery = function(focusOn) {
		dbApp = this;
		jQuery.each(this.filterPanels,function() {
			if(this != focusOn) {
				dbApp.queryArgs[this+'s'] = new Array();
			}
		})
		this.queryArgs.word = '';
		$('#queryArgs-word').val('');
		this.updateResults(true);
	}
	this.describeQueryYears = function(out) {
		dbApp = this;
		$(this.queryArgs.years).each(function() {
			el = $('<a rel="' + this + '" href="#">' + this + '</a>');
			el.bind('click',function(e) {
				dbApp.queryArgs.years = new Array($(this).attr('rel'));
				dbApp.focusQuery('year');
				return false;
			});
			out.push(el);
		})
		return out;
	}
	this.describeQueryPrograms = function(out) {
		dbApp = this;
		$(this.queryArgs.programs).each(function() {
			cbId = $('input.queryArgs-program[value=' + this + ']').attr('id');
			l = $('label[for=' + cbId + ']');
			t = l.text();
			el = $('<a rel="' + this + '" href="#">' + t + '</a>');
			el.bind('click',function(e) {
				dbApp.queryArgs.programs = new Array($(this).attr('rel'));
				dbApp.focusQuery('program');
				return false;
			});
			out.push(el);
		})
		return out;
	}
	this.describeQueryWord = function(out) {
		if(this.queryArgs.word) {
			word = this.queryArgs.word.replace(/(<.*?>)/ig,"");
			el = $('<strong>' + word + '</strong>');
			out.push(el);
		}
		return out;
	}
	this.describeQuery = function(){
		var out = new Array();
		out = this.describeQueryYears(out);
		out = this.describeQueryPrograms(out);
		out = this.describeQueryWord(out);
		this.outputDescribeQuery(out);
	}
	this.outputDescribeQuery = function(out) {
		var length = out.length;
		$('#searchOutput-description').empty();
		if(length == 0) {
			$('#searchOutput-description').append($('<span>Your ' + this.label + ' search </span>'));
		} else {
			$('#searchOutput-description').append($('<span>Your ' + this.label + ' search for </span>'));
		}
		$(out).each(function(i) {
			$('#searchOutput-description').append(' ').append(this);
			i = i+1;
			if(i != length) {
				$('#searchOutput-description').append(' + ');
			} else {
				$('#searchOutput-description').append(' ');
			}
		});
		$('#searchOutput-description').append($('<span> has returned ' + this.queryArgs.resultCount + ' RESULTS.</span>'));
		$('#searchOutput-description').slideDown('slow');
	}

	this.init = function() {
		if(this.checkInit()) {
			this.initForms();
			this.initResultCount();
			this.initPages();
			this.initQueryTriggers();
			this.initFilterToggleTriggers();
			this.initRowTriggers();
			this.describeQuery();
			this.checkPrevNextButtons();
		}
	}
	this.initResultCount = function() {
		this.queryArgs.resultCount = $('#resultCount').val();
	}
	this.initForms = function() {
		$('#' + this.formId + ' input').each(function() {
			this.setAttribute("autocomplete","off");
		});
		$('#' + this.formId + ' form').each(function() {
			$(this).bind('submit',function() {
				return false;
			});
		});
	}
	this.checkPrevNextButtons = function() {
		dbApp = this;
		if(this.queryArgs.page == 1) {
			$('.queryArgs-prevPage').each(function() {
				$(this).addClass('inactive');
			});
		} else {
			$('.queryArgs-prevPage').each(function() {
				$(this).removeClass('inactive');
			});
		}
		if (this.queryArgs.page == dbApp.pageRange.stop) {
			$('.queryArgs-nextPage').each(function() {
				$(this).addClass('inactive');
			});
		} else {
			$('.queryArgs-nextPage').each(function() {
				$(this).removeClass('inactive');
			});
		}
	}
	this.initPages = function() {
		dbApp = this;
		this.pageRange.stop = parseInt($('#queryArgs-pageCount').html());
		this.checkPrevNextButtons();
		$('.queryArgs-page').each(function() {
			$(this).bind('click',function(e) {
				$(this).select();
			});
		})
		$('.queryArgs-page').each(function() {
			$(this).bind('keypress blur',function(e) {
				if(e.keyCode == 13 || e.type == 'blur') {
					$this = $(this);
					val = $this.val();
					intVal = parseInt(val);
					if(val > dbApp.pageRange.stop) {
						val = dbApp.pageRange.stop;
						intVal = val;
					}
					if(intVal == val && intVal > 0) {
						if(val != dbApp.queryArgs.page && val <= dbApp.pageRange.stop) {
							dbApp.updatePage(val);
							return false;
						} else {
							$this.val(dbApp.queryArgs.page);	
						}
					} else {
						$this.val(dbApp.queryArgs.page);	
					}
				}
			});
		});
   		$('.queryArgs-prevPage').each(function() {
			$(this).bind('click',function() {
				if($(this).hasClass('inactive') == false) {
					dbApp.updatePagePrev();
				}
				return false;
			});
   		});
   		$('.queryArgs-nextPage').each(function() {
			$(this).bind('click',function() {
				if($(this).hasClass('inactive') == false) {
					dbApp.updatePageNext();
				}
				return false;
			});
   		});
	}
	this.updatePagePrev = function() {
		targetPage = parseInt(this.queryArgs.page) - 1;
		if(targetPage > 0) {
			this.updatePage(targetPage);
		}
	}
	this.updatePageNext = function() {
		targetPage = parseInt(this.queryArgs.page) + 1;
		if(targetPage <= this.pageRange.stop) {
			this.updatePage(targetPage);
		}
	}
	this.updatePage = function(page) {
		dbApp = this;
		this.queryArgs.page = page;
		this.checkPrevNextButtons();
		$('.queryArgs-page').each(function() {
			$(this).val(dbApp.queryArgs.page);
		});
		this.updateResults(false,true);
	}
	this.initFilterToggleTriggers = function() {
		dbApp = this;
		jQuery.each(this.filterPanels,function(i,el) {
			var type = el;
			$('#' + el + '-panel-trigger').bind('click',function() {
				if($(this).hasClass('open')) {
					if ($.browser.msie) {
						$('#' + type + '-panel').hide();
					} else {
						$('#' + type + '-panel').fadeOut();
					}
					$('#' + type + '-panel-trigger').removeClass('open');
					$('#' + type + '-panel-trigger').addClass('closed');
				} else if ($(this).hasClass('closed')) {
					if ($.browser.msie) {
						$('#' + type + '-panel').show();
					} else {
						$('#' + type + '-panel').fadeIn();
					}
					$('#' + type + '-panel-trigger').removeClass('closed');
					$('#' + type + '-panel-trigger').addClass('open');
				}
			})
			
			if(type != 'legacy') {
			l = dbApp.queryArgs[type + 's'].length;
				if(l > 0) {
					$('#' + type + '-panel').show();
					$('#' + type + '-panel-trigger').removeClass('closed');
					$('#' + type + '-panel-trigger').addClass('open');
				}
			}
		});
	}
	this.initRowTriggers = function() {
		dbApp = this;
		$('.resultsInner tbody tr').each(function() {
			$(this).bind('click',function() {
				dbApp.toggleRow(this);
			});
		})
		if(dbApp.queryArgs.resultCount == 1) {
			$('.resultsInner tbody tr').each(function() {
				dbApp.toggleRow(this);
			})
		}
		
		$('.resultsInner a.queryArgs-word').each(function() {
			$(this).bind('click', function(e) {
				dbApp.queryArgs.word = $(this).attr('rel');
				$('#queryArgs-word').val($(this).attr('rel'));
				dbApp.updateResults(true);
				return false;
			});
		});
	}
	this.toggleRow = function(row) {
		row = $(row);
		if(row.hasClass('open')) {
			this.closeRow(row);
		} else if (row.hasClass('closed')) {
			this.openRow(row);
		}
	}
	this.openRow = function(row) {

		/*
		CHANGED 01-08-10
		$.fadeIn() does not work in IE 8
		using $.show() for now
		
		- Michael McManus
		*/

		row.next().fadeIn();
		row.next().show();
		row.removeClass('closed');
		row.addClass('open');
	}
	this.closeRow = function(row) {
		row.next().hide();
		row.removeClass('open');
		row.addClass('closed');
		
	}
	this.initProgramTriggers = function() {
		dbApp = this;
		$('.queryArgs-program').each(function() {
			$(this).bind('click', function() {
				dbApp.updateCheckBox('programs',this);
				dbApp.updateResults(true);
			});
			dbApp.updateCheckBox('programs',this);
		});
	}
	this.initLegacyTriggers = function() {
		dbApp = this;
		$('.queryArgs-legacy').each(function() {
			$(this).bind('click', function() {
				dbApp.updateCheckBox('programs',this);
				dbApp.updateResults(true);
			});
			dbApp.updateCheckBox('programs',this);
		});
	}
	this.handleOtherYear = function() {
		t = $('#search-year-other-text').val();
		checkbox = $('#search-year-other');
		if (/^\d*$/.test(t)) {
			currentYear = t;
		} else {
			currentYear = false;
			$('#search-year-other-text').val('');
			checkbox.attr('checked','');
		}
		setYear = this.otherYear;
		checked = this.getCheckboxValue(checkbox);
		setNew = false;
		// position of checked year
		ci = jQuery.inArray(currentYear, this.queryArgs.years);
		si = jQuery.inArray(setYear, this.queryArgs.years);
		if(checked) {
			if (currentYear) {
				if(ci == -1) {
					setNew = currentYear;
					this.queryArgs.years.push(currentYear);
					if(si != -1) {
						this.queryArgs.years.splice(si,1);
					}
					this.otherYear = setNew;
					checkbox.attr({'value':setNew});
					dbApp.updateResults(true);
				}
			}
		} else {
			// unset the old year
			if(si != -1) {
				this.queryArgs.years.splice(si,1);
				dbApp.updateResults(true);
			}
		}
	}
	this.initYearTriggers = function() {
		dbApp = this;
		$('input.queryArgs-year[type=\'checkbox\']').each(function() {
			$(this).bind('click', function() {
				if(!$(this).hasClass('otherYear')) {
					dbApp.updateCheckBox('years',this);
					dbApp.updateResults(true);
				} else {
					dbApp.handleOtherYear();
				}
			});
			dbApp.updateCheckBox('years',this);
		});
		$('#search-year-other-text').bind('change keypress',function(e) {
			if(e.keyCode == 13) {
				check = $('#search-year-other');
				checked = dbApp.getCheckboxValue(check);
				if(checked == false) {
					check.attr('checked','checked');
					dbApp.handleOtherYear();
				} else {
					dbApp.handleOtherYear();
				}
			}
		});
	}
	this.initResultsPerPageTriggers = function() {
		dbApp = this;
		$('.queryArgs-resultsPerPage').each(function() {
			$(this).bind('change', function() {
				dbApp.updateSelect('resultsPerPage', this);
				if(this.id == 'resultCount-1') {
					$('#resultCount-2').val($(this).val());
				} else {
					$('#resultCount-1').val($(this).val());
				}
				dbApp.updateResults(true,true);
			});
			dbApp.updateSelect('resultsPerPage', this);
		});
	}
	this.initSearchWordTriggers = function() {
		dbApp = this;
		$('#queryArgs-word').bind('change keypress',function(e) {
			dbApp.queryArgs.word = $('#queryArgs-word').val();
			if(e.keyCode == 13) {
				$('.updateResults').click();
			}
		});
		dbApp.queryArgs.word = $('#queryArgs-word').val();
	}
	this.initSortFieldTriggers = function() {
		dbApp = this;
		$('th.sortHeader').each(function() {
			var sub = this.id.substring(0,5);
			if(sub == 'sort-') {
				$(this).bind('click',function() {
					$this = $(this);
					sortType = this.id.substring(5);
					span = $this.find('span');
					if(dbApp.queryArgs.resultCount > 0) {
						if(span.hasClass('asc')) {
							dbApp.removeSortingLabels()
							span.addClass('desc');
							dbApp.queryArgs.sortField = sortType;
							dbApp.queryArgs.sortDirection = 'desc';
							dbApp.updateResults(true);
						} else {
							dbApp.removeSortingLabels()
							span.addClass('asc');
							dbApp.queryArgs.sortField = sortType;
							dbApp.queryArgs.sortDirection = 'asc';
							dbApp.updateResults(true);
						}
					}
				});
			}
			sortType = this.id.substring(5);
			$this = $(this);
			span = $this.find('span');
			if(span.hasClass('asc')) {
				dbApp.queryArgs.sortField = sortType;
				dbApp.queryArgs.sortDirection = 'asc';			
			} else if (span.hasClass('desc')) {
				dbApp.queryArgs.sortField = sortType;
				dbApp.queryArgs.sortDirection = 'desc';				
			}
		}); // close each
	}
	this.initSubmitButtonTriggers = function() {
		dbApp = this;
		$('.updateResults').each(function() {
			$(this).bind('click',function() {
				dbApp.updateResults(true);
				return false;
			})
		});
	}
	this.initQueryTriggers = function() {
		this.initProgramTriggers();
		// CHANGE
		this.initLegacyTriggers();
		this.initYearTriggers();
		this.initResultsPerPageTriggers();
		this.initSearchWordTriggers();
		this.initSortFieldTriggers();
		this.initSubmitButtonTriggers();
	}
	this.removeSortingLabels = function() {
		$('.sortHeader span').each(function() {
			$(this).removeClass('asc');
			$(this).removeClass('desc');
		});
	}
	this.updateResults = function(resetPage,doNotUpdateQueryString) {
		dbApp = this;
		loadUrl = this.loadUrl;
		doNotUpdateQueryString = (doNotUpdateQueryString == null) ? false : doNotUpdateQueryString;
		queryArgs = jQuery.extend({}, dbApp.queryArgs);

		// convert arrays to strings
		jQuery.each(this.filterPanels,function(i,el) {
			if(el != 'legacy') {
				queryArgs[el+'s'] = queryArgs[el+'s'].toString();
			}
		});
		if(resetPage) queryArgs.page = 1;

		$.ajax({
			url: loadUrl,
			data: queryArgs,
			dataType: 'json',
			beforeSend: function() {
				$('.overlay').css({
					top : (
						$('.opacity').offset().top < $(window).scrollTop()
							? $(window).scrollTop() + 60 - $('.opacity').offset().top
							: 60
					)
				});
				$('.overlayBack').height($('.opacity').height() + 40);
				$('.overlayBack').fadeIn('fast');
				if(!doNotUpdateQueryString) $('#searchOutput-description').slideUp();
			},
			error: function(data,textStatus) {
				dbApp.pageRange.stop = 1;
				dbApp.queryArgs.page = 1;
				dbApp.queryArgs.resultCount = 0;
			},
			success: function(data,textStatus) {
				// update the content
				$('#searchOutput-results').html(data.view);
				dbApp.pageRange.stop = data.pageCount;
				dbApp.queryArgs.page = data.page;
				dbApp.queryArgs.resultCount = data.resultCount; 
			},
			complete: function(data,textStatus) {
				// update pagination interface
				dbApp.checkPrevNextButtons();
				$('.queryArgs-pageCount').each(function() {
					$(this).html(dbApp.pageRange.stop);
				});
				$('.queryArgs-page').each(function() {
					$(this).val(dbApp.queryArgs.page);
				});
				
				// remove sorting on no results
				if(dbApp.queryArgs.resultCount == 0) {
					dbApp.removeSortingLabels()
				}
				
				// create row events
				dbApp.initRowTriggers();

				// output query message
				if(textStatus == 'success') {
					if(!doNotUpdateQueryString) dbApp.describeQuery();
				} else {
					dbApp.displayQueryError();
				}
				
				// check filters
				dbApp.checkFilters();
				
				// hide overlay
				$('.overlayBack').hide();
			}
		});
	}
	this.checkFilters = function() {
		dbApp = this;
		jQuery.each(this.filterPanels,function(i,el) {
			$('.queryArgs-' + el).each(function() {
				// CHANGE
				if(el == 'legacy') el = 'program';
				if(jQuery.inArray($(this).val(),dbApp.queryArgs[el+'s']) != -1) {
					$(this).attr('checked',true);
				} else {
					$(this).attr('checked',false);
				}
			});
		})
	}
	this.reset = function() {
		dbApp = this;
		jQuery.each(this.filterPanels,function(i,el) {
			$('.queryArgs-' + el).each(function() {
				$(this).attr('checked', false);

				if(el == 'legacy') el = 'program';
				dbApp.queryArgs[el+'s'] = new Array();
			})
		});
		$('#queryArgs-word').val('');
		this.queryArgs.word = '';
		this.setPage(1);
		this.updateResults(true);
	}
	this.setPage = function(num) {
		this.queryArgs.page = num;
		$('#currentPage-1').val(num);
		$('#currentPage-2').val(num);
	}
	this.updateSelect = function(type, el) {
		value = $(el).val();
		this.queryArgs[type] = value;
	}
	this.updateCheckBox = function(type, el) {
		dbApp = this;
		value = $(el).attr('value');
		add = this.getCheckboxValue(el);
		i = jQuery.inArray(value, dbApp.queryArgs[type]);
		if(add == true) {
			if(i == -1) {
				dbApp.queryArgs[type].push(value);
			}
		} else if (i != -1) {
			dbApp.queryArgs[type].splice(i,1);
		}
	}
	this.getCheckboxValue = function(el) {
		$el = $(el);
		if ($el.attr("checked")) {
			return true;
		} else {
			return false;
		}
	}
	this.checkInit = function() {
		if($('#grantsDb').length > 0) return true;
		return false;
	}
};

function grantsDb() {	
}
grantsDb.prototype = new baseDb();

function pubsDb() {
	this.label = 'publication';
	this.formId = 'pubsDb';
	this.filterPanels = ['year','program','type'];
	this.loadUrl = '/index.php?type=238&handler=tx_ccnypubs_pi1&tx_ccnypubs_pi1[action]=JSONResultslist';
	this.queryArgs = {
		page: 1,
		programs: [],
		types: [],
		years: [],
		word: false,
		sortField: false,
		sortDirection: false,
		resultsPerPage: 25,
		resultCount: 0
	}
	this.checkInit = function() {
		if($('#pubsDb').length > 0) return true;
		return false;
	}
	this.describeQueryTypes = function(out) {
		dbApp = this;
		$(this.queryArgs.types).each(function() {
			cbId = $('input.queryArgs-type[value=' + this + ']').attr('id');
			l = $('label[for=' + cbId + ']');
			t = l.text();
			el = $('<a rel="' + this + '" href="#">' + t + '</a>');
			el.bind('click',function(e) {
				dbApp.queryArgs.types = new Array($(this).attr('rel'));
				dbApp.focusQuery('type');
				return false;
			});
			out.push(el);
		})
		return out;
	}
	this.initPublicationTypeTriggers = function() {
		$('.queryArgs-type').each(function() {
			$(this).bind('click', function() {
				pubsDb.updateCheckBox('types',this);
				pubsDb.updateResults(true);
			});
		});
	}
	this.describeQuery = function(){
		var out = new Array();
		out = this.describeQueryYears(out);
		out = this.describeQueryPrograms(out);
		out = this.describeQueryTypes(out);
		out = this.describeQueryWord(out);
		this.outputDescribeQuery(out);
	}
	this.initQueryTriggers = function() {
		this.initProgramTriggers();
		this.initLegacyTriggers();
		this.initYearTriggers();
		this.initPublicationTypeTriggers();
		this.initResultsPerPageTriggers();
		this.initSearchWordTriggers();
		this.initSortFieldTriggers();
		this.initSubmitButtonTriggers();
	}
}
pubsDb.prototype = new baseDb();

var utility = {
	email: function() {
		addresses = $('a[href^=mailto]');
		if(addresses) {
			$.each(addresses, function() {
				obj = $(this);
				orig = obj.attr('href');
				subj = '?subject=Link from the Carnegie Corporation of New York website';
				t = $(this).text();
				replace = orig+subj;
				obj.attr('href', replace);
				obj.empty();
				obj.text(t);
			});
		}
	}
}

var lightboxes = {
	init : function() {
		$("a.lightbox").fancybox();
		var w = 700;
		var h = 418;

		if ($.browser.msie && $.browser.version.substr(0,1)<7) {
			w = w + 40;
			h = h + 60;
		}

		$("a.vidLightbox").fancybox({
			frameWidth: w,
			frameHeight: h, 
			hideOnContentClick: false
		});
	}
}

var gTracking = {
	init : function() {
		gTracking.initMoments();
		gTracking.initHero();
	},
	initMoments : function() {
		$("a.lightbox, a.vidLightbox").click(function() {
			pageTracker._trackPageview('/events/centennial-moments/' + $(this).attr('title'));
		});
	},
	initHero : function() {
		$('a.g_heroEvent').click(function() {
			// pageTracker._trackEvent(category, action, optional_label, optional_value);
			pageTracker._trackEvent('Hero', 'Hero Button Click', $(this).attr('title'));
		});
	}
}

jQuery(document).ready(function($) {
	forms.init();
	navs.init();
	hero.init();
	lightboxes.init();
	grantsDb = new grantsDb();
	grantsDb.init();
	
	pubsDb = new pubsDb();
	pubsDb.init();
	
	if($('#issuesList').length > 0) {
		$('#issuesList').listmenu({
			showCounts: false, 
			menuWidth: 857,
			// cols:{ count:2, gutter:25},
			cols:{ count:2, gutter:0},
			includeNums: false
		}); 
	}
  		
	jQuery('.tabsWrap').tabs();
	jQuery('.change-submit').change(function(){
		$(this).closest('form').submit();
	});
	// utility.email();
	
	// IE png fix
	if ($.browser.msie && $.browser.version.substr(0,1)<7) {
		DD_belatedPNG.fix('.pngFix');
	}
	
	var myFile = document.location.toString();
	if (myFile.match('#printPage')) { // the URL contains an anchor
		window.print();
	}
	
	// Special Google tracking stuff (lighbox clicks and home page hero clicks)
	gTracking.init();
	
});
