$(document).ready(function(){   
	pl_init('a.popup_layer');
});
function pl_init(domChunk){
	$(domChunk).click(function(e) {
		var t = this.title || this.name || null;
		var a = this.href || this.alt;
		var g = this.rel || false;
		pl_show(t,a,g,e);
		this.blur();
		return false;
	});
}
function pl_get_div_html(middle) {
	if(middle){
		var res = '<div class="pop-up-wrapper" id="PL_window">' +      
			'<div class="pop-up-body">' + 
				'<div class="pop-up-top"><a class="close-link" onclick="self.pl_remove(); return false;" id="pl-btn-close" href="#">Luk</a></div>' +
				'<div class="pop-up-left">&nbsp;</div>' +
				'<div class="pop-up-right">&nbsp;</div>' +
				'<div class="pop-up-content" id="PL_content">' +
					'<div id="PL_load">&nbsp;</div>' + 
				'</div>' +
				'<div class="pop-up-content-right">&nbsp;</div>' +
				'<div class="clear">&nbsp;</div>' +
				'<div class="pop-up-bottom">&nbsp;</div>' +
				'<div class="pop-up-left-bottom">&nbsp;</div>' +
				'<div class="pop-up-right-bottom">&nbsp;</div>' +
			'</div>' +
			'<div class="pop-up-tip-right">&nbsp;</div>' +
			'<div class="clear">&nbsp;</div>' +
		'</div>';
		return res;
	}
	var res = '<div class="pop-up-wrapper" id="PL_window">' +
		'<div class="pop-up-tip">&nbsp;</div>' +
		'<div class="pop-up-body">' + 
			'<div class="pop-up-top"><a class="close-link" onclick="self.pl_remove(); return false;" id="pl-btn-close" href="#">Luk</a></div>' +
			'<div class="pop-up-left">&nbsp;</div>' +
			'<div class="pop-up-right">&nbsp;</div>' +
			'<div class="pop-up-content" id="PL_content">' +
				'<div id="PL_load">&nbsp;</div>' + 
			'</div>' +
			'<div class="pop-up-content-right">&nbsp;</div>' +
			'<div class="clear">&nbsp;</div>' +
			'<div class="pop-up-bottom">&nbsp;</div>' +
			'<div class="pop-up-left-bottom">&nbsp;</div>' +
			'<div class="pop-up-right-bottom">&nbsp;</div>' +
		'</div>' +
		'<div class="pop-up-tip-right">&nbsp;</div>' +
		'<div class="clear">&nbsp;</div>' +
	'</div>';
	return res;
}
function pl_show(caption, url, imageGroup, e, middle) {
	try {
		if(caption===null){caption="";}
		$("#PL_window").remove();
//		$("body").append('<div id="PL_window"><div id="content-wraper-wrap" class="h6-tipenven"><div id="content-wraper" class="newsletter-box"><a class="close-link" onclick="self.pl_remove(); return false;" id="btn-fwd-close" href="#">Luk</a><div id="PL_load"></div><div id="PL_content"></div></div></div>');
	$("body").append(pl_get_div_html(middle));
	$("#PL_window").css({display:"block"});
		$('#PL_load').show();//show loader
	
		var baseURL;
		if(url.indexOf("?")!==-1){ //ff there is a query string involved
			baseURL = url.substr(0, url.indexOf("?"));
		}else{ 
			baseURL = url;
		}
		 
		var urlString = /\.jpg$|\.jpeg$|\.png$|\.gif$|\.bmp$/;
		var urlType = baseURL.toLowerCase().match(urlString);
	 
		var queryString = url.replace(/^[^\?]+\??/,'');
		var params = pl_parseQuery( queryString );
		PL_WIDTH = parseInt(params['width']) || 350; //defaults to 350 if no paramaters were added to URL
		PL_HEIGHT = parseInt(params['height']) || 440; //defaults to 440 if no paramaters were added to URL

		pl_position(e, middle);

		urlNoQuery = url.split('PL_');
		$("#PL_content").load(urlNoQuery[0], {}, function() {
		$('.scrollarea').jScrollPane();
		});
		
		document.onkeyup = function(e){   
			if (e == null) { // ie
				keycode = event.keyCode;
			} else { // mozilla
				keycode = e.which;
			}
			if(keycode == 27){ // close
				pl_remove();
			}  
		};
		
	} catch(e) {
		//nothing here
	}
}

