$.fn.infoWin = function() {
	var obj = $("#winInfo_DIV");
	$(this).bind("mouseover, mousemove", function(e){
		try {
			if (obj.size() == 0) {
				obj = $("<div>");
				obj.attr("id", "winInfo_DIV");
				obj.css({
					"position":"absolute"
					, "padding":"0px"
					, "color":"#ffffff"
					, "font-family":"verdana"
					, "font-size":"10px"
					, "overflow":"hidden"
					, "width":"auto"
					, "height":"auto"
					, "max-width":"300px"
					, "border-top":"0px outset #81b2e9"
					, "border-right":"0px outset #000000"
					, "border-left":"0px outset #000000"
					, "border-bottom":"0px outset #000000"
					, "background-image":"url(/images/template/bgAlpha_000000_80.png)"
				});
				obj.html("<div style='background-color:#047b99; height:14px; line-height:14px; padding:0px 5px 0px 5px; color:#ffffff; font-weight:bold;'>Informazioni</div><div style='padding:10px;'>"+$(this).attr("rel").substring(8, $(this).attr("rel").length-1)+"</div>");
				$(this).bind("mouseout, mousedown", function(){
					obj.remove();
				});
				$(document.body).append(obj);
			}
			posFix = 10;
			posX = e.pageX+posFix;
			if ((posX+obj.width()+posFix) > $(window).width()) {
				posX = e.pageX-obj.width()-posFix;
			}
			posY = e.pageY+posFix;
			if ((posY+obj.height()+posFix) > $(window).height()+$(document).scrollTop()) {
				posY = e.pageY-obj.height();
			}
			obj.offset({left:posX, top:posY});
		} catch(err) {
			obj.remove();
		}
	});
}






$.fn.callError = function(errNumber, scrollPosition) {
	var objId = "error_DIV";
	$("#"+objId).remove();
	$.ajax({
		cache: false
		, async: true
		, dataType: "text"
		, contentType: "application/x-www-form-urlencoded"
		, type: "get"
		, url: "/err.asp?err="+errNumber
		, data: ""
		, success: function(returnValue){
			$("#middlePage_Left_DIV").prepend(returnValue);
			reloadEventLoad();
		}
		, error: function(errorObject, errorNumber, errorString){
			reloadEventLoad();
		}
	});
	if ((scrollPosition != null) && (parseInt(scrollPosition))) {
		$(document).scrollTop(scrollPosition);
	}
	return false;
}


$.fn.vibrate = function(speed, duration, maxmove) {
	var obj = $(this);
	var starts = 0;
	var objCssPosition = (obj.css("position") == null) ? "static" : obj.css("position");
	var objCssLeft = (obj.css("left") == null) ? "auto" : obj.css("left");
	var objCssTop = (obj.css("top") == null) ? "auto" : obj.css("top");
	var effect = setInterval(function(){
		obj.css({
			"position" : "relative"
			, "left" : Math.floor(Math.random()*maxmove).toString()+"px"
			, "top" : Math.floor(Math.random()*maxmove).toString()+"px"
		});
		if (starts == 0) {
			return setTimeout(function(){
				clearInterval(effect);
				obj.css({
					"position" : objCssPosition
					, "left" : objCssLeft
					, "top" : objCssTop
				});
			}, duration);
		}
		starts++;
	}, speed);
	return obj;
};


$.fn.formElementsEscapeString = function() {
	var formElements = $(this).serializeArray();
	var returnString = "";
	$.each(formElements, function(i, element){
		if (i > 0) {
			returnString += "&"
		}
		returnString += element.name + "=" + escape(element.value);
	});
	return returnString;
}





$.fn.graphicSubmit_click = function() {
	var objForm = $(this).parents("form");
	if (objForm.size() > 0) {
		if (objForm.attr("id") == null) {
			objForm.attr("id", "form_temp_id");
		}
		sendFormModule(objForm.attr("id"), 1);
	}
	return false;
}






$.fn.getValueFromString = function(checkString, separator, getValue) {
	var returnValue = "";
	checkString = (checkString.indexOf("?") > -1) ? checkString.substring(checkString.indexOf("?")+1, checkString.length) : checkString;
	checkString = checkString.split(separator);
	for (var i=0; i<checkString.length; i++) {
		if (checkString[i].indexOf("=") > -1) {
			if (checkString[i].substring(0, checkString[i].indexOf("=")) == getValue) {
				returnValue = checkString[i].substring(checkString[i].indexOf("=")+1, checkString[i].length);
				break;
			}
		}
	}
	return returnValue;
}



$.fn.graphicCheckbox_checkAll = function(check) {
	if (check) {
		$(this).parent().css("background", "url(/images/template/checkbox_on.png) 0% 0% no-repeat");
		$(this).attr("checked", true);
	} else {
		$(this).parent().css("background", "url(/images/template/checkbox_off.png) 0% 0% no-repeat");
		$(this).attr("checked", false);
	}
}

$.fn.graphicCheckbox_click = function(rst) {
	if (rst) {
		if ($(this).parent().attr("rel") == "off") {
			$(this).parent().css("background", "url(/images/template/checkbox_off.png) 0% 0% no-repeat");
			$(this).attr("checked", false);
		} else {
			$(this).parent().css("background", "url(/images/template/checkbox_on.png) 0% 0% no-repeat");
			$(this).attr("checked", true);
		}
	} else {
		if ($(this).is(":checked")) {
			$(this).parent().css("background", "url(/images/template/checkbox_off.png) 0% 0% no-repeat");
			$(this).attr("checked", false);
		} else {
			$(this).parent().css("background", "url(/images/template/checkbox_on.png) 0% 0% no-repeat");
			$(this).attr("checked", true);
		}
	}
}

