﻿/*********************************************************************
 *  Copyright � 2008 UniteU Technologies Inc. 
 *  All rights are reserved. Reproduction or transmission in whole or
 *  in part, in any form or by any means, electronic, mechanical or 
 *  otherwise, is prohibited without the prior written consent of 
 *  the copyright owner.
 *
 *	UniteU eStore Platform 
 *  
 *  DropDownAttributeSelector.js
 *  
 * *******************************************************************/

// register the namespace
Type.registerNamespace('UniteU.FrontEnd.LayoutControls.SkuSelection');

// define the client class
UniteU.FrontEnd.LayoutControls.SkuSelection.DropDownAttributeSelector = function(element) {

    UniteU.FrontEnd.LayoutControls.SkuSelection.DropDownAttributeSelector.initializeBase(this, [element]);
    this._selectedIndexChangedHandler = null;
    this._selectedIndex = null;
    this._selectedValue = null;
    this._dropdownId = null;
    this._attributeLabel = null;
    this._skuSelectorControl = null;
    this._constraintChangedDelegate = null;
    this._enabledCssClass = null;
    this._disabledCssClass = null;
    this._addDefaultListItem = null;
    this._disabledValuesSelectable = null;
}

UniteU.FrontEnd.LayoutControls.SkuSelection.DropDownAttributeSelector.prototype = {

    // properties

    get_selectedIndex: function() {
        return this._selectedIndex;
    },

    set_selectedIndex: function(value) {
        if (this._selectedIndex !== value) {
            this._selectedIndex = value;
        }
    },

    get_selectedValue: function() {
        return this._selectedValue;
    },

    set_selectedValue: function(value) {
        if (this._selectedValue !== value) {
            this._selectedValue = value;
        }
    },

    get_dropdownId: function() {
        return this._dropdownId;
    },

    set_dropdownId: function(value) {
        if (this._dropdownId !== value) {
            this._dropdownId = value;
        }
    },

    get_attributeLabel: function() {
        return this._attributeLabel;
    },

    set_attributeLabel: function(value) {
        this._attributeLabel = value;
    },

    get_skuSelectorControl: function() {
        return this._skuSelectorControl;
    },

    set_skuSelectorControl: function(value) {
        this._skuSelectorControl = value;
    },

    get_enabledCssClass: function() {
        return this._enabledCssClass;
    },

    set_enabledCssClass: function(value) {
        this._enabledCssClass = value;
    },

    get_disabledCssClass: function() {
        return this._disabledCssClass;
    },

    set_disabledCssClass: function(value) {
        this._disabledCssClass = value;
    },

    get_addDefaultListItem: function() {
        return this._addDefaultListItem;
    },

    set_addDefaultListItem: function(value) {
        this._addDefaultListItem = value;
    },

    get_disabledValuesSelectable: function() {
        return this._disabledValuesSelectable;
    },

    set_disabledValuesSelectable: function(value) {
        this._disabledValuesSelectable = value;
    },


    // public events
    add_selectedIndexChanged: function(handler) {

        this.get_events().addHandler('selectedIndexChanged', handler);
    },

    remove_selectedIndexChanged: function(handler) {
        this.get_events().removeHandler('selectedIndexChanged', handler);
    },

    add_constraintsUpdating: function(handler) {

        this.get_events().addHandler('constraintsUpdating', handler);
    },

    remove_constraintsUpdating: function(handler) {
        this.get_events().removeHandler('constraintsUpdating', handler);
    },

    add_constraintsUpdated: function(handler) {

        this.get_events().addHandler('constraintsUpdated', handler);
    },

    remove_constraintsUpdated: function(handler) {
        this.get_events().removeHandler('constraintsUpdated', handler);
    },

    initialize: function() {

        UniteU.FrontEnd.LayoutControls.SkuSelection.DropDownAttributeSelector.callBaseMethod(this, 'initialize');
        var browserType = navigator; // get the browser type because we need two different ways to register events

        // note here we get the dropdown ID which is pushed up from DropDownAttributeSelector.cs
        // this needs to be done since change events do not bubble to parent containers in IE
        // http://www.johnvey.com/blog/2007/07/ie-does-not-bubble-form-select-element-onchange-events
        var element = $get(this._dropdownId);
        this._selectedIndexChangedHandler = Function.createDelegate(this, this._onSelectedIndexChanged);
        this._constraintChangedDelegate = Function.createDelegate(this, this._constraintChangedHandler);
        $addHandler(element, 'change', this._selectedIndexChangedHandler);

        var onAppLoad = Function.createDelegate(this, this._onApplicationLoad);
        Sys.Application.add_load(onAppLoad);
    },

    _onApplicationLoad: function() {
        var skuSelectorControl = this.get_skuSelectorControl();
        if (skuSelectorControl) { skuSelectorControl.add_constraintChanged(this._constraintChangedDelegate); }
        this._raiseSelectedIndexChanged(Sys.EventArgs.Empty);
    },

    _onSelectedIndexChanged: function(e) {
        // is anyone subscribed to h, if so fire the delegate
        // need to maintain selected index here
        this._selectedIndex = e.target.selectedIndex;
        this._selectedValue = e.target.options[e.target.selectedIndex].value;
        this._raiseSelectedIndexChanged(Sys.EventArgs.Empty);
    },

    _raiseSelectedIndexChanged: function(args) {
        var h = this.get_events().getHandler('selectedIndexChanged');
        if (h) {
            h(this, args);
        }
    },

    _constraintChangedHandler: function(e, args) {
        var availAttrValues = args[this.get_attributeLabel()];

        var contraintUpdatingArgs = new Object();
        contraintUpdatingArgs.enabledAttrValues = availAttrValues;
        contraintUpdatingArgs.cancel = false;

        //fire constraintsUpdating event
        var h = this.get_events().getHandler('constraintsUpdating');
        if (h) {
            h(this, contraintUpdatingArgs);
        }

        //allow the constraintsUpdating event handler to cancel the default behavior
        if (contraintUpdatingArgs == null || !contraintUpdatingArgs.cancel) {
            //default behavior
            var element = $get(this._dropdownId);
            if (element != null) {
                for (var i = this.get_addDefaultListItem() ? 1 : 0; i < element.options.length; i++) {
                    this._disableChildControl(element.options[i]);
                }
                for (var i = 0; i < availAttrValues.length; i++) {
                    this._enableChildControl(this.getChildControlByValue(availAttrValues[i]));
                }
            }
        }

        //fire constraintsUpdated event
        var h = this.get_events().getHandler('constraintsUpdated');
        if (h) {
            h(this, contraintUpdatingArgs);
        }
    },


    _disableChildControl: function(control) {
        if (control != null) {
            if (!this.get_disabledValuesSelectable()) { control.disabled = true; }
            Sys.UI.DomElement.removeCssClass(control, this.get_enabledCssClass());
            Sys.UI.DomElement.addCssClass(control, this.get_disabledCssClass());
        }
    },

    _enableChildControl: function(control) {
        if (control != null) {
            if (!this.get_disabledValuesSelectable()) { control.disabled = false; }
            Sys.UI.DomElement.removeCssClass(control, this.get_disabledCssClass());
            Sys.UI.DomElement.addCssClass(control, this.get_enabledCssClass());
        }
    },

    getChildControlByValue: function(value) {

        var element = $get(this._dropdownId);
        if (element != null) {
            for (var i = 0; i < element.options.length; i++) {

                if (element.options[i].value == value) {
                    return element.options[i];
                }
            }
        }
        return null;
    },

    dispose: function() {
        $clearHandlers(this.get_element());
        UniteU.FrontEnd.LayoutControls.SkuSelection.DropDownAttributeSelector.callBaseMethod(this, 'dispose');
    }

}

UniteU.FrontEnd.LayoutControls.SkuSelection.DropDownAttributeSelector.registerClass('UniteU.FrontEnd.LayoutControls.SkuSelection.DropDownAttributeSelector', Sys.UI.Control);
if(typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();