/*!
 * jrCMS Config class (php)
 *
 * @package jrCMS
 * @author Jens Raabe <jens@raabe-berlin.de>
 * @version $Id: cConfig.js 547 2011-06-28 14:51:42Z jensraabe $ 
 * 
 * @copyright Copyright (c) 2010 Jens Raabe
 * license: MIT-style license, see jrcms.js
 */

//pre-definition of class containted in the script file 
//for references and to speed up loading and availability in DOM

var JRCmsInit = new Class({
	
	_coreInfo: {
		name:	 		"JRCmsInit",
		instance:		"jrCmsInit",
		version:	 	"0.3.0",				//Release.Version.Minor
		revision:		"$Revision: 547 $",		//Revision set by svn		
		developer:		"Jens Raabe",
		developer_url:	"http://www.raabe-berlin.de",
		license: 		"MIT",
		classId:		0000
	}  
});


(function(){

	JRCmsInit.implement({

		Implements: [Options, JRCmsApi]

   		,options: new Object
		    		
   		,pageOptions: {
			//project settings
			jr_refreshVMenu:	true,			// permanent refresh of vertical menu (true/false)
			jr_menuType:		1,				// 0 css expand of submenu / 1 js expand of submenu
			jr_backupDB:		false,			// backup DB (webmaster tasks only) (true/false)
			jr_loginload:		'home',			// TODO: is this really used??
			jr_buttonType:		'iconText',		// Buttons with help text
												// 'home' reload last content or 'All' complete reload
			jr_logoutOnUnload:	false,			//
			jr_repMasterView:	true,			// enable/disable [true/false] master report view
			jr_repIntroLength: 	240,			// number of character in intro text
			jr_epAllId:			42,				// menu id which shows all events
			jr_epSelectRule:	'month',		// select default for eventlist
			jr_epSortOrder:		'ASC',			// default sort order in event list
			jr_loadPage:		true,			// enable inital loading
			jr_xinhaContentCss: 'css/xinha.css', // special styles of xinha
			jr_xinhaShowLoading: true,			// show xinha loading
			jr_epSelectRule:	'month',		// select default for eventlist
			jr_epSortOrder:		'ASC',			// default sort order in event list
			
			//do not change, for internal usage only
			jr_debug:			false,				// act/deact debugging window (true=1/false=0 or level 0..4)
			
			/* Carrousel option to turn it on/off. A lot of other options are in the plugin itself.*/
			jr_carrousel:		null,			// possible values ['divColHeader', 'divRTMenu']; divRTMenu isn't supported yet
			
			//list of plugins as array of {plugin, cmp, userlevel}
			//the plugin is identified by the pathname = filename 
			//plugins using the editor must be loaded after the JRCmsEditor
			jr_pluginList:  [
//							{pluginName:'JRCmsSpeed',			cmp:false, userLevel:'000'}
							{pluginName:'JRCmsGrid',			cmp:false, userLevel:'000'}
							,{pluginName:'JRCmsPuw',			cmp:false, userLevel:'000'}
							,{pluginName:'JRCmsMenu',			cmp:false, userLevel:'220'}
							,{pluginName:'JRCmsUser',			cmp:false, userLevel:'110'}
//							,{pluginName:'JRCmsUserProf',		cmp:false, userLevel:'000'}
							,{pluginName:'JRCmsEditor',			cmp:false, userLevel:'220'}
							,{pluginName:'JRCmsReports',		cmp:false, userLevel:'000'}
							,{pluginName:'JRCmsEventPlanner',	cmp:false, userLevel:'000'}
							 //must be placed after editors using the image show						
							,{pluginName:'JRCmsImgShow',		cmp:false, userLevel:'000'}
//							,{pluginName:'JRCmsRegatta',		cmp:false, userLevel:'000'}
			               ],
	
			//SlideShow options
			//should be optimized for projects by loading used types only
			jr_imgShowTypes : ['Flash', 'Lightbox', 'LightboxKB', 'Kenburns', 'Standard']
	
		}
		
		,jr_taskList: null						//TODO: special variable only used for task lists
	
		,getClassId: function() {
			return this._coreInfo.classId;
		}
		
		,initialize: function(options){
			
			//1) global settings
			this.setOptions(this._globalOptions);
			this.setOptions(this.pageOptions);
			if(this.options.jr_debug || this.options.jr_debug > 0) options.jr_debug = this.options.jr_debug;
			
			//2) initial setting
			this.setOptions(options);
	
			//3) preload images
			this.preLoadImages();
	
			//4) internationalization		
			//TODO: how could we use the MooTools mechanism??
			MooTools.lang.setLanguage(this.options.jr_lang+'-'+this.options.jr_lang.toUpperCase());
			this.retrieveLangFile('../lang/', 'lang.' + this.options.jr_lang,
					function(){this.setOptions({jr_langLoaded: true});
						//alert(this.getLangItem('data_length'));
					}.bind(this));
			
			//5) check for browser to prepare special handling
			if(Browser.Engine.trident) {
				var searchString = "MSIE";
				var idx = navigator.appVersion.indexOf(searchString);
				this.options.jr_ie_version = parseFloat(navigator.appVersion.substring(idx+searchString.length+1));
			
				//special processing for IE less version 7?
				if(this.options.jr_ie_version < 7) {
					//set global flag
					this.options.jr_ie_bug = true;
				}
			}
			
			//6) activate some dynamic options
			this.setOptions({
				jr_projURL:		this.convertDirToUrl(),
				jr_srcUrl:		this.convertDirToUrl(this.options.jr_srcUrl),
				jr_uploadUrl:	this.convertDirToUrl(this.options.jr_uploadUrl),
				jr_pluginsUrl:	this.convertDirToUrl(this.options.jr_srcUrl) + this.options.jr_pluginsUrl,
				jr_assetsUrl:	this.convertDirToUrl(this.options.jr_srcUrl) + this.options.jr_assetsUrl,
				jr_initParam:	JSON.decode('{"root":1,"id":2,"cssid":1}'),
				jr_maintParam:	JSON.decode('{"root":1,"id":2,"cssid":1}'),
				jr_base64:		(this.options.jr_base64 || !this.options.jr_debug) && !this.options.jr_ie_bug,
				jr_ie_version:	this.options.jr_ie_version,
				jr_ie_bug:		this.options.jr_ie_bug,
				//histoy switch consider at least IE6
				jr_histEnabled:	this.options.jr_ie_bug ? false : this.options.jr_histEnabled,
				jr_pluginList:	(!this.options.jr_ie_bug && this.options.jr_histEnabled
								? (!this.options.jr_debug
								  ?	([{pluginName:'JRCmsRsh', cmp:false, userLevel:'000'}].combine(this.options.jr_pluginList))
								  : ([{pluginName:'JRCmsLog', cmp:false, userLevel:'000'}
						             ,{pluginName:'JRCmsRsh', cmp:false, userLevel:'000'}].combine(this.options.jr_pluginList)))
						        : (!this.options.jr_debug
						          ? (this.options.jr_pluginList)
						          : ([{pluginName:'JRCmsLog', cmp:false, userLevel:'000'}].combine(this.options.jr_pluginList))))		  
				//TODO: set editorLevel on base of the Plugin setting
				//jr_editorLevel:	'100'
			});
	
			//7) set plugin Editorlevel on base of the provided (php) level
			//TODO:
	
		}
	}); //implements end
})();	

