function check_login(userid){
	if($('logged_in_div') && userid == null ) location.reload(true);
	return;
}


function ingredientChange(el){
	if(el.options[el.selectedIndex].value == "other"){
		document.getElementById("addnew").style.display= "";
		
		document.getElementById("other_ingredient").focus();
		otherID = el.id;
		
	}else { 	
			document.getElementById("addnew").style.display= "none";	
			
			//if(el.id.split("_")[1] >= iNUM ) insertIngredient(el.id); //only add another ingredient if this is the last one in the list
	}
}

function ingredientAmountChange(el){
	if(el.options[el.selectedIndex].value == "other"){
		document.getElementById("addnew_amount").style.display= "";
		
		document.getElementById("other_amount").focus();
		otherAmountID = el.id;
		
	}else { 	
			document.getElementById("addnew_amount").style.display= "none";	
			
			//if(el.id.split("_")[1] >= iNUM ) insertIngredient(el.id); //only add another ingredient if this is the last one in the list
	}
}

var iNUM = 1;
var otherID;
var otherAmountID;
function count_ingredients(){
	var cnt = 0;	
	var selects = document.getElementsByTagName('select');
		
		
		for (var i =0; i < selects.length; i++ ) {
			s = selects[i];
			if(s.id.indexOf('ingredient_') != -1 &&  s.id.indexOf('ingredient_label') == -1 )cnt++;
			
		}
		
		return cnt;
}

function removeIngredient(obj){
	var cnt = 1;
	var el = document.getElementById("ingredients"); //Ingredients Div that holds all the ingredients
	var iDelNum = obj.split("_")[2];  //get the number of the ingredient from the div's id
	
	var i_div=document.getElementById('i_container_'+(iDelNum));
	var i_amount_div=document.getElementById('i_amount_container_'+(iDelNum));
	
	if(count_ingredients() > 1){
	el.removeChild(i_div);
	el.removeChild(i_amount_div);
	
	}
	
		
	//reset the ingredient #'s
	var labels = document.getElementsByTagName('label');		
		for (var i =0; i < labels.length; i++ ) {
			s = labels[i];
			if(s.id.indexOf('ingredient_label_') != -1 ){
				s.innerHTML = "Ingredient "+cnt;
				cnt++;
			}
		}
		
			

	return true;
}



function insertIngredient(obj){
	iNUM++;
	
	//get the ingredient number of the one selected to add from its id
	var iAddNum = (obj.split("_")[2]) ? obj.split("_")[2] : obj.split("_")[1];
	
	var el = document.getElementById("ingredients"); //Ingredients Div that holds all the ingredients
	
	//Check to see of the ingredient Select exists, otherwise use the default one and also reset the counter.
	if(document.getElementById('ingredient_'+(iAddNum)) != null)
		iSelect = document.getElementById('ingredient_'+(iAddNum));
	
	else { iSelect = document.getElementById('ingredient_1'); iNUM =2;}
	
	//var iSelect =  document.getElementById('ingredient'+(iNUM-1)) != null ?  document.getElementById('ingredient'+(iNUM-1)) : document.getElementById('ingredient1'); //Ingredient Select 
	

	var iSelectLabel=document.getElementById('ingredient_label_'+(iAddNum)); //Label for the select
	var iAddImg=document.getElementById('i_add_'+(iAddNum)); //Label for the select
	var iDelImg=document.getElementById('i_del_'+(iAddNum)); //Label for the select
	
	var iAmountLabel=document.getElementById('i_amount_label_'+(iAddNum)); //Label for the select
	var iAmount=document.getElementById('i_amount_'+(iAddNum)); //Label for the select
	var iAmountUnit=document.getElementById('i_amount_unit_'+(iAddNum)); //Label for the select

	var newSelect=iSelect.cloneNode(true);
	var newSelectLabel=iSelectLabel.cloneNode(true);
	var newAddImg=iAddImg.cloneNode(true);
	var newDelImg=iDelImg.cloneNode(true);
	
	var newAmountLabel=iAmountLabel.cloneNode(true);
	var newAmount=iAmount.cloneNode(true);
	var newAmountUnit=iAmountUnit.cloneNode(true);
	
	//var newSpacer=document.getElementById('i_spacer_'+(iAddNum)).cloneNode(true);
	
	
	
	newSelect.id = "ingredient_"+iNUM;
		
	newAddImg.id = "i_add_"+iNUM
	newDelImg.id = "i_del_"+iNUM
	
	newAmount.id = "i_amount_"+iNUM
	newAmountLabel.id = "i_amount_label_"+iNUM
	newAmountUnit.id = "i_amount_unit_"+iNUM
	
	//newSpacer.id = "i_spacer_"+iNUM
	
	newSelectLabel.id = "ingredient_label_"+iNUM;
	newSelectLabel.innerHTML = "Ingredient "+ (count_ingredients() + 1);
	
	
	var i_div = document.createElement('div');	
	var i_amount_div = document.createElement('div');
	
	i_div.id = "i_container_"+(iNUM);
	i_div.style.position = "relative";
	i_div.style.top = "12px";
	
	i_amount_div.id = "i_amount_container_"+(iNUM);
	i_amount_div.style.position = "relative";
	i_amount_div.style.top = "12px";
	
	//el.appendChild(newSpacer);
	el.appendChild(i_div);
	i_div.appendChild(newSelectLabel);
	i_div.appendChild(newSelect);
	i_div.appendChild(newDelImg);
	i_div.appendChild(newAddImg);
	
	el.appendChild(i_amount_div);	
	i_amount_div.appendChild(newAmountLabel);	
	i_amount_div.appendChild(newAmount);
	i_amount_div.appendChild(newAmountUnit);	


}

