﻿/*
Diese Datei enthält Fixes für die aktuell eingesetzte original Ext Version, die nur in SVN Versionen verfügbar sind
also beim nächstes Ext Release nicht mehr notwendig sind und dann getestet/entfernt werden müssten
*/

var stringver = Ext.version.replace(/\./g,"")+"00";
var extCalVersion = parseInt(stringver.substr(0,3),10);


/*
---------------------------------------------------------------------------
//Bugfixes up to V 2.0.2
---------------------------------------------------------------------------
*/


if (extCalVersion<=202) {
	
/*
---------------------------------------------------------------------------


//http://extjs.com/forum/showthread.php?p=152979#post152979	
---------------------------------------------------------------------------
*/

	Ext.override(Ext.tree.TreeNodeUI,{
		toggleCheck : function(value){
			var cb = this.checkbox;
			if(cb){
				var checkvalue = (value === undefined ? !cb.checked : value);
				cb.checked = checkvalue;
				this.node.attributes.checked = checkvalue;
			}
		}
	});


/*
---------------------------------------------------------------------------


//http://extjs.com/forum/showthread.php?t=31604
---------------------------------------------------------------------------
*/

	Ext.override(Ext.form.Label,{
		onRender : function(ct, position){
			if(!this.el){
				this.el = document.createElement('label');
				this.el.innerHTML = this.text ? Ext.util.Format.htmlEncode(this.text) : (this.html || '');
				if(this.forId){
					this.el.setAttribute('htmlFor', this.forId);
				}
				if (!this.el.id) {
					this.el.id = this.getId();
				}
			}
			Ext.form.Label.superclass.onRender.call(this, ct, position);
		}
	});

	
}

/*
---------------------------------------------------------------------------
//Bugfixes up to V 2.1
---------------------------------------------------------------------------
*/

