$.blockUI.defaults.css = {};
$.blockUI.defaults.message = '<img src="http://www.macfound.org/atf/cf/%7Bb0386ce3-8b29-4162-8098-e466fb856794%7D/AJAX-LOADER.GIF" />';
$.blockUI.defaults.overlayCSS.opacity =  '0.8';

$(document).ready(function() {
	if (window.self == window.parent) {
		searchForm.initialize();
	} else {
		$('#contentSearchForm').remove();
	}
});

var searchForm = new function() {
	this.search_frame = null;
	this.timer_reference = null;
	this.timer_period = 500;
	this.dirty = false;
	this.result_count = 0;
	this.results_per_page = 15;
	this.max_pages = 10;
	this.current_page = null;
	this.end_year_created = 0;
	this.start_year_created = 0;
	this.current_year_start = 0;
	this.current_year_end = 0;
	this.form_name = '';
	this.selected_segments = new Array();

	this.initialize = function() {
		this.prepareForm();

		if (0 < $('#contentSearchForm').length) {
			this.loadFromSession();
		} else {
			$('#sidebar').remove();
		}
	}

	this.initializeFormSettings = function() {
		$('#formSettings input:hidden').each(function() {
			$('#contentSearchForm input:radio[name="' + this.name
					+ '"][value="' + this.value + '"]').attr('checked', true);
			$('#contentSearchForm input:text[name="' + this.name + '"]')
					.val(this.value);
			$('#contentSearchForm input:checkbox[name="' + this.name
					+ '"][value="' + this.value + '"]').attr('checked', true);
		});
	}

	this.prepareForm = function() {
		if (0 < $('#contentSearchForm').length) {
			$('#start_month_created option[value="1"]').attr('selected', true);
			$('#start_day_created option:first').attr('selected', true);
			$('#start_year_created option:first').attr('selected', true);
			$('#end_month_created option[value="12"]').attr('selected', true);
			$('#end_day_created option:last').attr('selected', true);
			$('input:radio[name="datecreated"][value="between"]').attr(
					'checked', true);
			$('#searchtypeOR').attr('checked', true);
			$('#usedatecreated').attr('checked', true);

			$('#contentSearchForm')
					.after('<div id="search_results" align="left" width="100%"><div class="pager"><div class="pager_text">Showing '
							+ '<span class="page_start_num"></span>-<span class="page_end_num"></span> of <span class="count_items"></span></div>'
							+ '<div class="page_links_wrapper"><a href="#" class="prev_page" onclick="searchForm.start();">&laquo;</a>'
							+ '<span class="page_links"></span><a class="next_page" href="#" onclick="searchForm.end();">&raquo;</a></div></div>'
							+ '<div style="clear: both;"><ol id="search_links"></ol></div></div>');

			var name = $('span.pagehead_lg').text();
			name = name.replace(/^\s+|\s+$/g, "");
			this.form_name = name;
			this.initializeSlider();

			this.search_frame = $('<iframe id="searchFrame" src="about:blank" name="search_frame" onload="searchForm.getResults();"></iframe>');
			$(this.search_frame).insertAfter('#contentSearchForm');
			$(this.search_frame).attr('src', 'about:blank');
			$('#contentSearchForm').attr('target', 'search_frame');
			this.addSelectClearActions();
			this.addCheckboxFunction($('#formTopics'));
			this.addCheckboxFunction($('#formCountry'));
			this.initializeFormSettings();
			$('#sidebar').css('visibility', 'visible');

			$('table.contentTable hr')
					.after('<div class="pager"><div class="pager_text">Showing '
							+ '<span class="page_start_num"></span>-<span class="page_end_num"></span> of <span class="count_items"></span></div>'
							+ '<div class="page_links_wrapper"><a href="#" class="prev_page" onclick="searchForm.start();">&laquo;</a>'
							+ '<span class="page_links"></span><a class="next_page" href="#" onclick="searchForm.end();">&raquo;</a></div>');
		}

		$('table.contentTable').show();
	}

	this.initializeSlider = function() {
		this.end_year_created = $('#end_year_created option:selected').val();
		this.start_year_created = $('#start_year_created option:selected')
				.val();

		$('#filter_year').slider({
			min : this.start_year_created,
			max : this.end_year_created,
			range : true,
			stepping : 1,
			change : function(e, ui) {
				searchForm.setFormYears(ui);
			}
		});
		$('<div style="margin: 0px 0 0 0px; position: relative;"><div style="position: absolute;">'
				+ this.start_year_created
				+ '</div><div style="margin-left: 182px; position: absolute;">'
				+ this.end_year_created + '</div></div>')
				.insertAfter('#filter_year');
	}

	this.setFormYears = function(ui) {
		searchForm.current_year_start = $('#filter_year').slider('value', 0);
		searchForm.current_year_end = $('#filter_year').slider('value', 1);
		$('#start_year_created option[value="' + searchForm.current_year_start
				+ '"]').attr('selected', true);
		$('#end_year_created option[value="' + searchForm.current_year_end
				+ '"]').attr('selected', true);

		searchForm.search();
	}

	this.cookie = function(key, value) {
		if (typeof value != 'undefined') { // name and value given, set cookie
			return $.cookie(this.form_name + '|' + key, value);
		} else {
			return $.cookie(this.form_name + '|' + key);
		}
	}

	this.loadFromSession = function() {
		var start_year = this.cookie('start_year');
		var end_year = this.cookie('end_year');
		var segments = this.cookie('segments');

		if (start_year && 0 != start_year) {
			$('#filter_year').slider('moveTo', start_year, 0);
		}

		if (end_year && 0 != end_year) {
			$('#filter_year').slider('moveTo', end_year, 1);
		}

		if (!segments) {
			var all_segments = new Array();
			var segments = '';
			/*$('#sidebar input:checkbox').each(function() {
				all_segments.push(this.value);
			});

			segments = all_segments.join(',');
			this.cookie('segments', segments);*/
		}
		
		
		segments = segments.split(',');
		for (var i = 0; i < segments.length; i++) {
			$('input:checkbox[value="' + segments[i] + '"]').attr('checked',
					true);
			this.selected_segments.push(segments[i]);
		}

		this.search();
	}

	this.commitSession = function() {
		this.cookie('start_year', this.current_year_start);
		this.cookie('end_year', this.current_year_end);
		this.cookie('segments', this.selected_segments.join(','));
	}

	this.search = function() {
		clearTimeout(this.timer_reference);

		this.commitSession();

		this.timer_reference = setTimeout(searchForm.doSearch,
				this.timer_period);
	}

	this.doSearch = function() {
		clearTimeout(this.timer_reference);
		this.timer_reference = null;
		$('#contentSearchSubmit').click();
		$.blockUI();
		setTimeout('$.unblockUI()', 60000);
	}

	this.getResults = function() {
		if (frames[0] && frames[0].$) {
			$('div.pager').show();
			this.setPage(0);
		}

		$.unblockUI();
	}

	this.setResultCount = function(count) {
		$('span.count_items').html(count);
		$('span.page_links').empty();

		if (this.results_per_page >= count) {
			$('div.pager').hide();
			$('div.page_links_wrapper').css('visibility',
					'hidden');
		} else {
			$('span.pager').show();
			$('a.prev_page').hide();
			$('div.page_links_wrapper').css('visibility',
					'visible');

			var link_area = $('span.page_links');
			var pages = parseInt(count / this.results_per_page) + 1;
			for (var i = 1; i <= pages; i++) {
				$('<a href="#" onclick="searchForm.setPage(' + (i - 1)
						+ '); this.blur();">' + i + '</a>').appendTo(link_area);
			}
		}

		this.result_count = count;
	}

	this.setPage = function(page) {
		if (this.current_page == null) {
			var saved_page = this.cookie('page');
			if (saved_page) {
				page = saved_page;
			}
		}

		$('span.search_links').empty();
		$('span.page_links a.selected').removeClass('selected');

		var text = frames[0].$('table.contentTable').html();
		var results = text.split(/\d+\.&nbsp;&nbsp;&nbsp;/gi)
		
		if (1 >= results.length) {
			results = text.split(/\d+\.\W\W\W/gi);
		}
		
		this.setResultCount((results.length - 1));
		
		page = parseInt(page);
		var start = page * this.results_per_page;
		var end = Math.min(((page + 1) * this.results_per_page),
				this.result_count);
		var result_holder = $('#search_links');
		result_holder.attr('start', (start + 1));
		result_holder.empty();
		
		$('span.page_start_num').html((start + 1));
		$('span.page_end_num').html(end);

		start += 1;

		var i = 0;
		
		for (i = start; i <= end; i++) {
			result_holder.append('<li>' + results[i] + '</li>');
		}

		$('span.page_links').find('a:eq(' + page + ')')
				.addClass('selected');

		this.current_page = page;
		this.cookie('page', page);
		
		if (0 == this.result_count) {
			$('#search_links').append('<li>Unfortunately your search did not match any documents.</li>');	
		}
	}

	this.start = function() {
		this.setPage(0);
	}

	this.end = function() {
		var pages = parseInt(this.result_count / this.results_per_page);

		this.setPage(pages);
	}

	this.clearCheckboxes = function(link) {
		var forms = $(link).parent('form').get(0);
		$(forms).find('input:checkbox').each(function() {
			if (this.checked) {
				this.checked = false;
				$('#contentSearchForm input[value="' + this.value + '"]').attr(
						'checked', false);
			}
		});

		searchForm.search();
	}

	this.selectAllCheckboxes = function(link) {
		var forms = $(link).parent('form').get(0);
		$(forms).find('input:checkbox').each(function() {
			if (!this.checked) {
				this.checked = true;
				$('#contentSearchForm input[value="' + this.value + '"]').attr(
						'checked', true);
			}
		});

		searchForm.search();
	}

	this.addSelectClearActions = function() {
		$('a.select_all').click(function() {
			searchForm.selectAllCheckboxes(this);
			return false;
		});

		$('a.clear').click(function() {
			searchForm.clearCheckboxes(this);
			return false;
		});
	}

	this.setFormCheckbox = function(input) {
		$('#contentSearchForm input[value="' + input.value + '"]').attr(
				'checked', input.checked);

		if (input.checked) {
			this.selected_segments.push(input.value);
		} else {
			for (var i = 0; i < this.selected_segments.length; i++) {
				if (this.selected_segments[i] == input.value) {
					this.selected_segments.splice(i, 1);
					break;
				}
			}
		}

		this.search();
	}

	this.addCheckboxFunction = function(forms) {
		forms.find('input:checkbox').click(function() {
			searchForm.setFormCheckbox(this);
		});
	}
}

/*
	Cross-Browser Split 0.2.1
	By Steven Levithan <http://stevenlevithan.com>
	MIT license
*/



