/* check for a product list before we display the dialogbox */
(function() {
	if( typeof nbn_product_list !== "undefined" && nbn_product_list.length > 0 ) {
        var NBN_WIDGET_VERSION = "1.5.0";

	    var NBN_WIDGET = {
            uri: "/capr?productid=",
            runtime: {},
            apikey: "09aced66-b224-4c4e-8753-0e8f31d6654a",
            hostname: "http://widget.nearbynow.com",
            ie6: (window.ActiveXObject && !window.XMLHttpRequest) ? true:false,
            _DialogClass: function() {
                var _me = this;
                var d = document;
                var w = window;
                var nw = NBN_WIDGET;
                var modalOn = (typeof nbn_modal_on === "boolean"?nbn_modal_on:true);

                this.init = function() {
                    if( d.getElementById("nbn_widget_dialog") === null ) {
                        _me.build();
                        nw.addEvent(d.getElementById('nbn_widget_dialog_closelink'), 'click', _me.hide);
                    }
                };
                this.build = function() {
                    var dBoxEle = d.createElement("div");
                    dBoxEle.id = "nbn_widget_dialog";
                    dBoxEle.className = "nbn-dialog";
                    dBoxEle.style.display = "none";

                    //dialog header elements
                    var dBoxHeaderEle = d.createElement("div");
                    dBoxHeaderEle.id = "nbn_widget_dialog_header";
                    dBoxHeaderEle.className = "nbn-dialogHeader";

                    var dBoxHeaderMainEle = d.createElement("div");
                    dBoxHeaderMainEle.className = "nbn-dialogHeaderMain";
                    dBoxHeaderMainEle.innerHTML = '<h4 class="nbn-dialogHeaderText">' + (typeof nbn_title === 'string'?nbn_title:'Local Product Finder') + '</h4>';
                    
                    var dBoxCloseLinkEle = d.createElement("a");
                    dBoxCloseLinkEle.id = "nbn_widget_dialog_closelink";
                    dBoxCloseLinkEle.className = "nbn-dialogCloseLink";
                    dBoxCloseLinkEle.href = "#";
                    dBoxCloseLinkEle.innerHTML = '<span class="nbn-dialogCloseText">Close</span>';

                    dBoxHeaderMainEle.appendChild(dBoxCloseLinkEle);
                    dBoxHeaderEle.appendChild(dBoxHeaderMainEle);

                    dBoxEle.appendChild(dBoxHeaderEle);
                    
                    //dialog body element
                    var dBoxContentEle = d.createElement("div");
                    dBoxContentEle.id = "nbn_widget_dialog_content";
                    dBoxContentEle.className = "nbn-dialogBody";

                    dBoxEle.appendChild(dBoxContentEle);
                    _me.element = d.body.appendChild(dBoxEle);
                };
                this.getIframeObj = function(id) {
                    return d.getElementById(id);
                };
                this.getIframeDoc = function(iframeObj) {
                    var iframeDoc = iframeObj.contentWindow || iframeObj.contentDocument;
                    if( iframeDoc.document )
                        iframeDoc = iframeDoc.document
                    return iframeDoc;
                };
                this.update = function(url) {
                    var dContentEle = d.getElementById("nbn_widget_dialog_content");
                    var iframeObj = d.getElementById("nbn_widget_iframe");
                    if( iframeObj ) {
                        dContentEle.removeChild(iframeObj);
                    }
                    var new_iframeObj = nw.createIframe("nbn_widget_iframe");
                    dContentEle.appendChild(new_iframeObj);

                    var iframeDoc = _me.getIframeDoc(new_iframeObj);
                    iframeDoc.location.replace(url);
                };
                this.getProductid = function(evt) {
                    var ele = nw.getEvtElement(evt);
                    if( ele.tagName == 'a' ) 
                        return ele._nbn_productid;

                    return ele.parentNode._nbn_productid;
                };
                this.show = function(evt) {
                    nw.stopEvent(evt);
                    _me.update(nw.hostname + nw.uri + _me.getProductid(evt) + nw.stylingConfigs() + nw.buttonConfigs() + "&apikey=09aced66-b224-4c4e-8753-0e8f31d6654a");
                    _me.center();
                    if( modalOn === true )
                        _me.showModal();
                    _me.element.style.visibility = "visible";
                    _me.element.style.display = "block";
                };
                this.hide = function(evt) {
                    nw.stopEvent(evt);
                    if( modalOn === true )
                        _me.hideModal();
                    _me.element.style.display = "none";
                };
                this.center = function() {
                    var ds = _me.element.style;
                    
                    //need to do this so that we could get the dialog and window size
                    ds.visibility = 'hidden';
                    ds.display = 'block';
                    
                    var dw = _me.element.offsetWidth;
                    var scrolltop = window.pageYOffset||d.documentElement.scrollTop;
                    var scrollleft = window.pageXOffset||document.body.scrollLeft;
                    var ww = nw.width();
                    
                    var top = scrolltop + 25;
                    if( top < 0 ) top = 25;

                    var left = (ww - dw)/2 + scrollleft;
                    if( left < 0 )
                        left = 0;

                    ds.top = top + 'px';
                    ds.left = left + 'px';                     
                };
                this.hideModal = function() {
                    if( nw.runtime.modalizer ) {
                        nw.runtime.modalizer.style.display = "none";
                        if(nw.ie6) {
                            var sb = document.getElementsByTagName('select');
                            if( !sb ) return;

                            for(var i=0; i < sb.length; i++) {
                                sb[i].style.visibility = "visible";
                            }
                        }
                    }
                };
                this.showModal = function() {
                    if( !nw.runtime.modalizer ) {
                        var overlay = d.createElement('div');
                        overlay.id = "nbn_widget_modalizer_overlay";
                        overlay.className = "nbn-dialogModalizer";

                        if(!overlay.currentStyle||overlay.currentStyle.hasLayout) overlay.style.zoom = 1;   //opera
                        if(window.ActiveXObject) overlay.style.filter = "alpha(opacity=80)";   //ie6
                        else overlay.style.opacity = "0.8";  //rest

                        d.body.appendChild(overlay);
    
                        //nw.addEvent(overlay, 'click', _me.hide);
                        nw.runtime.modalizer = overlay;
                    }
                    var modalizer = nw.runtime.modalizer;
                    modalizer.style.width = nw.scrollWidth() + 'px';
                    modalizer.style.height = nw.scrollHeight() + 'px';
                    modalizer.style.display = "block";

                    if(nw.ie6) {
                        var sb = document.getElementsByTagName('select');
                        if( !sb ) return;

                        for(var i=0; i < sb.length; i++) {
                            sb[i].style.visibility = "hidden";
                        }
                    }
                };
            },
            createIframe: function(id) {
                var iframeEle = document.createElement("iframe");
                iframeEle.id = id;
                iframeEle.name = id;
                iframeEle.scrolling = "no";
                iframeEle.frameBorder = "0";
                iframeEle.marginWidth = "0";
                iframeEle.marginHeight = "0";
                iframeEle.width = "786px";
                iframeEle.height = "705px";
                
                var ifs = iframeEle.style;
                ifs.border = "0px";
                return iframeEle;
            },
            scrollWidth: function() {
                return (d.compatMode != 'CSS1Compat') ? d.body.scrollWidth : d.documentElement.scrollWidth;
            },
            scrollHeight: function() {
                return (d.compatMode != 'CSS1Compat') ? d.body.scrollHeight : d.documentElement.scrollHeight;
            },
            width: function() {
                if (typeof window.innerWidth != 'undefined') return window.innerWidth;
                if (typeof document.documentElement != 'undefined'
                    && typeof document.documentElement.clientWidth != 'undefined'
                    && document.documentElement.clientWidth != 0 ) return document.documentElement.clientWidth;
                return document.body.clientWidth;
            },
            init: function() {
                this.addEvent(window, 'load', this.load);
                this.addEvent(window, 'unload', this.unload);
            },
            stopEvent: function(evt) {
                if( !evt ) evt = window.event;
                if( evt.stopPropagation ) evt.stopPropagation();
                else evt.cancelBubble = true;

                if( evt.preventDefault ) evt.preventDefault();
                else evt.returnValue = false;
            },
            getEvtElement: function(evt) {
                if(evt.target) {
                    return (evt.target.nodeType == 3) ? evt.target.parentNode : evt.target;

                } else if( window.event ) {
                    return window.event.srcElement;
                }
                return null;
            },
            addEvent: function(ele, type, func) {
                if(ele.addEventListener) {
                    ele.addEventListener(type, func, false);

                } else if(ele.attachEvent) {
                    ele.attachEvent("on" + type, func);

                } else {
                    ele['on'+type] = func;
                }
            },
            load: function() {
                NBN_WIDGET.runtime.dialog = new NBN_WIDGET._DialogClass();
                NBN_WIDGET.runtime.dialog.init();

                //add product button events    
                var d = document;
                for(var i=0; i < nbn_product_list.length; i++) {
                    if( nbn_product_list[i].btn ) {
                        var btnEle = d.getElementById(nbn_product_list[i].btn);
                        if( btnEle && nbn_product_list[i].productid ) {
                            btnEle._nbn_productid = nbn_product_list[i].productid;
                            NBN_WIDGET.addEvent(btnEle, 'click', NBN_WIDGET.runtime.dialog.show);
                        }
                    }
                }
            },
            unload: function() {
                //remove dialog and other dom objects
                //detach product button events
            },
            destroy: function() {
                //empty out elements and removes it from DOM
            },
            setContentStyles: function(styleObject) {
                this.contentStyles = styleObjects;
            },  
            stylingConfigs: function() {
                if(typeof this.contentStyles == 'undefined')
                    this.contentStyles = (typeof nbn_dialog_content_styles == 'object'?nbn_dialog_content_styles:null);
                    
                if(this.contentStyles == null)   return '';
                
                var configs = '';
                var style = this.contentStyles;
                if(style['background_color']) configs += ('&style.container_bgcolor='+style['background_color']);
                if(style['link_color']) configs += ('&style.link_color='+style['link_color']);
                if(style['text_color']) configs += ('&style.text_color='+style['text_color']);
                if(style['formlabel_color']) configs += ('&style.formlabel_color='+style['formlabel_color']);
                if(style['pagetitle_color']) configs += ('&style.pagetitle_color='+style['pagetitle_color']);
                return configs;
            },
            setButtons: function(btnsObject) {
                this.buttonConfigs = btnsObject;
            },
            buttonConfigs: function() {
                if(typeof this.buttonConfigs == 'undefined')
                    this.buttonConfigs = (typeof nbn_dialog_buttons != 'object'?nbn_dialog_buttons:null);
                    
                if(this.buttonConfigs == null)   return '';
                 
                var configs = '';
                var btns = this.buttonConfigs;
                if(btns['change_location']) configs += ('&btn.change_loc='+btns['change_location']);
                if(btns['in_stock']) configs += ('&btn.in_stock='+btns['in_stock']);
                if(btns['send_request']) configs += ('&btn.send_request='+btns['send_request']);
                if(btns['find_nearby']) configs += ('&btn.find_nearby='+btns['find_nearby']);
                return configs;
            },
            renderDialogCSS: function() {
                this.dialogCSS = (typeof nbn_dialog_css == 'object'?nbn_dialog_css:null);
                    
                if(this.dialogCSS == null)   return '';
                var css_keys = ['.nbn-dialogHeader', '.nbn-dialogHeaderText', '.nbn-dialogBody', '.nbn-dialogContent', '.nbn-dialogCloseLink','.nbn-dialogCloseText'];
                for(var i=0; i < css_keys.length; i++) {
                    if(typeof this.dialogCSS[css_keys[i]] != 'undefined') {
                        if(css_keys[i] == '.nbn-dialogContent') {
                            //legacy support - lucky web site.
                            d.write('#nbn_widget_dialog .nbn-dialogBody {' + this.dialogCSS[css_keys[i]] + '}');
                        } else {
                            d.write('#nbn_widget_dialog ' + css_keys[i] + '{' + this.dialogCSS[css_keys[i]] + '}');
                        }//endif
                    }//endif
                }//endfor
            }
        };
	    NBN_WIDGET.init();

	    var d = document;
        d.write('<style type="text/css">');
        d.write('.nbn-dialog DIV, .nbn-dialogHeaderText { margin: 0; padding: 0; }');
        d.write('.nbn-dialog IMG { border: 0; }');
        d.write('.nbn-dialog { position: absolute;top:0;left:0;z-index:100001;font-family:Arial, sans-serif;}');
        d.write('.nbn-dialogHeader, .nbn-dialogHeaderMain: { line-height: 0; }');
        d.write('.nbn-dialogModalizer {position: absolute;padding:0;margin:0;top:0;left:0;width:100%;height:100%;z-index:1000;background-color:#666;}');
        d.write('.nbn-dialogCloseLink { position: absolute; display:block; right: 10px; top: 6px; background: transparent no-repeat; }');
        d.write('.nbn-dialogBody { background-color: #FFF; }');
        d.write('#nbn_widget_dialog .nbn-dialogCloseLink { text-decoration: underline; }');
        d.write('.nbn-dialogHeaderText {padding: 6px 10px 1px 10px; font-size: 16px; color: #FFF;}');
        d.write('.nbn-dialogBody { border: 5px solid #369; }');
        d.write('.nbn-dialogHeader { background-color: #369;}');
        d.write('#nbn_widget_dialog .nbn-dialogCloseLink { color:#FFF; font-size: 14px; }');
        NBN_WIDGET.renderDialogCSS();

        if(typeof nbn_title_color === 'string') d.write('.nbn-dialogHeaderText { color:'+nbn_title_color+'; }');
        if(typeof nbn_border_color === 'string') d.write('.nbn-dialogBody { border: 5px solid '+nbn_border_color+'; }');
        if(typeof nbn_header_color === 'string') d.write('.nbn-dialogHeader { background-color:'+nbn_header_color+'; }');
        if(typeof nbn_modal_color === 'string') d.write('.nbn-dialogModalizer { background-color:'+nbn_modal_color+'; }');
        d.write('</style>');
	}
})();
