<?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("Layout")): print $core->msgAlert(_CG_ONLYADMIN, false); return; endif;
?>
<?php include_once("help/layout.php");?>
<?php 
  $pageid = ($content->pageid) ? intval($_GET['pageid']): 1;
  $modulerow = $content->getAvailableModules();
  $layrow = $content->getLayoutOptions();
?>
<h1><img src="images/layout-sml.png" alt="" /><?php echo _LY_TITLE;?></h1>
<p class="info"><span><?php echo $core->langIcon();?></span><?php echo _LY_INFO;?></p>
    <h2><span><a href="javascript:void(0);" onclick="$('#dialog').dialog('open'); return false"><img src="images/help.png" alt="" /></a></span>
    <span>
      <select name="page_id" class="select" style="width:200px" onchange="if(this.value!='0') window.location = 'loader.php?do=layout&amp;pageid='+this[this.selectedIndex].value; else window.location = 'loader.php?do=layout';">
        <?php $pagerow = $content->getPages();?>
        <option value="0"><?php echo _LY_SEL_PAGE;?></option>
        <?php foreach ($pagerow as $prow):?>
        <?php $sel = ($content->pageid == $prow['id']) ? ' selected="selected"' : '' ;?>
        <option value="<?php echo $prow['id'];?>"<?php echo $sel;?>><?php echo $prow['title'.$core->dblang];?></option>
        <?php endforeach;?>
      </select>&nbsp;&nbsp;
      </span>
	  <?php echo _LY_VIEW_FOR;?></h2>

    <table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td width="250" valign="top" style="padding-top:6px">
		<?php if ($modulerow == 0): ?>
          <div class="msgInfo"><?php echo _LY_NOMODS;?></div>
          <?php else:?>
          <ul id="default-<?php echo $pageid;?>" class="modList">
            <?php foreach ($modulerow as $mrow): ?>
            <li id="list-<?php echo $mrow['id'];?>"><?php echo $mrow['title'.$core->dblang];?></li>
            <?php endforeach; ?>
            <?php unset($mrow);?>
          </ul>
          <?php endif;?>
          </td>
        <td valign="top"><table border="0" cellpadding="0" cellspacing="3" id="layout">
            <tr>
              <td colspan="3" valign="top" class="top-position">
              <ul id="top-<?php echo $pageid;?>" class="modList">
              	<li style="display:none"></li>
                  <?php if ($layrow != 0): ?>
                  <?php foreach ($layrow as $trow): ?>
                  <?php if ($trow['place'] == "top"): ?>
                  <li id="list-<?php echo $trow['modid'];?>"><?php echo $trow['title'.$core->dblang];?></li>
                  <?php endif; ?>
                  <?php endforeach; ?>
                  <?php unset($trow);?>
                  <?php endif; ?>
                </ul></td>
            </tr>
            <tr>
              <td width="240" valign="top" class="left-position">
              <ul id="left-<?php echo $pageid;?>" class="modList">
              	<li style="display:none"></li>
                  <?php if ($layrow != 0): ?>
                  <?php foreach ($layrow as $lrow): ?>
                  <?php if ($lrow['place'] == "left"): ?>
                  <li id="list-<?php echo $lrow['modid'];?>"><?php echo $lrow['title'.$core->dblang];?></li>
                  <?php endif; ?>
                  <?php endforeach; ?>
                  <?php unset($lrow);?>
                  <?php endif;?>
                </ul></td>
              <td valign="top" nowrap="nowrap" class="main-position">&nbsp;</td>
              <td width="240" valign="top" class="right-position">
              <ul id="right-<?php echo $pageid;?>" class="modList">
              	<li style="display:none"></li>
                  <?php if ($layrow != 0): ?>
                  <?php foreach ($layrow as $rrow): ?>
                  <?php if ($rrow['place'] == "right"): ?>
                  <li id="list-<?php echo $rrow['modid'];?>"><?php echo $rrow['title'.$core->dblang];?></li>
                  <?php endif; ?>
                  <?php endforeach; ?>
                  <?php unset($rrow);?>
                  <?php endif; ?>
                </ul></td>
            </tr>
            <tr>
              <td colspan="3" valign="top" class="bottom-position">
              <ul id="bottom-<?php echo $pageid;?>" class="modList">
              	<li style="display:none"></li>
                  <?php if ($layrow != 0): ?>
                  <?php foreach ($layrow as $brow): ?>
                  <?php if ($brow['place'] == "bottom"): ?>
                  <li id="list-<?php echo $brow['modid'];?>"><?php echo $brow['title'.$core->dblang];?></li>
                  <?php endif; ?>
                  <?php endforeach; ?>
                  <?php unset($brow);?>
                  <?php endif; ?>
                </ul></td>
            </tr>
          </table></td>
      </tr>
    </table>
<script type="text/javascript">
// <![CDATA[
$(function() {
	$("#default-<?php echo $pageid;?>, #bottom-<?php echo $pageid;?>,#left-<?php echo $pageid;?>,#right-<?php echo $pageid;?>,#top-<?php echo $pageid;?>").sortable({
		connectWith: '.modList',
		placeholder: 'modPlace',
		update: savePosition
	});
	
});

function savePosition() {
	var place = "";
	var count = 0;
	$("[id^=list-]").each(function() {
		count++;
		place += (this.parentNode.id + "[]" + "=" + count + "|" + this.id + "&");
	});
	$.ajax({
		type: "post",
		url: "ajax.php?layout=" + this.id,
		data: place
	});
}
// ]]>
</script>