function pl_remove() {
	//Ie bugging
	//$("#PL_window").fadeOut("fast",function(){$('#PL_window').trigger("unload").unbind().remove();});
	$('#PL_window').hide().trigger("unload").unbind().remove();
	document.onkeydown = "";
	document.onkeyup = "";
	return false;
}
function pl_position(e, middle) {	
	var page_size = pl_getPageSize();
	if(middle){
		var window_left = page_size[0] - PL_WIDTH;
		if(window_left > 0 ){
			window_left  = window_left/2 + 20;
		} else {
			window_left = 20;
		}
		
		if (document.documentElement.scrollTop) {
			var scrollTop = document.documentElement.scrollTop;
		} else {
			var scrollTop = document.body.scrollTop;
		}
		var window_top = (page_size[1] - PL_HEIGHT);    
		
		if(window_top > 0 ){
			window_top  = window_top/2 + 20;
		} else {
			window_top = 20;
		}
		
		window_top = scrollTop + window_top + 95;    
	} else {
		var window_left = e.pageX + 10;
		var window_top = e.pageY;
		if(window_left + PL_WIDTH + 90 > page_size[0]) {
			window_left = e.pageX - (PL_WIDTH + 90);
			if(window_left < 0)
				window_left = 20;
			$('.pop-up-tip').hide();
			$('.pop-up-tip-right').show();
		}
	}
	$("#PL_window").css({left: window_left + 'px', top: window_top - 105 + 'px'});
	if(!middle){
		$('.pop-up-tip').css('margin-top', '85px');
	}
	$('.pop-up-body').css({width: PL_WIDTH + 34 + 'px'});
	$('.pop-up-content').css('height', PL_HEIGHT + 'px');
	$('.pop-up-content-right').css('height', PL_HEIGHT + 20 + 'px').css('margin-top', -1*(PL_HEIGHT + 20) + 'px');
	var load_top = (PL_HEIGHT - parseInt($("#PL_load").css('height'))) / 2;
	$("#PL_load").css({top: load_top + 'px'});
}
function pl_parseQuery ( query ) {
	 var Params = {};
	 if ( ! query ) {return Params;}// return empty object
	 var Pairs = query.split(/[;&]/);
	 for ( var i = 0; i < Pairs.length; i++ ) {
			var KeyVal = Pairs[i].split('=');
			if ( ! KeyVal || KeyVal.length != 2 ) {continue;}
			var key = unescape( KeyVal[0] );
			var val = unescape( KeyVal[1] );
			val = val.replace(/\+/g, ' ');
			Params[key] = val;
	 }
	 return Params;
}
function pl_getPageSize(){
	var de = document.documentElement;
	var w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
	var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;
	arrayPageSize = [w,h];
	return arrayPageSize;
}
function pl_detectMacXFF() {
	var userAgent = navigator.userAgent.toLowerCase();
	if (userAgent.indexOf('mac') != -1 && userAgent.indexOf('firefox')!=-1) {
		return true;
	}
}

