﻿
;(function($){
$.fn.popup = function(options){
	//Az átadott kapcsolók összefűzése az alapértelmezésekkel
	var opts = $.extend({}, $.fn.popup.defaults, options);
	var popupStatus = 0;
	
	return this.each(function(){
		var obj = $(this);
		/*css beállítások*/
		$(obj).css({
			"display": "none",
			"position": "fixed",
			"_position": "absolute",
			"border": "2px solid #cecece",
			"z-index": "2",
		});
		
		//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);
		
		//dragg
		if(opts.draggable){
			draggPopup($(obj),opts);
		}
		
		//resize
		if(opts.resizable){
			$(obj).resizable();

		}
		
		//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
		if($('#popupFrame').length == 0){
			$('<iframe><p>Your browser does not support iframes.</p></iframe>')
			.attr({
				id : "popupFrame"
			})
			.prependTo($obj);
		}
		$('#popupFrame').attr({
			scrolling: "no",
			src: opts.href
		})
		.css({
			"width": opts.width-4,
			"height": opts.height-24
		})		
		
	}	
	function backgroundPopup($obj,opts){
		if($('#backgroundPopup').length == 0)
		$('<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
		$obj.css({
			"height": opts.height,
			"width": opts.width,
			"position": "fixed",
			"top": myHeight/2-opts.height/2,
			"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();
			popupStatus = 1;
		}
	}
	function closePopup($obj,opts){
		if($('#popupClose').length == 0)
		$('<a id="popupClose">x</a>')
		.css({
			"font-size": "14px",
			"line-height": "14px",
			"right": "6px",
			"top": "2px",
			"position": "absolute",
			"color": "#6fa5fd",
			"font-weight": "700",
			"display": "block",
			"cursor": "pointer"
		})
		.prependTo($obj);
	}
	
	
	
	function draggPopup($obj,opts){
		if($('.popupHeader').length == 0)
		$('<div class="popupHeader"></div>')
		.css({
			"background-color": "#cecece",
			"height": "20px",
			"width": "100%",
			"cursor": "pointer"
		})
		.prependTo($obj);
		
		$obj.draggable({ handle: ".popupHeader" });//szükség van jquery UI-ra
	}

	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");
			
			$('#popupFrame').attr({
				src: ""
			})
			popupStatus = 0;
		
		}
	}
};

//alapértelmezett beállítások
$.fn.popup.defaults = {
	width: 450,
	height: 450,
	backGroundopacity: 0.7,
	draggable: true,
	resizable: false,
	href: ""
};
})(jQuery);
