/**
add a wine to winecellar
@param wineid - wine number
@param successmessage - localized success message
@param failmessage - localized fail message
@param diggbuttonid - the button used to submit the request - will be hidden during the request!
@param digglikedid - the element to show when the request is successfull (set inline css to display:none first)
@param diggboxid - the container that shows the number of people who liked the item - will be updated if specified
@param loaderid - this element will be shown during the request if specified (for example a loader gif image)
@author ade
**/
function likeWine(wineid, successmessage, failmessage, diggbuttonid, digglikedid, diggboxid, loaderid){
	var ajaxurl = "/winecellar/add/"+wineid;
	
	//Hide digg number
	like_wine_setvisible(diggboxid, false);
	
	//Hide button
	like_wine_setvisible(diggbuttonid,false);

	//Check if we should show a loader
	like_wine_setvisible(loaderid,true);
    
    //Make web request
	var myAjax = new Ajax.Request(
	ajaxurl,
	{ 	method: 'get', 
		onSuccess: function(req) {
			//The request should return the number of people who like the wine
			var value = parseInt(req.responseText);
			if(diggboxid != undefined && diggboxid != null) {
				//If there's a digg counter, update it
				if(value > 0) {
					$(diggboxid).update(value);
				}
			}
			
			//Show confirmation element
			like_wine_setvisible(digglikedid, true);
			
			//Hide loader
			like_wine_setvisible(loaderid, false);
			
			//Show the number
			like_wine_setvisible(diggboxid, true);
			
			//Show popup message
			var g = new k.Growler();
			g.growl(successmessage, {header: "Winelinking", life: "5"});
		}, 
		onFailure: function() {
			//Show fail message
			var g = new k.Growler();
			g.growl(failmessage, {header: "Winelinking", life: "5"});
			
			//Hide loader
			like_wine_setvisible(loaderid, false);
			
			//Show button again
			like_wine_setvisible(diggbuttonid, true);
			
			//Show number
			like_wine_setvisible(diggboxid, true);
		} 
	});
}

/**
* remove wine
@author ade
@param wineid wine id
@param hideitemid id of a item to hide
@param successmessage - localized success message
@param failmessage - localized fail message
**/
function removeWine(wineid, hideitemid, successmessage, failmessage) {
	var ajaxurl = "/winecellar/remove/"+wineid;
	//new Effect.Opacity(hideitemid, {from: 1.0, to: 0.3, duration: 0.5});
	
	var myAjax = new Ajax.Request(
	ajaxurl,
	{ 	method: 'get', 
		onSuccess: function(req) {
			new Effect.SlideUp(hideitemid, {duration: 1.0});
			//Show popup message
			growl(successmessage);
		}, 
		onFailure: function() {
			//Show fail message
			growl(failmessage);
			//new Effect.Opacity(hideitemid, {from: 0.3, to: 1.0, duration: 0.5});
			
		} 
	});	
	
}

function like_wine_setvisible(elemid, visible) {
	if(elemid != undefined && elemid != null && $(elemid) != null) {
		if(visible)
			$(elemid).show();
		else
			$(elemid).hide();
	}
}
