<?php include dirname(__FILE__) . '/crumb.html.php'; ?>

<div id="category_add_form">
    <div class="panel">
        <div class="title" id="category_add_close">
            Add Category - <a href="javascript:">Cancel</a>
        </div>
        <div class="content clear">
            <div>
                <?php echo $form->render(); ?>
            </div>
            <div>
                <input id="file" name="file" type="file" /><br />
                <button onclick="return ajaxFileUpload();">Upload</button>
                <div class="loading"></div>
                <div id="category_image"></div>
            </div>
        </div>
    </div>
</div>

<?php $url = str_replace('admin.', '', $request->base()); ?>
<div id="category_add_list" class="clear">
    <?php foreach ($category as $c) : ?>
        <?php $is_featured = category::is_featured($c['id']); ?>
        <div class="icon<?php if ($is_featured) : echo ' featured'; endif; ?>" id="category_<?php echo htmlspecialchars($c['id']); ?>">
            <div class="content">
                <a href="category/index/<?php echo htmlspecialchars($c['id']); ?>">
                    <img src="<?php echo htmlspecialchars(($c['image']) ? category::DIRECTORY . $c['image'] . '-s.jpg' : 'admin/skin/icon/folder1.png'); ?>?<?php echo time(); ?>" /><br /><?php echo htmlspecialchars((strlen($c['name']) > 20) ? substr($c['name'], 0, 17) . '...' : $c['name']); ?>
                </a>
            </div>
            <div class="action">
                <a target="_blank" href="<?php echo htmlspecialchars($url); ?>product/index/<?php echo htmlspecialchars(ltrim(implode('/', $path) . '/', '/') . $c['path']); ?>">View</a>
                <a href="category/edit/<?php echo htmlspecialchars($c['id']); ?>" id="category_edit_<?php echo htmlspecialchars($c['id']); ?>">Edit</a>
                <?php if ($is_featured) : ?>
                    <a href="javascript:unfeature_category(<?php echo htmlspecialchars($c['id']); ?>);" id="category_unfeature_<?php echo htmlspecialchars($c['id']); ?>">Unfeature</a>
                <?php else : ?>
                    <a href="javascript:feature_category(<?php echo htmlspecialchars($c['id']); ?>);" id="category_feature_<?php echo htmlspecialchars($c['id']); ?>">Feature</a>
                <?php endif; ?>
                <a href="javascript:delete_category(<?php echo htmlspecialchars($c['id']); ?>);">Delete</a>
            </div>
        </div>
    <?php endforeach; ?>
    <?php foreach ($product as $p) : ?>
        <?php $is_featured = product::is_featured($p['id']); ?>
        <div class="icon product<?php if ($is_featured) : echo ' featured'; endif; ?>" id="product_<?php echo htmlspecialchars($p['id']); ?>">
            <div class="content">
                <a href="product/edit/<?php echo htmlspecialchars($p['id']); ?>">
                    <img src="<?php echo htmlspecialchars(($p['image']) ? product::DIRECTORY . $p['image'] . '-s.jpg' : 'admin/skin/icon/file1.png'); ?>?<?php echo time(); ?>" /><br /><?php echo htmlspecialchars((strlen($p['name']) > 20) ? substr($p['name'], 0, 17) . '...' : $p['name']); ?>
                </a>
            </div>
            <div class="action">
                <a target="_blank" href="<?php echo htmlspecialchars($url); ?>product/view/<?php echo htmlspecialchars(ltrim(implode('/', $path) . '/', '/') . $p['path']); ?>">View</a>
                <a href="product/edit/<?php echo htmlspecialchars($p['id']); ?>" id="product_edit_<?php echo htmlspecialchars($p['id']); ?>">Edit</a>
                <?php if ($is_featured) : ?>
                    <a href="javascript:unfeature_product(<?php echo htmlspecialchars($p['id']); ?>);" id="product_unfeature_<?php echo htmlspecialchars($p['id']); ?>">Unfeature</a>
                <?php else : ?>
                    <a href="javascript:feature_product(<?php echo htmlspecialchars($p['id']); ?>);" id="product_feature_<?php echo htmlspecialchars($p['id']); ?>">Feature</a>
                <?php endif; ?>
                <a href="javascript:delete_product(<?php echo htmlspecialchars($p['id']); ?>);">Delete</a>
            </div>
        </div>
    <?php endforeach; ?>
