function Ptreal_Engine(){
	this.majorVersion="1";
	this.minorVersion="0.1";
	this.releaseDate="2007-01-25";
	this.instances=new Array();
	this.switchClassCache=new Array();
	this.windowArgs=new Array();
	this.loadedFiles=new Array();
	this.configs=new Array();
	this.settings=new Array();
	this.currentConfig=0;
	this.eventHandlers=new Array();
	var ua=navigator.userAgent;
	this.isMSIE=(navigator.appName=="Microsoft Internet Explorer");
	this.isMSIE5=this.isMSIE&&(ua.indexOf('MSIE 5')!=-1);
	this.isMSIE5_0=this.isMSIE&&(ua.indexOf('MSIE 5.0')!=-1);
	this.isGecko=ua.indexOf('Gecko')!=-1;
	this.isSafari=ua.indexOf('Safari')!=-1;
	this.isOpera=ua.indexOf('Opera')!=-1;
	this.isMac=ua.indexOf('Mac')!=-1;
	this.isNS7=ua.indexOf('Netscape/7')!=-1;
	this.isNS71=ua.indexOf('Netscape/7.1')!=-1;
	this.dialogCounter=0;
	this.plugins=new Array();
	this.themes=new Array();
	this.loadedPlugins=new Array();
	this.buttonMap=new Array();
	this.isLoaded=false;
	if(this.isOpera){
		this.isMSIE=true;
		this.isGecko=false;
		this.isSafari=false;
	}
	this.idCounter=0;
};