/* ==================================================================== */
/* jrCMS: init sequence                                                 */
/* ==================================================================== */

//(1) intialize options provided by PHP
window.jrCmsInit = new JRCmsInit({jr_lang:jrlang
								,jr_userLevel: ulevel
								,jr_uploadUrl: uploadDIR
								,jr_editorLevel: editorLevel
								,jr_webmasterLevel: webmasterLevel
								,jr_loginHidden: loginHidden
								,jr_debug: debug
								});	
jrCmsInit.jr_taskList = taskList;

window.addEvent("domready",function(){
		// init core
//		if ($defined(jrCmsInit.options.jr_langLoaded) && jrCmsInit.options.jr_langLoaded){
			window.jrCMS = new JRCmsCore(jrCmsInit.options, 'jrCMS');
//		} else {
//			(function(){
//				window.jrCmsInit.jrLog('>>>> jrCMS init called by delay function <<<<');
//				window.jrCMS = new JRCmsCore(jrCmsInit.options, 'jrCMS');
//				}).delay(100);
//		}
});

window.onunload = function() {
	// start async DB backup
	if(jrCmsInit.options.jr_dbischanged) jrCMS.backup();
	// logout
	if(jrCmsInit.options.jr_logoutOnUnload) jrCmsInit.jrLog('out');
};
