/************************************
COMPARE
Written by Ethan Gruber, ewg4x@virginia.edu
Library: jQuery
Description: This handles generating the query to
solr in the compare pages for both the left and right columns.
The results from solr are run through a cocoon pipeline and displayed
via ajax.
************************************/

$(function () {
	// display error if server doesn't respond
	$("#search") .ajaxError(function (request, settings) {
		$(this) .html('<div id="error">Error requesting page/service unavailable.</div>');
	});
	
	// total options for advanced search - used for unique id's on dynamically created elements
	var total_options = 1;
	
	// the boolean (and/or) items. these are set when a new search criteria option is created
	var gate_items = {
	};
	
	// focus the text field after selecting the field to search on
	$('#searchItemTemplate_1 select') .change(function () {
		$(this) .siblings('.search_text') .focus();
	})
	// copy the base template
	
	
	function gateTypeBtnClick(btn) {
		// increment - this is really just used to created unique ids for new dom elements
		total_options++;
		
		var tpl = $('#searchItemTemplate') .clone();
		
		// reset the id
		tpl.attr('id', 'searchItemTemplate_' + total_options);
		// reset the copied item's select element id
		$(tpl) .children('select') .attr('id', 'search_option_' + total_options);
		// reset the copied item's remove button id
		$(tpl) .children('#removeBtn_1') .attr('id', 'removeBtn_' + total_options);
		
		// focus the text field after select
		$(tpl) .children('select') .change(function () {
			$(this) .siblings('input') .focus();
		});
		
		// set up the text input
		$(tpl) .children('input') .each(function () {
			$(this) .attr('value', '');
			$(this) .attr('id', 'search_text_' + total_options);
		});
		
		// assign the handler for all gateTypeBtn elements within the new item/template
		$(tpl) .children('.gateTypeBtn') .click(function () {
			gateTypeBtnClick($(this));
		});
		
		// store in a "lookup" object
		gate_items[total_options] = btn.text();
		
		// add the new template to the dom
		$(btn) .parent() .after(tpl);
		
		// display the entire new template
		tpl.fadeIn('slow');
		
		// re-adjust the footer (footer is absolutely positioned)
		$('#footer') .css('top', $('#advancedSearchForm') .height() + 'px');
		
		// text style the remove part of the new template
		$('#removeBtn_' + total_options) .before(' |&nbsp;');
		
		// make the remove button visible
		$('#removeBtn_' + total_options) .css('visibility', 'visible');
		// assign the remove button click handler
		$('#removeBtn_' + total_options) .click(function () {
			var id = $(this) .attr('id');
			var num = id.substring(id.indexOf('_') + 1);
			// remove the gate/boolean item so the query is still valid
			delete gate_items[num];
			// fade out the entire template
			$(this) .parent() .fadeOut('slow', function () {
				$(this) .remove();
			});
			// move the footer back up
			$('#footer') .css('top', $('#advancedSearchForm') .height() + 'px');
		});
		toggle_options();
	}
	
	// assign the gate/boolean button click handler
	$('.gateTypeBtn') .click(function () {
		gateTypeBtnClick($(this));
	})
	
	// activates the advanced search action
	$('.compare_button') .click(function () {
		
		var image = $('#image') .attr('value');
		
		var query1 = '';
		var query2 = '';
		var sep1 = '';
		var sep2 = '';
		
		
		// clone the "gate_items" object
		// we want a fresh copy everytime the search button is pressed
		// just incase an item has been removed or more have been added
		var tmp_gate_items = {
		};
		for (i in gate_items) {
			tmp_gate_items[i] = gate_items[i];
		}
		
		// loop through each ".searchItemTemplate" and build the query
		$('#dataset1 .searchItemTemplate') .each(function () {
			
			var gate1;
			if ($(this) .attr('id') !== 'searchItemTemplate_1') {
				gate1 = ' AND ';
			}
			
			var val = $(this) .children('.category_list') .attr('value');
			
			
			if (val != 'year' && val != 'weight' && val != 'dimensions') {
				if (gate1 != null) {
					query1 += gate1 + val;
				}
				else {
					query1 += val;
				}
				query1 += ':' + $(this) .children('.option_container') .children('#search_text') .attr('value');
				sep = ' ';
			}
			else if (val == 'weight') {
				query1 += sep + '(' + val;
				var weight = $(this) .children('.option_container') .children('.weight_int') .attr('value');
				var range_value = $(this) .children('.option_container') .children('.weight_range') .attr('value');
				if (range_value == 'lessequal') {
					query1 += ':[* TO ' + weight + ']';
				} else if (range_value == 'greaterequal') {
					query1 += ':[' + weight + ' TO *]';
				}
				else if (range_value == 'equal'){
					query1 += ':' + weight;
				}
				query1 += ')';
				sep = ' ';
			} else if (val == 'dimensions') {
				query1 += sep + '(' + val;
				var dimensions = $(this) .children('.option_container') .children('.dimensions_int') .attr('value');
				var range_value = $(this) .children('.option_container') .children('.dimensions_range') .attr('value');
				if (range_value == 'lessequal') {
					query1 += ':[* TO ' + dimensions + ']';
				} else if (range_value == 'greaterequal') {
					query1 += ':[' + dimensions + ' TO *]';
				}
				else if (range_value == 'equal') {
					query1 += ':' + dimensions;
				}
				query1 += ')';
				sep = ' ';
			}
			else if (val == 'year') {
				if (gate1 != null) {
					query1 += gate1 + '(' + val;
				}
				else {
					query1 += '(' + val;
				}
				var year = $(this) .children('.option_container') .children('.year_int') .attr('value');
				var era = $(this) .children('.option_container') .children('.year_era') .attr('value');
				if (era == 'minus') {
					era = '\\-';
				} else {
					era = '';
				}
				var range_value = $(this) .children('.option_container') .children('.year_range') .attr('value');
				if (range_value == 'less') {
					query1 += ':[* TO ' + era + year + '] AND NOT year:' + era + year;
				}
				else if (range_value == 'lessequal') {
					query1 += ':[* TO ' + era + year + ']';
				}
				else if (range_value == 'equal') {
					query1 += ':' + era + year;
				}
				else if (range_value == 'greaterequal') {
					query1 += ':[' + era + year + ' TO *]';
				}
				else if (range_value == 'greater') {
					query1 += ':[' + era + year + ' TO *] AND NOT year:' + era + year;
				}
				query1 += ')';
				sep = ' ';
			}
			
			// object "popping/shifting" - working with object not indexed array!
			for (i in tmp_gate_items) {
				// get the first, delete it, and STOP
				delete tmp_gate_items[i];
				break;
			}
		});
		// pass the query to the search_results url passing the needed url params:
		$.get('compare_results', {
			q : query1, start:0, rows:25, image:image, side:'1'
		}, function (data) {
			//$('#error').html('');
			// push the result into the document
			$('#search1') .html(data);
			// hide the load indicator
		});
		
		//grabbing the data from the second dataset
		
		$('#dataset2 .searchItemTemplate') .each(function () {
			
			var gate2;
			if ($(this) .attr('id') !== 'searchItemTemplate_1') {
				gate2 = ' AND ';
			}
			
			var val = $(this) .children('.category_list') .attr('value');
			
			
			if (val != 'year' && val != 'weight' && val != 'dimensions') {
				if (gate2 != null) {
					query2 += gate2 + val;
				}
				else {
					query2 += val;
				}
				query2 += ':' + $(this) .children('.option_container') .children('#search_text') .attr('value');
				sep = ' ';
			}
			else if (val == 'weight') {
				query2 += sep + '(' + val;
				var weight = $(this) .children('.option_container') .children('.weight_int') .attr('value');
				var range_value = $(this) .children('.option_container') .children('.weight_range') .attr('value');
				if (range_value == 'lessequal') {
					query2 += ':[* TO ' + weight + ']';
				} else if (range_value == 'greaterequal') {
					query2 += ':[' + weight + ' TO *]';
				}
				else if (range_value == 'equal'){
					query2 += ':' + weight;
				}
				query2 += ')';
				sep = ' ';
			} else if (val == 'dimensions') {
				query2 += sep + '(' + val;
				var dimensions = $(this) .children('.option_container') .children('.dimensions_int') .attr('value');
				var range_value = $(this) .children('.option_container') .children('.dimensions_range') .attr('value');
				if (range_value == 'lessequal') {
					query2 += ':[* TO ' + dimensions + ']';
				} else if (range_value == 'greaterequal') {
					query2 += ':[' + dimensions + ' TO *]';
				}
				else if (range_value == 'equal') {
					query2 += ':' + dimensions;
				}
				query2 += ')';
				sep = ' ';
			}
			else if (val == 'year') {
				if (gate2 != null) {
					query2 += gate2 + '(' + val;
				}
				else {
					query2 += '(' + val;
				}
				var year = $(this) .children('.option_container') .children('.year_int') .attr('value');
				var era = $(this) .children('.option_container') .children('.year_era') .attr('value');
				if (era == 'minus') {
					era = '\\-';
				} else {
					era = '';
				}
				var range_value = $(this) .children('.option_container') .children('.year_range') .attr('value');
				if (range_value == 'less') {
					query2 += ':[* TO ' + era + year + '] AND NOT year:' + era + year;
				}
				else if (range_value == 'lessequal') {
					query2 += ':[* TO ' + era + year + ']';
				}
				else if (range_value == 'equal') {
					query2 += ':' + era + year;
				}
				else if (range_value == 'greaterequal') {
					query2 += ':[' + era + year + ' TO *]';
				}
				else if (range_value == 'greater') {
					query2 += ':[' + era + year + ' TO *] AND NOT year:' + era + year;
				}
				query2 += ')';
				sep = ' ';
			}
			
			// object "popping/shifting" - working with object not indexed array!
			for (i in tmp_gate_items) {
				// get the first, delete it, and STOP
				delete tmp_gate_items[i];
				break;
			}
		});
		// pass the query2 to the search_results url passing the needed url params:
		$.get('compare_results', {
			q :query2, start:0, rows:25, image:image, side:'2'
		}, function (data) {
			//$('#error').html('');
			// push the result into the document
			$('#search2') .html(data);
			// hide the load indicator
		});
		// cancel the default action of the submit buttton (don't want to re-direct)
	});
});