/**
* Provides basic functionality
*
* Ensures cross-browser-compatibility for IE6/7 and Gecko
*
* @package Ceasy_Core
* @author Ralf Glaser
*
*
*/

// -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
MadChickMasterYoda=function(settings) {

	var self=this;

	this._settings=settings;
	this._protocolObj=null;
	this._autoloadObj=null;
	this._curtainObj=null;
	this._cssStyleUrls=null;
	this._portManager=null;

	// --------------------------------------------------------------------------------
	this.ask=function (arguments) {

		var answer=this._settings;
		var answerPath='';

		try {

			for (var i=0; i<this.ask.arguments.length; i++) {

				answerPath+=this.ask.arguments[i]+'->';
				answer=answer[this.ask.arguments[i]];

			}

		} catch (e) {

			throw new Error('MasterYoda::ask(): setting not found for: '+answerPath);

		}

		return answer;

	};

	// --------------------------------------------------------------------------------
	this.getCssStyleUrls=function () {

		if (!this._cssStyleUrls) {

			this._cssStyleUrls=[];

			var cssStyleElements=document.getElementsByTagName('link');

			for (var i=0; i<cssStyleElements.length; i++) {

				if (cssStyleElements[i].getAttribute('rel')=='stylesheet') {

					this._cssStyleUrls.push(cssStyleElements[i].getAttribute('href'));

				}

			}

		}

		return this._cssStyleUrls;

	};

	// --------------------------------------------------------------------------------
	this.getAutoload=function () {

		return this._autoloadObj;

	};

	// --------------------------------------------------------------------------------
	this.getCurtain=function () {

		return this._curtainObj;

	};

	// --------------------------------------------------------------------------------
	this.getPortManager=function () {

		return this._portManager;

	};

	// --------------------------------------------------------------------------------
	this.getPortName=function () {

		if (this._portManager) {

			return this._portManager.getPortName();

		}

	};

	// --------------------------------------------------------------------------------
	this.getProtocol=function () {

		return this._protocolObj;

	};

	// --------------------------------------------------------------------------------
	this.getWsdlUrl=function (wsdl) {

		return this._settings['url']['core']['getWsdl']+'?wsdl='+wsdl;

	};

	// --------------------------------------------------------------------------------
	this.setAutoload=function (autoloadObj) {

		this._autoloadObj=autoloadObj;

	};

	// --------------------------------------------------------------------------------
	this.setCurtain=function (curtainObj) {

		this._curtainObj=curtainObj;

	};

	// --------------------------------------------------------------------------------
	this.setPortManager=function (portManager) {

		this._portManager=portManager;

	};



	// --------------------------------------------------------------------------------
	this.setProtocol=function (protocolObj) {

		this._protocolObj=protocolObj;

	};

	// *****************************************************************
	// ******************** for internal use only **********************
	// *****************************************************************

	//--- Constructor -------------------------------------------------------------------------
	this._init=function()  {


	};

	/// call constructor
	this._init();

};

