<?php
  /**
   * Copyright 2011 Zazavi - http://www.zazavi.com
   * Zazavi is free software released under the terms of the GNU General Public License.
   * You should have received a copy of the license.  If not, see <http://www.gnu.org/licenses/>.
   */
  if (!defined("_VALID_PHP"))
      die('Direct access to this location is not allowed.');
	  
  if(!$user->getAcl("Menus")): print $core->msgAlert(_CG_ONLYADMIN, false); return; endif;
?>
<?php switch($core->action): case "edit": ?>
<?php $row = $core->getRowById("menus", $content->id);?>
<h1><img src="images/menus-sml.png" alt="" /><?php echo _MU_TITLE1;?></h1>
<p class="info"><span><?php echo $core->langIcon();?></span><?php echo _MU_INFO1 . _REQ1 . required() . _REQ2;?></p>
<h2><?php echo _MU_SUBTITLE1 . $row['name'.$core->dblang];?></h2>
<form action="" method="post" id="admin_form" name="admin_form">
  <table cellspacing="0" cellpadding="0" class="formtable">
    <tr>
      <td width="150"><?php echo _MU_NAME;?>: <?php echo required();?></td>
      <td><input name="name<?php echo $core->dblang;?>" type="text" class="inputbox required" value="<?php echo $row['name'.$core->dblang];?>" size="55" title="<?php echo _MU_NAME_R;?>"/>
        <?php echo tooltip(_MU_NAME_T);?></td>
      <td width="300" rowspan="7" valign="top" style="background-color:#FFF;min-height:250px"><strong><?php echo _MU_MENUS;?></strong>
<div class="sortable"></div>
        <img src="images/save.png" alt="" id="serialize" title="<?php echo _MU_SAVE;?>" class="tooltip" /></td>
    </tr>
    <tr>
      <td><?php echo _MU_PARENT;?>:</td>
      <td><select name="parent_id" class="select" style="width:200px">
          <option value="0"><?php echo _MU_TOP;?></option>
          <?php $content->getMenuDropList(0, 0,"&#166;&nbsp;&nbsp;&nbsp;&nbsp;", $row['parent_id']);?>
        </select>
        &nbsp;<?php echo tooltip(_MU_TOP_T);?></td>
    </tr>
    <tr>
      <td><?php echo _MU_TYPE;?>: <?php echo required();?></td>
      <td><select name="content_type" class="select" style="width:200px" id="contenttype">
          <option value="NA" selected="selected"><?php echo _MU_TYPE_SEL;?></option>
          <?php echo $content->getContentType($row['content_type']);?>
        </select>
        &nbsp;<?php echo tooltip(_MU_TYPE_SEL_T);?></td>
    </tr>
    <tr>
      <td><?php echo _MU_LINK;?>:</td>
      <td><span id="contentId">
        <?php if($row['content_type'] == "web"):?>
        <input name="web" type="text" class="inputbox" size="45" value="<?php echo $row['link'];?>"/>
        &nbsp;<?php echo tooltip(_MU_LINK_T);?>
        <select name="target" style="width:100px">
          <option value=""><?php echo _MU_TARGET;?></option>
          <option value="_blank"<?php if ($row['target'] == "_blank") echo ' selected="selected"';?>><?php echo _MU_TARGET_B;?></option>
          <option value="_self"<?php if ($row['target'] == "_self") echo ' selected="selected"';?>><?php echo _MU_TARGET_S;?></option>
        </select>
        <input name="page_id" type="hidden" value="0" />
        <?php else:?>
        <select name="page_id" class="select" style="width:200px">
          <?php $clist = $content->getPages();?>
          <?php foreach($clist as $crow):?>
          <?php $sel = ($crow['id'] == $row['page_id']) ? " selected=\"selected\"" : "" ?>
          <option value="<?php echo $crow['id'];?>"<?php echo $sel;?>><?php echo $crow['title'];?></option>
          <?php endforeach;?>
          <?php unset($crow);?>
        </select>
        <?php endif;?>
        </span></td>
    </tr>
    <tr>
      <td><?php echo _MU_PUB;?>:</td>
      <td><span class="input-out">
        <label for="active-1"><?php echo _YES;?></label>
        <input name="active" type="radio" id="active-1" value="1" <?php getChecked($row['active'], 1); ?> />
        <label for="active-2"><?php echo _NO;?></label>
        <input name="active" type="radio" id="active-2" value="0" <?php getChecked($row['active'], 0); ?> />
        </span></td>
    </tr>
    <tr>
      <td><?php echo _MU_HOME;?>:</td>
      <td><span class="input-out">
        <label for="home_page-1"><?php echo _YES;?></label>
        <input name="home_page" type="radio" id="home_page-1" value="1" <?php getChecked($row['home_page'], 1); ?> />
        <label for="home_page-2"><?php echo _NO;?></label>
        <input name="home_page" type="radio" id="home_page-2" value="0" <?php getChecked($row['home_page'], 0); ?> />
        <?php echo tooltip(_MU_HOME_T);?></span></td>
    </tr>
    <tr>
      <td><input type="submit" name="submit" value="<?php echo _MU_UPDATE;?>" class="button"/></td>
      <td><a href="loader.php?do=menus" class="button-alt"><?php echo _CANCEL;?></a></td>
    </tr>
  </table>
  <input name="id" type="hidden" value="<?php echo $content->id;?>" />
