<?php
////////////////////////////////////////////////////////////////////////
// Breeze Website Builder
// Copyright EB Dynamics, Inc., Edward Lemmers, P.E.
// For licensing, see license.txt.
// Created:	3/9/2013	ELL

	include ("check_session.php");
		
	$mode = $_REQUEST['mode'];
	
	$page_name = $_REQUEST['page_name'];
	$blog_id = $_REQUEST['blog_id'];
	$special = $_REQUEST['special'];	//Flag to tell if editing special page (header, footer, side panel...).
	if ($mode == "blog")
	{
		$_SESSION['last_blog_edited'] = $blog_id;
		if (!isset($page_name) || ($page_name == ""))
		{
			// If editing blog, page name not sent with blog ID so need to retrieve from blogs table.
			$query = "SELECT * FROM " . BWB_TABLE_PREFIX . "blogs WHERE id='$blog_id'";
			$result = $bwb_dbh->query($query);
			$row = $result->fetch(PDO::FETCH_ASSOC);
			$page_name = $row['page_name'];
		}
		$_SESSION['last_page_edited'] = $page_name;
		$special = 0;
	}
	elseif (!isset($special))
	{
		$_SESSION['last_page_edited'] = $page_name;
		$special = 0;
	}
	else
		$_SESSION['last_border_edited'] = $page_name;
	
	
	if (BWB_BASE_DIR == '')
		$userfiles = '';
	else
		$userfiles = "/" . BWB_BASE_DIR;
		
	if (BWB_WEBPAGE_DIR == '')
		$website_folder = '';
	else
		$website_folder = "/" . BWB_WEBPAGE_DIR;
	
?>
<html>
<head>
<title>Breeze Website Builder - Editing <?php echo $page_name; ?></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" href="manager.css" type="text/css">
<link rel="shortcut icon" href="images/favicon.ico" type="image/x-icon">

<script type="text/javascript" src="../ckeditor/ckeditor.js"></script>
<script type="text/javascript" src="js/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="js/mouse_position.js"></script>
<script type="text/javascript" src="js/edit_page.js"></script>

<script type="text/javascript" language="javascript">
 var page_name = "<?php echo $page_name; ?>";
 var special = "<?php echo $special; ?>";
 var blog_id = "<?php echo $blog_id; ?>";
 var mode = "<?php echo $mode; ?>";
 var prepend = "<?php echo $prepend; ?>";
 var postpend = "<?php echo $postpend; ?>";
 var userfiles = "<?php echo $userfiles; ?>";
 
 var saved = true;
</script>

</head>

<body>

<form action="edit_page.php" method="post" name="frmEditPage">
<div style="height:93%; width:100%;" id="editorContainer" name="editorContainer"></div>

	<script type="text/javascript" language="javascript">
    //<![CDATA[
		CKEDITOR_BASEPATH = '../ckeditor/';
		
		CKEDITOR.replace( 'editorContainer', {
			filebrowserBrowseUrl: '/kfm/index.php?startup_folder=<?php echo BWB_WEBPAGE_DIR; ?>',
			filebrowserImageBrowseUrl: '/kfm/index.php?startup_folder=<?php echo BWB_IMAGE_DIR; ?>&type=Images',
			filebrowserFlashBrowseUrl: '/kfm/index.php?startup_folder=<?php echo BWB_FLASH_DIR; ?>&type=Flash'
		});

		CKEDITOR.config.extraPlugins = 'codemirror';
		CKEDITOR.config.skin = 'kama';
		
		var ckeditor = CKEDITOR.instances['editorContainer'];
		function somethingChanged()	{
			saved = false;
			ckeditor.commands.save.enable();
		}
		function somethingHappened(e)	{
			if (e.data.name == "newpage")
			{
				e.data.command = null;
				ClearContent();
			}
			else if (e.data.name == "preview")
			{
				e.data.command = null;
				Preview();
			}
			else if (e.data.name == "save")
			{
				e.data.command = null;
				Save('save_draft');
				ckeditor.commands.save.disable();
			}
		}
		ckeditor.on( 'saveSnapshot', function(e) { somethingChanged(); });
		ckeditor.on( 'beforeCommandExec', function(e) { somethingHappened(e); });
		
		
		// If IE6 or IE7, can't apply CSS.
		if (!((navigator.appName == "Microsoft Internet Explorer") && ((navigator.appVersion.indexOf('MSIE 7') > 0) || (navigator.appVersion.indexOf('MSIE 6') > 0))))
		{
<?php 
	echo "\nCKEDITOR.config.contentsCss = [";
	$query = "SELECT * FROM " . BWB_TABLE_PREFIX . "style_sheets";
	$result = $bwb_dbh->query($query);
	$delim ="";
	while ($row = $result->fetch(PDO::FETCH_ASSOC))
	{
		$style_sheet = $row["name"] . ".css";
		echo $delim . "'" . $userfiles . "/" . BWB_CSS_DIR . "/" . $style_sheet . "'";
		$delim = ",";
	}
	echo "];\n";
?>
		}
		CKEDITOR.config.stylesSet = 'eb_styles:/ckeditor/styles.js';
        var editorElem = document.getElementById("editorContainer");
        CKEDITOR.config.width = '100%';
		if (navigator.appName == "Microsoft Internet Explorer")
        	CKEDITOR.config.height = document.body.clientHeight - 200;
		else if (navigator.appName == "Opera")
			CKEDITOR.config.height = window.innerHeight - 210;
		else
			CKEDITOR.config.height = editorElem.clientHeight - 135;
        CKEDITOR.config.resize_enabled = false;
		
		LoadPage();
		
    //]]>
    </script>
            
      <br/>
	  <table width="90%" align="center"><tr>
	  <td align="left" class="small_message" width="9%">
      Page Name:<br/><span id="page_name_label"><?php echo $page_name; ?></span>
	  </td>
      <td align="center">
      <input type="button" value="Preview" name="preview" onClick="Preview();" />
	  </td>
      <td align="center">
      <input type="button" value="Save" name="save_draft" onClick="Save('save_draft');" />
	  </td>
      <td align="center" id="save_as_td">
      <input type="button" value="Save As" name="save_as" onClick="ShowSaveAsMsg();" />
	  </td>
	  <td align="center">
      <input type="button" value="Publish" name="publish" onClick="Save('publish');" />
	  </td>
      <td align="center">
      <input type="button" value="Revert" name="revert" onClick="Revert();" />
	  </td>
      <td align="center">
      <input type="button" value="See Site" name="live_view" 
