/**
 * ESV Popup Reference.
 *
 * Use ESV's Javascript services to create an in-document popup window
 * containing the reference to Bible passage. It can be used together with
 * Scripturizer JS (http://fucoder.com/code/scripturizer-js/). 
 *
 * For more information, see:
 *
 *   http://fucoder.com/code/esvpopup/
 *
 * @author Scott Yang <scotty@yang.id.au>
 * @version 1.2
 */ 

var ESVPopup = {
    baseurl: '',
    frame: null,
    height: 300, //345, // 470,    // Default height of the popup window
    width: 750//824 //540  //764    // Default width of the popup window
    //section: 1
};

ESVPopup.show = function(passage, x, y) {
   
    var ie = navigator.userAgent.toLowerCase().indexOf('msie') >= 0;
    var ie7 = navigator.userAgent.toLowerCase().indexOf('msie 7') >= 0;

    if (!ESVPopup.baseurl) {
        var elms = document.getElementsByTagName('script');

        for (var i = 0; i < elms.length; i++) {
            if (elms[i].src && (elms[i].src.indexOf("esvpopup.js") >= 0)) {
                var src = elms[i].src;
                var qs = __decodeQS(src);
                ESVPopup.baseurl = src.substring(0, src.lastIndexOf('/')+1);
                if (qs.height){
                    ESVPopup.height = qs.height;
                    //alert(passage);
                }
                if (qs.width)
                    ESVPopup.width = qs.width;
                break;
            }
        }
    }

    url = ESVPopup.baseurl + 'http://calc2008.costs.infomine.com/BrowsePads/Types.aspx?section=' + passage;
    if (!ESVPopup.frame) {
        var frame = null;

        // MSIE does not have a proper DOM2 implementation so <iframe/>
        // created by DOM2 does not work sometimes. Fortunately we can use
        // insertAdjacentHTML()
        if (ie) {
            var html = '<iframe id="ESVPopupFrame" frameborder="0" '+
                'src="'+url+'" style="position:absolute;top:'+y+
                'px;left:'+x+'px;" scrolling="no" marginheight="0" '+
                'marginwidth="0"></iframe>';
            document.body.insertAdjacentHTML('afterbegin', html);
            frame = document.getElementById('ESVPopupFrame');
            frame.attachEvent('onblur', ESVPopup.hide);
            frame.style.filter = 'progid:DXImageTransform.Microsoft.Shadow'+
                '(color=#888888, Direction=135, Strength=5)';
            document.body.attachEvent('onmousedown', ESVPopup.hide);
        } else {
            var frame = document.createElement('IFRAME');
            document.body.appendChild(frame);
            frame.id = 'ESVPopupFrame';
            frame.frameBorder = 0;
            frame.marginHeight = 0;
            frame.marginWidth = 0;
            frame.src = url;

            frame.style.position = 'absolute';
            frame.style.top = y+'px';
            frame.style.left = x+'px';

            frame.addEventListener('blur', ESVPopup.hide, false);
            window.addEventListener('mousedown', ESVPopup.hide, false);
        }

        if (!ie || ie7) {
            frame.style.border = '#888 solid 0px';
        }
        frame.style.height = ESVPopup.height+'px';
        frame.style.width = ESVPopup.width+'px';
        
        ESVPopup.frame = frame;
    } else {
        ESVPopup.frame.src = url;
        ESVPopup.frame.style.top = y + 'px';
        ESVPopup.frame.style.left = x + 'px';
        ESVPopup.frame.style.display = '';
    }

    // Fixing up the position so no overflow is needed. We need to detect
    // whether we are under XHTML or HTML 4
    var b;
    if (ie) {
        b = document.documentElement.clientWidth ? document.documentElement :
            document.body;
    } else {
        b = (document.doctype && document.doctype.publicId && 
             (document.doctype.publicId.indexOf('XHTML') >= 0)) ?
            document.documentElement : document.body;
    }

    var dh = b.clientHeight;
    var dw = b.clientWidth;
    dh = (ESVPopup.frame.offsetHeight+y) - dh;
    dw = (ESVPopup.frame.offsetWidth+x)  - dw;

    ESVPopup.frame.style.left = x-(dw>0?dw:0)+b.scrollLeft + 'px';
    ESVPopup.frame.style.top  = y-(dh>0?dh:0)+b.scrollTop  + 'px';
};