</form>
<?php break;?>
<?php default: ?>
<h1><img src="images/menus-sml.png" alt="" /><?php echo _MU_TITLE2;?></h1>
<p class="info"><span><?php echo $core->langIcon();?></span><?php echo _MU_INFO2;?></p>
<h2><?php echo _MU_SUBTITLE2;?></h2>
<form action="" method="post" id="admin_form" name="admin_form">
  <table cellspacing="0" cellpadding="0" class="formtable">
    <tr>
      <td width="150"><?php echo _MU_NAME;?>: <?php echo required();?></td>
      <td><input name="name<?php echo $core->dblang;?>" type="text" class="inputbox required" size="45" title="<?php echo _MU_NAME_R;?>"/>
        &nbsp;<?php echo tooltip(_MU_NAME_T);?></td>
      <td width="300" rowspan="7" valign="top" style="background-color:#FFF;min-height:250px"><strong><?php echo _MU_MENUS;?></strong>
<div class="sortable"></div>
        <img src="images/save.png" alt="" id="serialize" title="<?php echo _MU_SAVE;?>" class="tooltip" /></td>
    </tr>
    <tr>
      <td><?php echo _MU_PARENT;?>:</td>
      <td><select name="parent_id" class="select" style="width:200px">
          <option value="0"><?php echo _MU_TOP;?></option>
          <?php $content->getMenuDropList(0, 0,"&#166;&nbsp;&nbsp;&nbsp;&nbsp;");?>
        </select>
        &nbsp;<?php echo tooltip(_MU_TOP_T);?></td>
    </tr>
    <tr>
      <td><?php echo _MU_TYPE;?>: <?php echo required();?></td>
      <td><select name="content_type" class="select" style="width:200px" id="contenttype">
          <option value="NA" selected="selected"><?php echo _MU_TYPE_SEL;?></option>
          <?php echo $content->getContentType();?>
        </select>
        &nbsp;<?php echo tooltip(_MU_TYPE_SEL_T);?></td>
    </tr>
    <tr>
      <td><?php echo _MU_LINK;?>:</td>
      <td><span id="contentId">
        <select name="page_id" id="content_id" class="select">
          <option value="0"><?php echo _MU_NONE;?></option>
        </select>
        </span></td>
    </tr>
    <tr>
      <td><?php echo _MU_PUB;?>:</td>
      <td><span class="input-out">
        <label for="active-1"><?php echo _YES;?></label>
        <input name="active" type="radio" id="active-1" value="1" checked="checked" />
        <label for="active-2"><?php echo _NO;?></label>
        <input name="active" type="radio" id="active-2" value="0" />
        </span></td>
    </tr>
    <tr>
      <td><?php echo _MU_HOME;?>:</td>
      <td><span class="input-out">
        <label for="home_page-1"><?php echo _YES;?></label>
        <input name="home_page" type="radio" id="home_page-1" value="1" />
        <label for="home_page-2"><?php echo _NO;?></label>
        <input name="home_page" type="radio" id="home_page-2" value="0" checked="checked" />
        <?php echo tooltip(_MU_HOME_T);?></span></td>
    </tr>
    <tr>
      <td><input type="submit" name="submit" value="<?php echo _MU_ADD;?>" class="button"/></td>
      <td>&nbsp;</td>
    </tr>
  </table>
