﻿(function() {
	Event.delegate = function(rules) {
		return function(e) {
				var element = $(e.element());
				while(element && !Object.isUndefined(element) && //element might be null if the child was appendChild'ed (parentNode never gets set) 
							element.nodeName != "#document-fragment" && //ie weirdness
							element != this) {
					for (var selector in rules) {
						if (element.match(selector)) rules[selector].apply(element, $A(arguments));
					}
					element = element.up();
				 }
			}
	}

	Event.addBehavior({
		'a[rel=to-pdf]:click': function() {
			var html = document.documentElement.innerHTML;
			html = html.replace(/<head>/i, '<head><base href="' + location.protocol + '//' + location.host + '"/>');

			var form = document.createElement("form");
			form.setAttribute("method", "post");
			form.setAttribute("action", "/public/Ajax/PdfGenerate.aspx");
			var input2 = document.createElement("input");
			input2.setAttribute("type", "hidden");
			input2.setAttribute("name", "filename");
			input2.setAttribute("value", document.title);
			var input = document.createElement("input");
			input.setAttribute("type", "hidden");
			input.setAttribute("name", "html");
			input.setAttribute("value", html);
			form.appendChild(input);
			form.appendChild(input2);
			document.body.appendChild(form);
			form.submit();
		},
		'a[rel=print]:click, input[rel=print]:click': function() {
			window.print();
		},
		'a[rel=font-size-toggle]:click': function() {
			if($(document.body).hasClassName("large")) {
				this.innerHTML = "Stor skrift";
				$(document.body).removeClassName("large");
			}
			else {
				this.innerHTML = "Normal skrift";
				$(document.body).addClassName("large");
			}
		},
		'a[rel~=info-icon]': function() {
			new Tip(this, this.getAttribute('title'), {
				title: 'Hjælp til ' +this.down('img').getAttribute('alt'),
				border: 2,
				radius: 1,
				borderColor: '#1B4972',
				closeButton: false,
				hideAfter: false,
				hideOn: 'mouseleave',
				showOn: 'mousemove',
				stem: { position: 'bottomRight', width: 0, height: 0 },
				hook: { target: 'topRight', tip: 'bottomLeft', mouse: true },
				offset: { x: 14, y: -14 }
			});
			this.removeAttribute('title');
		},
		'#content-main-container:click' :	Event.delegate({
			'.panel-collapse-button' : function() {
				var panel = this.up(".panel-container").down(".panel");
				if(panel.visible()) {
					this.writeAttribute("src","/images/elements/panel.expand.gif");
					panel.blindUp({duration: 0.3});
				}
				else {
					this.writeAttribute("src","/images/elements/panel.collapse.gif");
					panel.blindDown({duration: 0.3});
				}
			}
		}),
		"#content-main-container form": function() {
			var elms = $$(".error-field").concat(this.getElements()).select(function(elm){
				return elm.match(":visible") && (elm.match("[type=text]") || elm.match("textarea") || elm.match("select"));
			});
			if (elms.first()) elms.first().focus();
		},
		"#content-main-container form:keyup, #content-main-container form:keypress": function(e) {
			if (e.keyCode == Event.KEY_RETURN) {
				Event.stop(e);
			}
		},
		"#content-main-container form:keydown": function(e) {
			if (e.keyCode == Event.KEY_RETURN) {
				var elm = e.element();
				if (elm.match("input, select") && !this._defaultButtonDisabled) {
					var button = this.select(".default-button, [type=submit]").first();
					if (button) {
						Event.stop(e);
						button.click();
					}
				}
			}
		}
	});
})();