/*
 * CodePress - Real Time Syntax Highlighting Editor written in JavaScript - http://codepress.org/
 * 
 * Copyright (C) 2006 Fernando M.A.d.S. <fermads@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify it under the terms of the 
 * GNU Lesser General Public License as published by the Free Software Foundation.
 * 
 * Read the full licence: http://www.opensource.org/licenses/lgpl-license.php
 */

CodePress = function(obj) {
	var cp_self = document.createElement('iframe');
	cp_self.textarea = obj;
	cp_self.textarea.disabled = true;
	cp_self.textarea.style.overflow = 'hidden';
	cp_self.style.height = cp_self.textarea.clientHeight +'px';
	cp_self.style.width = cp_self.textarea.clientWidth +'px';
	cp_self.textarea.style.overflow = 'auto';
	cp_self.style.border = '1px solid gray';
	cp_self.frameBorder = 0; // remove IE internal iframe border
	cp_self.style.visibility = 'hidden';
	cp_self.style.position = 'absolute';
	cp_self.options = cp_self.textarea.className;
	
	cp_self.initialize = function() {
		cp_self.editor = cp_self.contentWindow.CodePress;
		cp_self.editor.body = cp_self.contentWindow.document.getElementsByTagName('body')[0];
		cp_self.editor.setCode(cp_self.textarea.value);
		cp_self.setOptions();
		cp_self.editor.syntaxHighlight('init');
		cp_self.textarea.style.display = 'none';
		cp_self.style.position = 'static';
		cp_self.style.visibility = 'visible';
		cp_self.style.display = 'inline';
	};
	
	// obj can by a textarea id or a string (code)
	cp_self.edit = function(obj,language) {
		if(obj) cp_self.textarea.value = document.getElementById(obj) ? document.getElementById(obj).value : obj;
		if(!cp_self.textarea.disabled) return;
		cp_self.language = language ? language : cp_self.getLanguage();
		cp_self.src = CodePress.path+'codepress.html?language='+cp_self.language+'&ts='+(new Date).getTime();
		if(cp_self.attachEvent) cp_self.attachEvent('onload',cp_self.initialize);
		else cp_self.addEventListener('load',cp_self.initialize,false);
//		window.setTimeout(cp_self.initialize(), 100);
	};

	cp_self.getLanguage = function() {
		for (language in CodePress.languages) 
			if(cp_self.options.match('\\b'+language+'\\b')) 
				return CodePress.languages[language] ? language : 'generic';
	};
	
	cp_self.setOptions = function() {
		if(cp_self.options.match('autocomplete-off')) cp_self.toggleAutoComplete();
		if(cp_self.options.match('readonly-on')) cp_self.toggleReadOnly();
		if(cp_self.options.match('linenumbers-off')) cp_self.toggleLineNumbers();
	};
	
	cp_self.getCode = function() {
		return cp_self.textarea.disabled ? cp_self.editor.getCode() : cp_self.textarea.value;
	};

	cp_self.setCode = function(code) {
		cp_self.textarea.disabled ? cp_self.editor.setCode(code) : cp_self.textarea.value = code;
	};

	cp_self.toggleAutoComplete = function() {
		cp_self.editor.autocomplete = (cp_self.editor.autocomplete) ? false : true;
	};
	
	cp_self.toggleReadOnly = function() {
		cp_self.textarea.readOnly = (cp_self.textarea.readOnly) ? false : true;
		if(cp_self.style.display != 'none') // prevent exception on FF + iframe with display:none
			cp_self.editor.readOnly(cp_self.textarea.readOnly ? true : false);
	};
	
	cp_self.toggleLineNumbers = function() {
		var cn = cp_self.editor.body.className;
		cp_self.editor.body.className = (cn==''||cn=='show-line-numbers') ? 'hide-line-numbers' : 'show-line-numbers';
	};
	
	cp_self.toggleEditor = function() {
		if(cp_self.textarea.disabled) {
			cp_self.textarea.value = cp_self.getCode();
			cp_self.textarea.disabled = false;
			cp_self.style.display = 'none';
			cp_self.textarea.style.display = 'inline';
		}
		else {
			cp_self.textarea.disabled = true;
			cp_self.setCode(cp_self.textarea.value);
			cp_self.editor.syntaxHighlight('init');
			cp_self.style.display = 'inline';
			cp_self.textarea.style.display = 'none';
		}
	};

	cp_self.edit();
	return cp_self;
};

CodePress.languages = {	
	csharp : 'C#', 
	css : 'CSS', 
	generic : 'Generic',
	html : 'HTML',
	java : 'Java', 
	javascript : 'JavaScript', 
	perl : 'Perl', 
	ruby : 'Ruby',	
	php : 'PHP', 
	text : 'Text', 
	sql : 'SQL',
	vbscript : 'VBScript'
};


CodePress.run = function() {
	s = document.getElementsByTagName('script');
	for(var i=0,n=s.length;i<n;i++) {
		if(s[i].src.match('codepress.js')) {
			CodePress.path = s[i].src.replace('codepress.js','');
		}
	}
	t = document.getElementsByTagName('textarea');
	for(var i=0,n=t.length;i<n;i++) {
		if(t[i].className.match('codepress')) {
			id = t[i].id;
			t[i].id = id+'_cp';
			eval(id+' = new CodePress(t[i])');
			t[i].parentNode.insertBefore(eval(id), t[i]);
		} 
	}
};

if(window.attachEvent) window.attachEvent('onload',CodePress.run);
else window.addEventListener('DOMContentLoaded',CodePress.run,false);


