/*
 * ProFrom - Professzionális Felhasználói Felületek $ proform.plugin.js,v 1.7 2005/06/10 14:13:04 gyuris Exp $
 */

proform.plugin.datepicker = {
	init : function() {
		var els, i, loadScript, elStyle;
		if ( !proform.plugin.datepicker.config.use ) {
			return; // ki van kapcsolva
		};
		if ( !( is.ie || is.gecko || ( is.opera && is.appVer >= 7.5 ) ) ) {
			return false;
		}
		// local-datetime
		if ( proform.plugin.datepicker.config.useTimeZone ) {
			proform.plugin.datepicker.config.formatString['local-datetime'] += proform.plugin.datepicker.getTimeZone();
		};
		// megnézzük, hogy szükség van-e ltölteni a plugin-t;
		els = proform.form.getElementsByTagName('input');
		for ( i = 0; i < els.length; i++ ) {
			if ( hasClass( els[i], proform.plugin.datepicker.config.initClass ) ) {
				// stílsulap letöltése
				elStyle = document.createElement('link');
				elStyle.setAttribute('rel', 'stylesheet');
				elStyle.setAttribute('type', 'text/css');
				elStyle.setAttribute('charset', 'utf-8');
				elStyle.setAttribute('href', proform.plugin.datepicker.config.pathToRoot + 'calendar.css' );
				document.getElementsByTagName('HEAD')[0].appendChild( elStyle );
				// scriptek letöltése
				loadScript = new ScriptLoader( document, function() { 
					proform.plugin.datepicker.start(); 
				} );
				loadScript.add( { 
					src : proform.plugin.datepicker.config.pathToRoot + 'calendar.js', 
					charset : 'UTF-8'
				} );
				loadScript.add( { 
					src : proform.plugin.datepicker.config.pathToRoot + 'lang/calendar-' + proform.plugin.datepicker.config.lang + '.js',
					charset : 'UTF-8'
				} );
				loadScript.add( { 
					src : proform.plugin.datepicker.config.pathToRoot + 'calendar-setup.js', 
					charset : 'UTF-8'
				} );				
				loadScript.load();
				break;
			};
		};
	},
	start : function() {
		var els, i, j, aMatch;
		if ( typeof Calendar == 'undefined' || typeof Calendar.setup == 'undefined' || typeof Calendar._DN == 'undefined' ) {
			return; // nincs minden szükséges állomány csatolva
		};
		// elindítjuk a kért elemekre a datepicker-t
		els = proform.form.getElementsByTagName('input');
		for ( i = 0; i < els.length; i++ ) {
			if ( hasClass( els[i], proform.plugin.datepicker.config.initClass ) ) {
				if ( ( aMatch = els[i].className.match( proform.condition.supportedDateFormats ) ) != null ) {
					if ( typeof proform.condition.format[aMatch[1]] != 'undefined' ) { // telepítve van már
						Calendar.setup( { 
							inputField :  els[i].id, 
							ifFormat : proform.plugin.datepicker.config.formatString[aMatch[1]], //             proform.condition.getFormatString( aMatch[1] ), 
							showsTime : ( /time/.test( aMatch[1] ) ),
							onUpdate : function( cal ) { proform.onModify( null, cal.params.inputField ); }
						} );
					};
				};
			};
		};
	},
	disable : function() {
		proform.plugin.datepicker.setConfig( 'use', false );
	},
	config : {
		use : true,
		useTimeZone : false,
		pathToRoot : '/ui/plugin/calendar/',
		initClass : 'datepicker', // CSS osztály
		lang : document.documentElement.getAttribute('lang'),
		formatString : {
			'datetime'       : '%Y-%m-%dT%H:%M:%S.00Z',      // 1995-09-31T23:59:59.99Z  1995-09-31T23:59:59.99+12:00
			'datetime-hu'    : '%Y.%m.%d. %H:%M:%S',         // 2002.12.31. 23:59:59
			'datetime-de'    : '%d.%m.%Y %H:%M:%S',          // 31.12.2002 23:59:59
			'datetime-en'    : '%d-%m-%Y %H:%M:%S',          // 32-12-2002 23:59:59
			'local-datetime' : '%Y-%m-%dT%H:%M:%S.00',       // 2005-05-14T01:00:00.00+02:00 (kiegészítődik inicializáláskor)
			'date'           : '%Y-%m-%d',                   // 1995-12-31
			'date-hu'        : '%Y.%m.%d.',                  // 1995.12.31.
			'date-de'        : '%d.%m.%Y',                   // 31.12.1995
			'date-en'        : '%d/%m/%Y',                   // 31/12/1995
			'month'          : '%Y-%m',                      // 1995-12
			'month-hu'       : '%Y.%m.',                     // 1995.12.
			'month-de'       : '%m.%Y',                      // 12.1995
			'month-en'       : '%m/%Y',                      // 12/1995
			'week'           : '%Y-W%W',                     // 1995-W52
			'week-hu'        : '%Y %W. hét',                 // 1995. 52. hét
			'week-de'        : '%W. Woche %Y',               // 52. Woche 1995
			'week-en'        : '%Y %W. week',                // 1995 52. week
			'time'           : '%H:%M:%S.0',                 // 23:59:00.0 vagy 00:00:00.0000
			'time-hu'        : '%H:%M:%S',                   // 23:59:00
			'time-de'        : '%H:%M:%S',                   // 23:59:00
			'time-en'        : '%H:%M:%S'                    // 23:59:00 (britt)
		}
	},
	setConfig: function( sConfig, sValue ) {
		proform.plugin.datepicker.config[sConfig] = sValue;
	},
	getTimeZone : function () {
		var nTimezone, nHours, nMinutes;
		nTimezone = new Date().getTimezoneOffset();
		nHours = Math.floor( Math.abs( nTimezone / 60 ) );
		nMinutes = Math.abs( nTimezone % 60 );
		return ( ( nTimezone <= 0 ) ? '+' : '-' ) + ( nHours < 10 ? '0' + nHours : nHours ) + ':' + ( ( nMinutes == 0 ) ? '00' : nMinutes );
	}
};
proform.plugin.richtext = {
	init : function() {
		var els, i, loadScript;
		if ( !proform.plugin.richtext.config.use ) {
			return; // ki van kapcsolva
		};
		if ( !( is.ie6 || ( is.gecko && is.geckoRv >= 1.3 && !is.cm ) ) ) {
			return;
		};
		els = proform.form.getElementsByTagName('textarea');
		for ( i = 0; i < els.length; i++ ) {
			if ( hasClass( els[i], proform.plugin.richtext.config.initClass ) ) {
				loadScript = new ScriptLoader( document, function() { 
					proform.plugin.richtext.start(); 
				} );
				loadScript.add( { 
					src : proform.plugin.richtext.config.pathToRoot + 'project/' + proform.plugin.richtext.config.projectId + '/project.js', 
					id : 'xeditorJS-project', 
					charset : 'UTF-8'
				} );
				loadScript.load();
				break; // csak 1× kell lehívni
			};
		};
	},
	start : function() {
		var loadScript, i;
		loadScript = new ScriptLoader( document, function() { 
			// átallítjuk a szokásos 'xe'-ről a beállítására
			xEConfig.initClass = proform.plugin.richtext.config.initClass;
			xEConfig.pathToRoot = proform.plugin.richtext.config.pathToRoot;
			xEConfig.projectId = proform.plugin.richtext.config.projectId;
			xEConfig.articleId = proform.plugin.richtext.config.articleId;
			bxEditorDocumentLoaded = true;
			createEditor();
		} );
		// 2004.08.03. 12:26 plugin nem írhat felül semmilyen lib funkciót!
		//loadScript.add( { src :  proform.plugin.richtext.config.pathToRoot + 'js/_lib.js', id : 'xeditorJS-lib', charset : 'UTF-8' } );
		for ( i = 0; i < xEProject.acceptedLang.length; i++ ) {
			loadScript.add( { 
				src : proform.plugin.richtext.config.pathToRoot + 'lang/' + xEProject.acceptedLang[i] + '/lang.js', 
				id : 'xeditorJS-lang-' + xEProject.acceptedLang[i], 
				charset : 'UTF-8'
			} );
		};
		loadScript.add( { 
			src :  proform.plugin.richtext.config.pathToRoot + 'js/xe.js', 
			id : 'xeditorJS-xeditor', 
			charset : 'UTF-8'
		} );
		loadScript.load();
	},
	disable : function() {
		proform.plugin.richtext.setConfig( 'use', false );
	},
	config : {
		use : false,
		// CSS osztály
		initClass : 'richtext',
		// beállítások
		pathToRoot : '../ui/plugin/richtext/',
		projectId : 'my-project',
		articleId : null
	},
	setConfig: function( sConfig, sValue ) {
		proform.plugin.richtext.config[sConfig] = sValue;
	}
};
proform.plugin.textareaAutoHeight = {
	init : function() {
		var loadScript, els, i;
		if ( !proform.plugin.textareaAutoHeight.config.use ) {
			return;
		};
		els = proform.form.getElementsByTagName('textarea');
		for ( i = 0; i < els.length; i++ ) {
			if ( hasClass( els[i], proform.plugin.textareaAutoHeight.config.initClass ) ) {
				loadScript = new ScriptLoader( document, function() {
					proform.plugin.textareaAutoHeight.start(); 
				} );
				loadScript.add( {
					src : proform.plugin.textareaAutoHeight.config.pathToRoot + 'textareaautoheight.js', 
					charset : 'UTF-8'
				} );
				loadScript.load();
				break;
			};
		};
	},
	start : function() {
		var els, i;
		if ( typeof textareaAutoHeight == 'undefined' ) { // nincs minden szükséges állomány csatolva
			return; 
		};
		els = proform.form.getElementsByTagName('textarea');
		for ( i = 0; i < els.length; i++ ) {
			if ( hasClass( els[i], proform.plugin.textareaAutoHeight.config.initClass ) ) {
				textareaAutoHeight.init( els[i] );
			};
		};
	},
	disable : function() {
		proform.plugin.textareaAutoHeight.setConfig( 'use', false );
	},
	config : {
		use : true,
		initClass : 'textareaautoheight',
		pathToRoot : '/ui/plugin/textareaautoheight/'
	},
	setConfig: function( sConfig, sValue ) {
		proform.plugin.textareaAutoHeight.config[sConfig] = sValue;
	}
};