// -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
MadChickAutoload=function(madChickCurtainObj, masterYodaObj) {

	var self=this;

	this._instanciateCounter={};

	this._madChickCurtainObj=(madChickCurtainObj || madChickCurtain);
	this._masterYoda=(masterYodaObj || madChickMasterYoda);

	// --------------------------------------------------------------------------------
	this.instanciate=function(className, parameters, moduleId, callBack, callBackArguments)  {

		this._instanciateCounter[className]= this._instanciateCounter[className] || 1;

		if (eval('typeof('+className+')==\'undefined\'')) {

			if (this._instanciateCounter[className]==1) {

				this._madChickCurtainObj.show('Funktion wird geladen...', this.instanciate);

				if (moduleId) {

					var scriptUrl=this._masterYoda.ask('url', moduleId, 'js')+'/'+className+'.class.js';

				} else {

					var scriptUrl=this._masterYoda.ask('url', 'core', 'jsBasic')+'/'+className+'.class.js';

				}

				var scriptObject = document.createElement('script');
				scriptObject.setAttribute('type', 'text/javascript');
				scriptObject.setAttribute('src', scriptUrl);
				document.getElementsByTagName('head')[0].appendChild(scriptObject);
				this._instanciateCounter[className]++;

			} else if (this._instanciateCounter[className]>60) {

				throw new Error('MadChickAutoload::instanciate(): '+className+' could not be loaded.');

				return;

			} else {

				this._instanciateCounter[className]++;

			}

			setTimeout(function () {
				self.instanciate(className, parameters, moduleId, callBack, callBackArguments);
			}, 1000);

		} else {

			var instanciateString='new '+className+'(';

			for (var i=0; i<parameters.length; i++) {

				instanciateString+='parameters['+i+']'+(parameters.length>i+1 ? ', ': '');

			}

			instanciateString+=');';

			var retVal=eval(instanciateString);

			this._madChickCurtainObj.hide(this.instanciate);

			if (callBack) {

				return callBack(retVal, callBackArguments || null);

			}

		}

	};

	// *****************************************************************
	// ******************** for internal use only **********************
	// *****************************************************************

	//--- Constructor -------------------------------------------------------------------------
	this._init=function()  {


	};

	// call constructor
	this._init();


};

// -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
MadChickClipboard=function() {

	this._mode=false;

	// --------------------------------------------------------------------------------
	this.copyTextToClipboard= function(txt)  {

		txt=txt.replace(/&nbsp;/ig, ' ');
		txt=txt.replace(/&gt;/ig, '>');
		txt=txt.replace(/&lt;/ig, '<');

		if (this._mode=='IE') {

			window.clipboardData.setData('Text', txt);

			return true;

		} else if (this._mode=='gecko') {

			try {

				netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');

				try {

					var clipBoardObj = Components.classes["@mozilla.org/widget/clipboardhelper;1"].getService(Components.interfaces.nsIClipboardHelper);
					clipBoardObj.copyString(txt);

					return true;

				} catch (e) {

					alert('Beim Kopieren in die Zwischenablage trat ein Fehler auf');
					return false;

				}

			} catch (e) {

				alert('Der Zugriff auf die Zwischenablage wurde verweigert.\n\nRufen Sie in Ihrem Browser die Seite "about:config" auf und ändern Sie dort den Wert "signed.applets.codebase_principal_support" auf "true".');

			}

		}

		return false;

	};

	// *****************************************************************
	// ******************** for internal use only **********************
	// *****************************************************************

	//--- Constructor -------------------------------------------------------------------------
	this._init=function()  {

		if (window.clipboardData) {

			this._mode='IE';

		} else if (window.netscape) {

			this._mode='gecko';

		}

	};

	// call constructor
	this._init();

};

