/**************************************************************************************************
 * Background Scaling (requires jQuery)
 *
 * Place the background image at the beginning of the <body> scope:
 *
 * <body>
 *   <div id="containerBG" style="position: absolute; overflow: hidden;">
 *     <img src="..." alt="" id="imgBG">
 *   </div>
 *   ...
 * </body>
 */

var BGScaler = function() {
	var _private = {
		getViewportHeight: function() {
			if ((document["compatMode"] || navigator.userAgent.toLowerCase().match(/ie/)) && !navigator.userAgent.toLowerCase().match(/opera/)) {
				if (document["compatMode"] == "CSS1Compat") {
					// Mozilla, IE (standard mode)
					return document.documentElement.clientHeight;
				}
				else {
					// Mozilla, IE (quirks mode)
					return document.body.clientHeight;
				}
			}
			else {
				// Safari, Opera
				return self.innerHeight;
			}
		},
		getViewportWidth: function() {
			if (document["compatMode"] || navigator.userAgent.toLowerCase().match(/ie/)) {
				if (document["compatMode"] == "CSS1Compat") {
					// Firefox, IE, Opera (standard mode)
					return document.documentElement.clientWidth;
				}
				else {
					// Firefox, IE, Opera (quirks mode)
					return document.body.clientWidth;
				}
			}
			else {
				// Safari
				return self.innerWidth;
			}
		},
		max: function(n, m) {
			return (n > m) ? n : m;
		},
		scale: function() {
			var nAddToHeight = 0;
			
			if (location.pathname == "/home" || location.pathname == "/home-en" || location.pathname == "/page/node/1") {
				nAddToHeight = 129;
			}
			try {
				var oContent = document.getElementById("containerPage");
				var nWidth = _private.max(oContent.offsetWidth, _private.getViewportWidth());
				var nHeight = _private.max(oContent.offsetHeight + Math.ceil(nAddToHeight / 2), _private.getViewportHeight());
	
				$(document.body).css("width", nWidth + "px");
				$(document.body).css("height", nHeight + "px");
				$("#containerPage").css("height", nHeight + "px");
			} catch(e) {}
		}
	};
	var _public = {
		init: function() {
			_private.scale();
			_private.scale(); // do not remove
			$(window).resize(_private.scale);
		}
	};
	return _public;
}();

var BGLeft = function() {
	var _public = {
		init: function() {
			try {
				var aBGImage = $("#boxBGLeft img");

				if (aBGImage.size() > 0) {
					$("#containerPage").css({"background-image": "url(" + aBGImage.eq(0).attr("src") + ")"});
				}
			}
			catch (e) {}
		}
	}
	return _public;
}();

var Forms = function() {
	var _private = {
		addHandlers: function(oLabel) {
			var szID = oLabel.attr("for");
			var oInput = $(".boxLabeled > input#" + szID + ", .boxLabeled > select#" + szID + ", .boxLabeled > textarea#" + szID);

			if (oInput.size() == 1) {
				oInput = $(oInput.eq(0));

				// add handlers
				oInput.focus(function() {
					oLabel.hide();
				});
				oInput.blur(function() {
					if (oInput.val() == "") {
						oLabel.show();
					}
				});
				// initial check
				if (oInput.val() == "") {
					oLabel.show();
				}
				else {
					oLabel.hide();
				}
			}
		}
	};
	var _public = {
		init: function() {
			var aInput = $(".boxLabeled > label[for]");

			for (var i=0; i<aInput.size(); i++) {
				_private.addHandlers($(aInput.eq(i)));
			}
		}
	};
	return _public;
}();

$(function() {
	BGScaler.init();
	BGLeft.init();
	Forms.init();
});