Ptreal_Engine.prototype={
	
	init:function(settings){
		var theme;
		this.settings=settings;
	},
	
	
	/*cancelEvent=function(e){
		if(this.isMSIE){
			e.returnValue=false;
			e.cancelBubble=true;
		}else 
			e.preventDefault();
	},*/

	execCommand:function(file,command,user_interface,value){
		user_interface=user_interface?user_interface:false;
		value=value?value:null;
		
		switch (command) {
			case "gallery":
				var template = new Array();
				template['file']   = file;
				
                template['width']  = 1024;
			    template['height'] = 670;
				/*var inst = this.getInstanceById(editor_id);
				var elm = inst.getFocusElement();

				if (elm != null && this.getAttrib(elm, 'class').indexOf('mceItem') != -1)
					return true;*/

				this.openWindow(template, {inline : "yes",scrollbars: "yes"/*,fullscreen: "no"*/});
				return;
		}

	},
	
	/*,applyTemplate:function(h,as){
		var i,s,ar=h.match(new RegExp('\\{\\$[a-z0-9_]+\\}','gi'));
		for(i=ar.length-1;i>=0;i--){
			s=ar[i].substring(2,ar[i].length-1);
			if(s.indexOf('lang_')==0&&tinyMCELang[s])
				h=this.replaceVar(h,s,tinyMCELang[s]);
			else if(as&&as[s])
				h=this.replaceVar(h,s,as[s]);
			else if(this.settings[s])
				h=this.replaceVar(h,s,tinyMCE.settings[s]);
		}
		h=tinyMCE.replaceVar(h,"themeurl",tinyMCE.themeURL);
		return h;
		},
		*/
	
	replaceVar:function(h,r,v){
		return h.replace(new RegExp('{\\\$'+r+'}','g'),v);
	},
	openWindow:function(template,args){
		var html,width,height,x,y,resizable,scrollbars,url;
		args['mce_template_file']=template['file'];
		args['mce_width']=template['width'];
		args['mce_height']=template['height'];
		this.windowArgs=args;
		html=template['html'];
		if(!(width=parseInt(template['width'])))
			width=320;
		if(!(height=parseInt(template['height'])))
			height=200;
		if(this.isMSIE)
			height+=40;
		else 
			height+=20;
		x=parseInt(screen.width/2.0)-(width/2.0);
		y=parseInt(screen.height/2.0)-(height/2.0);
		resizable=(args&&args['resizable'])?args['resizable']:"no";
		scrollbars=(args&&args['scrollbars'])?args['scrollbars']:"no";
		if(template['file'].charAt(0)!='/'&&template['file'].indexOf('://')==-1)
			url=this.baseURL+"/themes/"+this.getParam("theme")+"/"+template['file'];
		else 
			url=template['file'];
		for(var name in args){
			if(typeof(args[name])=='function')
			continue;
		url=this.replaceVar(url,name,escape(args[name]));
		}
		url=template['file'];
		if(html){
			html=this.replaceVar(html,"css",this.settings['popups_css']);
			html=this.applyTemplate(html,args);
			var win=window.open("","mcePopup"+new Date().getTime(),"top="+y+",left="+x+",scrollbars="+scrollbars+",dialog=yes,minimizable="+resizable+",modal=yes,width="+width+",height="+height+",resizable="+resizable);
		
		
			if(win==null){
				alert(thisLang['lang_popup_blocked']);
				return;
			}
			win.document.write(html);
			win.document.close();
			win.resizeTo(width,height);
			win.focus();
		}else{
			if((this.isMSIE&&!this.isOpera)&&resizable!='yes'&&this.settings["dialog_type"]=="modal"){
				height+=10;
				var features="resizable:"+resizable+";scroll:"+scrollbars+";status:yes;center:yes;help:no;dialogWidth:"+width+"px;dialogHeight:"+height+"px;";
				
			window.showModalDialog(url,window,features);
			}else{
				var modal=(resizable=="yes")?"no":"yes";
				if(this.isGecko&&this.isMac)
					modal="no";
				if(template['close_previous']!="no")
					try{
					this.lastWindow.close();
				}catch(ex){}
				if(args['fullscreen']){
					var win=window.open(url, '', 'fullscreen=yes, scrollbars=auto');
				}else
					var win=window.open(url,"mcePopup"+new Date().getTime(),"top="+y+",left="+x+",scrollbars="+scrollbars+",dialog="+modal+",minimizable="+resizable+",modal="+modal+",width="+width+",height="+height+",resizable="+resizable);
				
				if(win==null){
					alert(thisLang['lang_popup_blocked']);
					return;
				}
				if(template['close_previous']!="no")
					this.lastWindow=win;
				eval('try { win.resizeTo(width, height); } catch(e) { }');
				if(this.isGecko){
					if(win.document.defaultView.statusbar.visible)
						win.resizeBy(0,this.isMac?10:24);
				}
				win.focus();
			}
		}
	},

	closeWindow: function(win){
		win.close();
	},
	
	getParam:function(name,default_value,strip_whitespace,split_chr){
		var value=(typeof(this.settings[name])=="undefined")?default_value:this.settings[name];
		if(value=="true"||value=="false")
			return(value=="true");
		if(strip_whitespace)
			value=this.regexpReplace(value,"[ \t\r\n]","");
		if(typeof(split_chr)!="undefined"&&split_chr!=null){
			value=value.split(split_chr);
			var outArray=new Array();
			for(var i=0;i<value.length;i++){
				if(value[i]&&value[i]!="")
					outArray[outArray.length]=value[i];
			}
			value=outArray;
		}return value;
	},
	regexpReplace:function(in_str,reg_exp,replace_str,opts){
		if(in_str==null)
			return in_str;
		if(typeof(opts)=="undefined")
			opts='g';
		var re=new RegExp(reg_exp,opts);
		return in_str.replace(re,replace_str);
	},
	
	trim:function(s){
		return s.replace(/^\s*|\s*$/g,"");
	}
	
	,getInstanceById:function(editor_id){
		var inst=this.instances[editor_id];
		if(!inst){
			for(var n in ptreal.instances){
				var instance=ptreal.instances[n];
				if(!ptreal.isInstance(instance))
					continue;
				if(instance.formTargetElementId==editor_id){
					inst=instance;
					break;
				}
			}
		}
		return inst;
	}
	
	,setWindowArg:function(n,v){
		this.windowArgs[n]=v;
	}

	,getWindowArg:function(n,d){
		return(typeof(this.windowArgs[n])=="undefined")?d:this.windowArgs[n];
	}
};

var ptreal = Ptreal_Engine;
var ptreal = new Ptreal_Engine();

