Ext.applyIf(String.prototype,{
	trim : function() { return this.replace(/^\s+/g, '').replace(/\s+$/g, ''); },
	endsWith : function(str) {
    if (str.length > this.length) return false;
    if (this.lastIndexOf(str) + str.length == this.length) return true;
    return false;
  }
});

Ext.applyIf(Array.prototype,{
	indexOf : function(item) {
    for (var i=0;i<this.length;i++) if (this[i] == item) return i;
    return -1;
  },
	removeIndex:function(index){
		if(typeof index == 'number'){
    	this.splice(index, 1);
    }
	 	return this;
	},
	remove : function(o){
		 var index = this.indexOf(o);
		 if(index != -1){
			 this.splice(index, 1);
		 }
		 return this;
	}
});


function $(el){return Ext.getDom(el);}
var COS = COS||{};
COS.util=function(){
	var _browser=function(){
			var appName = window.navigator.appName.toLowerCase();
			var userAgent = navigator.userAgent;
			var navType = function(){
				if (!Ext.isIE) {
					if (userAgent.indexOf("Firefox") != -1) return "Firefox";
					else if (userAgent.indexOf("Opera") != -1) return "Opera";
					else if (userAgent.indexOf("Safari") != -1) return "Safari";
					else if (userAgent.indexOf("Netscape") != -1) return "Netscape";
					else return "Mozilla";
				}
				else return "IE";			
			};
			return{		
				isNav : appName.indexOf("netscape") != -1,
				isIE : Ext.isIE,
				isIE7 : Ext.isIE7,
				isFF15 : userAgent.indexOf("Firefox/1.5") != -1,
				isFF20 : userAgent.indexOf("Firefox/2.0") != -1,
				isSafari:Ext.isSafari,				
				isOpera:Ext.isOpera,
				isGecko:Ext.isGecko,
				isWindows:Ext.isWindows,
				isLinux:Ext.isLinux,
				isMac:Ext.isMac,				
				navType : navType()
			}
	}();
	return {
		browser:function(){						
			return{		
				isNav : _browser.isNav,
				isIE : _browser.isIE,
				isIE7 : _browser.isIE7,
				isFF15 : _browser.isFF15,
				isFF20 : _browser.isFF20,
				isSafari:Ext.isSafari,				
				isOpera:Ext.isOpera,
				isGecko:Ext.isGecko,
				isWindows:Ext.isWindows,
				isLinux:Ext.isLinux,
				isMac:Ext.isMac,				
				navType : _browser.navType
			}
		}(),
		button:{
			getKeyCode : function(e) {
				//if (_browser.isIE) return window.event.keyCode;
				if (window.event) return window.event.keyCode;
				else return e.which;
			},
			leftButton : 1,
			rightButton:function(){
				if(_browser.isNav) return 3;
				else return 2
				
			}(),			
			mouseWheelUnit : function(){
				if(_browser.isIE) return 120;
				return 3;
			}(),
			KEY_LEFT : 37,
			KEY_UP : 38,
			KEY_RIGHT : 39,
			KEY_DOWN : 40,
			KEY_ENTER : 13,
			KEY_ESCAPE : 27
		},
		element:{
			getElementsByClassName:function(className,parent){
				var node = (typeof parent == "string")?Ext.getDom(parent):parent;
				var children = (node || document.body).getElementsByTagName('*');
				var elements = [], child;
				for (var i = 0, length = children.length; i < length; i++) {
					child = children[i];
					 if (child.className == className || child.className.match(new RegExp("(^|\\s)" + className + "(\\s|$)")))
						elements.push(child);
				}
				if(elements.length>0)	return elements;
				return false;				
			},
			hide : function(el) { el.style.display = "none"; },
			show : function(el) { el.style.display = "block"; },
			/*
			 *to use this function, dont forget to se the display property to something or it wont work on the first call
			 *
			 */
			toggle : function(el) {
				(el.style.display.toLowerCase() == "block") ? el.style.display = "none" : el.style.display = "block"; 
			},
			/*
			 * Sets style of an element
			 * params: el, obj element
			 *		  css, obj css on the form: {width:"100px",height:"100px"} for example
			 *
			**/
			setElementStyle : function(el, css) {				
				if(typeof el =="string") el=YD.get(el);
				for (var i in css) {
				  Ext.get(el).setStyle(i,css[i]);
				}
			},			
			/*
			 * returns XY position of the element on the page on the form [x,y]
			 *
			 */
			getXY:function(el){
				return {x:Ext.get(el).getXY()[0],y:Ext.get(el).getXY()[1]};
			},
			getY:function(el){
				return Ext.get(el).getY();
			},
			getX:function(el){
				return Ext.get(el).getX();
			},
			/*
			 * sets XY position of the element on the page
			 * @params: el, obj element
			 *			pos, array[x,y]
			 *			animate, (optional) True for the default animation, or a standard Element animation config object
			 */
			setXY:function(el,pos,ainimate){
				Ext.get(el).setXY(pos,animate);
			},
			setY:function(el,y,animate){
				Ext.get(el).setY(y,animate);
			},
			setX:function(el,x,animate){
				Ext.get(el).setX(x,animate);
			},
			pointer:{
				getXinElement:function(el,e){ //event should be added using YAHOO
					return COS.util.element.pointer.getXYinElement(el,e).x
				},
				getYinElement:function(el,e){//for click only
					return COS.util.element.pointer.getXYinElement(el,e).y
				},
				getXYinElement:function(el,e){
					if(!e.browserEvent) return;								
					return {x:e.getPageX()-Ext.get(el).getX(),y:e.getPageY()-Ext.get(el).getY()};
				},			
				toStringXYinElement:function(el,e){
					return ("x : "+COS.util.element.pointer.getXYinElement(el,e).x+", y: "+COS.util.element.pointer.getXYinElement(el,e).y);//test
				}
			},
			/*
			 * The Element should be position to absolute in order to move
			 * TODO, variable to store original XY and the el names and create a move to Orig function

			 */
			moveElement : function(el, leftIn, topIn) { 
				if(typeof el == "string") el=Ext.getDom(el);
				if(el.style.position!="absolute") el.style.position="absolute";
				COS.util.element.setElementStyle(el, {left:leftIn+"px",top:topIn+"px"}); 
			},
			createElement:function(/*e,attr,css,handler*/obj){
				var el = document.createElement(obj.el);
				for(var i in obj.attr){
					if(el.i){
						el.i = obj.attr[i];
						continue;
					}
					el[i]=obj.attr[i];
					/*if(i!="className")	el[i]=attr[i];
					else el.className = attr[i];*/					
				}
				if(obj.css) COS.util.element.setElementStyle(el,obj.css);			
				if(obj.on){
					Ext.get(el).on(obj.on.ev,function(){obj.on.cb(obj.on.param||null)},this);
				}
				return el;
			},
			createImage : function(/*attr,css*/obj) {				
				var img;
				var cu = COS.util;
				if (cu.browser.isIE /*&& ! cu.browser.isIE7*/ && ! (obj.attr.src.toLowerCase().endsWith(".gif") || obj.attr.src.toLowerCase().endsWith(".jpg"))) {
				  var src = obj.attr.src; delete obj.attr.src;
				  img=cu.element.createElement({el:"div",attr:obj.attr,css:obj.css});
				  img.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "')";
				}
				else {
				 img= cu.element.createElement({el:"img",attr:obj.attr,css:obj.css});
				}
				return img;
			},			
			append:function(parent,el){
				var cu = COS.util;				
				if (typeof el =="string") el=Ext.getDom(el);
				if (typeof parent =="string") parent=Ext.getDom(el);
				parent.appendChild(el);				
			}
		},
		Event:{
			getEventSource : function(e) {
				if (COS.util.browser.isIE) return window.event.srcElement;
				else return e.target;
			},
			stopEvent : function(e) {
				if (COS.util.browser.isIE) {
				  window.event.returnValue = false;
				  window.event.cancelBubble = true;
				}
				else {
				  e.preventDefault();
				  e.stopPropagation();
				}
			},
			getKeyCode : function(e) {
				if (this.isIE) return window.event.keyCode;
				else return e.which;
			},
			/*
			 * WARNING, Ext will modify the e object, for pure e, see /output/Ext.EventManager.html
			 * @params String/HTMLElement el 
			 * @params String eventName
			 * @params Function fn
			 * @params [Object scope]
			 * @params [Object options [op]] 
			**/
			addListener:function(el, eventName, fn,scope,op){				
				Ext.EventManager.addListener(el, eventName, fn, scope, op);
			},
			//String/HTMLElement element, String eventName, Function fn
			removeListener:function(el, eventName, fn){
				Ext.EventManager.removeListener(el,eventName,fn);
			},
/*			Object.extend(Event, {
	wheel:function (event){
		var delta = 0;
		if (!event) event = window.event;
		if (event.wheelDelta) {
			delta = event.wheelDelta/120; 
			if (window.opera) delta = -delta;
		} else if (event.detail) { delta = -event.detail/3;	}
		return Math.round(delta); //Safari Round
	}
});
*/			wheelAction:function(){
				var wheelTimer,cb,el;
				var _xy, wheelCount=0,sendValues;
				function processWheel(e){					
					var wheelValue ="";
					//if added with Ext
					if(e.browserEvent)	wheelValue = e.browserEvent.wheelDelta?e.browserEvent.wheelDelta/120:-e.browserEvent.detail/3;
					else wheelValue = (window.event.wheelDelta) ? window.event.wheelDelta/120 : -e.detail/3;
					COS.util.Event.stopEvent(e);
					
					var value = wheelValue;
					//Ext.getDom("debug2").value=value;
					if(Ext.isSafari)	value=(wheelValue/10); //SAFARI FIX
					
					if (wheelTimer) clearTimeout(wheelTimer);
					wheelTimer = setTimeout(function(){updateCount(Math.round(value));}, 100);//test
					return false;
				}
				function updateCount(v){
					wheelCount+=v;
					if(sendValues) clearTimeout(sendValues);
					sendValues=setTimeout(function(){
																				 //Ext.getDom("debug").value=wheelCount;
											if(Ext.isOpera)	wheelCount=-wheelCount;//opera fix
											cb(_xy,wheelCount);											
											wheelCount=0;
										},300);
				}
				function catchPosition(e){
					_xy = {x:COS.util.element.pointer.getXinElement(el,e),y:COS.util.element.pointer.getYinElement(el,e)};					
					//Ext.getDom("x").value=_xy.x;Ext.getDom("y").value=_xy.y;
				}
				return{
					on:function(elIn,callBack){
						el=(typeof elIn == "string")?Ext.getDom(elIn):elIn;
						cb =callBack;						
						Ext.get(elIn).on({"mousemove":{fn:catchPosition,scope:this},"mousewheel":{fn:processWheel,delay:0,scope:this}});
						//Ext.getDom(elIn).addEventListener("DOMMouseScroll",processWheel,true);//resolves FF
					},
					un:function(elIn){
						el=(typeof elIn == "string")?Ext.getDom("elIn"):elIn;
						//Ext.get(elIn).removeAllListeners();						
						Ext.get(elIn).un("mousemove",catchPosition);
						Ext.get(elIn).un("mousewheel",processWheel);
/*						if(_browser.isGecko) Ext.EventManager.removeListener(el,"DOMMouseScroll", processWheel);
						else Ext.EventManager.removeListener(el,"mousewheel", processWheel);*/
					}
				};
			
			}()
		},
		/**
		 * A function used to extend one class with another
		 * 
		 * @param {Object} subClass
		 * 		The inheriting class, or subclass
		 * @param {Object} baseClass
		 * 		The class from which to inherit
		 */
		extend : function(subClass, baseClass) {
		   function inheritance() {}
			   inheritance.prototype = baseClass.prototype;
			
			   subClass.prototype = new inheritance();
			   subClass.prototype.constructor = subClass;
			   subClass.baseConstructor = baseClass;
			   subClass.superClass = baseClass.prototype;
		},
		/*
		 * Gets the right screen measurements cross browser
		 *
		**/
		screenPosition:function(){			
			 if(window.screenLeft ||_browser.isIE ){//IE and others (window.screenLeft test wont work on IE7)
				var getWindowX = function(){return window.screenLeft;};
				var getWindowY = function(){return window.screenTop;};
			}
			else if(window.screenX){ //FF
				var getWindowX = function(){return window.screenX;};	
				var getWindowY = function(){return window.screenY;};
			}			
			if(window.innerWidth && !_browser.isIE){//All less IE
				var getViewportWidth = function(){return window.innerWidth;};
				var getViewportHeight = function(){return window.innerHeight;};
				var getHorizontalScroll = function(){return window.pageXOffset;};
				var getVerticalScroll = function(){return window.pageYOffset;};
			}
			else if(document.documentElement && document.documentElement.clientWidth ||_browser.isIE){//IE
				var getViewportWidth=function(){return document.documentElement.clientWidth;};
				var getViewportHeight=function(){return document.documentElement.clientHeight};
				var getHorizontalScroll=function(){return document.documentElement.scrollLeft;};
				var getVerticalScroll=function(){return document.documentElement.scrollTop};
			}
			else if(document.body.clientWidth){
				//IE4,5,6 with no doctype
				var getViewportWidth=function(){return document.body.clientWidth;};
				var getViewportHeight=function(){return document.body.clientHeight};
				var getHorizontalScroll=function(){return document.body.scrollLeft;};
				var getVerticalScroll=function(){return document.body.scrollTop};
			}
			return{
				//gets the x position of the browser on the screen
				getWindowX:function(){return getWindowX()},
				//gets the y position of the browser on the screen
				getWindowY:function(){return getWindowY()},
				//gets the innerWidth of the browser
				getViewportWidth:function(){return getViewportWidth()},
				//gets the innerHeight of the broser
				getViewportHeight:function(){return getViewportHeight()},
				//gets the offset of the horizontal scrolling
				getHorizontalScroll:function(){return getHorizontalScroll()},
				//gets the offset of the vertical scrolling
				getVerticalScroll:function(){return getVerticalScroll()}
			}//end return
		}()
	}	
}();