<?php
////////////////////////////////////////////////////////////////////////
// Breeze Website Builder
// Copyright Silver Dolphin Solutions, LLC, Edward Lemmers, P.E.
// For licensing, see license.txt.
// Created:	3/9/2013	ELL
// Revised: 6/28/2013	ELL
// Implemented popup message class.
// Revised: 12/13/2013	ELL
// Added refresh of main menu to update buttons and set to last selections.
// Revised: 12/22/2013	ELL
// Restored ability to check for saved and prompt user for action.

	include ("check_session.php");
		
	$mode = $_REQUEST['mode'];
	$refresh_buttons = $_REQUEST['refresh_buttons'];
	
	$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 language="javascript" type="text/javascript" src="../ckeditor/ckeditor.js"></script>
<script type="text/javascript" src="js/jquery-1.9.1.min.js"></script>
<script language="javascript" type="text/javascript" src="js/browser_detect.js"></script>
<script language="javascript" type="text/javascript" src="js/mouse_position.js"></script>
<script language="javascript" type="text/javascript" src="js/popup_msg.js"></script>
<script language="javascript" 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;
 
 var browser = BrowserDetect.browser;
 var browser_version = BrowserDetect.version;
// alert(browser + " " + browser_version);
</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;
			window.parent.SetWorkSavedNotSaved(saved);
			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 (!((browser == "Explorer") && (browser_version <= 7)))
		{
<?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 = '99%';

		if ((browser == "Explorer") && (browser_version <= 7))
        	CKEDITOR.config.height = document.body.clientHeight - 235;
		else if ((browser == "Opera") && (browser_version < 13))
			CKEDITOR.config.height = window.innerHeight - 210;
		else if ((browser == "Explorer") && (browser_version == 8))
			CKEDITOR.config.height = document.body.clientHeight - 210;
		else
			CKEDITOR.config.height = editorElem.clientHeight - 140;
        CKEDITOR.config.resize_enabled = false;
		
		LoadPage();
		
    //]]>
    </script>
            
      
	  <table width="100%" 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" style="min-width:90px;">
      <div class='button_console_preview_sm' style="min-width:90px;" onClick="Preview();"><span class='button_console_text_w_sm_icon'>Preview</span></div>
	  </td>
      <td align="center" style="min-width:80px;">
      <div class='button_console_save_sm' style="min-width:80px;" onClick="Save('save_draft');"><span class='button_console_text_w_sm_icon'>Save</span></div>
	  </td>
      <td align="center" id="save_as_td" style="min-width:90px;">
      <div class='button_console_save_sm' style="min-width:90px;" onClick="ShowSaveAsMsg();"><span class='button_console_text_w_sm_icon'>Save As</span></div>
	  </td>
	  <td align="center" style="min-width:90px;">
      <div class='button_console_save_sm' style="min-width:90px;" onClick="Save('publish');"><span class='button_console_text_w_sm_icon'>Publish</span></div>
	  </td>
      <td align="center" style="min-width:80px;">
      <div class='button_console_cancel_sm' style="min-width:80px;" onClick="Revert();"><span class='button_console_text_w_sm_icon'>Revert</span></div>
	  </td>
      <td align="center">
      <div class='button_console_preview_sm' style="min-width:90px;"
<?php
		echo "onClick=\"window.open('..";
		echo $userfiles . $website_folder . "/";
      	if ($special == 1)
	  		echo "index";
		else
			echo $page_name;
		echo ".php', '_blank');\"";
?>
      ><span class='button_console_text_w_sm_icon' style="min-width:90px;">See Site</span></div>
	  </td>
      <td align="center" style="min-width:75px;">
      <div class='button_console_delete_sm' style="min-width:75px;" onClick="ClearContent();"><span class='button_console_text_w_sm_icon'>Clear</span></div>
      </td>
	  <td width="2%">&nbsp;
      
	  </td>
      <td align="center" style="min-width:145px;">
      <div class='button_console_image_sm' style="min-width:145px;" onClick="OptimizeResetImages('optimize');"><span class='button_console_text_w_sm_icon'>Optimize Images</span></div>
      </td>
      <td align="center" style="min-width:125px;">
      <div class='button_console_image_sm' style="min-width:125px;" onClick="OptimizeResetImages('reset');"><span class='button_console_text_w_sm_icon'>Reset Images</span></div>
      </td>
      <td style="min-width:115px;">
<?php if ($access_level != "restricted") { ?>
      <div class='button_console_edit_sm' style="min-width:115px;" onClick="window.open('edit_css.php?style_sheet=site','_blank');"><span class='button_console_text_w_sm_icon'>Edit Styles</span></div>
<?php } else echo "&nbsp;"; ?>
      </td>
      <td width="1%">&nbsp;</td>
      <td align="center" width="2%">
      <div class="help_button" onClick="OpenHelpWindow();">
	    &nbsp;
      </div>
	  </td>
	  </tr></table>
    </form>

<?php include("lib/popup_msg.html"); ?>

<script language="javascript" type="text/javascript">
	HidePopupMsg();
	
	if ((mode == "blog") || (special == 1))
		document.getElementById("save_as_td").style.display = "none";
		
<?php 
	if ($refresh_buttons)	{
		echo "	parent.LoadPluginButtons();\n";
	}
?>
</script>
</body>
</html>
