/*
Common functions
@author Filatov Dmitry
@date   22.08.2006
*/

var Common = {

	// Common classes methods
	
	Class : {
		
		match : function(	
			oElement,
			sClassName
			) {
	
			return oElement.className.match(new RegExp('(^|\\s+)' + sClassName + '($|\\s+)'));	
		
		},
		
		add : function(
			oElement,
			sClassName
			) {

			if(!Common.Class.match(oElement, sClassName)) {
				oElement.className += ' ' + sClassName;
			}
			
		},
		
		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 coords = {
				left: 0,
				top: 0
			};

			if(oEvent.pageX) {
  
				coords.left = oEvent.pageX;
				coords.top = oEvent.pageY;
		
			}
			else {
	
				coords.left = oEvent.clientX + document.body.scrollLeft - document.body.clientLeft;
				coords.top = oEvent.clientY + document.body.scrollTop - document.body.clientTop;

				if(document.body.parentElement && document.body.parentElement.clientLeft) {
		
					var bodyParent = document.body.parentElement;
		
					coords.left += bodyParent.scrollLeft - bodyParent.clientLeft;
					coords.top += bodyParent.scrollTop - bodyParent.clientTop;  
			
				}
		
			}

			return coords;
			
		}
		
	},	
		
	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 = this.extend(
				oOptions,
				this.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;
			
		}
		
	},
	
	HTMLObject : {
		
		getParentByTagName : function ( oObj, sTagName ) {
			while (oObj.tagName.toUpperCase() != sTagName.toUpperCase()) {
				oObj = oObj.parentNode;
			}
			return oObj;
		}
		
	}

}
