var pcalPentryCloneMoveManager = function(websocket, pcalview) {
	var _ws = websocket;
	var _pcalview = pcalview;
	var _date;
	var _pentrytype;
	var _uuid;
	var _clone_or_move;
	var _is_active = false;

	_ws.bind('pcal_clone_move_get_parishday_result',function(data){
			console.log('got_parishday');
			console.log(data);
			var html = pentry_clone_move_parishday_template({day: data.day } );
			$("#pcal_pentry_clone_move_dayinfo").html(html);
			$("#pcal_pentry_clone_move_scroll_parent").scrollTop(0);
		});

	_ws.bind('pentry_clone_move_result', function(data){

		var message = errorview_inlay_template({ messages: data.messages, is_error: (data.result === 'OK' ? false : true), session_ended: false});

		saved_messages.push(message);

		pcal_pentry_clone_move_manager.finish();
		history.back();
	});

	this.get_date = function() { return _date; }

	this.set_date = function(date) {
		_date = date;
		console.log(_date);
	}

	this.is_active = function() {
		return _is_active;
	}

	this.start = function(pentrytype, uuid, clone_or_move) {
		_is_active = true;
		_pentrytype = pentrytype;
		_uuid = uuid;
		_clone_or_move = clone_or_move;

		console.log('start', _date, _pentrytype, _uuid, _clone_or_move);

		if(_pentrytype === "event" || history.state.direction === 'clone_move')
			history.replaceState({direction: 'clone_move', pentrytype: _pentrytype, uuid: _uuid, clone_or_move: _clone_or_move, date: _date.date}, document.title, "/"+_pcalview+"/"+_date.date);
		else
			history.pushState({direction: 'clone_move', pentrytype: _pentrytype, uuid: _uuid, clone_or_move: _clone_or_move, date: _date.date}, document.title, "/"+_pcalview+"/"+_date.date);

		var pentry = $("[data-pentryuuid="+uuid+"]");

		console.log(pentry);

		if(pentry.length != 1)
		{
			console.log('ERROR IN GETTING PENTRY!!!')
			history.popState();
			return;
		}

		var renderdata = {	pentrytype	: _pentrytype,
							date 		: _date,
							uuid 		: _uuid
						};

		switch(clone_or_move) {
			case "clone":
				renderdata["clone_or_move"] = "clone";
				renderdata["clone_or_move_pretty_verb"] = "geklont";
				renderdata["clone_or_move_pretty_verb2"] = "klonen";
				renderdata["clone_or_move_pretty_noun"] = "Klonen";
				break;
			case "move":
				renderdata["clone_or_move"] = "move";
				renderdata["clone_or_move_pretty_verb"] = "verschoben";
				renderdata["clone_or_move_pretty_verb2"] = "verschieben";
				renderdata["clone_or_move_pretty_noun"] = "Verschieben";
		}

		var name = pentry.data('pentryname');
		var addname = pentry.data("pentryaddname");
		if(addname.length > 0)
			name = name + " (" + addname + ")";

		console.log(name);
		renderdata["pentryname"] = name;

		switch(_pentrytype) {
			case "event":
				renderdata["pentrytype_pretty"] = "Ereignis";
				break;
			case "away":
				renderdata["pentrytype_pretty"] = "Abwesenheit";
				break;
			case "keep_in_mind":
				renderdata["pentrytype_pretty"] = "Zu-Beachten";
				break;
			case "todo":
				renderdata["pentrytype_pretty"] = "Zu-Tun";
				break;
			case "vehicle_use":
				renderdata["pentrytype_pretty"] = "Fahrzeugbenutzung";
				break;
		}
		var html = pentry_clone_move_template(renderdata);

		$("#pentry_clone_move_parent").html(html);

		$('#pcal_pentry_clone_move_datepicker').datepicker({format: "yyyy-mm-dd", language: "de", calendarWeeks: true, todayHighlight: true});
		$("#pcal_pentry_clone_move_datepicker").on("changeDate", function(event) {
			var d = $("#pcal_pentry_clone_move_datepicker").datepicker('getFormattedDate');
			console.log('date: ', d);

			$("#pcal_pentry_clone_move_new_date").val(d);

			$("#pcal_pentry_clone_move_alert").hide();

			ws.send('get_parishday', { answer_prefix: "pcal_clone_move_", date: d });
		});

		$("#form-clonemove").on('submit', function(e){
			e.preventDefault();

			var new_date = $("#pcal_pentry_clone_move_new_date").val();
			console.log(new_date);

			if(new_date === _date.date) {
				$("#pcal_pentry_clone_move_alert").show();
				return;
			}

			var data = {
				"request"	: "pentry_clone_move",
				"type"		: _clone_or_move,
				"date"		: _date.date,
				"pentrytype": _pentrytype,
				"uuid"		: _uuid,
				"new_date"	: new_date
			}

			console.log(data);

			ws.send('pentry_clone_move', data);
		});

		$("#pentry_clone_move_parent").show();

		ws.send('get_parishday', { answer_prefix: "pcal_clone_move_", date: _date.date });
	}

	this.finish = function() {
		$("#pentry_clone_move_parent").hide();
		$("#pentry_clone_move_parent").empty();
		_is_active = false;
	}
}





/*$("[data-hide]").on("click", function(){
	$("." + $(this).attr("data-hide")).hide();*/
/*
 * The snippet above will hide all elements with the class specified in data-hide,
 * i.e: data-hide="alert" will hide all elements with the alert property.
 *
 * Xeon06 provided an alternative solution:
 * $(this).closest("." + $(this).attr("data-hide")).hide();
 * Use this if are using multiple alerts with the same class since it will only find the closest element
 *
 * (From jquery doc: For each element in the set, get the first element that matches the selector by
 * testing the element itself and traversing up through its ancestors in the DOM tree.)
*/
