﻿// README
//
// There are two steps to adding a property:
//
// 1. Create a member variable to store your property
// 2. Add the get_ and set_ accessors for your property.
//
// Remember that both are case sensitive!
//

Type.registerNamespace('SS_AjaxPopupValidator');

SS_AjaxPopupValidator.SS_AjaxPopupValidatorBehavior = function(element) {

    SS_AjaxPopupValidator.SS_AjaxPopupValidatorBehavior.initializeBase(this, [element]);

    this._closeImageUrl = null;
    this._containerDiv = null;
    this._highlightCssClass = null;
    this._width = '180px';
    this._invalid = false;
    this._originalValidationMethod = null;
    this._validationMethodOverride = null;
    this._elementToValidate = null;
    this._closeImage = null;
    this._popupBehavior = null;
    this._onShowJson = null;
    this._onHideJson = null;
    this._focusAttached = false;
    this._isOpen = false;
    this._focusHandler = Function.createDelegate(this, this._onfocus);
    this._closeClickHandler = Function.createDelegate(this, this._oncloseClick);
}

SS_AjaxPopupValidator.SS_AjaxPopupValidatorBehavior.prototype = {
    initialize : function() {
        SS_AjaxPopupValidator.SS_AjaxPopupValidatorBehavior.callBaseMethod(this, 'initialize');

        var elt = this.get_element();
        //               
        // Override the evaluation method of the current validator
        //
        if (elt.evaluationfunction) {
            this._originalValidationMethod = Function.createDelegate(elt, elt.evaluationfunction);
            this._validationMethodOverride = Function.createDelegate(this, this._onvalidate);
            elt.evaluationfunction = this._validationMethodOverride;            
        }
        //
        // create the DOM elements
        //
        var elementToValidate = this._elementToValidate = $get(elt.controltovalidate);
        var containerDiv = this._containerDiv = document.createElement("div");
        var divMessageContainer = document.createElement("div");
        var divTop = document.createElement("div");
        var divBottom = document.createElement("div");
        var divMessage = document.createElement("div");
        var divArrow = document.createElement("div");
        var divMessage = document.createElement("div");
        var closeImage = this._closeImage = document.createElement("img");
        
        //containers
        containerDiv.className = 'VCEPopupContainer';
        containerDiv.style.display = 'none';
        
        divTop.className = 'VCETop';
        divBottom.className = 'VCEBottom';
        divMessage.className = 'VCEMessage';
        divArrow.className = 'VCEArrowContainer';
        divMessageContainer.className = 'VCEMessageContainer';
      
        divMessage.innerHTML = this._getErrorMessage();
	    
        closeImage.src = this.get_closeImageUrl();
        closeImage.style.cursor = 'pointer';
        //
        // Create the DOM tree
        //
        elt.parentNode.appendChild(containerDiv)
        containerDiv.appendChild(divTop);
        containerDiv.appendChild(divMessageContainer);
        containerDiv.appendChild(divBottom);
        divMessageContainer.appendChild(divArrow);
        divMessageContainer.appendChild(divMessage);
        
        //
        // initialize behaviors
        //
        this._popupBehavior = $create(
            AjaxControlToolkit.PopupBehavior, 
            { 
                positioningMode : AjaxControlToolkit.PositioningMode.Absolute,
                parentElement : elementToValidate
            }, 
            { }, 
            null,
            this._containerDiv);
        
        // Create the animations (if they were set before initialize was called)
        if (this._onShowJson) this._popupBehavior.set_onShow(this._onShowJson);
        if (this._onHideJson) this._popupBehavior.set_onHide(this._onHideJson);
        
        $addHandler(this._closeImage, "click", this._closeClickHandler);
    },

    dispose : function() {
        this.hide();
        
        if (this._focusAttached) {
            $removeHandler(this._elementToValidate, 'focus', this._focusHandler);
            this._focusAttached = false;
        }
        
        $removeHandler(this._closeImage, 'click', this._closeClickHandler);
        
        this._onShowJson = null;
        this._onHideJson = null;
        if (this._popupBehavior) {
            this._popupBehavior.dispose();
            this._popupBehavior = null;
        }
        if (this._closeBehavior) {
            this._closeBehavior.dispose();
            this._closeBehavior = null;
        }
        if (this._containerDiv) {
            this._containerDiv.parentNode.removeChild(this._containerDiv);
            this._containerDiv = null;
            this._closeImage = null;
        }

        SS_AjaxPopupValidator.SS_AjaxPopupValidatorBehavior.callBaseMethod(this, 'dispose');
    },

    _getErrorMessage : function() {
        return this.get_element().errormessage || AjaxControlToolkit.Resources.ValidatorCallout_DefaultErrorMessage;
    },
    
    show : function(force) {        
        if (force || !this._isOpen) {
            this._isOpen = true;
            if (force && SS_AjaxPopupValidator.SS_AjaxPopupValidatorBehavior._currentCallout) SS_AjaxPopupValidator.SS_AjaxPopupValidatorBehavior._currentCallout.hide();
            if (SS_AjaxPopupValidator.SS_AjaxPopupValidatorBehavior._currentCallout != null) return;
            
            SS_AjaxPopupValidator.SS_AjaxPopupValidatorBehavior._currentCallout = this;        
            
            this._popupBehavior.set_x(($common.getSize(this._elementToValidate).width) + 5);
            this._popupBehavior.set_y(-18);
            this._popupBehavior.show();
            
            window.scroll(0, 0);
        }
    },
    
    hide : function() {
        if (SS_AjaxPopupValidator.SS_AjaxPopupValidatorBehavior._currentCallout == this) SS_AjaxPopupValidator.SS_AjaxPopupValidatorBehavior._currentCallout = null;
        
        if (this._isOpen || $common.getVisible(this._containerDiv)) {
            this._isOpen = false;
            this._popupBehavior.hide();
        }
    },

    _onfocus : function(e) {
        if (!this._originalValidationMethod(this.get_element())) {
            if (this._highlightCssClass) Sys.UI.DomElement.addCssClass(this._elementToValidate, this._highlightCssClass);
            this.show(true);
            return false;
        }
        else {
            this.hide();
            return true;
        }
    },
    
    _oncloseClick : function(e) {
        this.hide();
    },
    
    _onvalidate : function(val) {
        if (!this._originalValidationMethod(val)) {
            if (this._highlightCssClass) Sys.UI.DomElement.addCssClass(this._elementToValidate, this._highlightCssClass);
            if (!this._focusAttached) {
                $addHandler(this._elementToValidate, 'focus', this._focusHandler);
                this._focusAttached = true;
            }
            this.show(false);
            this._invalid = true;
            return false;
        }
        else {
            if (this._highlightCssClass && this._invalid) Sys.UI.DomElement.removeCssClass(this._elementToValidate, this._highlightCssClass)
            this._invalid = false;
            this.hide();
            return true;
        }
    },
    
    get_onShow : function() {
        /// <value type="String" mayBeNull="true">
        /// Generic OnShow Animation's JSON definition
        /// </value>
        return this._popupBehavior ? this._popupBehavior.get_onShow() : this._onShowJson;
    },
    set_onShow : function(value) {
        if (this._popupBehavior) this._popupBehavior.set_onShow(value)
        else this._onShowJson = value;
        this.raisePropertyChanged('onShow');
    },
    get_onShowBehavior : function() {
        /// <value type="AjaxControlToolkit.Animation.GenericAnimationBehavior">
        /// Generic OnShow Animation's behavior
        /// </value>
        return this._popupBehavior ? this._popupBehavior.get_onShowBehavior() : null;
    },
    onShow : function() {
        /// <summary>
        /// Play the OnShow animation
        /// </summary>
        /// <returns />
        if (this._popupBehavior) this._popupBehavior.onShow();
    },
        
    get_onHide : function() {
        /// <value type="String" mayBeNull="true">
        /// Generic OnHide Animation's JSON definition
        /// </value>
        return this._popupBehavior ? this._popupBehavior.get_onHide() : this._onHideJson;
    },
    set_onHide : function(value) {
        if (this._popupBehavior) this._popupBehavior.set_onHide(value);
        else this._onHideJson = value;
        this.raisePropertyChanged('onHide');
    },
    get_onHideBehavior : function() {
        /// <value type="AjaxControlToolkit.Animation.GenericAnimationBehavior">
        /// Generic OnHide Animation's behavior
        /// </value>
        return this._popupBehavior ? this._popupBehavior.get_onHideBehavior() : null;
    },
    onHide : function() {
        /// <summary>
        /// Play the OnHide animation
        /// </summary>
        /// <returns />
        if (this._popupBehavior) this._popupBehavior.onHide();
    },
    
    get_closeImageUrl : function() {
        return this._closeImageUrl;
    },
    set_closeImageUrl : function(value) {
        if (this._closeImageUrl != value) {
            this._closeImageUrl = value;
            if (this.get_isInitialized()) this._closeImage.src = value;
            this.raisePropertyChanged('closeImageUrl');
        }
    },
        
    get_width : function() {
        return this._width;
    },
    set_width : function(value) {
        if (this._width != value) { 
            this._width = value;
            if (this.get_isInitialized()) this._popupTable.style.width = _width;
            this.raisePropertyChanged('width');
        }
    },
    
    get_highlightCssClass : function() {
        return this._highlightCssClass;
    },
    set_highlightCssClass : function(value) {
        if (this._highlightCssClass != value) {
            this._highlightCssClass = value;
            this.raisePropertyChanged('highlightCssClass');
        }
    },
    
    get_isOpen : function() {
        return this._isOpen;
    }
}

SS_AjaxPopupValidator.SS_AjaxPopupValidatorBehavior.registerClass('SS_AjaxPopupValidator.SS_AjaxPopupValidatorBehavior', AjaxControlToolkit.BehaviorBase);

if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();