/**
 * Recipe
 *
 * @author Mike Bosworth
 * @classDescription Recipe handles dwr calls, edit-in-place, and event listeners for Recipe
 **/

mdp.app.Recipe = function(){
    /* ---[ CLASS VARIABLES ]--- */
	
	/* private */
	var noteText = "";
	
    /* ---[ CONSTRUCTOR ]--- */
    this.init = function(){
		/* initialization code */
		setupEventListeners();
    };
	

    /* ---[ PUBLIC METHODS ]--- */
	this.editInPlace = function(element){
		var text = noteText = element.text();
		element.text("");
		element.html("<textarea id='noteinput' style='width:99%' >"+ text +"</textarea>");
		$("#noteactionlayer").attr('class','');
	};

	this.unedit = function(saveText){
		var noteinput = $("#noteinput");
		var note = noteinput.parent();
		noteinput.remove();
		note.text(noteText);
		$("#noteactionlayer").attr('class','hide');
	};

	this.handleSaveNote = function(){
		var noteinput = $("#noteinput");
		var note = noteinput.val();
		var clippingId = noteinput.parent().attr('id').split(":")[1];
		//RecipeFileService.updateFolderNote(folderId,note, saveNoteCallback);
		ClippingService.updateNote(clippingId,note,saveNoteCallback);
	};
	
	this.addIngredients = function(link){
		var data = link.attr("data").split(":");
		var profileId = data[0];
		var recipeId = data[1];
		$("<img>").attr({'src':'http://www.bhg.com/bhg/images/reg/ajax_load_indicator.gif'}).appendTo(link);
		RecipeFileService.addIngredientsToShoppingList(profileId,recipeId,addIngredientsCallback);
	};
	
	this.showThumbnail = function(element){
		/* get image */
		
		
	};
	
    /* ---[ PRIVATE METHODS ]--- */
	function saveNoteCallback(remoteResult){
		if(remoteResult.statusCode == 0){
			var noteinput = $("#noteinput");
			var notetext = noteinput.val();
			var note = noteinput.parent();
			noteinput.remove();
			note.text(notetext);
			$("#noteactionlayer").attr('class','hide');
		}
	}	
	
	function addIngredientsCallback(remoteResult){
		if (remoteResult.statusCode == 0) {
			var addingredientslink = $("#addingredientslink");
			$("<span>").html('ingredients added').insertBefore(addingredientslink);
			addingredientslink.remove();
		}		
	}

    /* ---[ EVENT LISTENERS ]--- */
    function setupEventListeners(){	
	    /* attach edit-in-place event handler to #editnotelink */
		var editnote;
		if(editnote = $("#editnotelink")){
			editnote.click( function(){ mdp.recipe.editInPlace($('#notesContainer').find('p[id^=note]'));});

			/* attach ajax handler method to #savenotelink */
			$("#savenotelink").click( function(){ mdp.recipe.handleSaveNote(); });
		}
		
		var canceledit = $("#canceledit");
		if(canceledit != null){
			canceledit.click(function(){ mdp.recipe.unedit(); });
		}
		
		var addIngredients;
		if(addIngredients = $("#addingredientslink")){
			addIngredients.click( function(){mdp.recipe.addIngredients(this);});
		}
				
    }
    
    /* ---[ RUN ]---  */
    this.init();
};

 $(document).ready( function(){
    mdp.recipe = new mdp.app.Recipe();
});


/* --- [ LEGACY JS ] ---*/

function go(box)
{
	if(box != null) {
        var destination = box.options[box.selectedIndex].value;
    }
    if(destination){
        var isPremium = destination.indexOf("xml");
        if(isPremium != -1){
            location.href = "/recipes/collectionDisplay.jsp?collectionId=" + destination;
        }
        else{
            location.href = destination;
        }
    }
}

var rollover;
function toggleDisplay(parent){
    if(rollover == null){
        rollover = $("#rcrollover");
        rollover.style.visibility = "hidden";
    }
    if(rollover != null){
        if(rollover.style.visibility == "hidden"){
          rollover.style.visibility = "visible";
        }
        else{
          rollover.style.visibility = "hidden";
        }
    }
}
