/*
 Modal Login v0.1 by O.G
 2009.04.01

 Hasznalat:

 1. A <head> reszbe:

	<script type="text/javascript" src="mootools.v1.11.js"></script>
	<script type="text/javascript" src="modal_login.js"></script>

 2. Alulra, amikor mar kb tudni lehet mekkora lesz a megjelenitett oldal merete.
    Alapesetben ondomready-hez pakold.

	<script type="text/javascript">
	toggleModal("#fff", {
		zIndex: 1000,
		width: 395,
		height: 591,
		widthCorrection: 1,
		heightCorrection: 1,
		events: {
			click: function() {
				// valami akcio
			}
		}
	});
	</script>
*/

var toggleModalLogin = function(backgroundColour, options) {
    // modal login view (mootools v1.11 szukseges)
    // flash folott is megjelenik, de a position:fixed miatt nemely bongeszoben buggos (pl: IE7-ben ha scrollozol, akkor mar a flash ala kerul)
    // Firebug-al nezve a trukkhoz hasznalt iframe nem "mozog" a fixed-nek megfeleloen, hanem vegig ugyanott marad, mintha absolute lenne a pozicioja

    try {
	if (MooTools.version != "") {
		// Van mootools, minden okay
	} else {
		// nincs mootools
		return;
	}
    } catch (err) {
	// nincs mootools
	return;
    }


    if ($("modallogin")) {
	try {
	        $("modallogin").remove();
        	$("modallogin_inner").remove();
        	$("modallogin_iframe").remove();
	} catch (e) {
	}
        return false;
    }

    var options = $merge({
	modal_login_src: "/modal_login?nhf=1", // nhh=noheaderfooter
        zIndex: 10000000,
        opacity: .8,
	width: 10,
	height: 10,
	widthCorrection: 0, // a trukk iframe-hez
	heightCorrenction: 0, // a trukk iframe-hez
	windowWidth: window.getScrollWidth(), //window.getScrollWidth(),
	windowHeight: window.getScrollHeight(), //window.getScrollHeight(),
        events: function() {} //$empty()
    }, options);

    if (!$type(backgroundColour) && !$("modallogin"))
        return false;

    new Element("div", {
        id: "modallogin",
        styles: {
            position: "absolute",
            top: 0,
            left: 0,
            width: options.windowWidth,
            height: options.windowHeight,
            background: backgroundColour,
            "z-index": options.zIndex,
            opacity: options.opacity
        },
        events: options.events
    }).inject(document.body);

	var position = "absolute";
	if (window.ie7 || window.gecko || window.webkit || window.webkit420 || window.opera) {
		// Meghatarozott bongeszoknel fixed-re rakjuk (ie7 strict mode-ban 100% tamogatja, de transitional-ban is mukodik esetunkben)
		position = "fixed";
	}

	// window (ez jelenik meg a modal window felett)
	var pwindow = new Element("div", {
		"id": "modallogin_inner",
		//"class": "modal_inner",
		"styles": {
			//width: options.width+"px",
			//height: options.height+"px",
			position: position, // "absolute", "fixed"
			//left: (window.getWidth()/2 - width/2)+"px",
			//top: (window.getScrollTop() + (window.getHeight()/2 - height/2))+"px",
			left: (50 - options.width/window.getWidth() * 100 / 2)+"%",
			top: (50 - options.height/window.getHeight() * 100 / 2)+"%",

			//"background-color": "#0f0",
			//border: "1px solid #000",
            		"z-index": options.zIndex+2
		}
	});
	pwindow.setStyle("background", "#ffffff url(/skin/common/gfx/loading.gif) no-repeat scroll center center");

	// close div
	var pdiv = new Element('div'); // IE nem szereti setStyle-al, ha document.createElement()-el hozod letre
	pdiv.className = 'modal_login_header';
	pdiv.appendChild(document.createTextNode(" ")); // title / Bejelentkezés
	pdiv.setStyle("font-weight", "bold");
	pdiv.setStyle("overflow", "hidden");
	pdiv.setStyle("background-color", "#fff");

	// close anchor
	var panchor = new Element('a'); // IE nem szereti setStyle-al, ha document.createElement()-el hozod letre
	panchor.setStyle("margin-left", '320px'); // 200px
	panchor.setStyle("font-weight", "bold");
	//panchor.setStyle("float", "right");
	panchor.appendChild(document.createTextNode('Bezárás '));
	panchor.setAttribute("href", '');
	panchor.onclick = function() {
		new Fx.Style($("modallogin"), 'opacity', {duration: 500, wait: false}).start(0);
		(function() { toggleModalLogin(); }).delay(500); // removes the layer itself after fade done
		return false;
	}

        // close img
	var pimg = new Element('img'); // IE nem szereti setStyle-al, ha document.createElement()-el hozod letre
        pimg.setAttribute("src", '/gfx/admin/torol.gif');
        pimg.setAttribute("alt", '');

        panchor.appendChild(pimg); // add img to anchor
	pdiv.appendChild(panchor); // add anchor to div

	//------------------------------

	// magazin div
	var mdiv = new Element('div'); // IE nem szereti setStyle-al, ha document.createElement()-el hozod letre
	mdiv.className = 'modal_login_class';

	// magazin iframe
	var miframe = new Element("iframe", {
		"id": "logloglog",
		"src": options.modal_login_src,
		"frameborder": "no",
		"scrolling": "no",
		"styles": {
			width: options.width+"px",
			height: options.height+"px",
			border: "none",
            		"z-index": options.zIndex+999
		}
	});


	mdiv.appendChild(miframe); // add

	pwindow.appendChild(mdiv); // add

	pwindow.appendChild(pdiv); // add 

	pwindow.inject(document.body); // add to body

	// window (ez jelenik meg a modal window allatt. Ez a trukko iframe, ami kitakarja a flash-t)
	var piframe = new Element("iframe", {
		"id": "modallogin_iframe",
		"src": "about:blank",
		"styles": {
			width: options.width+options.widthCorrection+"px",
			height: "20px",
			position: position, // "absolute", "fixed"
			left: (50 - options.width/window.getWidth() * 100 / 2)+"%",
			top: (50 - options.height/window.getHeight() * 100 / 2)+"%",

			border: "none",
			"margin-top": "600px",
            		"z-index": options.zIndex+1
		}
	});
	piframe.inject(document.body); // add to body

} // end toggleModal

