if (!window.HN) HN = window.HN = {};

// taken from old jquery lib version
HN.userAgent = navigator.userAgent.toLowerCase();
HN.browser = {
	version: (HN.userAgent.match( /.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/ ) || [])[1],
	safari: /webkit/.test( HN.userAgent ),
	opera: /opera/.test( HN.userAgent ),
	msie: /msie/.test( HN.userAgent ) && !/opera/.test( HN.userAgent ),
	mozilla: /mozilla/.test( HN.userAgent ) && !/(compatible|webkit)/.test( HN.userAgent )
};

HN.imageToReplaceCount = 0;
HN.replacedImageCount = 0;
HN.replaceImageComplete = function(){};

HN.replaceImage = function (img) {
	var span = document.createElement("span");
	if (img.id) span.id = img.id;
	if (img.className) span.className = img.className;
	span.className += " ie6img";
	if (img.title) span.title = img.title;
	span.style.display = "inline-block";
	span.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=scale, src='" + img.src + "')";
	span.style.width = img.width+"px";
	span.style.height = img.height+"px";
	if (img.parentNode.nodeName.toLowerCase() == "a") {
		span.style.cursor = "pointer";
	}
	for (attr in img.style) {
		if (attr != "cssText" && attr != "accelerator") {
			if ((typeof img.style[attr] == "boolean" && img.style[attr] != false) ||
					(typeof img.style[attr] == "number" && img.style[attr] != 0) ||
					(typeof img.style[attr] == "string" && img.style[attr] != "")) {
				span.style[attr] = img.style[attr];
			}
		}
	}
	
	img.parentNode.replaceChild(span, img);
	HN.replacedImageCount++;
	if (HN.replacedImageCount == HN.imageToReplaceCount)
		HN.replaceImageComplete();
}

HN.png2alpha = function () {
	if ((HN.browser.msie && HN.browser.version >= 5.5 && HN.browser.version < 7.0)) {
		
		var class2ignore = String(arguments[0] ? arguments[0] : "");
		if (class2ignore != "")
			var class2ignore = new RegExp(arguments[0]);
	
		var images = document.getElementsByTagName("img");
		var images_to_change = [];
		for (var i=0; i<images.length; i++) {
			var imgName = images[i].src.toLowerCase();
			if (imgName.substring(imgName.length-3, imgName.length) == "png" && (class2ignore == "" || !class2ignore.test(images[i].className))) {
				images_to_change.push(images[i]);
			}
		}
		HN.imageToReplaceCount = images_to_change.length;
		for (i=0; i<HN.imageToReplaceCount; i++) {
			if (images_to_change[i].complete)
				HN.replaceImage(images_to_change[i]);
			else
				images_to_change[i].onload = function () { HN.replaceImage(this); };
		}
	}
};

HN.File = function (_name, _url, _dataType) {
	this.name = _name;
	this.url = _url;
	this.dataType = _dataType;
	this.data = null;
	this.loaded = false;
};

HN.FileManager = function () {};
HN.FileManager.prototype = {
	files: [],
	add: function (name, url, dataType) {
		this.files[name] = new HN.File(name, url, dataType);
	},
	getUrl: function (name) { return this.files[name] ? this.files[name].url : null },
	getDataType: function (name) { return this.files[name] ? this.files[name].dataType : null },
	getData: function (name, callback_function) {
		if (this.files[name]) {
			if (this.files[name].loaded) {
				callback_function(this.files[name].data);
			}
			else {
				var _this = this;
				$.ajax({
					async: true,
					data: "",
					dataType: this.files[name].dataType,
					error: function (XMLHttpRequest, textStatus, errorThrown) {
						callback_function(null);
					},
					success: function (data, textStatus) {
						_this.files[name].loaded = true;
						_this.files[name].data = data;
						callback_function(_this.files[name].data);
					},
					type: "GET",
					url: this.files[name].url
				});
			//callback_function(this.files[name].url);
			}
		}
		else {
			callback_function(null);
		}
	}
};

// Singleton per URL
HN.URLinfos_instances = [];
HN.URLinfos = function () {
	var url;
	if (arguments.length == 0)
		url = window.location.href;
	else
		url = arguments[0];
	
	if (!HN.URLinfos_instances[url]) {
		HN.URLinfos_instances[url] = new function () {
			this.absURL = url;
			this.protocol = this.absURL.substring(0, this.absURL.indexOf("://"));
			var noprotocolURL = this.absURL.substring(this.protocol.length + "://".length, this.absURL.length);
			this.siteURL = noprotocolURL.substring(0, noprotocolURL.indexOf("/"));
			this.baseURL = this.protocol + "://" + this.siteURL + "/";
			this.relURL = this.absURL.substring(this.baseURL.length, this.absURL.length);
			
			this.tree = [];
			var treeURLparts = this.relURL.split("/");
			var relURL = "";
			for (var i = 0; i < treeURLparts.length; i++) {
				relURL += treeURLparts[i] + (i != treeURLparts.length-1 ? "/" : "");
				this.tree[i] = {
					text: treeURLparts[i],
					relURL: relURL,
					absURL: this.baseURL + relURL
				};
			}
			
			var lastURLpart = this.tree[this.tree.length-1].text.split("#");
			
			this.fileName = lastURLpart[0];
			this.fileNameNoExt = this.fileName.substr(0, this.fileName.lastIndexOf("."));
			this.fileNameExt = this.fileName.substr(this.fileName.lastIndexOf(".")+1, this.fileName.length);
			this.isIndex = this.fileName.toLowerCase() == "" || /^index\.\w+/.test(this.fileName.toLowerCase());
			
			this.anchor = lastURLpart.length > 1 ? lastURLpart[1] : "";
		}
	}
	return HN.URLinfos_instances[url];
};

HN.CookieManager = function(){};
HN.CookieManager.prototype = {
	read: function(name) {
		var arg = name + "=";
		var i = 0;
		while (i < document.cookie.length) {
			var offset = i + arg.length;
			if (document.cookie.substring(i, offset) == arg) {
				var endstr = document.cookie.indexOf (";", offset);
				if (endstr == -1) endstr = document.cookie.length;
				return unescape(document.cookie.substring(offset, endstr));
			}
			i = document.cookie.indexOf(" ",i) + 1;
			if (i == 0) break;
		}
		return null;
	},
	write: function(name, value) {
		var argv = this.write.arguments;
		var argc = this.write.arguments.length;
		var expires = (argc > 2) ? argv[2] : null;
		var path = (argc > 3) ? argv[3] : null;
		var domain = (argc > 4) ? argv[4] : null;
		var secure = (argc > 5) ? argv[5] : false;
		document.cookie = name + "=" + escape(value) +
			((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
			((path == null) ? "" : ("; path=" + path)) +
			((domain == null) ? "" : ("; domain=" + domain)) +
			((secure == true) ? "; secure" : "");	
	},
	del: function(name) {
		var date = new Date(0);
		this.write(name, null, date);
	}
};
