<?
	require_once('../include/user.php');
	require_once('../include/authenticate.php');
	require_once('../include/mysql.php');
	require_once('../include/functions.php');
    require_once('../include/fb.php');

	ob_start();

	session_start();

	// SETUP MYSQL HANDLERS
	$mysqldb1 = new mysql($host, $user, $pswd, $db); $mysqldb1->connect(); $mysqldb1->select();
	$mysqldb2 = new mysql($host, $user, $pswd, $db); $mysqldb2->connect(); $mysqldb2->select();

	// SETUP GENERAL VARIABLES
	$url = $_SERVER['REQUEST_URI'];
	$id = $_GET['id']; if ($id == '') { $id = $_POST['id']; }
	$date = date('Y-m-d H:i:s', time());
	$error = $_GET['error'];
	$log = '';

	$upload_file_types = '';
	foreach ($file_types as $file_type) {
		$upload_file_types .= '*.'.$file_type.';';
	}

	$show_file_uploader = true;

	// SETUP RECORD HANDLER
	$table = str_replace('.php', '', page_name());
	$query = "SELECT * FROM ".$table." WHERE id='".$id."'";
	if ($id != '') { $mysqldb1->query($query); }
	$ROW = $mysqldb1->fetchArray();

	// HANDLE RECORD DUPLICATION
	if ($_GET['duplicate']) { $id = ''; }

	// SETUP FORM BUTTONS
	define('UPDATE_BUTTON', '<input name="update" type="submit" class="button" value="UPDATE">');
	define('INSERT_BUTTON', '<input name="insert" type="submit" class="button" value="INSERT">');

	// MODIFY POSTED FIELDS HERE BEFORE IT GETS INJECTED INTO THE DATABASE
	if (isset($_POST['home'])) { $mysqldb2->query("UPDATE ".$table." SET home=0"); $_POST['home'] = 1; } else { $_POST['home'] = 0; }
	if (isset($_POST['hidden'])) { $_POST['hidden'] = 1; } else { $_POST['hidden'] = 0; }

    if ($_POST['page'] != $_POST['new_page']) {
        $_POST['page'] = $_POST['new_page'];
    }

    if ($_POST['subpage'] != $_POST['new_subpage']) {
        $_POST['subpage'] = $_POST['new_subpage'];
    }

    if ($_POST['subsubpage'] != $_POST['new_subsubpage']) {
        $_POST['subsubpage'] = $_POST['new_subsubpage'];
    }

	if ($_POST['name'] == '') {
		if ($_POST['page'] != '') {
			$_POST['name'] = $_POST['page'];
		}
		if ($_POST['subpage'] != '') {
			$_POST['name'] = $_POST['subpage'];
		}
		if ($_POST['subsubpage'] != '') {
			$_POST['name'] = $_POST['subsubpage'];
		}
	}

	$_POST['name'] = cleanSpecialChars($_POST['name']);

	// DATA ENGINE
    $debug = 0;
	require_once('../include/record.php');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title><? print $table.':'.$id; ?></title>
<meta name="robots" content="noindex, nofollow, noarchive" />
<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<!--STYLES-->
<link type="text/css" rel="stylesheet" href="../styles/default/table.css"/>
<!--/STYLES-->
<!--JQUERY-->
<link type="text/css" rel="stylesheet" href="../styles/smoothness/jquery.ui.css"/>
<script type="text/javascript" src="../scripts/jquery.js"></script>
<script type="text/javascript" src="../scripts/jquery.ui.js"></script>
<script type="text/javascript" src="../scripts/jquery.imgpreview.js"></script>
<!--/JQUERY-->
<!--UPLOADIFY-->
<link rel="stylesheet" href="../uploadify/uploadify/uploadify.css" type="text/css" />
<link rel="stylesheet" href="../uploadify/css/uploadify.jGrowl.css" type="text/css" />
<script type="text/javascript" src="../uploadify/js/jquery.uploadify.js"></script>
<script type="text/javascript" src="../uploadify/js/jquery.jgrowl_minimized.js"></script>
<!--/UPLOADIFY-->
<!--TINYMCE-->
<script type="text/javascript" src="../scripts/tiny_mce_3_2_7/tiny_mce_gzip.js"></script>
<!--/TINYMCE-->
<!--CUSTOM-->
<script type="text/javascript" src="../scripts/jquery.cookie.js"></script>
<script type="text/javascript" src="../scripts/php.js"></script>
<script type="text/javascript" src="../scripts/custom.js"></script>
<script type="text/javascript">
$(document).ready(function() {

	$("#tabs").tabs( { cookie: { expires: 30 } } );

	<? if ($show_file_uploader and $id != '') { ?>
	loadUploader('#fileUploadgrowl', '<? print $table; ?>', 0, '<? print $fileDIR; ?>', '<? print $upload_file_types; ?>', <? print $file_size; ?>);
	fetchFiles('<? print $table; ?>', 0);
	<? } ?>

    $("#page").combobox();
    $("#subpage").combobox();
    $("#subsubpage").combobox();

});

