var popupMessage = null;
var popupOptions = {speed: 1}

/******************************************************************************/
/*                         Показываем всплывающее окно                        */
/******************************************************************************/
function showPopupMessage () {
	
	/* Инициализация системы выпадающего блока */
	popupMessageInit();
	
	/* Показываем */
	popupMessage.show();	
	
}

/******************************************************************************/
/*                        Инициализация работы скрипта                        */
/******************************************************************************/
function popupMessageInit () {
	
	/* Инициализация класса */
	if (popupMessage != null) return;
	popupMessage = new PopupMessage();	
	popupMessage.timeStep = popupOptions.speed;

	/* Обработчики */
	setHandler(window, 'resize', resizePopupMessage);
	setHandler(window, 'scroll', resizePopupMessage);
	setHandler(window, 'load', loadEvent);	
	
}

function resizePopupMessage() {
	popupMessage.resize();
}

function loadEvent () {
	
	/* Расширяем окно сообщения */
	var object = document.getElementById('popup_message');
	if (object == null) return;
	object.innerHTML = '<div id="popup_message_content" onclick="popupMessage.hide()">' + object.innerHTML + '</div>';
	
	/* Обработчики */
	setHandler(document.body, 'copy', copyEvent);
	setHandler(document.body, 'keyup', keyUpEvent);

}

function keyUpEvent (e) {
	
	var key = (!e || !e.which) ? event.keyCode : e.which;
	
	if (key == 67 && e.ctrlKey) 
		copyEvent();
		
	if (key == 45 && e.ctrlKey) 
		copyEvent();
	
	
}

function copyEvent () {
	if (!popupMessage.showing)
		showPopupMessage();
}

/******************************************************************************/
/*                       Функция установки обработчика                        */
/******************************************************************************/

function setHandler (object, event, func) {
	
	if (object.addEventListener) {
		object.addEventListener(event, func, false);
	} else if (object.attachEvent) {
		object['e' + event] = func;
		object.attachEvent('on' + event, function() {
			object['e' + event]( window.event );
		});
	} else {
		object['on' + event] = func;
	}		
}

/******************************************************************************/
/*                   Класс для работы со всплывающими окнами                  */
/******************************************************************************/
PopupMessage = function () {
	
	this.showed = false;
	this.objectTop = 0;
	this.timeStep = 3; 
	this.showing = false;
	
	/* Показываем всплывающее окно */
	this.show = function () {
		
		/* Объект */
		var object = document.getElementById('popup_message_content');
		if (object == null) return;	
		var mobject = document.getElementById('popup_message');
		if (mobject == null) return;
		
		this.showing = true;
		
		this.objectTop = mobject.clientHeight;
		this.objectHeight = 0;
		
		object.style.overflow = 'hidden';
		object.style.position = 'relative';		
		object.style.top = this.objectTop + 'px';
		object.style.height = this.objectHeight + 'px';						
		
		/* Устанавливаем в угол */
		this.setPosition();
		
		/* Показываем */
		mobject.style.visibility = 'visible';
		this.showed = true;		
		
		/* Выезжающий эффект */
		setTimeout("popupMessage.timed()", 1);	
		
	}	
	
	this.hide = function () {
		this.showed = false;
		var mobject = document.getElementById('popup_message');
		if (mobject == null) return;		
		mobject.style.visibility = 'hidden';
	}
	
	/* Выезжающий эффект */
	this.timed = function () {
		var object = document.getElementById('popup_message_content');
		this.objectTop -= this.timeStep;
		this.objectHeight += this.timeStep;
		object.style.top = this.objectTop + 'px';
		object.style.height = this.objectHeight + 'px';
		if (this.objectTop - this.timeStep > 0)
			setTimeout("popupMessage.timed()", 10);	
		else {
			this.showing = false;
			var mobject = document.getElementById('popup_message');
			if (mobject == null) return;			
			object.style.top = '0px';
			object.style.height = mobject.clientHeight + 'px';
		}
	}
	
	/* Установка позиции */
	this.setPosition = function () {
		
		/* Объект */
		var object = document.getElementById('popup_message');
		if (object == null) return;
		
		/* Определяем координаты */
		var size = this.getWindowSize();
		object.style.left = (size.width - object.clientWidth + this.getBodyScrollLeft()) + 'px';
		object.style.top = (size.height - object.clientHeight + this.getBodyScrollTop()) + 'px';	
		
	}
	
	/* Функции для получения абсолютных координат */
	this.getBodyScrollTop = function () {
		return self.pageYOffset || (document.documentElement && document.documentElement.scrollTop) || (document.body && document.body.scrollTop);
	}
	
	this.getBodyScrollLeft = function () {
		return self.pageXOffset || (document.documentElement && document.documentElement.scrollLeft) || (document.body && document.body.scrollLeft);
	}
	
	this.getWindowSize = function () {
		
		var wwidth = 0, wheight = 0;
		
		if (typeof(window.innerWidth) == 'number' && document.documentElement) {
			wwidth = document.documentElement.offsetWidth;
			wheight = document.documentElement.offsetHeight;
		} else if (document.documentElement) {
			wwidth = document.documentElement.clientWidth;
			wheight = document.documentElement.clientHeight;			
		} else if(document.body && (document.body.clientWidth || document.body.clientHeight)) {
			wwidth = document.body.clientWidth;
			wheight = document.body.clientHeight;			
		}
		return {width: wwidth, height: wheight}
	}	
	
	/* При ресайзе документа */
	this.resize = function () {
		if (!this.showed) return;
		this.setPosition();		
	}
	
}