/*
$(document).ready(function(){   
	pl_init('a.popup_layer');
//  imgLoader = new Image();// preload image
//  imgLoader.src = pl_pathToImage;
});
function pl_init(domChunk){
	$(domChunk).click(function() {
		var t = this.title || this.name || null;
		var a = this.href || this.alt;
		var g = this.rel || false;
		pl_show(t,a,g);
		this.blur();
		return false;
	});
}
function pl_show(caption, url, imageGroup) {
	try {
		if (typeof document.body.style.maxHeight === "undefined") {//if IE 6
			$("body","html").css({height: "100%", width: "100%"});
			$("html").css("overflow","hidden");
			if (document.getElementById("PL_HideSelect") === null) {//iframe to hide select elements in ie6
				$("body").append("<iframe id='PL_HideSelect'></iframe><div id='PL_overlay'></div><div id='PL_window'></div>");
				$("#PL_overlay").click(pl_remove);
			}
		}else{//all others
			if(document.getElementById("PL_overlay") === null){
				$("body").append("<div id='PL_overlay'></div><div id='PL_window'></div>");
				$("#PL_overlay").click(pl_remove);
			}
		}
		
		if(pl_detectMacXFF()){
			$("#PL_overlay").addClass("PL_overlayMacFFBGHack");//use png overlay so hide flash
		}else{
			$("#PL_overlay").addClass("PL_overlayBG");//use background and opacity
		}
		
		if(caption===null){caption="";}
		$("body").append("<div id='PL_load'><img src='"+imgLoader.src+"' /></div>");//add loader to the page
		$('#PL_load').show();//show loader
		
		var baseURL;
		 if(url.indexOf("?")!==-1){ //ff there is a query string involved
			baseURL = url.substr(0, url.indexOf("?"));
		 }else{ 
				 baseURL = url;
		 }
		 
		 var urlString = /\.jpg$|\.jpeg$|\.png$|\.gif$|\.bmp$/;
		 var urlType = baseURL.toLowerCase().match(urlString);
	 
		var queryString = url.replace(/^[^\?]+\??/,'');
		var params = pl_parseQuery( queryString );
		PL_WIDTH = (params['width']*1) + 30 || 630; //defaults to 630 if no paramaters were added to URL
		PL_HEIGHT = (params['height']*1) + 40 || 440; //defaults to 440 if no paramaters were added to URL
		ajaxContentW = PL_WIDTH - 30;
		ajaxContentH = PL_HEIGHT - 45;
		
		urlNoQuery = url.split('PL_');
		$("#PL_iframeContent").remove();
		$("#PL_window").append("<div id='PL_title'><div id='PL_ajaxWindowTitle'>"+caption+"</div><div id='PL_closeAjaxWindow'><a href='#' id='PL_closeWindowButton' title='Close'>close</a> or Esc Key</div></div><iframe frameborder='0' hspace='0' src='"+urlNoQuery[0]+"' id='PL_iframeContent' name='PL_iframeContent"+Math.round(Math.random()*1000)+"' onload='pl_showIframe()' style='width:"+(ajaxContentW + 29)+"px;height:"+(ajaxContentH + 17)+"px;' > </iframe>");
				
		$("#PL_closeWindowButton").click(pl_remove);
		
		pl_position();
		if($.browser.safari){//safari needs help because it will not fire iframe onload
			$("#PL_load").remove();
			$("#PL_window").css({display:"block"});
		}
			
		document.onkeyup = function(e){   
			if (e == null) { // ie
				keycode = event.keyCode;
			} else { // mozilla
				keycode = e.which;
			}
			if(keycode == 27){ // close
				pl_remove();
			}  
		};
		
	} catch(e) {
		//nothing here
	}
}

//helper functions below
function pl_showIframe(){
	$("#PL_load").remove();
	$("#PL_window").css({display:"block"});
}
function pl_remove() {
	$("#PL_imageOff").unbind("click");
	$("#PL_closeWindowButton").unbind("click");
	$("#PL_window").fadeOut("fast",function(){$('#PL_window,#PL_overlay,#PL_HideSelect').trigger("unload").unbind().remove();});
	$("#PL_load").remove();
	if (typeof document.body.style.maxHeight == "undefined") {//if IE 6
		$("body","html").css({height: "auto", width: "auto"});
		$("html").css("overflow","");
	}
	document.onkeydown = "";
	document.onkeyup = "";
	return false;
}
function pl_position() {
$("#PL_window").css({marginLeft: '-' + parseInt((PL_WIDTH / 2),10) + 'px', width: PL_WIDTH + 'px'});
	if ( !(jQuery.browser.msie && jQuery.browser.version < 7)) { // take away IE6
		$("#PL_window").css({marginTop: '-' + parseInt((PL_HEIGHT / 2),10) + 'px'});
	}
}
function pl_parseQuery ( query ) {
	 var Params = {};
	 if ( ! query ) {return Params;}// return empty object
	 var Pairs = query.split(/[;&]/);
	 for ( var i = 0; i < Pairs.length; i++ ) {
			var KeyVal = Pairs[i].split('=');
			if ( ! KeyVal || KeyVal.length != 2 ) {continue;}
			var key = unescape( KeyVal[0] );
			var val = unescape( KeyVal[1] );
			val = val.replace(/\+/g, ' ');
			Params[key] = val;
	 }
	 return Params;
}
function pl_getPageSize(){
	var de = document.documentElement;
	var w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
	var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;
	arrayPageSize = [w,h];
	return arrayPageSize;
}
function pl_detectMacXFF() {
	var userAgent = navigator.userAgent.toLowerCase();
	if (userAgent.indexOf('mac') != -1 && userAgent.indexOf('firefox')!=-1) {
		return true;
	}
}
*/