$.fn.graphicCheckbox = function(cRel) {
	$.each($("input[rel^='"+cRel+"']"), function(i){
		if ($(this).attr("rel")) {
			cRel = $(this).attr("rel");
			originalRel = cRel;
			if (cRel.substring(cRel.length-2, cRel.length-1) == "|") {
				cRel = cRel.substring(cRel.length-1, cRel.length);
			} else {
				cRel = null;
			}
		}
		var inputStatus = "off";
		var func;
		if ($(this).attr("onclick")) {
			func = $(this).attr("onclick");
		}
		var c = $("<input>");
		if (cRel != null) {
			var minChecked = originalRel.match(/\(+[0-9]+\)/);
			if (minChecked == null) {
				minChecked = "";
			}
			c.attr("rel", "checkbox"+minChecked+"|"+cRel);
		}
		c.attr("type", "checkbox");
		c.attr("id", $(this).attr("id"));
		c.attr("name", $(this).attr("name"));
		c.attr("value", $(this).val());
		c.attr("checked", false);
		var rVal = $(this).getValueFromString(location.href, "&", $(this).attr("name"));
		if (rVal != "") {
			rVal = " "+rVal+","
		}
		if (($(this).is(":checked")) || (rVal.indexOf(" "+$(this).val()+",") > -1)) {
			c.attr("checked", true);
			inputStatus = "on";
		}
		c.hide();

		var a = $("<a>");
		a.attr("rel", inputStatus);
		a.attr("href", "javascript:void(0);");
		a.css({
			"float" : "left"
			, "width" : "auto"
			, "height" : "16px"
			, "line-height" : "16px"
			, "background" : "url(/images/template/checkbox_"+inputStatus+".png) 0% 0% no-repeat"
			, "overflow" : "hidden"
			, "padding" : "0px 5px 0px 20px"
			, "margin" : "0px 0px 0px 0px"
			, "display" : "block"
			, "font-weight" : "bold"
		});
		a.bind("click", function(){
			a.children("input:first-child").graphicCheckbox_click(false);
			if (func != null) {
				eval(func);
			}
		});
		a.append(c);
		a.append($(this).attr("title"));
		$(this).replaceWith(a);
	});
};













$.fn.graphicRadio_click = function() {
	var inputs = $("input[type='radio'][name='"+$(this).attr("name")+"']");
	inputs.parent().attr("rel", "off");
	inputs.parent().css("background", "url(/images/template/radio_off.png) 0% 0% no-repeat");
	inputs.attr("checked", false);
	$(this).parent().attr("rel", "on");
	$(this).parent().css("background", "url(/images/template/radio_on.png) 0% 0% no-repeat");
	$(this).attr("checked", true);
}



$.fn.graphicRadio = function(cRel) {
	$.each($("input[rel^='"+cRel+"']"), function(i){
		if ($(this).attr("rel")) {
			cRel = $(this).attr("rel");
			if (cRel.substring(cRel.length-2, cRel.length-1) == "|") {
				cRel = cRel.substring(cRel.length-1, cRel.length);
			} else {
				cRel = null;
			}
		}
		var inputStatus = "off";
		var func;
		if ($(this).attr("onclick")) {
			func = $(this).attr("onclick");
		}
		var c = $("<input>");
		if (cRel != null) {
			c.attr("rel", "radio|"+cRel);
		}
		c.attr("type", "radio");
		c.attr("id", $(this).attr("id"));
		c.attr("name", $(this).attr("name"));
		c.attr("value", $(this).val());
		c.attr("checked", false);
		var rVal = $(this).getValueFromString(location.href, "&", $(this).attr("name"));
		if (rVal != "") {
			rVal = " "+rVal+","
		}
		if (($(this).is(":checked")) || (rVal.indexOf(" "+$(this).val()+",") > -1)) {
			c.attr("checked", true);
			inputStatus = "on";
		}
		c.hide();
		var a = $("<a>");
		a.attr("rel", inputStatus);
		a.attr("href", "javascript:void(0);");
		a.css({
			"float" : "left"
			, "width" : "auto"
			, "height" : "16px"
			, "line-height" : "16px"
			, "background" : "url(/images/template/radio_"+inputStatus+".png) 0% 0% no-repeat"
			, "overflow" : "hidden"
			, "padding" : "0px 5px 0px 20px"
			, "margin" : "0px 0px 0px 0px"
			, "display" : "block"
			, "font-weight" : "bold"
		});
		a.bind("click", function(){
			a.children("input:first-child").graphicRadio_click();
			if (func != null) {
				eval(func);
			}
		});
		a.append(c);
		a.append($(this).attr("title"));
		$(this).replaceWith(a);
	});
};







$.fn.objSlide = function() {
	if ($(this).is(":hidden")) {
		$(this).slideDown();
	} else {
		$(this).slideUp();
	}
}






function ajaxReturn(pageUrl, returnId, returnMsg, add) {
	$.ajax({
		cache: false
		, async: false
		, dataType: "text"
		, contentType: "application/x-www-form-urlencoded"
		, type: "get"
		, url: pageUrl
		, data: ""
		, success: function(returnValue){
			if (returnId != null) {
				if (add == 0) {
					$("#"+returnId).html();
					$("#"+returnId).html(returnValue);
				} else if (add == 1) {
					$("#"+returnId).append(returnValue);
				} else if (add == 1) {
					$("#"+returnId).prepend(returnValue);
				}
			}
			if (returnMsg != null) {
				$(this).callError(returnMsg);
			} else {
				reloadEventLoad();
			}
		}
		, error: function(errorObject, errorNumber, errorString){
			if (returnMsg != null) {
				$(this).callError(errorString);
			}
		}
	});
	return false;
}

