﻿/**
 * Registers an popup onclick event on links having class .popupLink
 * You can optionally assign a class named WidthxHeight to set width and height of the popup.
 * Additionaly you can assign a class named printOnOpen, which forces the popup to make the print
 * window appear after the popup is loaded.
 * For instance you want a link, opening a popup having 300px width and 400px height, offering
 * the browser's print dialog on open you've got to create it like this:
 *
 * <a class="popupLink 300x400 printOnOpen">My Link</a>
 *
 * while the order of the classes is irrelevant. Any additional classes can be
 * given too, it won't affect the mentioned mechanisms
 */
jQuery.fn.popupLink = function(settings){
	return this.each(function(){
		jQuery(this).unbind('click').bind('click', function(event){
			event.preventDefault();
			event.stopPropagation();
			var viewportSize = {
				height : jQuery(window).height(),
				width  : jQuery(window).width()
			};
			var url = jQuery(this).attr('href');
			var popupSettings = 'dependent=yes,status=no,resizable=yes,location=no,menubar=no,toolbar=no,scrollbars=yes';
			event.preventDefault();
			event.stopPropagation();
			var classes = jQuery(this).attr('class').split(' ');
			var sizeMatch = null;
			var printOnOpen= null;
			var dim = [0,0];
			for(var i in classes)
			{
				if(typeof classes[i] == 'function')
				{
					continue;
				}
				if(sizeMatch === null)
				{
					sizeMatch = classes[i].match(/[0-9]+x[0-9]+/);
					if(sizeMatch !== null)
					{
						sizeMatch = new String(sizeMatch);
					}
				}
			}
			if(sizeMatch !== null)
			{
				dim = sizeMatch.split('x');
				var popupDims = {
					width: dim[0],
					height: dim[1],
					top: viewportSize.height / 2 - dim[1] / 2,
					left: viewportSize.width / 2 - dim[0] / 2
				};
				popupSettings += ',width='+popupDims.width+',height='+popupDims.height+',top='+popupDims.top+',left='+popupDims.left;
			}
			var popup = window.open(url,'popup', popupSettings);
			popup.focus();
		});
	});
};
jQuery.fn.printLink = function(){
	return this.each(function(){
		jQuery(this).unbind('click').bind('click',function(event){
			event.preventDefault();
			event.stopPropagation();
			window.print();
		});
	});
};
jQuery.fn.popupCloseLink = function(){
	return this.each(function(){
		jQuery(this).unbind('click').bind('click', function(event){
			event.preventDefault();
			event.stopPropagation();
			window.close();
		});
	});
};
jQuery.fn.popupCloseLink = function(){
	return this.each(function(){
		jQuery(this).unbind('click').bind('click', function(event){
			event.preventDefault();
			event.stopPropagation();
			window.close();
		});
	});
};
jQuery(function(){
	/* function for teasers on the right side. At id teaserFixedMarker the teaser stays at the top of the screen. */
	if (jQuery("#teaserFixedMarker").get(0)){
		jQuery(window).scroll(function(){
			var teaserY=jQuery("#teaserFixedMarker").position().top;
			var teaserX=jQuery("#teaserFixedMarker").position().left;
			var scrollY=jQuery(window).scrollTop();
			var scrollX=jQuery(window).scrollLeft();
			window.status=teaserY+", "+scrollY+", "+teaserX+", "+scrollX;
			jQuery("#teaserFixed").toggleClass("teaserFixed",scrollY>teaserY);
			jQuery("#teaserFixed").css('left',teaserX-scrollX);
		});
	}
	jQuery('.popupLink').popupLink();
	jQuery('.popupCloseLink').popupCloseLink();
	jQuery('.printLink').printLink();
	jQuery('#loadingLayerBg').css("height",jQuery('body').height()+36+"px");
});
