$(function(){
	// Popup window init.
	$("a.disabled").click(function(){return false});
	$("a.popup").click(function(){
		var options = "";
		var cImg = this.getElementsByTagName("img"),
			width = screen.availWidth * 0.85,
			height = screen.availHeight * 0.85;
		if (cImg.length) {
			var className = cImg[0].className.match(/(\d+%?)x(\d+%?)/);
			if (className) {
				width = className[1];
				height= className[2];
			}
		} else {
			options = ",scrollbars";
			
			var className = this.className.match(/(\d+%?)x(\d+%?)/);
			if (className) {
				width = className[1];
				height= className[2];
			}
			
			if (this.className.indexOf("reader") > -1) {
				width = parseInt(width,10) + 100;
				height = parseInt(height,10) + 17;
			}
		}
		if (String(width).indexOf("%") > -1) {
			width = screen.availWidth * (100 / parseInt(width,10));
		}
		if (String(height).indexOf("%") > -1) {
			height = screen.availHeight * (100 / parseInt(height,10));
		}
		
		var w = window.open("", "w", "width=" + width
			+ ",height=" + height
			+ ",left=" + ((screen.availWidth - width) / 2)
			+ ",top=" + ((screen.availHeight - height) / 2)
			+ ",resizable" + options);
		
		if (cImg.length) {
			with (w.document) {
				open();
				write("<html>");
				write("<head><title>" + this.firstChild.title + "<\/title><\/head>");
				write("<body style=\"margin:0\"><img src=\"");
				write(this.href);
				write("\" alt=\"" + this.href + "\"><\/body>");
				write("<\/html>");
				close();
			}
		} else {
			if (w) {
				w.location = this.href;
			}
		}
		if (w) {
			w.focus();
			return false;
		} else {
			return true;
		}
});

	// Email link init.
	$("a.email").each(function(){
		$(this).attr("href", "mailto:" + this.href.replace("+", "@").replace(/^.*\//, ""))
	});
	
	// Rajzolt novella Home Link init.
	$("a#home").click(function(){
		if (self.opener) {
			window.close();
			return false;
		}
	});
});

