﻿// JavaScript Document

if(document.location.href.indexOf('display=print') >= 0) {
	var head = document.getElementsByTagName('head')[0];
	
	function filter(element) {
		var media = element.getAttribute('media');
		if(media == 'screen')
			element.parentNode.removeChild(element);
		else if (media == 'print') {
			element.setAttribute('media', 'all');
		}
	}
	
	$A(head.getElementsByTagName('style')).each(filter);
	$A(head.getElementsByTagName('link')).each(filter);
}

function printableWindowURI(element) {
	element.setAttribute('href', document.location.href.indexOf('?') >= 0 
		? document.location.href + '&display=print'
		: document.location.href + '?display=print');
}

function openWindow(element, width, height) {
	var win = window.open(element.getAttribute('href'), 'ka',  'width=' + width + 'px, height=' + height + 'px');
	win.focus();
	return false;
}


window.onhashchange = function() {
	if(location.hash) {
		$$('.in-page-highlight').invoke('removeClassName', 'in-page-highlight');
		var elem = $(location.hash.substring(1));
		if(elem && elem.readAttribute('highlight')) {
			eval('var callable = (function(){return(' + elem.readAttribute('highlight') + ');})');
			callable.apply(elem).invoke('addClassName', 'in-page-highlight');
		}
	}
};

document.on('dom:loaded', window.onhashchange);