<?php
		echo "onClick=\"window.open('..";
		echo $userfiles . $website_folder . "/";
      	if ($special == 1)
	  		echo "index";
		else
			echo $page_name;
		echo ".php', '_blank');\"";
?>
      />
	  </td>
      <td align="center"><input type="button" name="Clear" value="Clear" onClick="ClearContent();" /></td>
	  <td align="center">
	  <input type="button" name="Back" value="Back to Main Menu" onClick="MainMenu(saved);" />
	  </td>
	  <td width="8%">&nbsp;
      
	  </td>
      <td align="center">
      <input type="button" name="optimize" value="Optimize Images" onClick="OptimizeResetImages('optimize');" />
      </td>
      <td align="center">
      <input type="button" name="reset" value="Reset Images" onClick="OptimizeResetImages('reset');" />
      </td>
      <td>
<?php if ($access_level != "restricted") { ?>
      <input type="button" name="edit_styles" value="Edit Styles" onClick="window.open('edit_css.php?style_sheet=site','_blank');" />
<?php } else echo "&nbsp;"; ?>
      </td>
      <td width="8%">&nbsp;</td>
      <td align="center">
      <div id="help_button_sm" onClick="OpenHelpWindow();">
	    &nbsp;
      </div>
	  </td>
	  </tr></table>
    </form>

<div id="popup_msg_div">
<div id="StatusMsg"> </div>
<img src="images/close-button.png" alt="Close" id="popup_msg_close_btn" onClick="HideStatusMsg();" />
</div>

<div id="alert_msg_div">
<table border="0">
	<tr class="popup_msg_row">
		<td colspan="2" align="center">Your page is not saved.  Do you want to save your changes?</td>
    </tr>
    <tr class="popup_button_row">
    	<td align="center"><span onClick="Save('save_draft'); mTimer2 = setTimeout('MainMenu(true);', 5000);" class="popup_button">Yes</span></td>
        <td align="center"><span onClick="MainMenu(true);" class="popup_button">No</span></td>
    </tr>
</table>
</div>

<div id="save_as_div">
<table border="0">
	<tr class="popup_msg_row">
		<td align="center">Save as: </td>
        <td><input type="text" name="new_page_name" id="new_page_name" /></td>
    </tr>
    <tr class="popup_button_row">
    	<td align="center"><span onClick="HideSaveAsMsg();" class="popup_button">Cancel</span></td>
        <td align="right"><span onClick="SaveAs();" class="popup_button">OK</span>&nbsp;&nbsp;&nbsp;</td>
    </tr>
</table>
</div>

<script language="javascript" type="text/javascript">
	HideStatusMsg();
	HideSaveAsMsg();
	if ((mode == "blog") || (special == 1))
		document.getElementById("save_as_td").style.display = "none";
</script>
</body>
</html>