// -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
MadChickProtocol=function(protocolAreaObj, protocolAreaBodyObj) {

	this._protocolAreaObj=protocolAreaObj;
	this._protocolAreaBodyObj=protocolAreaBodyObj;
	this._resetContent='';

	// --------------------------------------------------------------------------------
	this.copyProtocolToClipboard= function()  {

		var madChickClipBoard= new MadChickClipboard();

		if (madChickClipBoard.copyTextToClipboard(this._protocolAreaBodyObj.innerHTML.replace(/<br ?\/?>/ig, '\r\n'))) {

			alert('Das Logbuch wurde in die Zwischenablage kopiert.');

		} else {

			alert('Das Logbuch konnte nicht in die Zwischenablage kopiert werden');

		}

	};

	// --------------------------------------------------------------------------------
	this.copyDocumentHtmlToClipboard= function()  {

		var madChickClipBoard= new MadChickClipboard();

		if (madChickClipBoard.copyTextToClipboard(document.documentElement.innerHTML)) {

			alert('Der Seiten-Quelltext wurde in die Zwischenablage kopiert.');

		} else {

			alert('Der Seiten-Quelltext konnte nicht in die Zwischenablage kopiert werden');

		}

	};

	// --------------------------------------------------------------------------------
	this.protocolMsg= function(msg)  {

		this._protocolAreaBodyObj.innerHTML+=this._getTimeStamp()+' '+msg.replace(/\r?\n/g, '<br />')+'<br />';

		if (this._protocolAreaObj.style.display=='block') {

			this._protocolAreaBodyObj.scrollTop=this._protocolAreaBodyObj.clientHeight;

		}

	};

	// --------------------------------------------------------------------------------
	this.reset= function()  {

		this._protocolAreaBodyObj.innerHTML=this._resetContent;

	};

	//-------------------------------------------------------------------------------
	this.show=function()  {

		this._protocolAreaObj.style.display='block';

		this._protocolAreaBodyObj.scrollTop=this._protocolAreaBodyObj.clientHeight;

	};

	//-------------------------------------------------------------------------------
	this.hide=function()  {

		this._protocolAreaObj.style.display='none';

	};

	//-------------------------------------------------------------------------------
	this.send=function()  {

		location.href='mailto:technik@hitcom.de?subject=Client-Fehler&body='+encodeURIComponent(this._protocolAreaBodyObj.innerText ? this._protocolAreaBodyObj.innerText : this._protocolAreaBodyObj.textContent);

	};

	//-------------------------------------------------------------------------------
	this.protocolError=function(err)  {

		var errMsg='';

		if (typeof(err)=='string') {

			errMsg=err;

		} else {

			if (err.description) {

				errMsg=(err.lineNumber ? '('+err.lineNumber+') ' : '')+err.name+': '+err.description;

			} else if (err.message) {

				errMsg=(err.lineNumber ? '('+err.lineNumber+') ' : '')+err.message;

			} else {

				errMsg='unknwon error. no errorMessage present.';

			}

		}

		this._protocolAreaBodyObj.innerHTML+='<span class="cError"> '+this._getTimeStamp()+' '+errMsg.replace(/\r?\n/g, '<br />')+'</span><br />';

		this.show();

	};

	// *****************************************************************
	// ******************** for internal use only **********************
	// *****************************************************************

	//-------------------------------------------------------------------------------
	this._getTimeStamp=function()  {

		var timeStamp= new Date();

		return timeStamp.getHours()+':'+(timeStamp.getMinutes()<10 ? '0' : '')+timeStamp.getMinutes()+':'+(timeStamp.getSeconds()<10 ? '0' : '')+timeStamp.getSeconds();


	};

	//--- Constructor -------------------------------------------------------------------------
	this._init=function()  {

		this._resetContent=this._protocolAreaBodyObj.innerHTML;

		this.protocolMsg('ProtocolArea initialized');

	};

	/// call constructor
	this._init();

};

// -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
MadChickCurtain=function(masterYodaObj) {

	var self=this;

	this._curtainObj=null;
	this._masterYoda=(masterYodaObj || madChickMasterYoda);
	this._waitFor=[];

	// --------------------------------------------------------------------------------
	this.hide= function(caller)  {

		this.removeWaitFor(caller);

		if (this._waitFor.length==0 || caller==this.show) {

			this._curtainObj.style.display='none';
			this._waitFor=[];

		}

	};


	// --------------------------------------------------------------------------------
	this.show= function(msg, caller)  {

		if (!this._curtainObj) {

			this._curtainObj=document.createElement('div');
			this._curtainObj.innerHTML='<div id="cCurtain">' +
				'<div id="cCurtainMessageBox">' +
				'  <div id="cCurtainMessageTitle"><div id="cCurtainCloser">X</div><span><img id="cCurtainMessageIcon" src="'+this._masterYoda.ask('url', 'core', 'icons')+'/misc/loading.gif" alt="" /> Bitte warten...</span></div>'+
				'  <div id="cCurtainMessageText">Bitte warten</div>'+
				'</div>';

			document.body.appendChild(this._curtainObj);

			document.getElementById('cCurtainCloser').onclick=function() {self.hide(self.show);};

		}

		var curtainMessageObj=document.getElementById('cCurtainMessageText');

		curtainMessageObj.innerHTML=(msg ? msg : 'Bitte warten');

		this._curtainObj.style.display='block';

		if (this._waitFor.length>0) {

			return true;

		} else {

			if (caller) {

				this._waitFor.push(caller);

				return false;


			}

		}

	};

	// --------------------------------------------------------------------------------
	this.removeWaitFor= function(caller)  {

		var newWaitFor=[];

		for (var i=0; i<this._waitFor.length; i++) {

			if (this._waitFor[i]!=caller) {

				newWaitFor.push(this._waitFor[i]);

			}

		}

		this._waitFor=newWaitFor;


	};

	// --------------------------------------------------------------------------------
	this.waitFor= function(caller)  {

		this._waitFor.push(caller);

	};

	// *****************************************************************
	// ******************** for internal use only **********************
	// *****************************************************************

	//--- Constructor -------------------------------------------------------------------------
	this._init=function()  {


	};

	/// call constructor
	this._init();

};