tinymceStart("", "", 'content', '<? print $table; ?>', 0);

</script>
<!--/CUSTOM-->
</head>
<body>
<? if ($id != '') { ?>
<div id="controls">
	<? if ($_SESSION['panel_view_url'] != '') { ?><a href="<? print $_SESSION['panel_view_url']; ?>"><img src="../styles/default/back.png" alt="back" border="0" class="button"/></a><? } ?>
	<a href="<? print $table.'.php?id='.$id; ?>"><img src="../styles/default/refresh.png" alt="refresh" border="0" class="button"/></a>
    <a href="<? print $table.'.php?id='.$id.'&duplicate=1'; ?>"><img src="../styles/default/copy.png" alt="duplicate" border="0" class="button"/></a>
    <a href="javascript:deleteRecord('<? print $table; ?>', <? print $id; ?>, '<? print $_SESSION['panel_view_url']; ?>');" id="delete"><img src="../styles/default/delete.png" alt="delete" border="0" class="button"/></a>
</div>
<div id="feedback"><? print $log; ?></div>
<? } ?>
<div id="tabs" class="ui-tabs">
<ul>
    <li><a href="#tabs-1">Data</a></li>
    <li><a href="#tabs-2">Content</a></li>
    <? if ($show_file_uploader and $id != '') { ?>
    <li><a href="#tabs-3">Files</a></li>
    <? } ?>
</ul>
<form name="<? print $table.'_'.$id; ?>" id="<? print $table.'_'.$id; ?>" method="post" action="<? print $table.'.php'; ?>">
<!--DATA UPLOADER-->
<div id="tabs-1" class="ui-tabs-hide">
<input type="hidden" name="id" id="id" value="<? echo $id; ?>" />
<input type="hidden" name="date_modified" id="date_modified" value="<? echo $date; ?>" />
<input type="hidden" name="date_created" id="date_created" value="<? if ($id == '') { echo $date; } else { echo $ROW['date_created']; } ?>" />
<input type="hidden" name="sequence" id="sequence" value="<? echo $ROW['sequence']; ?>" />
<table border="0" cellspacing="1" cellpadding="1">
  <!-- <tr>
    <td class="field_name">page(menu)</td>
    <td class="field"><input name="page" type="text" id="page" value="<? echo $ROW['page']; ?>" size="60" maxlength="100" /></td>
  </tr> -->
  <tr>
    <td class="field_name">page(menu)</td>
    <td class="field">
        <div class="ui-widget">
        <select name="page" id="page">
            <option value=""></option>
        <?
        $query = "
        SELECT
            DISTINCT t1.page
        FROM
            page AS t1
        WHERE
            t1.page!=''
        ORDER BY
            t1.page ASC";
		$mysqldb1->query($query);
		while ($option = $mysqldb1->fetchArray()) {
			if ($ROW['page'] == $option['page']) { $selected = 'selected'; } else { $selected = ''; }
			print '<option value="'.$option['page'].'" '.$selected.'>'.$option['page'].'</option>'."\n";
		}
		?>
        </select>
        </div>
    </td>
  </tr>
  <!-- <tr>
    <td class="field_name">subpage(submenu)</td>
    <td class="field"><input name="subpage" type="text" id="subpage" value="<? echo $ROW['subpage']; ?>" size="60" maxlength="100" /></td>
  </tr> -->
  <tr>
    <td class="field_name">subpage(menu)</td>
    <td class="field">
        <div class="ui-widget">
        <select name="subpage" id="subpage">
            <option value=""></option>
        <?
        $query = "
        SELECT
            DISTINCT t1.subpage
        FROM
            page AS t1
        WHERE
            t1.subpage!=''
        ORDER BY
            t1.subpage ASC";
		$mysqldb1->query($query);
		while ($option = $mysqldb1->fetchArray()) {
			if ($ROW['subpage'] == $option['subpage']) { $selected = 'selected'; } else { $selected = ''; }
			print '<option value="'.$option['subpage'].'" '.$selected.'>'.$option['subpage'].'</option>'."\n";
		}
		?>
        </select>
        </div>
    </td>
  </tr>
  <!-- <tr>
    <td class="field_name">subsubpage(subsubmenu)</td>
    <td class="field"><input name="subsubpage" type="text" id="subsubpage" value="<? echo $ROW['subsubpage']; ?>" size="60" maxlength="100" /></td>
  </tr> -->
  <tr>
    <td class="field_name">subsubpage(menu)</td>
    <td class="field">
        <div class="ui-widget">
        <select name="subsubpage" id="subsubpage">
            <option value=""></option>
        <?
        $query = "
        SELECT
            DISTINCT t1.subsubpage
        FROM
            page AS t1
        WHERE
            t1.subsubpage!=''
        ORDER BY
            t1.subsubpage ASC";
		$mysqldb1->query($query);
		while ($option = $mysqldb1->fetchArray()) {
			if ($ROW['subsubpage'] == $option['subsubpage']) { $selected = 'selected'; } else { $selected = ''; }
			print '<option value="'.$option['subsubpage'].'" '.$selected.'>'.$option['subsubpage'].'</option>'."\n";
		}
		?>
        </select>
        </div>
    </td>
  </tr>
  <tr>
    <td class="field_name">title</td>
    <td class="field"><input name="title" type="text" id="title" value="<? echo $ROW['title']; ?>" size="60" maxlength="100" /></td>
  </tr>
  <tr>
    <td class="field_name">heading</td>
    <td class="field"><input name="heading" type="text" id="heading" value="<? echo $ROW['heading']; ?>" size="60" maxlength="100" /></td>
  </tr>
  <tr>
    <td class="field_name">link name(unique)</td>
    <td class="field"><input name="name" type="text" id="name" value="<? echo $ROW['name']; ?>" size="60" maxlength="100" /></td>
  </tr>
  <tr>
    <td class="field_name">external (youtube) #VIDEO#</td>
    <td class="field"><textarea name="external" cols="50" rows="5" id="external"><? echo $ROW['external']; ?></textarea></td>
  </tr>
  <tr>
    <td class="field_name">url (website)</td>
    <td class="field"><input name="url" type="text" id="url" value="<? echo $ROW['url']; ?>" size="60" maxlength="100" /></td>
  </tr>
  <tr>
    <td class="field_name">home page</td>
    <td class="field"><input name="home" id="home" type="checkbox" value="<? echo $ROW['home']; ?>" <? if ($ROW['home']) { print 'checked'; } ?> /></td>
  </tr>
  <tr>
    <td class="field_name">hide page</td>
    <td class="field"><input name="hidden" id="hidden" type="checkbox" value="<? echo $ROW['hidden']; ?>" <? if ($ROW['hidden']) { print 'checked'; } ?> /></td>
  </tr>
  <tr>
    <td class="field_name">date modified</td>
    <td class="field"><? if ($ROW['date_modified'] != '') { print $ROW['date_modified']; } ?></td>
  </tr>
  <tr>
    <td class="field_name">date created</td>
    <td class="field"><? if ($ROW['date_created'] != '') { print $ROW['date_created']; } ?></td>
  </tr>
  <tr>
    <td class="field_name">status</td>
    <td class="field"><div class="ui-widget"><? print drop_down('status', array('enabled', 'disabled'), $ROW['status']); ?></div></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td><? if ($id == '') { print INSERT_BUTTON; } else { print UPDATE_BUTTON; } ?></td>
    <td>&nbsp;</td>
  </tr>