</form>
<?php break;?>
<?php endswitch;?>
<div id="dialog-confirm" style="display:none;" title="<?php echo _DELETE.' '._MENU;?>">
  <p><span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span><?php echo _DEL_CONFIRM;?></p>
</div>
<script type="text/javascript"> 
// <![CDATA[
$(document).ready(function () {
    function loadList() {
        $.ajax({
            type: 'post',
            url: "ajax.php",
            data: 'getmenus=1',
            cache: false,
            success: function (html) {
                $("div.sortable").html(html);
            }
        });
    }

    loadList();

    $("#admin_form").ajaxForm({
        target: "#msgholder",
        url: "controller.php",
        data: {
            processMenu: 1
        },
        success: showResponse,
    });

    function showResponse(msg) {
        $(this).html(msg);
        setTimeout(function () {
            $(loadList()).fadeIn("slow");
        }, 2000);
        $("html, body").animate({
            scrollTop: 0
        }, 600);
    }

    $('div.sortable').nestedSortable({
        forcePlaceholderSize: true,
        handle: 'div',
        helper: 'clone',
        items: 'li',
        opacity: .6,
        placeholder: 'placeholder',
        tabSize: 25,
        tolerance: 'pointer',
        toleranceElement: '> div'
    });

    $('#serialize').live('click', function () {
        serialized = $('.sortable').nestedSortable('serialize');
        serialized += '&sortmenuitems=1';
        $.ajax({
            type: 'post',
            url: "ajax.php",
            data: serialized,
            success: function (msg) {
			$("#msgholder").html(msg);
			  setTimeout(function () {
				  $(loadList()).fadeIn("slow");
			  }, 2000);
            }

        });
    })

    $('#contenttype').change(function () {
        var option = $(this).val();
        $.get('ajax.php', {
            contenttype: option
        }, function (data) {
            $('#contentId').html(data).show();
        });

    });

    $('a.delete').live('click', function () {
        var id = $(this).attr('id').replace('item_', '')
        var parent = $(this).parent();
        var title = $(this).attr('rel');
        $("#dialog-confirm").data({
            'delid': id,
            'parent': parent,
            'title': title
        }).dialog('open');
        return false;
    });

    $("#dialog-confirm").dialog({
        resizable: false,
        bgiframe: true,
        autoOpen: false,
        width: 400,
        height: "auto",
        zindex: 9998,
        modal: false,
        buttons: {
            '<?php echo _DELETE;?>': function () {
                var parent = $(this).data('parent');
                var id = $(this).data('delid');
                var title = $(this).data('title');

                $.ajax({
                    type: 'post',
                    url: "ajax.php",
                    data: 'deleteMenu=' + id + '&menutitle=' + title,
                    beforeSend: function () {
                        parent.animate({
                            'backgroundColor': '#FFBFBF'
                        }, 400);
                    },
                    success: function (msg) {
                        parent.fadeOut(400, function () {
                            parent.remove();
                        });
                        $("html, body").animate({
                            scrollTop: 0
                        }, 600);
                        $("#msgholder").html(msg);
                    }
                });

                $(this).dialog('close');
            },
            '<?php echo _CANCEL;?>': function () {
                $(this).dialog('close');
            }
        }
    });
});
// ]]>
</script>