ESVPopup.onclick = function(e, passage) {
    if (!e) e = window.event;

    var x = e.clientX;
    var y = e.clientY;
    
   
//    if (!passage)
//        passage = (e.srcElement || e.target).innerText;
    //ESVPopup.show(passage, x, y);
    var ie7 = navigator.userAgent.toLowerCase().indexOf('msie 7') >= 0;   
    if (ie7) y += 15;    
    
    ESVPopup.show(passage, x, y);
    
    //ESVPopup.section = passage;

};

ESVPopup.hide = function(event) {
    var f = ESVPopup.frame;
    if (f)
        f.style.display = 'none';
};

function __decodeQS(qs) {
    var k, v, i1, i2, r = {};
    i1 = qs.indexOf('?');
    i1 = i1 < 0 ? 0 : i1 + 1;
    while ((i1 >= 0) && ((i2 = qs.indexOf('=', i1)) >= 0)) {
        k = qs.substring(i1, i2);
        i1 = qs.indexOf('&', i2);
        v = i1 < 0 ? qs.substring(i2+1) : qs.substring(i2+1, i1++);
        r[unescape(k)] = unescape(v);
    }
    return r;
}


//----------------------- Milling ----------------------------------------------------------------
var Milling = {
    baseurl: '',
    frame: null,
    height: 300, // 470,    // Default height of the popup window
    width: 750 //540  //764    // Default width of the popup window
    //section: 1
};

Milling.show = function(passage, x, y) {
   
    var ie = navigator.userAgent.toLowerCase().indexOf('msie') >= 0;
    var ie7 = navigator.userAgent.toLowerCase().indexOf('msie 7') >= 0;

    if (!Milling.baseurl) {
        var elms = document.getElementsByTagName('script');

        for (var i = 0; i < elms.length; i++) {
            if (elms[i].src && (elms[i].src.indexOf("esvpopup.js") >= 0)) {
                var src = elms[i].src;
                var qs = __decodeQS(src);
                Milling.baseurl = src.substring(0, src.lastIndexOf('/')+1);
                if (qs.height){
                    Milling.height = qs.height;
                    //alert(passage);
                }
                if (qs.width)
                    Milling.width = qs.width;
                break;
            }
        }
    }

    url = Milling.baseurl + 'http://calc2008.costs.infomine.com/BrowsePads/Types.aspx?section=' + passage;
    
    //alert(url);
    
    if (!Milling.frame) {
        var frame = null;

        // MSIE does not have a proper DOM2 implementation so <iframe/>
        // created by DOM2 does not work sometimes. Fortunately we can use
        // insertAdjacentHTML()
        if (ie) {
            var html = '<iframe id="ESVPopupFrame" frameborder="0" '+
                'src="'+url+'" style="position:absolute;top:'+y+
                'px;left:'+x+'px;" scrolling="no" marginheight="0" '+
                'marginwidth="0"></iframe>';
            document.body.insertAdjacentHTML('afterbegin', html);
            frame = document.getElementById('ESVPopupFrame');
            frame.attachEvent('onblur', Milling.hide);
            
            frame.style.filter = 'progid:DXImageTransform.Microsoft.Shadow'+
                '(color=#888888, Direction=135, Strength=5)';
            document.body.attachEvent('onmousedown', Milling.hide);
        } else {
            var frame = document.createElement('IFRAME');
            document.body.appendChild(frame);
            frame.id = 'ESVPopupFrame';
            frame.frameBorder = 0;
            frame.marginHeight = 0;
            frame.marginWidth = 0;
            frame.src = url;

            frame.style.position = 'absolute';
            frame.style.top = y+'px';
            frame.style.left = x+'px';

            frame.addEventListener('blur', Milling.hide, false);
            window.addEventListener('mousedown', Milling.hide, false);
        }

        if (!ie || ie7) {
            frame.style.border = '#888 solid 0px';
        }
        frame.style.height = Milling.height+'px';
        frame.style.width = Milling.width+'px';
        
        Milling.frame = frame;
    } else {
        Milling.frame.src = url;
        Milling.frame.style.top = y + 'px';
        Milling.frame.style.left = x + 'px';
        Milling.frame.style.display = '';
    }

    // Fixing up the position so no overflow is needed. We need to detect
    // whether we are under XHTML or HTML 4
    var b;
    if (ie) {
        b = document.documentElement.clientWidth ? document.documentElement :
            document.body;
    } else {
        b = (document.doctype && document.doctype.publicId && 
             (document.doctype.publicId.indexOf('XHTML') >= 0)) ?
            document.documentElement : document.body;
    }

    var dh = b.clientHeight;
    var dw = b.clientWidth;
    dh = (Milling.frame.offsetHeight+y) - dh;
    dw = (Milling.frame.offsetWidth+x)  - dw;

    Milling.frame.style.left = x-(dw>0?dw:0)+b.scrollLeft + 'px';
    Milling.frame.style.top  = y-(dh>0?dh:0)+b.scrollTop  + 'px';
};

