/*//Takes elements with class "kbpopup" and wraps them to use in
//two functions namely KB.Popup and KB.ModalPopup
//Displayed popup has id "kbpopup_frame" or "kbpopup_modalframe" depending on the called function,
//has a container with id "kbpopup_element" and has a close button with id "kbpopup_close".
//popup.css has necessary styles.
//
//KB.Popup(element) creates a centered popup
//Example;
//	KB.Popup($("id"));
//Parameters;
//Element element:
//	element to use as the popup content
//
//KB.ModalPopup(element) creates a centered modal popup
//Example;
//	KB.ModalPopup($("id"));
//Parameters;
//Element element:
//	element to use as the popup content
//
*/
document.observe
(
	"dom:loaded",
	function()
	{
		var usedframe;
        var frame=new Element("div",{id:"kbpopup_frame"});
		var header=new Element("div",{id:"kbpopup_frame_header"});
		var close=new Element("button",{id:"kbpopup_close"}).update("X");
		var element=new Element("div",{id:"kbpopup_element"});
		frame.insert(header.insert(close)).insert(element);
		var modalframe=new Element("div",{id:"kbpopup_modalframe"});

		$$(".kbpopup").each
		(
			function(pop)
			{
				//outerhtml for you
				pop.setStyle({display:"block"}).wrap("div").hide();
			}
		);

		$$("body")[0].insert(modalframe.hide()).insert(frame.hide());

		KB.Popup={
            open:function(el)
		    {
			    if(!el)
				    return;
			    //IE loses content here so copy innerhtml of wrapped content
			    element.update($(el).up().innerHTML);
			    frame.show();
			    var frm=frame.cumulativeOffset();
			    var dim=element.getDimensions();
			    frame.setStyle({top:(frm.top-dim.height/2)+"px",left:(frm.left-dim.width/2)+"px"});
		    },
            close:function(){
                frame.setStyle({top:"50%",left:"50%"});
                frame.hide();
                modalframe.hide();
            }
        };
		KB.ModalPopup={
            open:function(el)
            {
                KB.Popup.open(el);
                modalframe.show();
                window.scroll(-1,0);
            },
            close:function(){
                frame.setStyle({top:"50%",left:"50%"});
                frame.hide();
                modalframe.hide();
            }
        };
/*//WWS FIXIT this code breaks login in IE
//        KB.ImagePopup={
//            open:function(url, title, description)
//            {
//                var el = DIV({class:'kbpopup'},
//                    IMG({href:url}),
//                    DIV({}, title),
//                    DIV({}, description)
//                );
//                $$("body")[0].insert(el);
//                KB.ModalPopup.open(el);
//            },
//            close:function(){
//                KB.ModalPopup.close();
//            }
//        };
*/
		close.observe
        (
            "click",
            function()
            {
                frame.setStyle({top:"50%",left:"50%"});
                frame.hide();
                modalframe.hide();
            }
        );
	}
);
