// Use console (i.e. firebug if available or install fall back on fail
try {
	console.log('init console... done');
} catch (e) {
	console = {
		log : function() {
		},
		error : function() {
		}
	};
}

/* define selltec namespaces */
var SL = {
	utils : {},
	effects : {},
	ui : {},
	browser : {
		IE6 : Prototype.Browser.IE && parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE") + 5)) == 6
	},
	storage : $H()
};

(function() {

	SL.getIE = function() {
		if (navigator.appName != 'Microsoft Internet Explorer') {
			return -1;
		}

		var exp = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");

		if (exp.exec(navigator.userAgent) != null) {
			return parseFloat(RegExp.$1);
		}

		return -1;
	};

	Hash.prototype.setDefault = function(key, value) {
		if (this.get(key) == undefined) {
			this.set(key, value);
		}
	};

	Hash.prototype.ensureHash = function(key) {
		if (this.get(key) == undefined) {
			this.set(key, $H());
		}
		return this.get(key);
	};

	Element.addMethods({

		/*
		 * Temporal fixes for prototype Ignore bad numeric values (NaN and
		 * Infinity)
		 */

		setStyle : function(element, styles) {
			try {
				element = $(element);
				var elementStyle = element.style, match;
				if (Object.isString(styles)) {
					element.style.cssText += ';' + styles;
					return styles.include('opacity') ? element.setOpacity(styles.match(/opacity:\s*(\d?\.?\d*)/)[1]) : element;
				}
				for ( var property in styles) {
					var value = styles[property];
					try {
						if (value && (value.toLowerCase().startsWith('nan') || value.toLowerCase().startsWith('infinity'))) {
							// Fix IE bugs
							continue;
						}

						if (property == 'opacity') {
							element.setOpacity(value);
						} else {
							elementStyle[(property == 'float' || property == 'cssFloat') ? (Object.isUndefined(elementStyle.styleFloat) ? 'cssFloat' : 'styleFloat') : property] = value;
						}
					} catch (e) {
						console.error("Error setting style:", property, "->", value, " ", e);
					}
				}
			} catch (e) {
				console.error(e);
			}

			return element;
		},

		/*
		 * Use Element for storage to reduce memory leaks
		 */
		getStorage : function(element) {
			element = $(element);
			var h = element._slStorage;
			if (!h) {
				h = $H();
				element._slStorage = h;
			}
			return h;
		},

		hasFixedPosition : function(element) {
			return Element.getFixedParent(element) ? true : false;
		},

		getFixedParent : function(element) {
			while (element) {
				if (!element.tagName || element.tagName.toLowerCase() == 'body') {
					break;
				}

				if (element.getStyle('position') == 'fixed') {
					return element;
				}
				element = element.up();
			}
			return null;
		},

		cumulativeScrollOffsetFixed : function(element) {
			var valueT = 0, valueL = 0;
			do {
				valueT += element.scrollTop || 0;
				valueL += element.scrollLeft || 0;

				if (element.getStyle('position') == 'fixed') {
					break;
				}

				element = element.parentNode;
			} while (element);
			return Element._returnOffset(valueL, valueT);
		},

		findZIndex : function(element) {
			do {
				if (!element.tagName || element.tagName.toLowerCase() == 'body') {
					break;
				}

				var zIndex = element.getStyle('z-index');

				if (zIndex) {
					return parseInt(zIndex);
				}

				element = element.parentNode;
			} while (element);
		}
	});

})();

if (typeof PopUp != 'function') {
	function PopUp(URL, frame, size) {
		if (size == null) {
			var width = SLPopupSize ? SLPopupSize[0] : 650;
			var height = SLPopupSize ? SLPopupSize[1] : 600;

			var winl = (screen.width - width) / 2;
			var wint = (screen.height - height) / 2;

			var topArg = ',top=' + wint;
			var leftArg = ',left=' + winl;
			var widthArg = ',width=' + width;
			var heightArg = ',height=' + height;

			win = window.open(URL, frame, 'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1' + topArg + leftArg + widthArg + heightArg).focus();

		} else {
			win = window.open(URL, frame, 'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,' + size).focus();
		}
	}
}

