﻿
(function($) {
	$.xfn = {};

	$.xfn.ajaxLoadpage = {};
	$.xfn.ajaxLoadpage.last = {};

	$.xfn.ajaxLoadpage = function(options) {
		$("*[ajaxpage]").each(function(elm) {
			var sPagename = $(this).attr("ajaxpage");
			var sContent = "";
			if ($(this).attr("ajaxcontent") != undefined) {
				sContent = "#" + $(this).attr("ajaxcontent");
			} else {
				//if ($.xfn.ajaxLoadpage.last == null) {
				sContent = "#" + options.id_content;
				//} else {
				//sContent = $.xfn.ajaxLoadpage.last;
				//}
			}

			$(this).click(function() {
				$("#menu_selected2").val(sPagename)

				if ($(this).attr("ajaxindipendent") != "true") {
					if ($("#flagHomepage").val() == "true") {
						$("#flagHomepage").val("false");
						$("form").submit();
					}
				}
				/*
				PRIMA RICHIESTA: HTML
				*/

				$.ajax({
					type: "POST",
					url: "Dispatcher.aspx",
					data: "op=loadpage&page=" + sPagename,
					success: function(resp) {
						if ($("#flagHomepage") == false) {
							$(sContent).slideUp("slow").replaceWith(resp).slideDown("medium")
						} else {
							$(sContent).slideUp("slow").html(resp).slideDown("medium")
						}
						$.xfn.ajaxLoadpage.last = resp


						/*
						SECONDA RICHIESTA: JS
						*/
						$.ajax({
							type: "POST",
							url: "Dispatcher.aspx",
							data: "op=loadpage&page=" + sPagename + "&js=true",
							success: function(resp) {
								try {
									eval(resp);
								} catch (ex) {
								}
							}
						});
						/********************/
					}
				});
				/**********************/
			});

		});
	};

	$.xfn.ajaxPostback = function() {
		var sSearch = "*[ajaxpage='" + $("#menu_selected2").val() + "']";
		$(sSearch).trigger("click");
	};

})(jQuery);