﻿Type.registerNamespace("Focus.Availability");

Focus.Availability.AvailabilityLookup = function(popupElement) {
     Focus.Availability.AvailabilityLookup.initializeBase(this, [popupElement]);

     this._chkOffice = null;
     this._chkIndustrial = null;
     this._chkRetail = null;
     this._txtLocation = null;
     this._cmdGetAvailable = null;

     this._txtLocationKeyUp = null;
     this._cmdGetAvailableClick = null;
     this._bodyMouseUp = null;
     this._windowResize = null;
}

Focus.Availability.AvailabilityLookup.prototype = {

    // Getter & Setter for the office checkbox.
    set_ChkOffice : function(value) {
        this._chkOffice = value;
    },
    get_ChkOffice : function() {
        return this._chkOffice;
    },

    // Getter & Setter for the industrial checkbox.
    set_ChkIndustrial : function(value) {
        this._chkIndustrial = value;
    },
    get_ChkIndustrial : function() {
        return this._chkIndustrial;
    },

    // Getter & Setter for the retail checkbox.
    set_ChkRetail : function(value) {
        this._chkRetail = value;
    },
    get_ChkRetail : function() {
        return this._chkRetail;
    },

    // Getter & Setter for the location textbox.
    set_TxtLocation : function(value) {
        this._txtLocation = value;
    },
    get_TxtLocation : function() {
        return this._txtLocation;
    },

    // Getter & Setter for the perform search button.
    set_CmdGetAvailable : function(value) {
        this._cmdGetAvailable = value;
    },
    get_CmdGetAvailable : function() {
        return this._cmdGetAvailable;
    },

    initialize : function() {
        if (this._txtLocationKeyUp === null) {
            this._txtLocationKeyUp = Function.createDelegate(this, this.txtLocationKeyUp);
        }
        Sys.UI.DomEvent.addHandler(this._txtLocation, 'keyup', this._txtLocationKeyUp);

        if (this._cmdGetAvailableClick === null) {
            this._cmdGetAvailableClick = Function.createDelegate(this, this.cmdGetAvailableClick);
        }
        Sys.UI.DomEvent.addHandler(this._cmdGetAvailable, 'click', this._cmdGetAvailableClick);

        if (this._bodyMouseUp === null) {
            this._bodyMouseUp = Function.createDelegate(this, this.closeAvailabilityPopup);
        }
        Sys.UI.DomEvent.addHandler(document.body, 'mouseup', this._bodyMouseUp);

        if (this._windowResize === null) {
            this._windowResize = Function.createDelegate(this, this.windowResize);
        }
        Sys.UI.DomEvent.addHandler(window, 'resize', this._windowResize);
        Sys.UI.DomEvent.addHandler(window, 'scroll', this._windowResize);

        Focus.Availability.AvailabilityLookup.callBaseMethod(this, 'initialize');
    },

    dispose : function() {
        if (this._txtLocationKeyUp) {
            Sys.UI.DomEvent.removeHandler(this._txtLocation, 'keyup', this._txtLocationKeyUp);
            delete this._txtLocationKeyUp;
        }
        if (this._cmdGetAvailableClick) {
            Sys.UI.DomEvent.removeHandler(this._cmdGetAvailable, 'click', this._cmdGetAvailableClick);
            delete this._cmdGetAvailableClick;
        }
        if (this._bodyMouseUp) {
            Sys.UI.DomEvent.removeHandler(document.body, 'mouseup', this._bodyMouseUp);
            delete this._bodyMouseUp;
        }
        if (this._windowResize) {
            Sys.UI.DomEvent.removeHandler(window, 'resize', this._windowResize);
            delete this._windowResize;
        }

        Focus.Availability.AvailabilityLookup.callBaseMethod(this, 'dispose');
    },

    txtLocationKeyUp : function(eventElement) {
        if (eventElement.charCode == 13 || eventElement.keyCode == 13) {
            this._doAvailabilitySearch();
            eventElement.stopPropagation();
        }
    },

    cmdGetAvailableClick : function(eventElement) {
        eventElement.stopPropagation();
        eventElement.preventDefault();    
        this._doAvailabilitySearch();
    },

    closeAvailabilityPopup : function(eventElement) {
        var element = this.get_element();
        if (element.style.display == 'block') {
            // we need to restore the watermark value to txtLocation && remove the avail count from element.
            var behaviours = Sys.UI.Behavior.getBehaviorsByType(this._txtLocation, AjaxControlToolkit.TextBoxWatermarkBehavior);
            if (eventElement.target == this._txtLocation) {
                this._txtLocation.value = '';
            }
            else if (behaviours.length == 1) {
                behaviours[0]._applyWatermark();
            }
            element.style.display = 'none';
        }
    },

    windowResize : function(eventElement) {
        var element = this.get_element();
        if (element.style.display == 'block') {
            this._layout();
        }
    },

    // Performs an availability search, updating element with the search results.
    _doAvailabilitySearch : function() {

        if (Page_BlockSubmit == false) {
            var containerElem = $get('content');
            var content =   '<img src="/images/public/loader.gif" />' +
                            '<div class="resultText">Please wait, searching...</div>';
            containerElem.innerHTML = content;
            this._layout();

            //var resultCntElem = $get('resultCount');
            //resultCntElem.innerHTML = '<img src="/images/public/loader.gif" alt="loading" />';

            // Fire off the call to the web service to get the availability count.
            Sys.WebForms.PageRequestManager.getInstance().abortPostBack();  // This will result in only one result count, even after multiple serial requests.
            Portal.ProspectHome.AvailLookup.GetAvailabilities(this._txtLocation.value, this._chkOffice.checked, this._chkIndustrial.checked, this._chkRetail.checked, this._availabilityResults);

            this.get_element().style.display = 'block';
        }
    },

    _availabilityResults : function(result, eventArgs) {
        var containerElem = $get('content');
        if (result > 0) {
            var content =   '<div class="twoColLeft">' +
                            '<div id="resultCount" class="resultCount yellow">' + result + '</div>' +
                            '<div class="resultText">available property listings found</div>' +
                            '</div>' +
                            '<div class="twoColRight">' +
                            '<div class="resultRightLarge yellow">Like this result?</div>' +
                            '<div class="resultRightSmall white">To access the UK\'s largest availability database:</div>' +
                            '<div class="goldBulletItem">Telephone <b>020 7009 2900</b></div>' +
                            '<div class="goldBulletItem">Email <a href="mailto:info@costar.co.uk" class="whiteAnchor"><b>info@costar.co.uk</b></a></div>' +
                            '<div class="goldBulletItem">Or fill in our <b>online enquiry form.</b></div>' +
                            '<a href="/index.aspx?fuseaction=public.requestInfo"><img src="/images/public/button_requestMoreInfo.gif" class="buttonSubscribeOnline" alt="Request More Info" /></a>' +
                            '</div>' +
                            '<img src="/images/1x1.gif" class="clearBoth" width="0" height="0" alt="" />';

            containerElem.innerHTML = content;
        } else {
            var content =   '<div class="noResultLeft">' +
                            '<div class="noResultText">Sorry, we currently don\'t have any space that matches your criteria.</div>' +
                            '</div>' +
                            '<div class="noResultRight">' +
                            '<div class="noSpaceItem resultRightLarge yellow">However...</div>' +
                            '<div class="noSpaceItem">our database grows every month and we are aiming for complete national coverage, so watch this space!</div><br />' +
                            '<div class="noSpaceItem">If you are looking for a town in Scotland please visit our sister website <a href="http://www.scottishproperty.com" target="_blank" class="whiteAnchor">www.scottishproperty.com</a></div>' +
                            '</div>' +
                            '<img src="/images/1x1.gif" class="clearBoth" width="0" height="0" alt="" />';
            containerElem.innerHTML = content;
        }
    },

    _layout : function() {
        var element = this.get_element();
        var clientBounds = $common.getClientBounds();
        var scrollLeft = (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
        var scrollTop = (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);

        element.style.left = ((clientBounds.width - 498) / 2 + scrollLeft) + 'px';
        element.style.top = ((clientBounds.height - 245) / 2 + scrollTop) + 'px';
    }
}
Focus.Availability.AvailabilityLookup.inheritsFrom(Sys.UI.Behavior);
Focus.Availability.AvailabilityLookup.registerClass('Focus.Availability.AvailabilityLookup', Sys.UI.Behavior);
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();