dojo.require("dojo.io.*");

// check if namespace object already exists, if not create it
if(typeof(bpui) == 'undefined') {
    var bpui=new Object();
}

// create component namespace object
bpui.alone=new Object();
bpui.alone.timeout="";
bpui.alone.req="";


// This is an exposed function to show and position the popup
bpui.alone.showPopup=function(eventx, theme, from, i) {
//bpui.alone.showPopup=function(eventx, bookId) {

    // statically setup popup for simple case
    popupx="pop0";
    // take into account window scrolling for accurate popup position
    var xx=0;
    var yy=0;
    if (!eventx) var eventx=window.event;
    if (eventx.pageX || eventx.pageY){
        xx=eventx.pageX;
        yy=eventx.pageY;
    } else if (eventx.clientX || eventx.clientY) {
        xx=eventx.clientX + document.body.scrollLeft;
        yy=eventx.clientY + document.body.scrollTop;
    }

    // document.getElementById(popupx).style.left= (xx + 3) + "px";
    // document.getElementById(popupx).style.top= (yy - 50) + "px";

    document.getElementById(popupx).style.left= (100) + "px";
    document.getElementById(popupx).style.top= (yy + 40) + "px";

//    bpui.alone.timeout=setTimeout("bpui.alone.showPopupInternal('" + popupx + "', '" + theme + "', '" + from + "', '" + i + "')", 750);
     bpui.alone.timeout=setTimeout("bpui.alone.showPopupInternal('" + popupx + "', '" + theme + "', '" + from + "', '" + i + "')", 750);
}


// This function is called after initial timeout that represents the delay
//bpui.alone.showPopupInternal=function(popupx, theme, from, i) {
bpui.alone.showPopupInternal=function(popupx, theme, from, i) {
    // retrieve data through dojo call

    var bindArgs = {

        // url when using the jsp to serve the ajax request
        //url: "../book_lookup.jsp?bookId=" + escape(bookId),
        // url when using the servlet to serve the ajax request
//        url: "../php/GetAjaxMessage.php?theme=" + escape(theme) + "&from=" +escape(from) + "&i=" + escape(i),
        url: "../php/GetAjaxMessage.php?theme=" + escape(theme) + "&from=" + escape(from) + "&i=" + escape(i),

        mimetype: "text/xml",
        handle: bpui.alone.ajaxReturnFunction};

    // dispatch the request
    bpui.alone.req=dojo.io.bind(bindArgs);


}

// call back function that dojo calls once the AJAX call returns
bpui.alone.ajaxReturnFunction=function(type, data, evt) {
    // statically setup popup for simple case
    var componentId="pop0";
    // check return of the dojo call to make sure it is valid
//alert("Got result readyState=" + evt.readyState + " status = " + evt.status);
    if (evt.readyState == 4) {
        if (evt.status == 200) {
            // get results and replace dom elements
            var resultx=data.getElementsByTagName("response")[0];
            document.getElementById(componentId + "_title").innerHTML=resultx.getElementsByTagName("title")[0].childNodes[0].nodeValue;
            document.getElementById(componentId + "_message").innerHTML=resultx.getElementsByTagName("message")[0].childNodes[0].nodeValue;;
            // show popup with the newly populated information
            document.getElementById(componentId).style.visibility='visible';
        } else if (evt.status == 204){
            alert("204 return");
        }
    }

}

// This is an exposed function to hide the popup and/or cancel to showing of the popup
bpui.alone.hidePopup=function() {
    // statically setup popup for simple case
    popupx="pop0";
    document.getElementById(popupx).style.visibility='hidden';
    bpui.alone.clearTimeout();
}

// This is an exposed function to clear the timer of the popup in the mouseout event handler
// if the showing of the popup is required
bpui.alone.clearTimeout=function() {
    clearTimeout(bpui.alone.timeout);
}


