<?php
/**
* @author:  Bunzia Alexander <nifus@mail.ru> <http://www.weblancer.net/users/nifus/>
* @copyright: Copyright (c) 2010, Bunzia Alexander
* @version: 1.0
* @license: http://www.gnu.org/copyleft/gpl.html GNU/GPL
* @package HiLo
*/


/**
* редактор подсветки кода
*
*/

class codemirror{
	private $name = false;
	private $value='';
	private $vars = array();
	private $attr = array();

  static function load(){
  global $MAIN_PAGE;
    $MAIN_PAGE -> set_js(MAIN_SOURCE_URL.'/ext/codeMirror2/lib/codemirror.js');

    $MAIN_PAGE -> set_css(MAIN_SOURCE_URL.'/ext/codeMirror2/theme/default.css');

    $MAIN_PAGE -> set_js(MAIN_SOURCE_URL.'/ext/codeMirror2/mode/xml/xml.js');
    $MAIN_PAGE -> set_js(MAIN_SOURCE_URL.'/ext/codeMirror2/mode/htmlmixed/htmlmixed.js');
    $MAIN_PAGE -> set_js(MAIN_SOURCE_URL.'/ext/codeMirror2/mode/javascript/javascript.js');
    $MAIN_PAGE -> set_js(MAIN_SOURCE_URL.'/ext/codeMirror2/mode/css/css.js');
        $MAIN_PAGE -> set_js(MAIN_URL.'/source/tmpl/default/js/editor/codemirror.js');

    $MAIN_PAGE -> set_css(MAIN_SOURCE_URL.'/ext/codeMirror2/lib/codemirror.css');
  }
  
  		/*
  		*	забиваем стандартный набор панелей, теоретически нужны панели для админа и пользователя и 
  		*	это актуально для любого редактора. Потом можно будет менять всё это с помощью универсальной 
  		*	функции set_toolbar					
  		*/
  	function __construct(){
  	global  $MAIN_USER;
  		//include(MAIN_SOURCE_PATH."/ext/ckeditor/ckeditor.php") ;
  		
  		
		 
  	}
  	
  		//	устанавливаем name редактора
  	public function set_name($v){
  		if ( empty($v) ){
			  sys_error(ERROR_500,"1024: bad name '$v' for editor");
		  }
  		$this -> name = $v;
  		return true;
  	}	
  		//	устанавлиаем первончальное содержимое редактора
  	public function set_value($v){
  		$this -> value = $v;
  		return true;
  	}

		//	устанавливаем атрибуты
  	public function set_attr($k,$v){
  		
  	}
  	
  	
  	
  	
  	
  		//	возвращаем код редактора
  	public function show(){
	
    	if ( empty($this -> name) ){
			  sys_error(ERROR_500,"1024: empty name for editor");
		  }
		

		
		$text = str_replace('{MAIN_SOURCE_URL}',MAIN_SOURCE_URL,SCRIPT_TMPL);
		//$text = str_replace('{name}','{'.$this -> name.'}',$text);
		return  $text;
  	}
  	
  	
  	
 	 
 	 
 	 
}

 


define('SCRIPT_TMPL',
<<<SCRIPT_TMPL
 <link rel="stylesheet" href="{MAIN_SOURCE_URL}/ext/codeMirror2/lib/codemirror.css">
 
    <script src="{MAIN_SOURCE_URL}/ext/codeMirror2/lib/codemirror.js"></script>
    <link rel="stylesheet" href="{MAIN_SOURCE_URL}/ext/codeMirror2/theme/default.css">
    <script src="{MAIN_SOURCE_URL}/ext/codeMirror2/mode/xml/xml.js"></script>
<script src="{MAIN_SOURCE_URL}/ext/codeMirror2/mode/htmlmixed/htmlmixed.js"></script>
    <script src="{MAIN_SOURCE_URL}/ext/codeMirror2/mode/javascript/javascript.js"></script>
    <script src="{MAIN_SOURCE_URL}/ext/codeMirror2/mode/css/css.js"></script>
    
<script type="text/javascript">



editor = {}
editor.create = function(name){
	 
	if ( !document.getElementById(name) ){
	  return false;
		alert('empty name "'+name+'": '+$(name).size())
	}
	
 
    	
  var editor = CodeMirror.fromTextArea(document.getElementById(name), {
   mode: "text/html",
   matchBrackets: true,
    tabMode: "indent",
   onKeyEvent: function(i, e) {
          // Hook into F11
          if ((e.keyCode == 122 || e.keyCode == 27) && e.type == 'keydown') {
            e.stop();
            return toggleFullscreenEditing();
          }
        },
  lineNumbers: true,
 

});
 function toggleFullscreenEditing()
    {
        var editorDiv = $('.CodeMirror-scroll');
        if (!editorDiv.hasClass('fullscreen')) {
            toggleFullscreenEditing.beforeFullscreen = { height: editorDiv.height(), width: editorDiv.width() }
            
           //editorDiv.parent().css('display','static')
            //$('body').append( editorDiv )
            //editorDiv.appeng()
            editorDiv.addClass('fullscreen');
            editorDiv.height('100%');
            editorDiv.width('100%');
            editor.refresh();
        }
        else {
            editorDiv.removeClass('fullscreen');
            editorDiv.height(toggleFullscreenEditing.beforeFullscreen.height);
            editorDiv.width(toggleFullscreenEditing.beforeFullscreen.width);
            editor.refresh();
        }
    }

  $(".CodeMirror").css('width', $('#'+name).css('width'))
//  $(".CodeMirror").css('height', $('#'+name).css('height'))
 
} 

</script>

SCRIPT_TMPL

);

?>