// -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
MadChickSelectControl=function() {


};

MadChickSelectControl.appendOption=function(selectObj, title, value) {

	var optionObj=selectObj.ownerDocument.createElement('option');

	optionObj.value=value;

	if (typeof(selectObj.options.appendChild)!='undefined') { //  IE

		optionObj.innerText=title;

		return selectObj.options.appendChild(optionObj);

	} else { // gecko

		optionObj.textContent=title;

		selectObj.add(optionObj, null);

		return optionObj;

	}

};

MadChickSelectControl.getOptionText=function(optionObj) {

	if (typeof(optionObj.innerText)!='undefined') { //  IE

		return optionObj.innerText;

	} else {

		return optionObj.text;

	}

};

MadChickSelectControl.removeOptions=function(selectObj) {

	if (selectObj.options.length>0) {

		try { // hack for IE 6

			for (var i=selectObj.options.length-1; i>=0; i--) {

				selectObj.options.removeChild(selectObj.options[i]);

			}

		} catch (e) {	}

		selectObj.options.length=0;

	}

};

MadChickSelectControl.setSelected=function(selectObj, selectedIndex) {

	try { // hack for IE 6

		selectObj.options[selectedIndex].selected=true;

	} catch (e) {}

	selectObj.selectedIndex=selectedIndex;

};

// -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
MadChickInnerText=function() {


};

MadChickInnerText.set= function(containerObj, text) {

	for (var i=0; i<containerObj.childNodes.length; i++) {

		containerObj.removeChild(containerObj.childNodes[i]);

	}

	return containerObj.appendChild(containerObj.ownerDocument.createTextNode(text));

};

MadChickInnerText.get= function(containerObj) {

	if (typeof(containerObj.innerText)!='undefined') { //IE

		return containerObj.innerText;

	} else {

		return containerObj.textContent;

	}

};


// -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
MadChickEvent=function() {


};

MadChickEvent.addListener= function(eventListenerObj, eventName, eventHandler) {

	if (typeof(eventListenerObj.attachEvent)!='undefined') {

		eventListenerObj.attachEvent('on'+eventName, eventHandler); // IE

	} else {

		eventListenerObj.addEventListener(eventName, eventHandler, false); // gecko

	}

};

MadChickEvent.removeListener= function(eventListenerObj, eventName, eventHandler) {

	if (typeof(eventListenerObj.detachEvent)!='undefined') {

		eventListenerObj.detachEvent('on'+eventName, eventHandler); // IE

	} else {

		eventListenerObj.removeEventListener(eventName, eventHandler, false); // gecko

	}

};

// -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
MadChickUrl=function() {


};

MadChickUrl.addParam= function(url, paramString) {

	if (url.indexOf('?')>0) {

		return url+'&'+paramString;

	} else {

		return url+'?'+paramString;

	}

};
