﻿/*
*	$("..").fixWidth(@tipo)
*		PARAMETRI:
*				@tipo (width/height) : tipo di misurazione			#default: width
*		RITORNO:
*				Nothing.
*		DESCRIZIONE:
*				Imposta la larghezza o altezza prendendo dai figli le misure e sommandole
*
*
*	$("..").realMisure(@tipo)
*		PARAMETRI:
*				@tipo (width/height) : tipo di misurazione			#default: width
*		RITORNO:
*				La misura in formato intero margin + padding + (width/height)
*
*
*	$("..").cssVal(@css_attributo)
*		PARAMETRI:
*				@css_attributo (String) : attributo del css
*		RITORNO:
*				Il valore numerico sistemato in caso di NaN
*/

(function($) {
	$.fn.realMisure = function(typeMis) {
		var defaults = {
			typeMis: "width"
		};
		if (typeMis == null) {
			typeMis = defaults.typeMis;
		};
		switch (typeMis) {
			case "width":
				return this.outerWidth(true);
				break;
			case "height":
				return this.outerHeight(true);
				break;
		};
	};

	$.fn.cssVal = function(cssType) {
		return this.css(cssType) != "0px" && isNaN(this.css(cssType)) != true ? 0 : parseInt(this.css(cssType));
	};

	$.fn.fixWidth = function(checkBeforeAfter) {
		var defaults = {
			checkBeforeAfter: false
		};
		if (checkBeforeAfter == null) {
			checkBeforeAfter = defaults.checkBeforeAfter;
		};

		var typeMis = "width";
		var width = 0;
		//alert(this.width());
		//if (this.width() > 3) return;
		//if (this.attr("fixedWidth") == "true") return;
		var iWidthBefore = this.width();

		this.children().each(function() {
			var child = $(this);
			width += child.realMisure(typeMis);
		});
		if ($.isIe(8)) width += 1;


		if (checkBeforeAfter) {
			if (width < iWidthBefore) return;
		}
		this.width(width + "px");

	}

	$.fn.backPos = function(valBackPos, typeBackPos) {
		var defaults = {
			typeBackPos: "y"
		};
		if (arguments.length < 2) {
			typeBackPos = arguments[0];

			if (typeBackPos == null) {
				typeBackPos = defaults.typeBackPos;
			};

			if ($.browser.mozilla) {
				var pos = this.css("background-position");
				if (typeBackPos == "y") {
					pos = pos.split(" ")[1];
				} else {
					pos = pos.split(" ")[0];
				}
				return parseInt(pos)
			} else {
				return parseInt(this.css("background-position-" + typeBackPos));
			}
		}


		if ($.browser.mozilla) {
			var pos = this.css("background-position");
			//alert(pos);
			if (typeBackPos == "y") {
				pos = pos.split(" ")[0] + " " + valBackPos + "px";
			} else {
				pos = valBackPos + "px" + " " + pos.split(" ")[1];
			}
			//alert(pos);
			this.css("background-position", pos);
		} else {


			this.css("background-position-" + typeBackPos, valBackPos + "px");

		}
	}

	$.fn.imageFit = function(stretch) {
		if (stretch == null) stretch = true;
		$(this).each(function() {
			var iWidth = $(this).cssVal("max-width");
			var iHeight = $(this).cssVal("max-height");
			//if ($.isIe(8) && !$.isExactIe(6)) iHeight --;
			$(this).attr({ "width": iWidth + "", "height": iHeight + "" });
		});
	}

	$.fn.mapReq = function() {
		var objReturn = {};
		$(this).each(function() {
			objReturn[$(this).attr("id")] = $(this).val();
		});
		var sReturn = "";
		for (i in objReturn) {
			sReturn += "&" + i + "=" + objReturn[i];
		};
		return sReturn;
	}

	$.fn.tagName = function() {
	    return this.get(0).tagName.toLowerCase();
	}
})(jQuery);

jQuery.isIe = function(iVers) {
	var browsV = parseFloat(jQuery.browser.version);
	if ($.browser.msie && browsV <= iVers) return true;
	else return false;
}

jQuery.isExactIe = function(iVers) {
	var browsV = parseFloat(jQuery.browser.version);
	if ($.browser.msie && browsV == iVers) return true;
	else return false;
}

jQuery.showProps = function(objToShow) {
	var sProps = "";
	for (i in objToShow) {
		sProps += "." + i + " = " + objToShow[i] + "\n";
	}
	alert(sProps);

}

//$({ gritter_popups: [] }).bind("gritter_addpopup", function(event, popup) {
//	alert("primo evento");
//}).trigger("gritter_addpopup");
//jQuery.event.special.gritter_addpopup = {
//	add: function(data, namespaces) {
//		alert("add");
//	},
//	setup: function(data, namespaces) {
//		var elem = this;
//		alert("setup");
//	},
//	teardown: function(namespaces) {
//		var elem = this;
//		alert("teardown");
//	},

//	handler: function(event) {
//		alert("faccio quel che voglio");
//	}
//};

$("*").bind("gritter_addpopup", function(e) {
	alert("..............");
});
//$.event.register("gritter_addpopup");
/*


function setBackPosY(obj, pxY) {
	if ($.browser.mozilla) {
		var pos = obj.css("background-position");
		pos = pos.split(" ")[0] + " " + pxY;
		obj.css("background-position", pos);

	} else {
		obj.css("background-position-y", pxY);
	}
}

function getBackPosY(obj) {
	var pxYreturn = "";
	if ($.browser.mozilla) {
		return obj.css("background-position").split(" ")[1];
	} else {
		return obj.css("background-position-y");
	}
}
*/