// JavaScript Document

//create the lightbox div dynamically

var bod;
var _lightbox_width = 790;
var panel;
var resizablepanel;

//initialize the resize functionality
YAHOO.util.Event.onDOMReady(
    function() {
        var body_tag = document.getElementsByTagName("body");
        bod = body_tag[0];
        //body_tag[0].setAttribute("className","yui-skin-sam");
        //body_tag[0].setAttribute("class","yui-skin-sam");
        if(!document.getElementById("panel")){
            resizablepanel = document.createElement("div");
            //create the container
            //resizablepanel.id = "resizablepanel";
            resizablepanel.innerHTML = "<div id=\"panel\"><div class=\"hd\"></div><div class=\"bd\"></div><div class=\"ft\"></div></div>";
            //resizablepanel.style.display = 'none';
            //resizablepanel.style.position = 'absolute';
            //resizablepanel.style.zIndex = '35';
            //resizablepanel.style.top = (YAHOO.util.Dom.getDocumentScrollTop() + 200)+'px';
            //resizablepanel.style.left = ((YAHOO.util.Dom.getViewportWidth()/2) - (_lightbox_width/2))+'px';
			
            bod.appendChild(resizablepanel);
        }
		
        // Create a panel Instance, from the 'resizablepanel' DIV standard module markup
        panel = new YAHOO.widget.Panel('panel', {
            draggable:true,
            /*width: _lightbox_width+'px',*/
            visible:false,
            fixedcenter: true,
            constraintoviewport: true,
            zIndex:5,
            modal:true,
            underlay:"none"
        });
        //panel.cfg.setProperty("underlay","matte");
        var container = YAHOO.util.Dom.getElementsByClassName('game_container');
        panel.render(container[0]);

        // Create Resize instance, binding it to the 'panel' DIV
        var resize = new YAHOO.util.Resize('panel', {
            handles: ['br'],
            autoRatio: false,
            minWidth: 300,
            minHeight: 100,
            status: true
        });

        resize.on('resize', function(args) {
            var panelHeight = args.height;
            var panelWidth = args.width;
            this.cfg.setProperty("height", panelHeight + "px");
            this.cfg.setProperty("width", panelWidth + "px");
            var iframe = document.getElementById('lightbox_iframe');
            if(iframe)
            {
                iframe.style.width = (panelWidth-20)+'px';
                iframe.style.height = (panelHeight-60)+'px';
            }

        }, panel, true);


        // Setup startResize handler, to constrain the resize width/height
        // if the constraintoviewport configuration property is enabled.
        resize.on('startResize', function(args) {

            if (this.cfg.getProperty("constraintoviewport")) {
                var D = YAHOO.util.Dom;

                var clientRegion = D.getClientRegion();
                var elRegion = D.getRegion(this.element);

                resize.set("maxWidth", clientRegion.right - elRegion.left - YAHOO.widget.Overlay.VIEWPORT_OFFSET);
                resize.set("maxHeight", clientRegion.bottom - elRegion.top - YAHOO.widget.Overlay.VIEWPORT_OFFSET);
            } else {
                resize.set("maxWidth", null);
                resize.set("maxHeight", null);
            }
        }, panel, true);

        document.getElementById('_yuiResizeMonitor').style.display = 'none';

    }
);

//helper function to load the YUI lightbox
function viewLightbox(content, header, modal, width, height, fixedcenter, no_iframe) {
    if(panel) {
        if(modal!==null && modal!="")
        {
            panel.cfg.setProperty("modal",modal);
        }

        var iframe_width;
        var iframe_height;
        if(width!==null && width!="") {
            panel.cfg.setProperty("width",width+'px');
            _lightbox_width = width;
             iframe_width = parseInt(panel.cfg.getProperty("width").replace('px',''))-20;

        } else
        {
            panel.cfg.setProperty("width",_lightbox_width+'px');
            iframe_width = _lightbox_width-20;
        }
        var elements = YAHOO.util.Dom.getElementsByClassName('bd');
        if(height!==null && height!="")	{
            panel.cfg.setProperty("height",height+'px');
            elements[0].style.height = height+'px';
            iframe_height = parseInt(panel.cfg.getProperty("height").replace('px',''))-60;
        } else
        {
            iframe_height = elements && elements.length>0 ?
                (parseInt(elements[0].style.height.replace('px',''))-60) : 400;
        }

        if(fixedcenter)
        {
            panel.cfg.setProperty("fixedcenter",true);
        } else if(fixedcenter!=null && fixedcenter==false)
        {
            panel.cfg.setProperty("fixedcenter",false);
        }

        if(header!==null && header!="")
        {
            panel.setHeader(header);
        }
        
        if(no_iframe)
        {
            panel.setBody(content);
        } else
        {
            var iframe = '<iframe frameborder="0" src="'+content+'" scrolling="auto" style="width:'
                +iframe_width+'px;height:'+iframe_height+'px;" id="lightbox_iframe" />';
            panel.setBody(iframe);
        }
		
        panel.show();
    } 
}