﻿
;(function($){
$.fn.galleryPopup = function(options){
	//Az átadott kapcsolók összefűzése az alapértelmezésekkel
	var opts = $.extend({}, $.fn.galleryPopup.defaults, options);
	var popupStatus = 0;
	
	return this.each(function(){
		var obj = $(this);
		$(this).empty();
		/*css beállítások*/
		$(obj).css({
			"display": "none",
			"position": "fixed",
			"_position": "absolute",
			"top": "0",
			"left": "0",
			"z-index": "2",
			"border": "none"
		});
		
		//iframe felhelyezés
		setIframePopup($(obj),opts);
		
		//háttér felhelyezése
		backgroundPopup($(obj),opts);
		
		//középre igazítás
		centerPopup($(obj),opts);
		
		//popup betöltés
		loadPopup($(obj),opts);
		
		//bezáró x felhelyezése
		closePopup($(obj),opts);
		
		//BEZÁRÓ FÜGGVÉNYEK
		//X re kattintás
		$("#popupClose").click(function(){
			disablePopup($(obj),opts);
		});
		
		//popup mellé kattintás
		$("#backgroundPopup").click(function(){
			disablePopup($(obj),opts);
		});
		//escape billentyű leütése
		$(document).keypress(function(e){
			if(e.keyCode==27 && popupStatus==1){
				disablePopup($(obj),opts);
			}
		});
	});
	
	function setIframePopup($obj,opts){
		//ha nem létezik betesszük a framet
		$('<iframe scrolling="no" allowtransparency="true"><p>Your browser does not support iframes.</p></iframe>')
		.attr({
			id : "popupFrame",
			frameborder: "0",
			allowtransparency:"true"
		})
		.prependTo($obj);
		
		$('#popupFrame').attr({
			scrolling: "no",
			src: opts.href
		})
		.css({
			"width": opts.width-4,
			"height": opts.height-24
		});
		
	}	
	function backgroundPopup($obj,opts){
		$('<div id="backgroundPopup"></div>')
		.css({
			"display": "none",
			"position": "fixed",
			"_position": "absolute",
			"height": "100%",
			"width": "100%",
			"top": "0",
			"left": "0",
			"background": "#000000",
			"border": "1px solid #cecece",
			"z-index": "1"
		})
		.prependTo('body');
	}
		
	function centerPopup($obj,opts){
		//ablak méretének lekérése
		var myWidth = 0, myHeight = 0;
		if( typeof( window.innerWidth ) == 'number' ) {
			//Non-IE
			myWidth = window.innerWidth;
			myHeight = window.innerHeight;
		} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
			//IE 6+ in 'standards compliant mode'
			myWidth = document.documentElement.clientWidth;
			myHeight = document.documentElement.clientHeight;
		} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
			//IE 4 compatible
			myWidth = document.body.clientWidth;
			myHeight = document.body.clientHeight;
		}
		
		//popup középre igazítása ha nagyobb a képméret mint a doboz ellenkező esetben fentre igazitjuk 0px hez
		if(myHeight>opts.height){
			$obj.css({
				"height": opts.height,
				"width": opts.width,
				"position": "fixed",
				"top": myHeight/2-opts.height/2,
				"left": myWidth/2-opts.width/2
			});
		}else{
			$obj.css({
				"height": opts.height,
				"width": opts.width,
				"position": "absolute",
				"top": '0px',
				"left": myWidth/2-opts.width/2
			});
		}		
		
		//csak IE6-hoz kell
		$("#backgroundPopup").css({
			"height": myHeight
		});
	}
	
	function loadPopup($obj,opts){
		
		//popup betöltés csak ha még nincs betöltve
		if(popupStatus==0){
			$("#backgroundPopup").css({
				"opacity": opts.backGroundopacity
			});
			$("#backgroundPopup").fadeIn("slow");
			$obj.fadeIn("slow");
			$(".banner").hide();
			$(".fb-forum").hide();
			$("object").hide();
			popupStatus = 1;
		}
	}
	
	function closePopup($obj,opts){
		if($('#popupClose').length == 0)
		$('<img src="public/images/gallery_close_button.png" alt="close" id="popupClose"/>')
		.css({
			"position": "absolute",
			"right": "15px",
			"top": "43px",
			"display": "block",
			"cursor": "pointer"
		})
		.prependTo($obj);
	}
	
	function disablePopup($obj,opts){
		
		//popup bezárás de csak ha aktív
		if(popupStatus==1){
			$("#backgroundPopup").fadeOut("slow");
			$obj.fadeOut("slow");
			$(".banner").fadeIn("slow");
			$(".fb-forum").show();
			$("object").show();
			
			$('#popupFrame').attr({
				src: ""
			});
			popupStatus = 0;
		
		}
	}
};

//alapértelmezett beállítások
$.fn.galleryPopup.defaults = {
	width: 990,
	height: 750,
	backGroundopacity: 0.9,
	href: ""
};
})(jQuery);