/*var Ptreal_ImageManagerPlugin = {
	getInfo : function() {
		return {
			longname : 'Ptreal Image Manager',
			author : 'Luciano Letra',
			authorurl : 'http://www.ptreal.com',
			infourl : '',
			version : '1.0.0'
		};
	},

	execCommand : function(editor_id, element, command, user_interface, value) {
		switch (command) {
			case "ImageManager":
				var template = new Array();
				template['file']   = ptreal.getParam('site')+'/index.php?m=jce&a=jce&suppressHeaders=true&task=plugin&plugin=imgmanager&file=manager.php';
                template['width']  = 560;
			    template['height'] = 560;

				var inst = ptreal.getInstanceById(editor_id);
				var elm = inst.getFocusElement();

				if (elm != null && ptreal.getAttrib(elm, 'class').indexOf('mceItem') != -1)
					return true;

				ptreal.openWindow(template, {editor_id : editor_id, inline : "yes"});

				return true;
		}

		return false;
	},
	getInstanceById:function(editor_id){
		var inst=this.instances[editor_id];
		if(!inst){
			for(var n in ptreal.instances){
				var instance=ptreal.instances[n];
				if(!ptreal.isInstance(instance))
					continue;
				if(instance.formTargetElementId==editor_id){
					inst=instance;
					break;
				}
			}
		}
		return inst;
	},
	
	cleanup : function(type, content) {
		switch (type) {
			case "insert_to_editor_dom":
				var imgs = content.getElementsByTagName("img");
				for (var i=0; i<imgs.length; i++) {
					var onmouseover = ptreal.cleanupEventStr(ptreal.getAttrib(imgs[i], 'onmouseover'));
					var onmouseout = ptreal.cleanupEventStr(ptreal.getAttrib(imgs[i], 'onmouseout'));

					if ((src = this._getImageSrc(onmouseover)) != "") {
						if (ptreal.getParam('convert_urls'))
							src = ptreal.convertRelativeToAbsoluteURL(ptreal.settings['base_href'], src);

						imgs[i].setAttribute('onmouseover', "this.src='" + src + "';");
					}

					if ((src = this._getImageSrc(onmouseout)) != "") {
						if (ptreal.getParam('convert_urls'))
							src = ptreal.convertRelativeToAbsoluteURL(ptreal.settings['base_href'], src);

						imgs[i].setAttribute('onmouseout', "this.src='" + src + "';");
					}
				}
				break;

			case "get_from_editor_dom":
				var imgs = content.getElementsByTagName("img");
				for (var i=0; i<imgs.length; i++) {
					var onmouseover = ptreal.cleanupEventStr(ptreal.getAttrib(imgs[i], 'onmouseover'));
					var onmouseout = ptreal.cleanupEventStr(ptreal.getAttrib(imgs[i], 'onmouseout'));

					if ((src = this._getImageSrc(onmouseover)) != "") {
						if (ptreal.getParam('convert_urls'))
							src = eval(ptreal.settings['urlconverter_callback'] + "(src, null, true);");

						imgs[i].setAttribute('onmouseover', "this.src='" + src + "';");
					}

					if ((src = this._getImageSrc(onmouseout)) != "") {
						if (ptreal.getParam('convert_urls'))
							src = eval(ptreal.settings['urlconverter_callback'] + "(src, null, true);");

						imgs[i].setAttribute('onmouseout', "this.src='" + src + "';");
					}
				}
				break;
		}

		return content;
	},

	handleNodeChange : function(editor_id, node, undo_index, undo_levels, visual_aid, any_selection) {
		if (node == null)
			return;

		do {
			if (node.nodeName == "IMG" && ptreal.getAttrib(node, 'class').indexOf('mceItem') == -1) {
				ptreal.switchClass(editor_id + '_imgmanager', 'mceButtonSelected');
				return true;
			}
		} while ((node = node.parentNode));

		ptreal.switchClass(editor_id + '_imgmanager', 'mceButtonNormal');

		return true;
	},

	_getImageSrc : function(s) {
		var sr, p = -1;

		if (!s)
			return "";

		if ((p = s.indexOf('this.src=')) != -1) {
			sr = s.substring(p + 10);
			sr = sr.substring(0, sr.indexOf('\''));

			return sr;
		}

		return "";
	}
};

Ptreal_Control.prototype={
	hasPlugin:function(n){
		var i;
		for(i=0 ; i < this.plugins.length ; i++){
			if(this.plugins[i]==n)
				return true;
		}
		return false;
	},
	
	addPlugin:function(n,p){
		if(!this.hasPlugin(n)){
			ptreal.addPlugin(n,p);
			this.plugins[this.plugins.length] = n;
		}
	}
};
*/