</div>

<script type="text/javascript" src="admin/widget/upload/ajaxfileupload.js"></script>
<script type="text/javascript">
    var id = '';
</script>
<script type="text/javascript" src="admin/category/widget/upload.js"></script>

<script type="text/javascript">
    $(document).ready(function() {
        $('#category_add_form form').submit(function() {
            form = this;
            $.post('category/add', { parent_id: this.parent_id.value, path:this.path.value, name:this.name.value },
                function(data, status) {
                    if (data.error != '')
                    {
                        alert(data.error);
                    }
                    else
                    {
                        $('#category_add_form').toggle();
                        $('#category_add_list').append('<div class="icon" id="category_' + data.id + '"><div class="content"><a href="category/index/' + data.id + '"><img src="' + data.image + '" /><br />' + form.name.value + '</a></div><div class="action"><a target="_blank" href="<?php echo htmlspecialchars($url); ?>category/' + data.id + '">View</a><a href="category/edit/' + data.id + '" id="category_edit_' + data.id + '">Edit</a><a href="javascript:feature_category(' + data.id + ');" id="category_feature_' + data.id + '">Feature</a><a href="javascript:delete_category(' + data.id + ');">Delete</a></div></div>');
                        form.name.value = '';
                        form.path.value = '';
                        $('#category_image').text('');
                    }
                }, 'json');
            return false;
        });
        $('#category_add_form input[name=name]').bind('keyup', function() {
            $('#category_add_form input[name=path]').val($(this).val().replace(/^[\s]+/,'').replace(/[\s]+$/,'').replace(/[\s]/g, '-').toLowerCase());
        });
        $('#category_add_close a').click(function() {
            $('#category_add_form').toggle();
        });
    });
    
    function feature_category(id)
    {
        $.post('category/feature', { id:id },
            function(data, status) {
                if (data.error != '')
                {
                    alert(data.error);
                }
                else
                {
                    $('#category_' + id).addClass('featured');
                    $('#category_feature_' + id).remove();
                    $('#category_edit_' + id).after('<a href="javascript:unfeature_category(' + id + ');" id="category_unfeature_' + id + '">Unfeature</a>');
                }
            }, 'json');
    }
    
    function unfeature_category(id)
    {
        $.post('category/unfeature', { id:id },
            function(data, status) {
                if (data.error != '')
                {
                    alert(data.error);
                }
                else
                {
                    $('#category_' + id).removeClass('featured');
                    $('#category_unfeature_' + id).remove();
                    $('#category_edit_' + id).after('<a href="javascript:feature_category(' + id + ');" id="category_feature_' + id + '">Feature</a>');
                }
            }, 'json');
    }
    
    function delete_category(id)
    {
        if (confirm('Are you sure?'))
        {
            $.post('category/delete', { id:id },
                function(data, status) {
                    if (data.error != '')
                    {
                        alert(data.error);
                    }
                    else
                    {
                        $('#category_' + id).remove();
                    }
                }, 'json');
        }
    }
    
    function feature_product(id)
    {
        $.post('product/feature', { id:id },
            function(data, status) {
                if (data.error != '')
                {
                    alert(data.error);
                }
                else
                {
                    $('#product_' + id).addClass('featured');
                    $('#product_feature_' + id).remove();
                    $('#product_edit_' + id).after('<a href="javascript:unfeature_product(' + id + ');" id="product_unfeature_' + id + '">Unfeature</a>');
                }
            }, 'json');
    }
    
    function unfeature_product(id)
    {
        $.post('product/unfeature', { id:id },
            function(data, status) {
                if (data.error != '')
                {
                    alert(data.error);
                }
                else
                {
                    $('#product_' + id).removeClass('featured');
                    $('#product_unfeature_' + id).remove();
                    $('#product_edit_' + id).after('<a href="javascript:feature_product(' + id + ');" id="product_feature_' + id + '">Feature</a>');
                }
            }, 'json');
    }
    
    function delete_product(id)
    {
        if (confirm('Are you sure?'))
        {
            $.post('product/delete', { id:id },
                function(data, status) {
                    if (data.error != '')
                    {
                        alert(data.error);
                    }
                    else
                    {
                        $('#product_' + id).remove();
                    }
                }, 'json');
        }
    }
</script>