/*
 * --------------------------------------------------------------------
 * In-place editing system
 * by Siddharth S, www.ssiddharth.com, hello@ssiddharth.com
 * and then changed by Ashok Datla.
 * for Net Tuts, www.net.tutsplus.com
 * then for TheLiv, livthis.com
 * Version: 1.1, 24 FEB 2011 	
 * --------------------------------------------------------------------
 */

$(document).ready(function() 
{
	makeOnlineEditable();
}
);

function makeOnlineEditable() {
	
  	$(".editable").hover(
		function()
		{
			$(this).addClass("editHover");
		}, 
		function()
		{
			
			$(this).removeClass("editHover");
		}
	);
  
  	$(".editable").bind("click", replaceHTML);
	 
	$(".btnSave").live("click", 
					function()
					{
						var id = $(this).parent().attr('id'); 
						if(id === undefined)
							return;
						newText[id] = $(this).siblings("form")
										 .children(".editBox")
										 .val().replace(/"/g, "&quot;");
						

						if(id.contains("titlewords")) {
							saveWishlistitemDetails(this, 'title', id, newText[id]);
						} else if(id.contains("descriptionwords")) {
							saveWishlistitemDetails(this, 'description', id, newText[id]);
							
						} else if(id.contains("friendscomment")) {
							saveWishlistitemDetails(this, 'caption', id, newText[id]);
							
							var gid = id.substring(id.indexOf('-')+1);
							var captionwordtext = '#addacaptionword-'+gid;
							var captionentered = newText[id];

							if(captionentered == '') {
								captionentered = 'Write a caption...';
							} 
							$(captionwordtext).text(captionentered);
						}
						//alert('Setting text to '+newText[id]);
						$(this).parent()
							   .html(newText[id])
							   .removeClass("noPad")
							   .bind("click", replaceHTML);
					}
					); 
	
	$(".btnDiscard").live("click", 
					function()
					{
						var id = $(this).parent().attr('id');
						//alert('Setting text to '+oldText[id]);
						$(this).parent()
							   .html(oldText[id])
							   .removeClass("noPad")
							   .bind("click", replaceHTML);
					}
					); 

	function saveWishlistitemDetails(obj, attr, id, value) {
		//alert('Into saveWishlistitemDetails');
		var gid = id.substring(id.indexOf('-')+1);
		$.post('/wishlistitems/saveWishlistItemDetails', {
				field: attr,
				value: value,
				gid: gid
			},
			function(data) {
				var message = data.message;
				
				if(message.contains("SUCCESS")) {
					notify("success", "Save successful");
					document.location='';
				return;
				} else {
					notify("failure", data.error);
					
				}
			}, "json"
		);
	};
	
	function replaceHTML()
					{					
						if(previouseditableid == "none") {
							previouseditableid = $(this).attr("id");
						} else {
							var editablefield = '#'+previouseditableid;
							$(editablefield)
								.html(oldText[previouseditableid])
								.removeClass("noPad")
								.bind("click", replaceHTML);
						}

						oldText[$(this).attr('id')] = $(this).html()
										 .replace(/"/g, "&quot;");
								 
						oldText[$(this).attr('id')] = trim(oldText[$(this).attr('id')]);
						$(this).addClass("noPad")
							   .html("")
							   .html("<form><textarea class=\"editBox\"> " + oldText[$(this).attr('id')] + "</textarea> </form><a href=\"#\" class=\"btnSave\">save</a><a href=\"#\" class=\"btnDiscard\">cancel</a>")
							   .unbind('click', replaceHTML);
			
					}

	function trim(str) {
		return str.replace(/^\s+|\s+$/g,"");
	}

}