Milling.onclick = function(e, passage) {
    if (!e) e = window.event;
        var x = e.clientX;
        var y = e.clientY;
        
        var ie7 = navigator.userAgent.toLowerCase().indexOf('msie 7') >= 0;   
        if (ie7)
            y += 15;    
    
    //var frame = document.getElementById('ESVPopupFrame');
    
    //if(frame == null)
        Milling.show(passage, x, y);
//    else
//        //alert(frame.style.display);
//    
//        if(frame.style.display == '')
//        {
//            frame.style.display = 'none';
//        }else{
//            Milling.show(passage, x, y);
//        }
        
        
    
    //Milling.section = passage;

};

Milling.hide = function(event) {
    var f = Milling.frame;
    if (f)
        f.style.display = 'none';
};

function __decodeQS(qs) {
    var k, v, i1, i2, r = {};
    i1 = qs.indexOf('?');
    i1 = i1 < 0 ? 0 : i1 + 1;
    while ((i1 >= 0) && ((i2 = qs.indexOf('=', i1)) >= 0)) {
        k = qs.substring(i1, i2);
        i1 = qs.indexOf('&', i2);
        v = i1 < 0 ? qs.substring(i2+1) : qs.substring(i2+1, i1++);
        r[unescape(k)] = unescape(v);
    }
    return r;
}

//--------------------- Miscellaneous ---------------------------------------------------------
var Miscellaneous = {
    baseurl: '',
    frame: null,
    height: 300, //600, // Default height of the popup window
    width: 750 //824 //540  //764    // Default width of the popup window
    //section: 1
};

