//--------------------------------------------------------------------------------
// Qwentès Italia SRL 	: http://www.qwentes.it
//--------------------------------------------------------------------------------

$(document).ready(function() {	
	
	$("a[class^='add-agenda-event']").click(function () {
		add_agenda_event($(this).attr("class"));
		$("#cluetip").css("display","none");
	});
	
	$("a[class^='remove-agenda-event-']").click(function() {
		remove_agenda_event(this.id, $(this).attr("class")); 
		$("#cluetip").css("display","none");
	});

});

function add_agenda_event(myclass){

	var ItemID = myclass.replace("add-agenda-event-", "");

	$.getJSON('/agenda_days_events_json',function(json){
		var str = 'Select the day'.localize() + ': <select name="myfield" id="myfield">';
		$.each(json,function(i,obj){
			str += '<option value="'+ obj.value +'">'+ obj.name +'</option>';
		});
		str += '</select>';
		str += '<input type="hidden" name="itemID" value="'+ItemID+'">'; 						
		function mycallbackform(v,m,f){							
			if (f) {
				
				//=> Let's switch the link: from ADD to REMOVE
				var MyNew_Html = '';
				MyNew_Html	+= '<a href="#" id="day-' + f.myfield + '" class="remove-agenda-event-' + ItemID + '">';
				MyNew_Html 	+= 'Remove from your agenda'.localize();
				MyNew_Html 	+= '</a>';
				$("div.Item-"+ItemID).html(MyNew_Html);
				//alert(New_Html);

					$("div.Item-"+ItemID + " > a").click(function() {
					//alert($(this).attr("class"));
					//alert(this.id);
					remove_agenda_event(this.id,$(this).attr("class")); 
					$("#cluetip").css("display","none");
				});

				//$("." + myclass).css("display","none");
				//$.prompt('Your agenda has been updated'.localize(), {persistent: false});								
				$("#useragenda").load("/agenda_events/add_event/"+f.itemID+"/" + f.myfield);

			}
		}
		$.prompt(str,{
		      callback: mycallbackform,
			  persistent : false
		});	
		
	});


}

function remove_agenda_event(myid, myclass){
	var ItemID = myclass.replace("remove-agenda-event-", "");
	var Day    = myid.replace("day-", "");
	var str = 'Are you sure?'.localize();
	str += '<input type="hidden" name="itemID" value="'+ItemID+'">'; 
	str += '<input type="hidden" name="myday" value="'+Day+'">'; 						
	function mycallbackform(v,m,f){							
		if (f) {
			//$.prompt('Your agenda has been updated'.localize(), {persistent: false});	
			//$("."+myclass).html("removed");
			//$("#row-itemID-"+f.itemID).hide();

			//=> Let's switch the link: from REMOVE to ADD
			var New_Html = '';
			New_Html	+= '<a href="#" id="add-agenda-event-' + ItemID + '" class="add-agenda-event-' + ItemID + '">';
			New_Html 	+= '<img src="/libs/images/addressbook-icon.gif" alt="" title="" /></a><br />';
			New_Html 	+= '<a href="#" id="add-agenda-event-' + ItemID + '" class="add-agenda-event-' + ItemID + '">' + "Add to your agenda".localize() + '</a>';
			$("div.Item-"+ItemID).html(New_Html);
			//alert(New_Html);
			
			$("div.Item-"+ItemID + " > a").click(function() {
				//alert("new add");
				add_agenda_event($(this).attr("class")); 
				$("#cluetip").css("display","none");
			});
			
			$("#useragenda").load("/agenda_events/remove_event/"+f.itemID+"/" + f.myday);
		}
	}

	$.prompt(str,{
	      callback: mycallbackform,
		  persistent: false
	});	

	
}
