var DialogBox = Class.create({
    initialize: function() {
        this.form = null;
        this.currentZip = "";
    },

    showOverlay: function(message) {
        //Show the translucent document overlay
        var myHeight = $$('body').reduce().getHeight();
        if (myHeight < document.viewport.getHeight()) {
            myHeight = document.viewport.getHeight();
        }
        var overlay = new Element('div', {id: 'overlay'}).setOpacity(0.0).setStyle({height: myHeight + "px"});
        document.body.insert({bottom: overlay});
        new Effect.Appear('overlay', { duration: 0.4, from: 0.0, to: 0.8 });

        /* Create the elements for the dialog box */
        var tlc = new Element("div", {'class': 'tlc'});
        var trc = new Element("div", {'class': 'trc'});
        var tb = new Element("div", {'class': 'tb'}).update(tlc);
        var lbLoading = new Element("div", {id: 'lightBox-loading'});
        var rightSide = new Element("div", {'class': 'rs'});
        var closeButton = new Element("div", {'class': 'lightBox-closebutton'}).observe('click', this.hideOverlay);
        var lbClose = new Element("div", {id: "lightBox-close"}).update(closeButton).hide();
        var putStuffHere = new Element("div", {'class': 'putStuffHere', id: "dialogbox-content"}).update(lbLoading).insert({bottom: lbClose});
        var content = new Element("div", {'class': 'lbcontent'}).update(rightSide).insert({bottom: putStuffHere});
        var blc = new Element("div", {'class': 'blc'});
        var brc = new Element("div", {'class': 'brc'});
        var bb = new Element("div", {'class': 'bb'}).update(blc);

        //Create the inital lightBox with a close link
        var lightBox = new Element("div", {id: "lightBox"}).hide();
        var offsets = document.viewport.getScrollOffsets();
        if (offsets.top > 150) {
            lightBox.setStyle({top: offsets.top + 75 + "px"});
        }
        var lbContent = new Element("div", {id: 'lightBox-content'});

        //if there's a loading message, insert it
        if (message) {
            lbLoading.update(new Element("p", {'class': 'loading-message'}).update(message));
        }

        /* Build the dialog box */
        lbContent.insert({bottom: trc}).insert({bottom: tb}).insert({bottom: content}).insert({bottom: brc}).insert({bottom: bb});

        /* Put the dialog into the lightbox */
        lightBox.insert({top: lbContent});

        /* Show the lightbox */
        $('overlay').insert({after: lightBox});
        new Effect.Appear('lightBox', { duration: 0.4, from: 0.0, to: 1.0 });
    },

    fillOverlay: function(width, height) {
        if (height < 125) {
            height = 125;
        }
        new Effect.Morph('lightBox-content', {style:'height: ' + height + 'px;', duration: 0.5});
        new Effect.Morph('lightBox-content', {style:'width: ' + width + 'px;', duration: 0.5, delay: 0.3});
        new Effect.Fade('lightBox-loading', {duration: 0.4, from: 1.0, to: 0.0});
        new Effect.Appear('lightBox-close', {duration: 0.4, from: 0.0, to: 1.0, delay: 0.8});
    },

    resetOverlay: function() {
        new Effect.Fade('lightBox-close', {duration: 0.4, from: 1.0, to: 0.0});
        new Effect.Opacity('dialogbox-content', {from: 1.0, to: 0.0, duration: 0.4, afterFinish: function() {
            $('dialogbox-content').update($('lightBox-loading')).setOpacity(1.0);
            new Effect.Appear('lightBox-loading', {duration: 0.4, from: 0.0, to: 1.0});
            new Effect.Morph('lightBox-content', {style:'height: 125px;', duration: 0.5});
            new Effect.Morph('lightBox-content', {style:'width: 225px;', duration: 0.5, delay: 0.3});
        }
        });
    },

    hideOverlay: function() {
        new Effect.Fade('overlay', {duration: 0.4, from: 0.8, to: 0.0});
        new Effect.Fade('lightBox', {duration: 0.4, from: 1.0, to: 0.0, afterFinish: function() {
            $('overlay').remove();
            $('lightBox').remove();
        }});
    },

    genericError: function(msg, dialogExists) {
        if (!dialogExists) {
            this.showOverlay();
        }
        this.fillOverlay(300, 150);
        var message = new Element("p", {'class': 'generic-error', id: 'dealer-error'}).update(msg).hide();
        $('dialogbox-content').insert({top: message});
        new Effect.Appear('dealer-error', {duration: 0.4, from: 0.0, to: 1.0, delay: 0.8});
    },

    requestZip: function(e, msgB, msgH) {
        var zipCookie = this.zipCheckForCookie();
        if (zipCookie) {
            this.currentZip = zipCookie;
        }

        var zipCookie = this.zipCheckForCookie();
        if (zipCookie) {
            this.currentZip = zipCookie;
        }
        
        var hideOverlay = this.hideOverlay.bindAsEventListener(this);

        this.showOverlay();
        var form = new Element("form", {id: "quickZip"}).hide();
        var label = new Element("label", {id: 'zipEntry-label', 'for': 'zipEntry'}).update("ZIP: ");
        var box = new Element("input", {type: 'text', name: 'zipEntry', id: 'zipEntry', maxlength: '5', value: this.currentZip});
        var button = new Element("button", {type: 'button', 'class': 'ok'}).update("Ok");
        var cancelButton = new Element("div", {'class': 'cancel', id: 'zip-cancel'}).update("Cancel").observe("click", hideOverlay);
        var boxFix = new Element("div").setStyle({'overflow': 'auto'}).insert({top: label}).insert({bottom: box}).insert({bottom: button});
        var block = new Element("div").insert({bottom: boxFix});
        var message = new Element("p", {id: 'zipMsg'}).update(msgB);
        var heading = null;
        if (msgH) {
            heading = new Element("h3", {id: 'zipHead'}).update(msgH);
            form.insert({bottom: heading});
        }

        var populateZip = this.checkZip.bindAsEventListener(this);
        form.observe('submit', populateZip);
        button.observe('click', populateZip);

        form.insert({bottom: message}).insert({bottom: block});
        this.fillOverlay(300, 180);
        $('dialogbox-content').insert({top: form});
        $('lightBox-close').update(cancelButton);
        new Effect.Appear('quickZip', {duration: 0.4, from: 0.0, to: 1.0, delay: 0.8});
    },

    checkZip: function(e) {
        var el = null;
        if (e.type == "click") {
            el = e.element().up().up().up();
        } else {
            e.stop();
            /* Need to stop the event so the form doesn't try to submit itself--it's not intended to */
            if (e.currentTarget) {
                el = $(e.currentTarget);
            } else {
                el = e.element();
            }
        }

        var field = el.serialize(true);
        var zipFormat = this.zipValidateFormat(field.zipEntry);
        if (zipFormat) {
            this.zipValidateWithMarket(field.zipEntry);
        } else {
            this.zipShowError();
            return false;
        }
        return this.currentZip;
    },

    zipCheckForCookie: function() {
        var c = document.cookie.split(';');
        var cLength = c.length;
        var cName = 'USER';
        for (var i = 0; i < cLength; i++) {
            var thisC = c[i];
            if (thisC.include(cName)) {
                return this.zipGetFromCookie(thisC);
            }
        }
        return null;
    },

    zipGetFromCookie: function(cookie) {
        var c = cookie.split('=');
        var cValue = unescape(c[1]).split('=')[1];
        return cValue;
    },

    zipValidateFormat: function(address) {
        var zipRegex = '[0-9][0-9][0-9][0-9][0-9]';
        if (address.length < 5 || !address.match(zipRegex) || address == 00000) {
            return false;
        }
        return true;
    },

    zipValidateWithMarket: function(address) {
        var callback = function (data) {
            var zip = data.zipCode;
            if (!zip || zip == "") {
                this.zipShowError();
                return false;
            }
            document.cookie = 'USER=' + zip + '; path=/';
            if(this.selectQuotableDealers){
                this.selectQuotableDealers(zip);
            }
            if(this.updateAds){
                this.updateAds(zip);
            }
            this.hideOverlay();
            if(this.form){
                if($('address')){
                    $('address').writeAttribute({'value':zip});
                }
                this.form.submit();
            }
        }.bind(this);
        MarketManager.getMarketByZipCode(address, callback);
    },

    zipShowError: function() {
        var zipError = $('zipError');
        if (zipError) {
            zipError.hide();
        } else {
            var el = $('quickZip');
            var error = new Element('p', {id: 'zipError'}).update("Please enter a valid 5-digit US ZIP code.<br/>").hide();
            var zipLink = new Element('a', {href: 'http://zip4.usps.com/zip4/welcome.jsp', target: "_blank"}).update('USPS ZIP Code Lookup');
            error.insert({bottom: zipLink});
            el.insert({bottom: error});
            el.setStyle({'marginBottom': '10px'});
        }
        new Effect.Appear('zipError', {duration: 0.4, from: 0.0, to: 1.0, delay: 0.8});
    },

    wOpen: function(e, redirectUrl) {
        var myWin = window.open(e.element().href, 'atc_popup', 'width=780, height=650, scrollbars=yes');
        e.stop();
        myWin.focus();
        if (redirectUrl) {
            window.location = redirectUrl;
        }
    },

    populateDialog: function() {
        /* Default function for populating the dialog--should be extended by the subclass. */
    },

    showMoreInfo: function(e) {
        e.stop();
        this.showOverlay();
        var el = $(e.element().next('div').cloneNode(true));
        $('dialogbox-content').insert({top: el});
        var height = this.getHeightEstimate(el) + 95;
        this.fillOverlay(500, height);
        $('lightBox-close').setStyle({'marginTop': 0});
        new Effect.Appear(el, {from: 0.0, to: 1.0, duration: 0.4, delay: 0.8});
    },

    getHeightEstimate: function(el) {
        var infoOptions = el.select('p');
        var height = 0;
        infoOptions.each(function(p) {
            var infoItem = p.down('span');
            if (infoItem.firstChild) {
                if (infoItem.firstChild.length > 630) {
                    height += 160;
                } else if (infoItem.firstChild.length > 560) {
                    height += 145;
                } else if (infoItem.firstChild.length > 490) {
                    height += 130;
                } else if (infoItem.firstChild.length > 420) {
                    height += 115;
                } else if (infoItem.firstChild.length > 350) {
                    height += 100;
                } else if (infoItem.firstChild.length > 280) {
                    height += 85;
                } else if (infoItem.firstChild.length > 210) {
                    height += 70;
                } else if (infoItem.firstChild.length > 140) {
                    height += 55;
                } else if (infoItem.firstChild.length > 70) {
                    height += 40;
                } else {
                    height += 30;
                }
            } else {
                height += 30;
            }

            if (height > 280) {
                height = 280;
                throw $break;
            }
        });
        return height;
    },

    throwAsis: function(code) {
        var myImg = new Image();
        myImg.src = "http://" + window.location.host + code;
    }

});