/**
 * Magento Enterprise Edition
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Magento Enterprise Edition License
 * that is bundled with this package in the file LICENSE_EE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.magentocommerce.com/license/enterprise-edition
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magentocommerce.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magentocommerce.com for more information.
 *
 * @category    design
 * @package     enterprise_connect
 * @copyright   Copyright (c) 2010 Magento Inc. (http://www.magentocommerce.com)
 * @license     http://www.magentocommerce.com/license/enterprise-edition
 */
var extensionEditionDataAndReleases = {
    compatibleEditions: null,
    hasCompatibleVersions: null,
    idPrefix: null,
    selectElement: null,
    versionsSpan: null,
    compatibilityTextContainer: null,
    caption: null,
    baseUrl: null,
    smthWrongMsg: null,
    loadWaiting: false,
    productId: null,
    container: null,
    releasesContainer: null,
    waitingEditionAndReleasesMessageContainer: null,
    waitingReleasesMessageContainer: null,
    isUpdatingEditionData: false,

    /**
     * Initialize compatibility select and versions span
     */
    init: function(config) {
        this.compatibleEditions = config.compatibleEditions;
        this.hasCompatibleVersions = config.hasCompatibleVersions;
        this.idPrefix = config.idPrefix;
        this.caption = config.caption;
        this.baseUrl = config.baseUrl;
        this.smthWrongMsg = config.smthWrongMsg;
        this.productId = config.productId;

        this.selectElement = $(this.idPrefix + '_edition_id');
        this.versionsSpan = $(this.idPrefix + '_supported_edition_versions');
        this.compatibilityTextContainer = $(this.idPrefix + '_compatibility_text');
        if (this.hasCompatibleVersions) {
            if (!this.compatibleEditions[this.selectElement.getValue()]) {
                this.compatibilityTextContainer.hide();
            } else {
                this.compatibilityTextContainer.show();
                this.versionsSpan.innerHTML = this.compatibleEditions[this.selectElement.getValue()];
            }
        } else {
            // if no compatible versions are available - remove div with compatibilities
            $(this.idPrefix + "_compatibility_div").remove();
        }

        if (this.selectElement) {
            Event.observe(this.selectElement, 'change', this.updateEditionData.bindAsEventListener(this, 1));
        }
    },

    /**
     * Bind event to pager link for ajax
     */
    bindPagerLink: function() {
        $$('.ajax-pager a').each(function(link) {
            Event.stopObserving(link, 'click');
            Event.observe(link, 'click', this.updateReleaseNotes.bindAsEventListener(this, link.rel));
        }.bind(this));
    },

    /**
     * Change compatible versions in accordance to selected edition
     */
    updateEditionData: function(event, page) {
        // put these variables here because these 2 blocks lay under platform select block
        this.container = $("product-edition-info");
        this.releasesContainer = $("extension-version-list-container");
        this.waitingEditionAndReleasesMessageContainer = $("edition-data-please-wait");
        this.waitingReleasesMessageContainer = $("versions-list-please-wait");

        if(!this.compatibleEditions[this.selectElement.getValue()]) {
            this.compatibilityTextContainer.hide();
        } else {
            this.compatibilityTextContainer.show();
            this.versionsSpan.innerHTML = this.compatibleEditions[this.selectElement.getValue()];
        }
        var editionId = this.selectElement.getValue();
        var params    = "editionId=" + editionId;
        this.isUpdatingEditionData = true;
        this.setLoadWaiting(true, page);
        new Ajax.Request(this.baseUrl + 'catalog/product/productEditionInfo/id/' + this.productId + '/',
        {
            method: 'post',
            parameters: params,
            onSuccess: function (transport) {
                this.container.update(transport.responseText);
                // we need pass all <script> tags from loaded by ajax content through eval()
                transport.responseText.evalScripts();
                this.bindPagerLink();
                this.container.removeClassName('styled');
                Niceforms.init({form:this.container});
                this.updateReleaseNotes(event, page);
                this.isUpdatingEditionData = false;
            }.bind(this),
            onException:function () {
                this.updateReleaseNotes(event, page);
                this.isUpdatingEditionData = false;
            }.bind(this),
            onFailure:function () {
                alert(this.smthWrongMsg);
                this.updateReleaseNotes(event, page);
                this.isUpdatingEditionData = false;
            }.bind(this)
        });
        Event.stop(event);
    },

    /**
     * Change release notes in accordance to selected edition
     */
    updateReleaseNotes: function(event, page) {
        // put these variables here because these 2 blocks lay under platform select block
        this.container = $("product-edition-info");
        this.releasesContainer = $("extension-version-list-container");
        this.waitingEditionAndReleasesMessageContainer = $("edition-data-please-wait");
        this.waitingReleasesMessageContainer = $("versions-list-please-wait");

        var editionId = this.selectElement.getValue();
        var params    = "editionId=" + editionId + "&p=" + page;
        this.setLoadWaiting(true, page);
        new Ajax.Request(this.baseUrl + 'catalog/product/versionsList/id/' + this.productId + '/',
        {
            method:'post',
            parameters:params,
            onSuccess:function (transport) {
                this.setLoadWaiting(false, page);
                this.releasesContainer.update(transport.responseText);
                // we need pass all <script> tags from loaded by ajax content through eval()
                transport.responseText.evalScripts();
                this.bindPagerLink();
                this.releasesContainer.removeClassName('styled');
                Niceforms.init({form:this.releasesContainer});
            }.bind(this),
            onException:function () {
                this.setLoadWaiting(false, page);
            },
            onFailure:function () {
                this.setLoadWaiting(false, page);
                alert(this.smthWrongMsg);
            }.bind(this)
        });
        Event.stop(event);
    },

    /**
     * Show/hide loading message
     */
    setLoadWaiting: function(state, page) {
        if (state) {
            if (!this.loadWaiting) {
                if (this.isUpdatingEditionData) {
                    this.container.setStyle({opacity:.5});
                    this.waitingEditionAndReleasesMessageContainer.show();
                } else {
                    this.waitingReleasesMessageContainer.show();
                }
                this.releasesContainer.addClassName('ajax-loading');
                this.releasesContainer.insert('<div class="ajax-fader"></div>');
                this.loadWaiting = state;
            }
        } else {
            this.container.setStyle({opacity:1});
            this.releasesContainer.removeClassName('ajax-loading');
            this.waitingReleasesMessageContainer.hide();
            this.waitingEditionAndReleasesMessageContainer.hide();
            this.loadWaiting = state;
        }
    }
};

