

function prepareEventsInSave(id, me_too, win_type) {

	//ako quote nije prazan a note jeste prebacujemo na njega
	if ($("#quote").val() != "" && $("#notes").val() == "") {
		$(".tab_2 div").removeClass("active");
		$("#button_quote").addClass("active");
	}

	//postavljanje note/quote
	if ($("#button_notes").attr("class") == "active")
		$("#notes").show();
	else
		$("#quote").show();


	//event note/quote buttons
	$("#button_notes, #button_quote").bind("click", function() {
		var this_target = $(this).attr("id").substr(7);

		if ($(this).attr("class") != "active") {
			$(".tab_2 div").removeClass("active");
			$(this).addClass("active");
			$(".tab_2 textarea").css("display", "none");
			$("#"+this_target).css("display", "block");
			$("#"+this_target).focus();
		}

	});

	var win_type_part = (win_type != "") ?  "&type" : "";

	$(".vote_up, .vote_down").bind("click", setSaveVoteEvent);

	$("#tag_info_wrapper .close").bind("click", function() {
		$("#tag_info_wrapper").hide();
		return false;
	});

	$("a#button_edit").bind("click", function() {

		if ($(".tab_2").css("display") == "none") {
			$(this).text("< edit tags");
			$(".tab_2").show();
			$(".tab_1").hide();

			if ($("#button_quote").attr("class") == "active")
				$("#quote").focus();
			else
				$("#notes").focus();

		}
		else {
			$(this).text("edit bookmark >");
			$(".tab_2").hide();
			$(".tab_1").show();

			$("#tags").select();
			$("#tags").focus();
		}

		return false;
	});

	setTagsEvents();

	$("#tags").bind("click", function() {
		if ($(this).val() == "add new tag") {
			$(this).val("");
		}
	});

	$("#tags").select();
	$("#tags").focus();


	//prepare google api stuff
	google_object = false;
	$("#searchcontrol").appendTo($("#searchcontrol_tags_container"));

	//event na google button
	$("#google_tags").bind("click", googleSearch);


	//cancel button
	$("#cancel").bind("click", function() {

		//ako nije iframe ili popup
		if (win_type == "") {
			google_object = true;
			$("#searchcontrol").hide().appendTo($("#searchcontrol_object_container"));

			$("#entries_list .selected").removeClass("selected");
			$("#save_holder").css("display", "none").html("");
			$("#bg_save").hide();
			$(".dark").remove();
		}
		else if (win_type == "popup")
			window.close();

	});


	//save button
	$("#submit_save").bind("click", function() {

		if ($("#current_tags .tag:visible").size() == 0) {
			noTags();
			return false;
		}

		if (win_type == "iframe" && $.browser.mozilla)
			parent.window.location.hash = "faviki_close";

		if (win_type == "")
			var no_subject = ($("#f"+id+" .subject").size() == 0) ? true : false;

		var inputs = new Array();
		var all_inputs = $("#save :input");
		all_inputs.each(function() {
			if (this.type == "checkbox") {
				if (this.checked)
					inputs.push(this.name + '=' + this.value);
				else
					inputs.push(this.name + '=');
			}
			else
				inputs.push(this.name + '=' + encodeURIComponent(this.value));
		});
		var post_data = inputs.join('&');

		if (me_too)
			post_data += "&me_too=true";

		if (id == "add_bookmark_submit") {
			post_data += "&new_from_site=true";
		}

		$(this).val("Saving...");

		var d_tags = '&tags=';
		$("#current_tags .tag:visible").each(function() {
			d_tags += $(this).text()+"|||";
		});


		//sync
		$.ajax({
			type: "POST",
			url: "/ajax/sync.php",
			data: (post_data+d_tags+'&action=s'),
			success: function(msg) {
				//alert(msg);
			}
		});

		$.ajax({
			type: "POST",
			url: "/ajax/edit_entry.php?id="+id+win_type_part,
			data: post_data,
			success: function(msg) {

				if (msg == 'no_tags') {
					noTags();
					return false;
				}

				msg = stripSashes(msg);
				if (win_type != "") {
					if (win_type == "popup")
						window.close();
					else {
						$("#submit_save").val("Save");
					}
				}
				else {
					$("#searchcontrol").hide().prependTo($("body"));
					//ako je new
					if (id == "add_bookmark_submit" || me_too) {
						location.href = '/?user';
					}
					// ako je edit
					else {
						$("#f"+id).removeClass("selected");
						$("#f"+id).fadeOut("fast", function() {
							
							$("#f"+id).html(msg);
							if (no_subject)
								$("#f"+id+" .subject").remove();
							
							$("#f"+id).fadeIn("fast", function() {
								$("#e"+id).bind("click", editEntry);
								$("#d"+id).bind("click", deleteEntry);
							});
						});
					}
					google_object = true;
					$("#searchcontrol").hide().appendTo($("#searchcontrol_object_container"));

					$("#save_holder").css("display", "none").html("");
					$("#bg_save").hide();

					setTimeout(function() {
						setEntriesEvents(id);
						setMediaEvents();
						JT_init(".t, .t_hl");
					}, 500);
				}

				fixImages();

				}
			});


		if (win_type == "") {
			$(".edit_button, .save_button").removeClass("dis");
			$(".dark").remove();
		}

		return false;
	});

	$("#button_suggested_tags, #button_recent_tags").bind("click", setSugRecEvents);

	if ($("#suggested_tags_wrapper").size())
		sugRecTags('suggested', $("#webpage").val(), isTitleinEnglish($("#title").val()));

	if ($("#recent_tags_wrapper").size()) {
		sugRecTags('recent');
		$("#recent_tags_left, #recent_tags_right").bind("click", sugRecTags);
	}

	fixSafari();
	JT_init(".tag_name");
	setTutEvents();
}


function noTags() {
	alert("You must add at least one tag!");
	$("#submit_save").val("Save");
	$("#tags").focus();
}


function setTagsEvents() {

	$("#tags").autocomplete("/ajax/get_tags.php", {
		extraParams : { all:'dojka', lang:lang },
		minChars: 1,
		max: 13,
		matchCase: true,
		mustMatch: true,
		matchContains: true,
		selectFirst: false,
		matchSubset: false
	});

	$("#tags").keypress(function (e) {
		if ((window.event && window.event.keyCode == 13) || ($.trim($(this).val()) != '' && e.which == 13)) {
			var tag_name = $(this).val();
			$("#add_tag_label").text("Type in one tag at a time");
			saveVote('custom', 'tags', tag_name, tag_name);
		}
	});
}


$(document).ready(
	function(){

	if ($("#save").size()) {

		initAjaxInfo();

		var ar=/type=(.*)$/i.exec(location.href);
		var win_type="";
		if (ar)
			win_type=ar[1];

		$("body").append("<div id=\"searchcontrol\">Loading...</div>");

		var id_temp = "";

		if ($("#id_rel").val() != null)
			var id_temp = $("#id_rel").val();

		prepareEventsInSave(id_temp, false, win_type);
		setTutEvents();
	}

});