</table>
</div>
<!--/DATA UPLOADER-->
<!--CONTENT-->
<div id="tabs-2" class="ui-tabs-hide">
<table border="0" cellspacing="1" cellpadding="1">
  <tr>
    <td><textarea name="content" id="content" class="tinymce"><? echo $ROW['content']; ?></textarea></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td><? if ($id == '') { print INSERT_BUTTON; } else { print UPDATE_BUTTON; } ?></td>
  </tr>
</table>
</div>
<!--/CONTENT-->
</form>
<!--FILE UPLOADER-->
<? if ($show_file_uploader and $id != '') { ?>
<div id="tabs-3" class="ui-tabs-hide">
<fieldset>
    <legend><strong>Upload Files</strong></legend>
    <p>File Size Limited to <? print number_format(($file_size / 1024 / 1024), 0, '.', ','); ?>MB<!-- - Queue Size <font id="queue_size"></font>--></p>
    <div id="fileUploadgrowl">You have a problem with your javascript</div>
    <div id="upload_controls" style="display:none;"><a href="javascript:$('#fileUploadgrowl').fileUploadStart()" id="start_upload"><img src="../styles/default/start.png" alt="start" border="0" class="button"/></a> <a href="javascript:clearUploadQueue();" id="clear_queue"><img src="../styles/default/clear.png" alt="clear" border="0" class="button"/></a></div>
    <p></p>
</fieldset>
<fieldset>
    <legend><strong>Uploaded Files</strong></legend>
    <div id="files"></div>
    <p></p>
</fieldset>
</div>
<? } ?>
<!--/FILE UPLOADER-->
</div>
<? require_once("../panel/dialog.php"); ?>
</body>
</html>