Miscellaneous.show = function(passage, x, y) {
   
    var ie = navigator.userAgent.toLowerCase().indexOf('msie') >= 0;
    var ie7 = navigator.userAgent.toLowerCase().indexOf('msie 7') >= 0;

    if (!Miscellaneous.baseurl) {
        var elms = document.getElementsByTagName('script');

        for (var i = 0; i < elms.length; i++) {
            if (elms[i].src && (elms[i].src.indexOf("esvpopup.js") >= 0)) {
                var src = elms[i].src;
                var qs = __decodeQS(src);
                Miscellaneous.baseurl = src.substring(0, src.lastIndexOf('/')+1);
                if (qs.height){
                    Miscellaneous.height = qs.height;
                    //alert(passage);
                }
                if (qs.width)
                    Miscellaneous.width = qs.width;
                break;
            }
        }
    }

    url = Miscellaneous.baseurl + 'http://calc2008.costs.infomine.com/BrowsePads/Types.aspx?section=' + passage;
    
    //alert(url);
    
    if (!Miscellaneous.frame) {
        var frame = null;

        // MSIE does not have a proper DOM2 implementation so <iframe/>
        // created by DOM2 does not work sometimes. Fortunately we can use
        // insertAdjacentHTML()
        if (ie) {
            var html = '<iframe id="ESVPopupFrame" frameborder="0" '+
                'src="'+url+'" style="position:absolute;top:'+y+
                'px;left:'+x+'px;" scrolling="no" marginheight="0" '+
                'marginwidth="0"></iframe>';
            document.body.insertAdjacentHTML('afterbegin', html);
            frame = document.getElementById('ESVPopupFrame');
            frame.attachEvent('onblur', Miscellaneous.hide);
            
            frame.style.filter = 'progid:DXImageTransform.Microsoft.Shadow'+
                '(color=#888888, Direction=135, Strength=5)';
            document.body.attachEvent('onmousedown', Miscellaneous.hide);
        } else {
            var frame = document.createElement('IFRAME');
            document.body.appendChild(frame);
            frame.id = 'ESVPopupFrame';
            frame.frameBorder = 0;
            frame.marginHeight = 0;
            frame.marginWidth = 0;
            frame.src = url;

            frame.style.position = 'absolute';
            frame.style.top = y+'px';
            frame.style.left = x+'px';

            frame.addEventListener('blur', Miscellaneous.hide, false);
            window.addEventListener('mousedown', Miscellaneous.hide, false);
        }

        if (!ie || ie7) {
            frame.style.border = '#888 solid 0px';
        }
        frame.style.height = Miscellaneous.height+'px';
        frame.style.width = Miscellaneous.width+'px';
        
        Miscellaneous.frame = frame;
    } else {
        Miscellaneous.frame.src = url;
        Miscellaneous.frame.style.top = y + 'px';
        Miscellaneous.frame.style.left = x + 'px';
        Miscellaneous.frame.style.display = '';
    }

    // Fixing up the position so no overflow is needed. We need to detect
    // whether we are under XHTML or HTML 4
    var b;
    if (ie) {
        b = document.documentElement.clientWidth ? document.documentElement :
            document.body;
    } else {
        b = (document.doctype && document.doctype.publicId && 
             (document.doctype.publicId.indexOf('XHTML') >= 0)) ?
            document.documentElement : document.body;
    }

    var dh = b.clientHeight;
    var dw = b.clientWidth;
    dh = (Miscellaneous.frame.offsetHeight+y) - dh;
    dw = (Miscellaneous.frame.offsetWidth+x)  - dw;

    Miscellaneous.frame.style.left = x-(dw>0?dw:0)+b.scrollLeft + 'px';
    Miscellaneous.frame.style.top  = y-(dh>0?dh:0)+b.scrollTop  + 'px';
};

Miscellaneous.onclick = function(e, passage) {
    if (!e) e = window.event;

    var x = e.clientX;
    var y = e.clientY;
    
   
    var ie7 = navigator.userAgent.toLowerCase().indexOf('msie 7') >= 0;   
    if (ie7) y += 15;
    
    Miscellaneous.show(passage, x, y);
    
    //Miscellaneous.section = passage;

};

Miscellaneous.hide = function(event) {
    var f = Miscellaneous.frame;
    if (f)
        f.style.display = 'none';
};

function __decodeQS(qs) {
    var k, v, i1, i2, r = {};
    i1 = qs.indexOf('?');
    i1 = i1 < 0 ? 0 : i1 + 1;
    while ((i1 >= 0) && ((i2 = qs.indexOf('=', i1)) >= 0)) {
        k = qs.substring(i1, i2);
        i1 = qs.indexOf('&', i2);
        v = i1 < 0 ? qs.substring(i2+1) : qs.substring(i2+1, i1++);
        r[unescape(k)] = unescape(v);
    }
    return r;
}

//--------------------- All ---------------------------------------------------------
var All = {
    baseurl: '',
    frame: null,
    height: 300, //600, // Default height of the popup window
    width: 750 //824 //540  //764    // Default width of the popup window
    //section: 1
};