function addNew(){
	var el=document.getElementById(otherID); //Selected select
	var elOther=document.getElementById('other_ingredient'); //new ingredient text box

	var newOption = document.createElement('option');
	
	el.appendChild(newOption);
	newOption.innerHTML = elOther.value;
	newOption.value = elOther.value;
	el.selectedIndex = el.options.length -1 ;
	document.getElementById("addnew").style.display= "none";
	//insertIngredient();
}

function addNewAmount(){
	var el=document.getElementById(otherAmountID); //Selected select
	var elOther=document.getElementById('other_amount'); //new ingredient text box

	var newOption = document.createElement('option');
	
	el.appendChild(newOption);
	newOption.innerHTML = elOther.value;
	newOption.value = elOther.value;
	el.selectedIndex = el.options.length -1 ;
	document.getElementById("addnew_amount").style.display= "none";
	//insertIngredient();
}



function update_rating(m_id, m_rating) {	
	var url = '/browse_recipes/update_rating/';
	var params = '&id='+m_id+'&rating='+m_rating;
	
	//var target = 'message';
	//var ajax = new Ajax.Updater(target, url, {method: 'post', parameters: params});
	//var ajax = new Ajax.Request(url+params, {method: 'post', parameters: params, onComplete: update_rating_callback});
	
	//$('rating_text_'+m_id).innerHTML = "(Rated)";
	
	LINK_ajax(url+params,'hidden_div');
}
function update_rating_callback(response) {
	var resp_a = response.responseText.split(",");
	update_rating_hidden(resp_a[0],resp_a[1]);
	change_stars_out(resp_a[0]);
}

function update_rating_hidden(id, rating) {
	var d = $('rating'+id);
	d.value = rating;
	return false;
}

function change_stars_hover(id, rating) {
	for(var s = 0; s < 10; s++){
		var star = $('star_'+id+'_'+(s+1));
	
		if (s < rating) {
			weight = "bold";
		} else {
			weight = "light";
		}
		
		if (s%2 == 0) {
			dir = "left";
		} else {
			dir = "right";
		}
		
		star.src = "http://recipes.vangoghvodka.com/images/icons/star_" + weight + "_" + dir + ".png";
	}
}

function change_stars_out(id) {
	var d = $('rating'+id);
	var rating = d.value;
	change_stars_hover(id, rating);
}



//for ajax (find the parent div to load a page in)
function getParentDiv(el){
	
	while(el = el.parentNode){
		if( (el.nodeName == "DIV" || el.nodeName == "div") && el.className != "recipe_content" && el.id) return el.id;
		
	}
	
	return 'content';
	
}


//------ Comments Functions -----//



function toggleComments(elID,obj){
	var el = document.getElementById(elID);
	var nodes = el.childNodes;
	if(nodes){
		for(var i = 0; i< nodes.length; i++){
			
			if(nodes[i].nodeName == "DIV"){
				if(nodes[i].style.display == 'none'){
				nodes[i].style.display = '';
				obj.innerHTML = '<img src="/admin/images/icons/comments_delete.png" />Hide Comments';
				}
				else {
				nodes[i].style.display = 'none';
				obj.innerHTML = '<img src="/admin/images/icons/comments.png" />Show Comments';	
				}
			
			}
			
		}
	}

return;
}


//----------------------------//
//window.onload = recipe_init();