<?php
/**
 * @category    design
 * @package     default_default
 * @copyright   Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
 * @copyright   Copyright (c) 2015 BlueVisionTec UG (haftungsbeschränkt) (http://www.bluevisiontec.de)
 * @license     http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 */
?>

<?php if ($this->getAttributeSetSelected()): ?>
<script type="text/javascript">
//<![CDATA[
Gcontent = {};
Gcontent.Attribute = Class.create();
Gcontent.Attribute.prototype = {
    idLabel : '<?php echo $this->getFieldId() ?>',
    top : '',
    templateSyntax : /(^|.|\r|\n)({{(\w+)}})/,
    templateText : '',
    itemsCount : 0,
    initialize : function(template) {
        this.templateText = template;
        this.top = $('gcontent_attribute_top');
    },

    add : function(data) {
        if(!data){
            data = {};
        }
        this.top = $('gcontent_attribute_top');

        data.index = this.itemsCount++;
        this.template = new Template(this.templateText, this.templateSyntax);
        Element.insert(this.top, {'after':this.template.evaluate(data)});
        this.top = $(this.idLabel + '_' + data.index);

        //set selected type
        if (data.attribute_id) {
            $A($(this.idLabel + '_'+data.index+'_attribute').options).each(function(option){
                if (option.value==data.attribute_id) option.selected = true;
            });
        }

        //set selected is_require
        if (data.gcontent_attribute) {
            $A($(this.idLabel + '_'+data.index+'_gattribute').options).each(function(option){
                if (option.value==data.gcontent_attribute) option.selected = true;
            });
        }
        return data.index;
    },

    remove : function(event) {
        var element = $(Event.findElement(event, 'div'));
        if(element){
            Element.select(element, '.delete').each(function(elem){elem.value='1'});
            Element.select(element, ['input', 'select']).each(function(elem){elem.hide(); elem.className = '';});
            Element.hide(element);
        }
    }
}

var attributesSelectTemplate = '<div id="<?php echo $this->getFieldId() ?>_{{index}}"  class="option-box"> ' +
    '<table class="option-header" cellpadding="0" cellspacing="0">' +
        '<thead>' +
            '<tr>' +
                '<th class="opt-type"><?php echo Mage::helper('googleshoppingapi')->__('Attribute') ?></th>' +
                '<th class="opt-req"><?php echo Mage::helper('googleshoppingapi')->__('Google Content Attribute') ?></th>' +
                '<th>&nbsp;</th>' +
            '</tr>' +
        '</thead>' +
        '<tbody>' +
            '<tr>' +
                '<td><?php echo $this->getAttributesSelectHtml(true) ?>' +
                '<input type="hidden" name="<?php echo $this->getFieldName() ?>[{{index}}][delete]" value="" class="delete">' +
                '</td>' +
                '<td><?php echo $this->getGcontentAttributesSelectHtml() ?></td>' +
                '<td>&nbsp;<?php echo $this->getDeleteButtonHtml() ?></td>' +
            '</tr>' +
        '</tbody>' +
    '</table>' +
'</div>';
var optionIndex = 0;
gContentAttribute = new Gcontent.Attribute(attributesSelectTemplate);
//]]>
</script>

<div class="entry-edit custom-options bundle" id="gcontent_attributes_container">
    <div class="entry-edit-head">
        <h4><?php echo $this->__('Attributes') ?></h4>
        <div class="right"><?php echo $this->getAddButtonHtml() ?></div>
    </div>

    <div id="gcontent_attribute" class="box">
        <div id="gcontent_attribute_top"></div>
    </div>
</div>

<?php if ($this->getAttributesData()): ?>
<script type="text/javascript">
//<![CDATA[
    <?php foreach ($this->getAttributesData() as $data): ?>
        gContentAttribute.add(<?php echo $this->_toJson($data) ?>);
    <?php endforeach; ?>
//]]>
</script>
<?php endif; ?>

<?php else: ?>
    <?php echo Mage::helper('googleshoppingapi')->__("Please, select Attribute Set to load attributes") ?>
<?php endif; ?>
