/*
Common functions
@author Filatov Dmitry
@date   22.08.2006
*/

var Common = {

	// Common classes methods
	
	Class : {
		
		match : function(	
			oElement,
			sClassName
			) {
	
			return oElement.className && oElement.className.match(new RegExp('(^|\\s+)' + sClassName + '($|\\s+)'));	
		
		},
		
		add : function(
			oElement,
			sClassName
			) {

			if(!Common.Class.match(oElement, sClassName)) {
				oElement.className += ' ' + sClassName;
			}
			
		},
		
		replace : function(
			oElement,
			sClassNameFrom,
			sClassNameTo
			) {

			oElement.className = ( oElement.className.replace( new RegExp("(^|\\s+)(" + sClassNameFrom + "|" + sClassNameTo + ")($|\\s+)", "g"), "$1" ) + ' ' + sClassNameTo ).replace( /^\s+/, '' );
			
		},
		
		remove : function(
			oElement,
			sClassName
			) {
	
			oElement.className = oElement.className.replace(new RegExp('(.*)(^|\\s+)(' + sClassName + ')($|\\s+)(.*)'), '$1$4$5').replace(/(^)\s/, '$1');	
			
		}
		
	},
		
	
	// Common event's methods
	
	Event : {
	
		add : function(
			oElement,
			sEventType,
			fEventFunc,
			bCapture
			) {
					
			if(oElement.addEventListener) {
				oElement.addEventListener(
					sEventType,
					fEventFunc,
					bCapture? true : false
					);
			}
			else if(oElement.attachEvent) {
				oElement.attachEvent(
					'on' + sEventType,
					fEventFunc
					);
			}
	
		},		

		remove : function(
			oElement,
			sEventType,
			fEventFunc,
			bCapture
			) {
	
			if(oElement.removeEventListener) {
				oElement.removeEventListener(
					sEventType,
					fEventFunc,
					bCapture? true : false
					);
			}
			else if(oElement.detachEvent) {
				oElement.detachEvent(
					'on' + sEventType,
					fEventFunc
					);
			}
			
		},
		
		getAbsoluteCoords : function(oEvent) {

			var oCoords = {
				iLeft : 0,
				iTop  : 0
			};

			if(oEvent.pageX) {
  
				oCoords.iLeft = oEvent.pageX;
				oCoords.iTop = oEvent.pageY;
		
			}
			else {
	
				oCoords.iLeft = oEvent.clientX + document.body.scrollLeft - document.body.clientLeft;
				oCoords.iTop = oEvent.clientY + document.body.scrollTop - document.body.clientTop;

				if(document.body.parentElement && document.body.parentElement.clientLeft) {
		
					var bodyParent = document.body.parentElement;
		
					oCoords.iLeft += bodyParent.scrollLeft - bodyParent.clientLeft;
					oCoords.iTop += bodyParent.scrollTop - bodyParent.clientTop;  
			
				}
		
			}

			return oCoords;
			
		},
		
		cancel : function(oEvent) {
					
			var oEvent = oEvent? oEvent : window.event;
			
			oEvent.cancelBubble = true;
			oEvent.returnValue = false;
			
			if(oEvent.cancelable){
				
				oEvent.preventDefault();
				oEvent.stopPropagation();
				
			}
			
			return false;
			
		},
		
		normalize : function(oEvent) {
						
			var oEvent = oEvent? oEvent : window.event;
			
			if(oEvent && oEvent.srcElement && !window.opera) {									
				oEvent.target = oEvent.srcElement;				
			}
			
			if(oEvent){
				
				oEvent.iKeyCode = oEvent.keyCode?
					oEvent.keyCode :
					(oEvent.which? oEvent.which : null)
					;
							
				if(oEvent.wheelDelta) {
									
					oEvent.iMouseWheelDelta = oEvent.wheelDelta / 120;
					
					if(window.opera) {
						oEvent.iMouseWheelDelta *= -1;
					}
					
				}
				else if(oEvent.detail) {
					oEvent.iMouseWheelDelta = -oEvent.detail / 3;
				}
				
			}
			
			return oEvent;
		
		}
		
	},	
	
	// Common DOM's methods
	
	Dom : {
			
		NODE_TYPE_ELEMENT : 1,
		NODE_TYPE_TEXT    : 3,
		
		getAbsoluteCoords : function(oElement) {
			
			var oResult = {
				iTop  : 0,
				iLeft : 0
				};
	
			while(oElement) {
	
				oResult.iTop += oElement.offsetTop;
				oResult.iLeft += oElement.offsetLeft;
		
				oElement = oElement.offsetParent;
		
			}
		
			return oResult;
			
		},
		
		getElementsByClassName : function(
			oElement,
			sClassName,
			sTagName,
			bRecursion
			) {
				
			var
				aResult = [],
				sTagName = sTagName || '*';
				;
			
			if(!bRecursion) {
			
				if(document.evaluate) {				
					
					oQueryResult = document.evaluate(
						'.//' + sTagName + '[contains(concat(\' \', @class, \' \'), \' ' + sClassName + ' \')]',
						oElement,
						null,
						XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
						null
						);
				
					for(var i = 0, iLength = oQueryResult.snapshotLength; i < iLength; i++) {
						aResult.push(oQueryResult.snapshotItem(i));
					}
				 
				 	return aResult;
				
				}												
			
				var aDescendants = oElement.getElementsByTagName(sTagName);
			
				if(aDescendants.length > 0 ||
					sTagName != '*'
					) {
					
					for(var i = 0, iLength = aDescendants.length, oDescendant; i < iLength; i++) {
					
						oDescendant = aDescendants[i];
					
						if(Common.Class.match(oDescendant, sClassName)) {
							aResult.push(oDescendant);						
						}
							
					}
				
					return aResult;
					
				}
				
			}
			
			for(var i = 0, iLength = oElement.childNodes.length, oChild; i < iLength; i++) {
				
				oChild = oElement.childNodes[i];
				
				if(oChild.nodeType != Common.Dom.NODE_TYPE_ELEMENT) {
					continue;
				}
				
				if(Common.Class.match(oChild, sClassName)) {
					aResult.push(oChild);						
				}
				
				aResult = aResult.concat(
					this.getElementsByClassName(
						oChild,
						sClassName,
						sTagName,			
						true
						)
					);
				
			}
			
			return aResult;
				
		}
	},
	
	// Common cookie's methods
	
	Cookie : {
	
		set : function(sName, sValue, sPath, sExpire) {
		
			/*
			document.cookie = sName + '=' + escape(sValue)
				+ ((sExpire == null)? '' : ('; expires=' + sExpire.toGMTString()))
				+ ((sPath == null)? '' : ('; path=' + sPath));
			*/
			document.cookie = sName + '=' + escape(sValue)
				+ (!sExpire ? '' : '; expires=' + sExpire.toGMTString())
				+ '; path=' + (!sPath ? '/' : sPath);
		},
		
		get : function(sName) {
		
			var sSearch = sName + '=';
			
			if(document.cookie.length > 0) {
			
				var iOffset = document.cookie.indexOf(sSearch);
				
				if(iOffset != -1) {
				
					iOffset += sSearch.length;
			
					var iEnd = document.cookie.indexOf(';', iOffset);
			
					if(iEnd == -1) {
						iEnd = document.cookie.length;
					}
					
					return unescape(document.cookie.substring(iOffset, iEnd));
					
				}
			}
			
			return '';			
		
		}
	
	},
	
	
	// Common xml's methods
	
	Xml : {
	
		NODE_ELEMENT : 1,
		NODE_TEXT    : 3,
		
		toString : function(oNode) {					
			
			var
				sXml = '',
				oChild
				;
			
			for(var i = 0; i < oNode.childNodes.length; i++) {
			
				oChild = oNode.childNodes[i];
			
				switch(oChild.nodeType) {
				
					case Common.Xml.NODE_ELEMENT: 
						
						sXml += '<' + oChild.nodeName;
						
						for(var j = 0; j < oChild.attributes.length; j++) {								
							if(oChild.attributes[j].nodeValue) {
								sXml += ' ' + oChild.attributes[j].nodeName + '="' + oChild.attributes[j].nodeValue + '"';
							}
						}
						
						if(oChild.childNodes.length > 0) {
						
							sXml += '>';
						
							sXml += Common.Xml.getString(oChild);
							
							sXml += '</' + oChild.nodeName + '>';
							
						}
						else {
						
							sXml += ' />';
						
						}						
											
					break;
										
					case Common.Xml.NODE_TEXT: 
					
						sXml += oChild.nodeValue;
					
					break;
					
					default:
					break;
				
				}
			
			}
		
			return sXml;
		
		},
		
		toArray : function(oNode) {
		
			var aValue = [];
			
			for(var i = 0; i < oNode.attributes.length; i++) {
			
				oChild = oNode.attributes[i];
								
				aValue[oChild.nodeName] = oChild.nodeValue;				

			}
			
			for(var i = 0; i < oNode.childNodes.length; i++) {
			
				oChild = oNode.childNodes[i];
				
				if(oChild.nodeType == Common.Xml.NODE_ELEMENT) {					
					aValue[oChild.nodeName] = Common.Xml.toString(oChild);
				}

			}						
			
			return aValue;
		
		}
	
	},


	// Common object's methods

	Object : {
				
		extend : function(
			oSource,
			oDestination
			) {				
		
			for(var i in oSource) {		
				oDestination[i] = oSource[i];
			}
		
			return oDestination;
		
		}
		
	},


	Utils : {
	
		oPopupDefaults : {	
	
			iWidth      : 540,
			iHeight     : 600,
			sToolbar    : 'no',
			sMenubar    : 'no',
			sResizeable : 'yes',
			sScrollbars : 'yes',
			sStatus     : 'yes'
	
		},
	
		popup : function(
			sUrl,
			sName,
			oOptions,
			bReplace
			) {
		
			oOptions = Common.Object.extend(
				oOptions,
				Common.Utils.oPopupDefaults
				);
		
			var iLeftOffset = screen.availWidth / 2 - oOptions.iWidth / 2;
			var iTopOffset = screen.availHeight / 2 - oOptions.iHeight / 2;			
		
			oNewWindow = window.open(
				sUrl,
				sName,
				'left=' + iLeftOffset + ', ' +
				'top = ' + iTopOffset + ', ' +
				'width=' + oOptions.iWidth + ', ' +
				'height=' + oOptions.iHeight + ', ' +
				'resizable=' + oOptions.sResizeable + ', ' +
				'toolbar=' + oOptions.sToolbar + ', ' +
				'scrollbars=' + oOptions.sScrollbars + ', ' +
				'status=' + oOptions.sStatus
				);
			
			if(sUrl.match(/\.(gif|jpe?g|png)$/i)) {
		
				oNewWindow.document.open();
			
				oNewWindow.document.write('<html><head></head>' +
					'<body style="background: #FFF; margin: 0px; padding: 0px;">' +
					'<table cellpadding="0" cellspacing="0" border="0" width="100%" height="100%"><tr><td align="center">' + 
					'<img src="' + sUrl + '" </td></tr></table></body></html>'
					);
				
				oNewWindow.document.close();
		
			}
			
			oNewWindow.focus();				

			return false;
			
		},
		
		generateUid : function() {
	
			var sUid = '';
	
			for(var i = 0; i < 4; i++) {	
				sUid += (i > 0? '-' : '') + Math.floor(Math.random() * 9999);	
			}	
	
			return sUid;
		
		}
		
	}

}


Function.prototype.inheritFrom = function(BaseClass) {
		
	var Inheritance = function() {};
			
	Inheritance.prototype = BaseClass.prototype;

	this.prototype = new Inheritance();
	this.prototype.constructor = this;
	this.baseConstructor = BaseClass;
	this.superClass = BaseClass.prototype;
	
}