dojo.require("dojo.fx");
dojo.require("qc.common");
dojo.require("qc.window");
dojo.requireLocalization("qc.i18n", "properties");

var nls = dojo.i18n.getLocalization("qc.i18n", "properties");

_eventButtons = function(){
	
	dojo.connect(dojo.byId('bAdd'), 'onclick', function(){
		_add_ingredient();
	});
		
	
	dojo.connect(dojo.byId('bSearch'), 'onclick', function(){
		
		dojo.removeClass(dojo.byId('iRecipeName'), 'field-error-mandatory');
		
		var ingredients=[];
		dojo.query(".ingredientListElement").forEach(function(pItem){		
			ingredients.push(dojo.query("label",pItem.parentNode.parentNode)[0].getAttribute('ingredientId'));			
		});

		_searchingLayer();
		
		dojo.xhrPost({
			//url: "recipes/findRecipesByIngredients",
			url:dojo.byId("RecipeFindRecipesByIngredientsForm").action,
			handleAs: "text",
			content:{"data[Recipe][ingredients_recipes][ingredientid][]":ingredients.join()},
			load: function(data,args){
				qc.common.hideLoading2('cLoading','cRecipes');
				dojo.byId("cRecipes").innerHTML = data;				
			},
			// if any error occurs, it goes here:
			error: function(error,args){
				console.warn("error!",error);
			}
		});		
	});
	
	dojo.connect(dojo.byId('bSearchRecipeByName'), 'onclick', function(){
		
		_findByName();
	});
	dojo.connect(dojo.byId('sIngredients'), 'onclick', function(){
		if(this.selectedIndex != -1){
			dojo.addClass(dojo.byId("error-message"),"d-none");
		}
	});
	dojo.connect(dojo.byId('iRecipeName'), 'onkeyup', function(evt){
		if(enterClick(evt)){
			_findByName();
		}
	});
}

_eventSelects = function(){
	dojo.connect(dojo.byId('sIngredients'), 'ondblclick', function(){
		_add_ingredient();
	});
}

_findByName = function(){
	if(_nameInputValidation()){
		_searchingLayer();
		dojo.xhrPost({
			//url: dojo.byId("RecipeFindRecipeByNameForm").action,
			handleAs: "text",
			form:"RecipeFindRecipeByNameForm",
			load: function(data,args){
				qc.common.hideLoading2('cLoading','cRecipes');
				dojo.byId("cRecipes").innerHTML = data;
				// fade out the node we're modifying
			},
			// if any error occurs, it goes here:
			error: function(error,args){
				console.warn("error!",error);
			}
		});
	}
}

_selectValidation = function(){
	if(dojo.byId('sIngredients').selectedIndex == -1){
		//dojo.removeClass(dojo.byId("error-message"),"d-none");
		return false;
	}
	return true;
}

_allowAddition = function(ingredientId){
	var found = dojo.some(dojo.query(".ingredientListElement"),	function(pItem){
				
		var currentIngredientId = dojo.query("label",pItem.parentNode.parentNode)[0].getAttribute('ingredientId');
		if(currentIngredientId == ingredientId){
			return true;
		}
		return false;
	});
	return !found && (dojo.query(".ingredientListElement").length <= 10);
}

_add_ingredient = function(){
	if(_selectValidation()){
		dojo.addClass(dojo.byId("promo_message"),"d-none");
		dojo.removeClass(dojo.byId("cIngredientsSummary"),"d-none");
		dojo.removeClass(dojo.byId('iRecipeName'), 'field-error-mandatory');
		
		var selectedElement = dojo.byId("sIngredients").options[dojo.byId("sIngredients").selectedIndex];
		if(_allowAddition(selectedElement.value)){
			var aNewIngredient = dojo.byId("cIngredientsList").innerHTML;
			aNewIngredient = aNewIngredient +"<p><label ingredientId='"+
						selectedElement.value + "'>"+ 
						selectedElement.text + "</label>";
			
			aNewIngredient = aNewIngredient+"<span><a href='#' class='ingredientListElement mar-lef-10'>"+ nls.textDelete+ "</a></span></p>";
				
			
			dojo.byId("cIngredientsList").innerHTML = aNewIngredient;
			// assign delete event controls
			_ingredientList();
		}
	}
}

_searchingLayer = function(){
//	dojo.removeClass(dojo.byId("cLoading"),"d-none");
//	dojo.addClass(dojo.byId("cRecipes"),"d-none");
	qc.common.showLoading2('cLoading','cRecipes')
}

_nameInputValidation = function(){
	
	var oInput = dojo.byId('iRecipeName');
	var sValueInput = oInput.value;
	
	if(sValueInput == ''){
		dojo.addClass(oInput, 'field-error-mandatory');
		return false;
	}
	
	dojo.removeClass(oInput, 'field-error-mandatory');
	
	return true;
}

_ingredientList = function(){
	dojo.query(".ingredientListElement").forEach(function(pItem){		
		dojo.connect(pItem, 'onclick', function(){			
			var pEntry = pItem.parentNode.parentNode;
			dojo.byId("cIngredientsList").removeChild(pEntry);
			if(dojo.query(".ingredientListElement").length == 0){
				dojo.addClass(dojo.byId("cIngredientsSummary"),"d-none");
				dojo.removeClass(dojo.byId("promo_message"),"d-none");
			}
		});
	});
}

_bannerClicks = function(){
	dojo.query(".banner").forEach(function(pItem){		
		dojo.connect(pItem, 'onclick', function(){			
			var bannerId = pItem.getAttribute('bannerId');
			dojo.xhrPost({
				url: "banners/registerClick",
				handleAs: "json",
//				load: function (data) {
//					//dojo.byId("cRecipes").innerHTML = data;
//                   alert(data);
//                },
				content:{"bannerId":bannerId}
			//,
				// if any error occurs, it goes here:
//				error: function(error,args){
//					console.warn("error!",error);
//				}
			});
		});
	});
}
_assignRightHeigth = function(){
	if(dojo.isIE && dojo.isIE < 7){
		var heigthAvailable =dojo.style(dojo.byId("center"), "height")+ "px";
		dojo.style(dojo.byId("centralleftSection"), "height", heigthAvailable);
	}else{
		var heigthAvailable =dojo.style(dojo.byId("center"), "minHeight")+ "px";
		dojo.style(dojo.byId("centralleftSection"), "minHeight", heigthAvailable);
	}
}

enterClick = function(event) {
	if (event.keyCode == 13) {
		return true;
	}
	return false;
}

function _onLoad(){
	// ejecutamos los metodos necesarios
	_eventButtons();
	_eventSelects();
	_bannerClicks();
	_assignRightHeigth();
}

dojo.addOnLoad(_onLoad);
