/******************************/
/* On LOAD										*/
/******************************/

jQuery(document).ready(function() {
	init_tips_showhide();
	init_foodchooser();
});

/******************************/
/* FOR food / category filter */
/******************************/

function init_foodchooser(){
	jQuery('#food_chooser_nojs').attr('id', 'food_chooser');
	
	//move food chooser to top of the page
	jQuery('#food_chooser').insertBefore('#results');
	
	//attach food category filter events
	jQuery('#food_chooser .category-choice h3').click(
		function(event){
			load_chosen_categories(jQuery(this).parents('.category-choice'));
			event.preventDefault();
		}	
	);
	
	//is there a chosen food? if so, set that as default.
	if(document.location.search.length > 0){
		highlight_default_chosen_food();
	}else{
		//load 'all food' as default
		load_chosen_categories(jQuery('#food_chooser .category-choice:first'), false);
	}
}

function highlight_default_chosen_food(){
	// move the chosen food into the left most card, and select its category
	// There must be an easier way to do this...
	var currentpath = document.location.pathname + document.location.search;
	var chosenfoodli = jQuery('#food_chooser .category-choice .food-choice[href='+currentpath+']:last');
	
	load_chosen_categories(chosenfoodli.parents('.category-choice'), false);
	
	//find the food in the food line, and make sure it is in the first box, and is selected
	var arrTempFoods = jQuery('#foodline li');
	var flag = true;
	for(var i = 0; i < arrTempFoods.length ; i++){
		var thehref = jQuery(arrTempFoods[i]).children('.food-choice').attr('href');
		thehref = thehref.replace('http://'+ document.location.host, '');
		
		if (flag){
			if (thehref != currentpath){
				jQuery(arrTempFoods[i]).appendTo(jQuery('#foodline'));
			}else{	
				//jQuery(arrTempFoods[i]).addClass('selected');
				flag = false;	
			}
		}
	}
	refreshFoodList();
	highlightSelectedFood('choice_1');
}

function load_chosen_categories(chosen_li, set_first_as_selected){
	var set_first_as_selected = (set_first_as_selected == null) ? true : set_first_as_selected;
	
	if(chosen_li.length){
		//Show chosen foods
		jQuery('#food_chooser .category-choice').removeClass('chosen');
		jQuery(chosen_li).addClass('chosen');
		showFoodList(jQuery('#food_chooser .chosen .food-list li'));
		
		//select first food by default
		if(set_first_as_selected){
			jQuery('#foodline li .food-choice:first').click();
		}
		
	}else{
		//show all as default
		showFoodList(jQuery('#food_chooser .category-choice .food-list li'));
	}
}

function showFoodList(arrayFoodsLi){

		//------------
		// make the ul#foodline element (parent element of the list of foods)
		//------------
		
		//remove existing
		jQuery('#foodline').remove();
		jQuery('#buttonleft').remove();
		jQuery('#buttonright').remove();
		
		//show less cards if total is less than 4
		var foodCount = arrayFoodsLi.length;
		var foodListClass = 'foodline_4';
		
		if(foodCount < 3){
			foodListClass = 'foodline_' + foodCount;
		}
		
		//generating the element
		var foodList = jQuery('<ul id="foodline"></ul>');
		foodList.addClass(foodListClass);
		jQuery('#food_chooser').append(foodList);
		
		//move foods to the foodline
		jQuery(arrayFoodsLi).clone().appendTo("#foodline");
		refreshFoodList();
		
		//show buttons if applicable
		if(arrayFoodsLi.length > 4){
				var buttonleft = jQuery('<a href="javascript:scrollFoodList(-1);" id="buttonleft">Browse left</a>');
				var buttonright = jQuery('<a href="javascript:scrollFoodList(+1);" id="buttonright">Browse right</a>');
				jQuery('#food_chooser').append(buttonleft);
				jQuery('#food_chooser').append(buttonright);
		}
		
}

function scrollFoodList(scrollby){
	var arrChosenFoods = jQuery('#foodline li');
	
	if(scrollby == '+1'){
		jQuery(arrChosenFoods[0]).insertAfter(jQuery(arrChosenFoods[arrChosenFoods.length-1]));
	}else{
		jQuery(arrChosenFoods[arrChosenFoods.length-1]).insertBefore(jQuery(arrChosenFoods[0]));
	}
	refreshFoodList();
}

function refreshFoodList(){
		//assign classes to food list
		var arrChosenFoods = jQuery('#foodline li');
		highlightSelectedFood('');//select none first
		jQuery.each(arrChosenFoods, 
				function(i, val) {
					jQuery(val).attr('class', '');
					if(i < 4){
						jQuery(val).addClass('choice_' + (i+1));
						//cheating.. using rel tag to pass on the singular form of the selected food
						if(jQuery(val).children('a').text() == jQuery('#tips_title span').attr('rel')){
							highlightSelectedFood('choice_' + (i+1));
						};
					}
				}
		);
		
		//assign events
		addFoodClickEvent(jQuery('#foodline li .food-choice'));
}


function addFoodClickEvent(elem){
	//unbind
	jQuery(elem).unbind('click');
	
	//bind
	jQuery(elem).click(
		function(event){
			var thehref = jQuery(this).attr('href') + '&isajax=1'; 
			jQuery("#tips_title").html('Loading...');
			jQuery("#results #tip_index_list").hide();
			var selectedFoodClass = jQuery(this).parent().attr('class');
			highlightSelectedFood(selectedFoodClass); //highlight selected food in the foodline
			
			jQuery.ajax({
			  type: "GET",
			  url: thehref,
			  cache: false,
			  success: function(html){
					jQuery("#results").html(html); //load the new results
					init_tips_showhide(); //apply tips show/hide to the new results
					jQuery("#results #tip_index_list").show();
			  },
			  error: function (XMLHttpRequest, textStatus, errorThrown) {
				//If failed
				jQuery("#results").append('<p>Sorry, There has been an error fetching the data.</p>'); // the options for this ajax request
			  }
			});
			
			event.preventDefault();
		}
	);	
}
/**/

function highlightSelectedFood(classname){
	var unselectedClass = jQuery('#foodline').attr('class');
	unselectedClass = unselectedClass.replace(/_choice_[1234]/gi, ""); //replace the old choice if it existed
	if (classname.length){
		jQuery('#foodline').attr('class', unselectedClass + '_' + classname);
	}else{
		jQuery('#foodline').attr('class', unselectedClass);
	}
}

/******************************/
/* FOR show/hide tips details */
/******************************/
function init_tips_showhide(){
	jQuery('#tip_index_list').css('display', 'block');
  jQuery("#tip_index_list .tip-open").attr('class', 'tip-closed');
  jQuery("#tip_index_list .tip-closed h3").click(openTip);
}

function closeTip(event, obj){
	//close all that are open
	jQuery("#tip_index_list .tip-open").attr('class', 'tip-closed');
	jQuery("#tip_index_list .tip-closed h3").unbind();
	jQuery("#tip_index_list .tip-closed h3").click(openTip);
	event.preventDefault();
}

function openTip(event, obj){
	//close all that are open
	closeTip(event, obj);
	//open this one
	jQuery(this).parents('.tip-closed').attr('class','tip-open');
	jQuery(this).unbind();
	jQuery(this).click(closeTip);
	
	event.preventDefault();
}