/*
JSTarget function by Roger Johansson, www.456bereastreet.com
*/
var JSTarget = {
	init: function(att,val,warning) {
		if (document.getElementById && document.createElement && document.appendChild) {
			var strAtt = ((typeof att == 'undefined') || (att == null)) ? 'class' : att;
			var strVal = ((typeof val == 'undefined') || (val == null)) ? 'non-html' : val;
			var strWarning = ((typeof warning == 'undefined') || (warning == null)) ? ' (opens in a new window)' : warning;
			var oWarning;
			var arrLinks = document.getElementsByTagName('a');
			var oLink;
			// original regex below
			// var oRegExp = new RegExp("(^|\\s)" + strVal + "(\\s|$)");
			var oRegExp = new RegExp("(^|\\s)(("+ strVal + ")|(" + strVal + "_(\\d){1,}x(\\d){1,}))(\\s|$)");
			
			for (var i = 0; i < arrLinks.length; i++) {
				oLink = arrLinks[i];
				if ((strAtt == 'class') && (oRegExp.test(oLink.className)) || (oRegExp.test(oLink.getAttribute(strAtt)))) {
					oWarning = document.createElement("em");
					oWarning.appendChild(document.createTextNode(strWarning));
					oLink.appendChild(oWarning);
					oLink.onclick = JSTarget.openWin;
				}
			}
			oWarning = null;
		}
	},
	openWin: function(e) {
		var event = (!e) ? window.event : e;
		if (event.shiftKey || event.altKey || event.ctrlKey || event.metaKey) return true;
		else {
		    
			/* 
			check for different class names to assign popup sizes.
			custom sized popup are also recognised if the class name 
			is constructed like: popup_<width>x<height>
			
			e.g. popup_400x600
			
			- added by krollster@gmail.com; 15/01/2007 
			*/
			var strWSize = '';
			//bloody IE6 always needs an extra treatment
			var className = (this.getAttribute('class')) ? this.getAttribute('class'): this.getAttribute('classname');
			var strDim = className.substr(className.lastIndexOf('_')+1);
			var wWidth = strDim.substr(0,strDim.indexOf('x'));
			var wHeight = strDim.substr(strDim.indexOf('x')+1);
			var sID = strDim.substr(strDim.indexOf('id')+1);
			
			//alert(wWidth + ' ' + wHeight);
			if (wWidth !== '' && wHeight !== ''){			
				strWSize = 'width=' + wWidth + ',height=' + wHeight;
			}
			else {
				switch (className){
					case 'popup_sml':
						strWSize = 'width=200,height=200';
					 break;
					case 'varaustilannepainike':
						strWSize = 'width=300,height=550';
					 break;
					case 'popup_lrg':
						strWSize = 'width=400,height=400';
					 break;				 
					default:
						strWSize = 'width=200,height=200';
					 break;
				}
			}
			
			// create a timestamp to attach to the window name so we can create a relativly (to the millisecond) 
			// unique window name to avoid popups from overwriting each other
			var dtNow = new Date();
			
			var oWin = window.open(this.getAttribute('href'), 'popup' + dtNow.getTime(), strWSize + ',resizable=yes, scrollbars=yes');
			
			if (oWin) {
				if (oWin.focus) oWin.focus();
				return false;
			}
			oWin = null;
			return true;
		}
	},
	/*
	addEvent function from http://www.quirksmode.org/blog/archives/2005/10/_and_the_winner_1.html
	*/
	addEvent: function(obj, type, fn) {
		if (obj.addEventListener)
			obj.addEventListener(type, fn, false);
		else if (obj.attachEvent) {
			obj["e"+type+fn] = fn;
			obj[type+fn] = function() {obj["e"+type+fn]( window.event );}
			obj.attachEvent("on"+type, obj[type+fn]);
		}
	}
};
JSTarget.addEvent(window, 'load', function(){JSTarget.init("class","popup_sml"," (small popup)");});
JSTarget.addEvent(window, 'load', function(){JSTarget.init("class","varaustilannepainike","");});
JSTarget.addEvent(window, 'load', function(){JSTarget.init("class","popup_lrg"," (large popup)");});
JSTarget.addEvent(window, 'load', function(){JSTarget.init("class","popup","");});
JSTarget.addEvent(window, 'load', function(){JSTarget.init("class","popup2","");});