All.show = function(passage, x, y) {
   
    var ie = navigator.userAgent.toLowerCase().indexOf('msie') >= 0;
    var ie7 = navigator.userAgent.toLowerCase().indexOf('msie 7') >= 0;

    if (!All.baseurl) {
        var elms = document.getElementsByTagName('script');

        for (var i = 0; i < elms.length; i++) {
            if (elms[i].src && (elms[i].src.indexOf("esvpopup.js") >= 0)) {
                var src = elms[i].src;
                var qs = __decodeQS(src);
                All.baseurl = src.substring(0, src.lastIndexOf('/')+1);
                if (qs.height){
                    All.height = qs.height;
                    //alert(passage);
                }
                if (qs.width)
                    All.width = qs.width;
                break;
            }
        }
    }

    url = All.baseurl + 'http://calc2008.costs.infomine.com/BrowsePads/Types.aspx?section=' + passage;
    if (!All.frame) {
        var frame = null;

        // MSIE does not have a proper DOM2 implementation so <iframe/>
        // created by DOM2 does not work sometimes. Fortunately we can use
        // insertAdjacentHTML()
        if (ie) {
            var html = '<iframe id="ESVPopupFrame" frameborder="0" '+
                'src="'+url+'" style="position:absolute;top:'+y+
                'px;left:'+x+'px;" scrolling="no" marginheight="0" '+
                'marginwidth="0"></iframe>';
            document.body.insertAdjacentHTML('afterbegin', html);
            frame = document.getElementById('ESVPopupFrame');
            frame.attachEvent('onblur', All.hide);
            frame.style.filter = 'progid:DXImageTransform.Microsoft.Shadow'+
                '(color=#888888, Direction=135, Strength=5)';
            document.body.attachEvent('onmousedown', All.hide);
        } else {
            var frame = document.createElement('IFRAME');
            document.body.appendChild(frame);
            frame.id = 'ESVPopupFrame';
            frame.frameBorder = 0;
            frame.marginHeight = 0;
            frame.marginWidth = 0;
            frame.src = url;

            frame.style.position = 'absolute';
            frame.style.top = y+'px';
            frame.style.left = x+'px';

            frame.addEventListener('blur', All.hide, false);
            window.addEventListener('mousedown', All.hide, false);
        }

        if (!ie || ie7) {
            frame.style.border = '#888 solid 0px';
        }
        frame.style.height = All.height+'px';
        frame.style.width = All.width+'px';
        
        All.frame = frame;
    } else {
        All.frame.src = url;
        All.frame.style.top = y + 'px';
        All.frame.style.left = x + 'px';
        All.frame.style.display = '';
    }

    // Fixing up the position so no overflow is needed. We need to detect
    // whether we are under XHTML or HTML 4
    var b;
    if (ie) {
        b = document.documentElement.clientWidth ? document.documentElement :
            document.body;
    } else {
        b = (document.doctype && document.doctype.publicId && 
             (document.doctype.publicId.indexOf('XHTML') >= 0)) ?
            document.documentElement : document.body;
    }

    var dh = b.clientHeight;
    var dw = b.clientWidth;
    dh = (All.frame.offsetHeight+y) - dh;
    dw = (All.frame.offsetWidth+x)  - dw;

    All.frame.style.left = x-(dw>0?dw:0)+b.scrollLeft + 'px';
    All.frame.style.top  = y-(dh>0?dh:0)+b.scrollTop  + 'px';
};

All.onclick = function(e, passage) {
    if (!e) e = window.event;

    var x = e.clientX;
    var y = e.clientY;
    
   
    var ie7 = navigator.userAgent.toLowerCase().indexOf('msie 7') >= 0;   
    if (ie7) y += 15; 
    
    All.show(passage, x, y);
    
    //All.section = passage;

};

All.hide = function(event) {
    var f = All.frame;
    if (f)
        f.style.display = 'none';
};

function __decodeQS(qs) {
    var k, v, i1, i2, r = {};
    i1 = qs.indexOf('?');
    i1 = i1 < 0 ? 0 : i1 + 1;
    while ((i1 >= 0) && ((i2 = qs.indexOf('=', i1)) >= 0)) {
        k = qs.substring(i1, i2);
        i1 = qs.indexOf('&', i2);
        v = i1 < 0 ? qs.substring(i2+1) : qs.substring(i2+1, i1++);
        r[unescape(k)] = unescape(v);
    }
    return r;
}
