<?php
/*
 *
 * @package		ARI Smart Content
 * @author		ARI Soft
 * @copyright	Copyright (c) 2011 www.ari-soft.com. All rights reserved
 * @license		GNU/GPL (http://www.gnu.org/copyleft/gpl.html)
 * 
 */

(defined('_JEXEC') && defined('ARI_FRAMEWORK_LOADED')) or die;
?>

<?php echo $this->commonSettingsForm->render('params'); ?>
<?php if ($this->providerSettingsForm) echo $this->providerSettingsForm->render('params'); ?>
<?php echo $this->sourceSettingsForm->render('settings'); ?>
<?php if ($this->formatterForm) echo $this->formatterForm->render('formatter'); ?>

<?php
	if ($this->isPopupMode):
?>
<input type="hidden" name="tmpl" value="component" />
<?php
	endif; 
?>
<?php
if ($this->callback):
    ?>
    <input type="hidden" name="callback" value="<?php echo $this->callback; ?>" />
<?php
endif;
?>
<?php
if ($this->directEdit):
    ?>
    <input type="hidden" name="direct" value="1" />
<?php
endif;
?>
<?php
	if (!empty($this->rurl)):
?>
<input type="hidden" name="rurl" value="<?php echo $this->rurl; ?>" />
<?php
	endif; 
?>

<script type="text/javascript">
YAHOO.util.Event.onDOMReady(function() {
	var Dom = YAHOO.util.Dom,
		page = YAHOO.ARISoft.page,
		pageManager = page.pageManager,
        validatorManager = YAHOO.ARISoft.validators.validatorManager;

	pageManager.subscribe('beforeAction', function(o) {
        if (o.action == 'cancel')
            pageManager.sourceManager.sourceClose();

		if ((o.action == 'save' || o.action == 'apply' || o.action == 'save2copy') &&
			(typeof(o.config["skipValidation"]) == "undefined" || !o.config["skipValidation"])) 
		{
			var task = o.action,
                valCallback = null;

            if (task == 'save2copy') {
                var nameValidators = [];
                for (var i = 0; i < validatorManager.validators.length; i++) {
                    var val = validatorManager.validators[i];
                    if (val.enabled && val.ctrlId == 'paramsName') {
                        nameValidators.push(val);
                        val.enabled = false;
                    }
                }

                if (nameValidators.length > 0)
                    valCallback = function() {
                        for (var i = 0; i < nameValidators.length; i++) {
                            (nameValidators[i]).enabled = true;
                        }
                    }
            }

			YAHOO.ARISoft.validators.alertSummaryValidators.asyncValidate({
				"success": function() {
                    if (valCallback)
                        valCallback();

					pageManager.triggerAction(task, {"skipValidation": true});
				},
                "failure": function() {
                    if (valCallback)
                        valCallback();
                }
			});

			return false;
		}
	});

    pageManager.sourceManager = {
        sourceCloseEvent: new YAHOO.util.CustomEvent('sourceClose'),

        sourceSavedEvent: new YAHOO.util.CustomEvent('sourceSaved'),

        sourceClose: function() {
            this.sourceCloseEvent.fire();
        },

        sourceSaved: function(name) {
            this.sourceSavedEvent.fire({'Name': name});
        }
    };

    <?php
        if ($this->callback):
    ?>
    var context = (window.top != window.self) ? window.top : window.self;
    if (context["<?php echo $this->callback; ?>"])
        context["<?php echo $this->callback; ?>"](window.self);
    <?php
        endif;
    ?>

    <?php
        if ($this->isSaved):
    ?>
    pageManager.sourceManager.sourceSaved("<?php echo $this->item->Name; ?>");
    <?php
        endif;
    ?>
});
</script>