/**
 * ScriptLoader v1.4
 * Letöltődés után új script betöltése. Meg kell adni, hogy mely dokumentum 
 * töltse le, mit csináljon a letöltés után, és hogy miket töltsön le. Ezeket 
 * egymás után kezdi el letölteni, hogy az objecktum-függések ne okozzanak 
 * problémát, tehát a szkriptek sorrendje fontos. IE és Geckó aszinkron 
 * betöltődésre megy tovább, Opera8 pedig nem aszikron módon tölti be
 * attribútumok: src kötelező, de más is megadható
 */
function ScriptLoader( oDoc, fCommand ) {
	function Script( oAttributes ) {
		this.attributes = oAttributes;
		this.src = oAttributes.src;
		this.el = null;
	};
	this.scripts = [];
	this.add = function( oAttributes ) {
		this.scripts[this.scripts.length] = new Script( oAttributes );
		return true;
	};
	this.scriptIndex = null;
	this.remove = function( sSrc ) {
		var i;
		for ( i = 0; i < this.scripts.length; i++ ) {
			if ( sSrc == this.scripts[i].src ) {
				return this.scripts.splice( i, 1 );
			};
		};
		return false;
	};
	this.load = function() {
		var i;
		this.scriptIndex = 0;
		if ( this.scripts.length > 0 ) {
			this.createScript( this.scripts[0], this.scriptIndex );
		};
		if ( is.opera && is.appVer >= 7.5 ) {
			this.interval = window.setInterval( function( obj ) {
				var i, bLoad;
				bLoad = obj.scripts[obj.scriptIndex].el.text != '' ? true : false;
				if ( bLoad ) {
					obj.scriptLoaded();
				};
			}, 10, this );
		};
	};
	this.createScript = function( oScript, nIndex ) {
		var i, elScript;
		elScript = document.createElement('SCRIPT');
		elScript.setAttribute( 'type', 'text/javascript'); // alapbeállítások, felül lehet őket definiálni
		elScript.setAttribute( 'charset', 'utf-8' );
		elScript.setAttribute( 'defer', 'defer' ); // defer || true - még nem tesztelt!
		elScript.loader = this;
		elScript.loader.scripts[nIndex].el = elScript;
		for ( i in oScript.attributes ) {
			elScript.setAttribute( i, oScript.attributes[i] )
		};
		elScript.onload = function() { // mozilla aszinkron
			this.loader.scriptLoaded();
		};
		elScript.onreadystatechange = function() { // ie aszinkron
			if ( this.readyState == 'complete' || this.readyState == 'loaded') { // ? 'loaded'
				this.loader.scriptLoaded();
			};
		};
		oDoc.getElementsByTagName('head')[0].appendChild( elScript );
	};
	this.scriptLoaded = function() {
		this.scriptIndex++;
		if ( this.scripts.length > this.scriptIndex ) {
			this.createScript( this.scripts[this.scriptIndex], this.scriptIndex );
		}
		else {
			window.clearInterval( this.interval );
			this.command();
		};
	};
	this.command = fCommand;
	return this;
};
