/**
 * @author samuelme
 */

(function($) {
  //
  // Creates the functionality for the Allergen Info page
  //
  $.fn.Ingredients = function(options) {
	//debug(this);
	// build main options before element iteration
	var opts = $.extend({}, $.fn.Ingredients.defaults, options);
	// iterate and reformat each matched element
//	return this.each(function() {
	  var scope = $(this);
//	  // build element specific options
//	  var o = $.meta ? $.extend({}, opts, $this.data()) : opts;
//	});

	//setupBtns($(this));
	handlexml($.fn.Ingredients.defaults.xml,scope);
	//debug("this is it: "+$(this).html());
	$('#foodType').change(function(){
		handlexml($.fn.Ingredients.defaults.xml,$('html'),$(this).val());
	})
  };
  
  
  
  //
  // private function for debugging
  //
  var firstcat_ingred;
  var $selectedObj;
  var forceSelection;
  
  function handlexml(ajaxURL,scope,type){
	  
	var $this = scope;
	
  	$.ajax({
		url: ajaxURL,
		type: "GET",
		dataType: "xml",
		success: function(xml) {
		
			
			//find main cats
			var food_cat_array = [];
			$(xml).find('menuSet').each(function(i){
				var foodType = $(this).attr('type');
				if (type == undefined) {
					food_cat_array.push(foodType);
					if(i == 0){
						 $selectedObj = $('<option  value="' + foodType + '">' + foodType + '</option>');
						 $selectedObj.appendTo('#foodType');
					}else{
						$('#foodType').append('<option value="' + foodType + '">' + foodType + '</option>');
					}
					
				}
			});
			
			//populate based on first cat
			var ingredient_cat_array = [];
			
			if(type == undefined){
				firstcat_ingred = food_cat_array[0];
				forceSelection = true;
			}else{
				firstcat_ingred = type;
				forceSelection = false;
				$("#nutri_content .food-type").remove();
			}
			$(xml).find("menuSet[ type = '"+firstcat_ingred+"' ]").find('food').each(function(i){
				var $wrapperObj = createWrapper();
				var ingred_title = $(this).attr('title');
				var ingred_bg = $(this).attr('title_bg');
				debug("##############"+ingred_bg+"##########");
				var $title_and_wrapper = createTitleBar(ingred_title,ingred_bg,$wrapperObj);
				var $ul_wrapper = createUlWrapper($wrapperObj);
				ingredient_cat_array.push(ingred_title);
				$(this).find('item').each(function(i){
					var title = $(this).attr('title');
					var Ingredients = $(this).attr('allergens');
					var desc = $(this).text();
					createLI(title,desc,Ingredients,$ul_wrapper);
				});
			});
			
			
		},
		error: function() {
		},
		complete: function(){
			setupBtns($("#nutri_content li"));
			setSelection($selectedObj);
			$('#IngredientsPage #nutri_content .food-type h3.title').txtToSwf({swfLocation:"../swf/",swf:"TradeGothic.swf",fontSize:"19px",letterSpacing:-0.4,color:"#FFFFFF",x:12,y:12}); 

		}
	});
	
	
  }
  function createWrapper(){
  	var $obj = $('<div class="food-type"> </div>');
	$obj.appendTo("#nutri_content");
  	return $obj;
  }
  function createTitleBar(title,bg,$scope){
  	var $obj = $('<h3 class="title" style="background: url(../'+bg+') no-repeat;">'+title+'</h3>');
	$obj.appendTo($($scope));
  	return $obj;
  }
  function createUlWrapper($scope){
  	var $obj = $('<ul> </ul>');
	$obj.appendTo($($scope));
  	return $obj;
  }
  function createLI(title,desc,Ingredients,$scope){
  	debug('Ingredients: '+Ingredients);
  	if(Ingredients == undefined) Ingredients = "None at this time.";
  	var $obj = $('<li><h4>'+title+'</h4><span class="arrow"> </span><div class="info"><h5>Contains</h5><p>'+desc+'</p><div class="allergeninfo"><h6>Allergen Info</h6><p>'+Ingredients+'</p><a href="allergen_info.jsp">MORE ON ALLERGENS</a></div></div> </li>');
	$obj.appendTo($($scope));
  	//return $obj;
  }
  
  function setupBtns(obj){
  	obj.each(function() {
		 $this = $(this);

			$this.click(function(){
				$(this).toggleClass('selected').toggleClass('');
				//;
			});
			
			$this.hover(over_handler, out_handler);
		
	});
	
  }
  
  function over_handler(ev) {
		$(this).addClass('active');
		//debug('this was hovered upon');
  }

  function out_handler() {
		$(this).removeClass('active');
  }
  
  function setSelection($obj){
  	if (forceSelection == true ) {
		setTimeout(function(){$obj.attr('selected', 'selected')},1);
		//setTimeout("$('#Test option:first').attr('selected','selected')",1);
	}	
  }
  
  function debug($obj) {
	if (window.console && window.console.log)
	  window.console.log($obj);
  };

 
  $.fn.Ingredients.defaults = {xml:'xml/ingredients_v2.23.00.xml'};
})(jQuery);