else if (extCalVersion<=210) {

/*
---------------------------------------------------------------------------
Dateformats were wrong in many cases

//http://www.extjs.com/forum/showthread.php?p=156047#post156047
//http://extjs.com/forum/showthread.php?p=173747#post173747
---------------------------------------------------------------------------
*/
	Ext.apply(Date, {
	  createParser : function(format) {
		  var funcName = "parse" + Date.parseFunctions.count++;
		  var regexNum = Date.parseRegexes.length;
		  var currentGroup = 1;
		  Date.parseFunctions[format] = funcName;
	
		  var code = "Date." + funcName + " = function(input){\n"
			  + "var y, m, d, h = 0, i = 0, s = 0, ms = 0, o, z, u, v;\n"
			  + "input = String(input);var d = new Date();\n"
			  + "y = d.getFullYear();\n"
			  + "m = d.getMonth();\n"
			  + "d = d.getDate();\n"
			  + "var results = input.match(Date.parseRegexes[" + regexNum + "]);\n"
			  + "if (results && results.length > 0) {";
		  var regex = "";
	
		  var special = false;
		  var ch = '';
		  for (var i = 0; i < format.length; ++i) {
			  ch = format.charAt(i);
			  if (!special && ch == "\\") {
				  special = true;
			  }
			  else if (special) {
				  special = false;
				  regex += String.escape(ch);
			  }
			  else {
				  var obj = Date.formatCodeToRegex(ch, currentGroup);
				  currentGroup += obj.g;
				  regex += obj.s;
				  if (obj.g && obj.c) {
					  code += obj.c;
				  }
			  }
		  }
	
		  code += "if (u){\n"
			  + "v = new Date(u * 1000);\n" // give top priority to UNIX time
			  + "}else if (y >= 0 && m >= 0 && d > 0 && h >= 0 && i >= 0 && s >= 0 && ms >= 0){\n"
			  + "v = new Date(y, m, d, h, i, s, ms);\n"
			  + "}else if (y >= 0 && m >= 0 && d > 0 && h >= 0 && i >= 0 && s >= 0){\n"
			  + "v = new Date(y, m, d, h, i, s);\n"
			  + "}else if (y >= 0 && m >= 0 && d > 0 && h >= 0 && i >= 0){\n"
			  + "v = new Date(y, m, d, h, i);\n"
			  + "}else if (y >= 0 && m >= 0 && d > 0 && h >= 0){\n"
			  + "v = new Date(y, m, d, h);\n"
			  + "}else if (y >= 0 && m >= 0 && d > 0){\n"
			  + "v = new Date(y, m, d);\n"
			  + "}else if (y >= 0 && m >= 0){\n"
			  + "v = new Date(y, m);\n"
			  + "}else if (y >= 0){\n"
			  + "v = new Date(y);\n"
			  + "}\n}\nreturn (v && (z || o))?" // favour UTC offset over GMT offset
			  +     " (Ext.type(z) == 'number' ? v.add(Date.SECOND, (v.getTimezoneOffset() * 60) + z) :" // reset to UTC, then add offset
			  +         " v.add(Date.HOUR, (v.getGMTOffset() / 100) + (o / -100))) : v;\n" // reset to GMT, then add offset
			  + "}";
	
		  Date.parseRegexes[regexNum] = new RegExp("^" + regex + "$", "i");
		  eval(code);
	  }
	});
	
	
	Ext.apply(Date.parseCodes, {
		j: {
			g:1,
			c:"d = parseInt(results[{0}], 10);\n",
			s:"(\\d{1,2})" // day of month without leading zeroes (1 - 31)
		},
		M: function() {
			for (var a = [], i = 0; i < 12; a.push(Date.getShortMonthName(i)), ++i); // get localised short month names
			return Ext.applyIf({
				s:"(" + a.join("|") + ")"
			}, Date.formatCodeToRegex("F"));
		},
		n: {
			g:1,
			c:"m = parseInt(results[{0}], 10) - 1;\n",
			s:"(\\d{1,2})" // month number without leading zeros (1 - 12)
		},
		o: function() {
			return Date.formatCodeToRegex("Y");
		},
		g: function() {
			return Date.formatCodeToRegex("G");
		},
		h: function() {
			return Date.formatCodeToRegex("H");
		},
		P: {
		  g:1,
		  c:[
			  "o = results[{0}];",
			  "var sn = o.substring(0,1);", // get + / - sign
			  "var hr = o.substring(1,3)*1 + Math.floor(o.substring(4,6) / 60);", // get hours (performs minutes-to-hour conversion also, just in case)
			  "var mn = o.substring(4,6) % 60;", // get minutes
			  "o = ((-12 <= (hr*60 + mn)/60) && ((hr*60 + mn)/60 <= 14))? (sn + String.leftPad(hr, 2, '0') + String.leftPad(mn, 2, '0')) : null;\n" // -12hrs <= GMT offset <= 14hrs
		  ].join("\n"),
		  s: "([+\-]\\d{2}:\\d{2})" // GMT offset in hrs and mins (with colon separator)
		}
	});
	
	// private
	Date.formatCodeToRegex = function(character, currentGroup) {
		// Note: currentGroup - position in regex result array (see notes for Date.parseCodes above)
		var p = Date.parseCodes[character];
	
		if (p) {
		  p = Ext.type(p) == 'function'? p() : p;
		  Date.parseCodes[character] = p; // reassign function result to prevent repeated execution      
		}
	
		return p? Ext.applyIf({
		  c: p.c? String.format(p.c, currentGroup || "{0}") : p.c
		}, p) : {
			g:0,
			c:null,
			s:Ext.escapeRe(character) // treat unrecognised characters as literals
		}
	};	
	
/*
---------------------------------------------------------------------------


//http://extjs.com/forum/showthread.php?p=165475#post165475
---------------------------------------------------------------------------
*/
	Ext.apply(Ext.tree.TreeNodeUI.prototype, {
		toggleCheckNoEvent : function(value){
			var cb = this.checkbox;
			if(cb){
				cb.checked = (value === undefined ? !cb.checked : value);
			// fix for IE6
			this.checkbox.defaultChecked = cb.checked;
				this.node.attributes.checked = cb.checked;
			}
		}
	});

/*
---------------------------------------------------------------------------


//http://extjs.com/forum/showthread.php?p=166201#post166201	
---------------------------------------------------------------------------
*/
	Ext.tree.TreeLoader.override({
	    processResponse : function(response, node, callback){
	        var json = response.responseText;
	        try {
	            var o = eval("("+json+")");
	            node.beginUpdate();
	            for(var i = 0, len = o.length; i < len; i++){
	                var n = this.createNode(o[i]);
	                if(n){
	                    node.appendChild(n);
	                    if(this.preloadChildren){
	                        this.doPreload(n);
	                    }
	                }
	            }
	            node.endUpdate();
	            if(typeof callback == "function"){
	                callback(this, node);
	            }
	        }catch(e){
	            this.handleFailure(response);
	        }
	    }		
	});
	
	
/*
---------------------------------------------------------------------------
subMenus disappear when not moving exactly the menuline


//mmh, funzt zwar, aber wenn dasselbe Menu genutzt wird, dann blinkt der im haupt menu immer hin und her
//http://extjs.com/forum/showthread.php?p=169275#post169275	
---------------------------------------------------------------------------
*/
	Ext.override(Ext.menu.Menu, {
	
		onMouseOver : function(e){
			var t;
			if(t = this.findTargetItem(e)){
				if(t.canActivate && !t.disabled){
					this.setActiveItem(t, true);
				}
			}
			this.over = true;
			this.fireEvent("mouseover", this, e, t);
		},
	
		onMouseOut : function(e){
			var t;
			if(t = this.findTargetItem(e)){
				if(t == this.activeItem && t.shouldDeactivate(e)){
					this.activeItem.deactivate();
					delete this.activeItem;
				}
			}
			this.over = false;
			this.fireEvent("mouseout", this, e, t);
		}
		
	});
	
	Ext.override(Ext.menu.Item, {
		deferHide : function() {
			delete this.hideTimer;
			if (this.menu.over) {
				this.parentMenu.setActiveItem(this, false);
			} else {
				this.menu.hide();
			}
		}
		
	});


/*
---------------------------------------------------------------------------
test, um gleiche submenu-objekte in mehreren menuitems zu verwenden, ohne, dass bei jedem zweiten das submenu versteckt wird ..
... geht noch nicht
---------------------------------------------------------------------------
*/
/*
Ext.override(Ext.menu.Menu, {
	
		show : function(el, pos, parentMenu){
			this.parentMenu = parentMenu;
			if (this.parentMenu) {
				this.parentMenu.lastSubMenu = this;
			}
			if(!this.el){
				this.render();
			}
			this.fireEvent("beforeshow", this);
			this.showAt(this.el.getAlignToXY(el, pos || this.defaultAlign), parentMenu, false);
		},
		 hide : function(deep){
//			if (this.parentMenu.lastSubMenu != this) {
				if(this.el && this.isVisible()){
					this.fireEvent("beforehide", this);
					if(this.activeItem){
						this.activeItem.deactivate();
						this.activeItem = null;
					}
					if (this.parentMenu && this.parentMenu.lastSubMenu != this) {			 					
						this.el.hide();
						this.hidden = true;
						this.fireEvent("hide", this);
					}
				}
//				if(deep === true && this.parentMenu){
				if(deep === true && this.parentMenu && this.parentMenu.lastSubMenu != this){					
					this.parentMenu.hide(true);
				}
//			}
		}
		
	});
*/

/*	
	Ext.override(Ext.menu.Item, {
		
		deferExpand : function(autoActivate){
			delete this.showTimer;
			this.menu.show(this.container, this.parentMenu.subMenuAlign || "tl-tr?", this.parentMenu);
			if(autoActivate){
				this.menu.tryActivate(0, 1);
			}
		}
		
	});
*/
}