var url = window.location.href;
var sf = (url.indexOf("server-asp")>-1) ? "www.miabbonodotcom/" : "";
var page_name = url.substring(url.lastIndexOf('/') + 1);

function ajax_form(form_id, action, update_id, redirect) {
	//alert(form_id + "|" + action + "|" + redirect)

	$(update_id).update("<div class=\"login_mex\">Aggiornamento in corso...</div>");
		
	$(form_id).action = action;
	
	$(form_id).getMyBody=function(){
		var objIn=this.serialize(true);
		var valOut="";
		for (var s in objIn){
			valOut+=(valOut==""?"":"&")+s+"="+escape(objIn[s]);
		}     
		return(valOut);
	};

	$(form_id).request({
		 //onFailure: function() { .... },
		encoding:'iso-8859-1',
		postBody:$(form_id).getMyBody(),
		onSuccess: function(t) {
			var resp = t.responseText;
            $(update_id).update(resp);
			
			var js1 = resp.indexOf("js[");
			//alert(js1);
			if (js1 > -1 ) {
				if (js1 > -1) {
					var js2 = resp.indexOf("]js");
					var js_str = resp.substr(js1 + 3, js2 - js1 - 3);
				}
				//alert(js_str);
				eval(js_str);
			}
			
			var num1 = resp.indexOf("goto[");
			if (redirect || num1 > -1 ) {
				//alert(redirect + "|" + t.responseText.indexOf("goto["))
				var redirect_str = "";
				if (redirect) redirect_str = redirect;
				if (num1 > -1) {
					var num2 = resp.indexOf("]goto");
					redirect_str = resp.substr(num1 + 5, num2 - num1 - 5);
				}
				//alert(redirect_str)
				redir(redirect_str, 500);
			}
			
        }
//		,
//		onFailure: function() {
//			alert("failure");
//		},
//		onException: function() {
//			alert("exception");
//		}

    });
	//return false;
}

function redir(address, time) {
	setTimeout("window.location = '" + address + "';", time);
}

function ajax_logout() {
	new Ajax.Updater("update_login", "/" + sf + "ajax/login.html?action=logout", {
		onComplete: function(t) {
			redir("home.html", 500);
		}
	});
}

function page_reload() {
	window.location.reload();
}

function clear_form() {
	if ($F("email") == "e-mail" && $F("password") == "password") {
		$("email").value = "";
		$("password").value = "";
	}
}

var Switcher = Class.create({
	initialize: function(container) {
		this.container = container;
		this.n = 0;
		this.items = [];
		
		$(this.container).select(".arrow").each(function(element) {
			element.onclick = function() {
				var which = "next";
				if (element.hasClassName("left")) which = "prev";
				this.do_switch(which);
			}.bind(this)
		}.bind(this));

		$(this.container).select(".item").each(function(element) {
			this.items.push(element.id);
		}.bind(this));
	},

	do_switch: function(which) {
		if (which == "next") this.n++;
		if (which == "prev") this.n--;
		//alert(this.n);
		if (this.n >= this.items.size()) this.n = 0;
		if (this.n < 0) this.n = this.items.size() - 1;
		$(this.container).select('.item').each(function(element) {
			element.hide();
			//element.fade({ duration: 0.5 });
		});					 
		//$(this.items[this.n]).show();
		$(this.items[this.n]).appear({ duration: 0.5, queue: 'end' });
	}

});

document.observe("dom:loaded", function() {
	if ($("box_digitale_1")) var b1 = new Switcher("box_digitale_1");
	if ($("box_digitale_2")) var b2 = new Switcher("box_digitale_2");
	if ($("box_digitale_0")) var b0 = new Switcher("box_digitale_0");
});
