Hootenanny = window.Hootenanny || {};

Hootenanny.Lunchbox = function() {
	var _selected_cuisines = [];
	var _selected_cuisines_display_chars = 55;
	var _restaurants_container = 'scrollable-content';
	
	function _initial_click_handler() {
		// make sure splash image is gone
		if ($('#splash-image').length > 0) {
			$('#splash-image').remove();
			$('#restaurants').css('display', 'block');
			Hootenanny.Scrollable.init( {slider_height: 260, content_height: 328} );
		}
	}
	
	function _init_cuisine_links() {
		$('#available-cuisines a').click(
			function() {
				Hootenanny.Lunchbox.initialize_restaurants();
				
				if (!$(this).hasClass('selected')) {
					// de-select all links
					$('#available-cuisines a').removeClass('selected');

					// select this link
					$(this).addClass('selected');

					// create new array with just this cuisine
					_selected_cuisines = [$(this).text()];

					// legacy code to enable mutliple cuisines selected
					//_selected_cuisines.push($(this).text());	
				} else {
					$(this).removeClass('selected');

					// legacy code to enable mutliple cuisines selected
					/*
					// find index of cuisine to remove
					var cuisine_index = -1;
					for (var i = 0; i < _selected_cuisines.length; i++) {
						if (_selected_cuisines[i] == $(this).text()) {
							cuisine_index = i;
							break;
						}
					}

					// remove cuisine
					if (cuisine_index > -1) {
						_selected_cuisines.splice(cuisine_index, 1);
					}*/

					_selected_cuisines = [];
				}
				
				_display_selected_cuisines();
				_get_restaurants();
				
				return false;
			}
		);
	}
	
	function _display_selected_cuisines() {
		_selected_cuisines.sort();
		var displayed = '';
		var displayed_test = '';
		
		for (var i = 0; i < _selected_cuisines.length; i++) {
			displayed_test = displayed + _selected_cuisines[i] + ', ';
			
			if (displayed_test.length > _selected_cuisines_display_chars) {
				displayed += 'more';
				break;
			} else {
				displayed += _selected_cuisines[i] + ', ';
			}
		}
		
		// chop off trailing comman and space
		if (displayed.charAt(displayed.length - 2) == ',') {
			displayed = displayed.substr(0, displayed.length - 2);
		}
		if( displayed == "View All" ) {
			displayed = "All Restaurants";
		}
		
		$('#selected-cuisines').empty();
		$('#selected-cuisines').text(displayed);
	}
	
	function _get_restaurants() {
		if (_selected_cuisines.length > 0) {
			var cuisines = '';
			for (var i = 0; i < _selected_cuisines.length; i++) {
				cuisines += 'cuisine[]=' + _selected_cuisines[i] + '&';
			}
		
			$.ajax({
				type: 'post',
				url: '/lib/hootenanny.webservice.php',
				data: 'command=get_restaurants&' + cuisines,
				success: function(json_data) {
					Hootenanny.Lunchbox.show_restaurants(json_data);
				}
			});
		} else {
			$('#' + _restaurants_container).empty();
		}
	}
	
	return {
		init: function() {
			_init_cuisine_links();
		},
		
		initialize_restaurants: function() {
			_initial_click_handler();
		},
		
		show_restaurants: function(json_data) {
			var restaurants = eval(json_data);
			
			if (restaurants.length > 0) {
				// clear old restaurants
				$('#' + _restaurants_container).empty();
				
				var restaurant, info_element, links_element, name_element, anchor_element, clear_element, text_node;
			
				for (var i = 0; i < restaurants.length; i++) {
					restaurant = document.createElement('div');
					$(restaurant).addClass('restaurant');
					
					if (i%2 != 0) { // stripe odd rows
						$(restaurant).addClass('alt');
					}
				
					info_element = document.createElement('div');
					$(info_element).addClass('restaurant-info');
					
					name_element = document.createElement('span');
					$(name_element).addClass('name');
					
					text_node = document.createTextNode(restaurants[i].name + ', ');
					name_element.appendChild(text_node);
					info_element.appendChild(name_element);
					
					text_node = document.createTextNode(restaurants[i].address);
					info_element.appendChild(text_node);
					
					restaurant.appendChild(info_element);
					
					links_element = document.createElement('div');
					$(links_element).addClass('restaurant-links');
					
					anchor_element = document.createElement('a');
					anchor_element.setAttribute('href', restaurants[i].menu);
					text_node = document.createTextNode('menu');
					anchor_element.appendChild(text_node);
					links_element.appendChild(anchor_element);
					
					text_node = document.createTextNode(' | ');
					links_element.appendChild(text_node);
					
					anchor_element = document.createElement('a');
					anchor_element.setAttribute('href', 'http://maps.google.com?q=' + restaurants[i].address + ' Chicago IL');
					text_node = document.createTextNode('map');
					anchor_element.appendChild(text_node);
					links_element.appendChild(anchor_element);
					
					restaurant.appendChild(links_element);
					
					clear_element = document.createElement('div');
					$(clear_element).addClass('clear');
					
					restaurant.appendChild(clear_element);
				
					$('#' + _restaurants_container)[0].appendChild(restaurant);
				}
				
				Hootenanny.Scrollable.calibrate_slider();
			}
		}
	}
}();

$(document).ready(function() {
	Hootenanny.Lunchbox.init();
});