/* Zoomation SubClass
 * ---------------------- 
 * наследует ASCIImation
 * htmlObjId - id элемента PRE, в который осуществлять вывод анимации
 * fileName - имя файла анимации на сервере
 * bAutoPlay - играть ли анимацию сразу после загрузки (здесь = true)
 * link - ссылка на элемент навигации, создавший этот объект
 */
function Zoomation (htmlObjId, fileName, bAutoPlay, link){
	this.navLink = link; // ссылка на управляющий элемент навигации
	this.htmlObj = {};
	this.htmlObj.xPos = 0;
	this.htmlObj.yPos = 0;
	this.htmlObj.dx = 0;
	this.htmlObj.dy = 0;

	Zoomation.baseConstructor.call(this, htmlObjId, fileName, bAutoPlay);
}
Zoomation.inheritFrom(ASCIImation);

Zoomation.prototype.initEvents = function(){

	var _this = this;
	this.htmlObj.onmousedown = function(e){
		if (!e) var e = window.event;
		e.cancelBubble = true;
		if (e.stopPropagation) e.stopPropagation();

		if(typeof addClass != 'undefined'){
			addClass(this, 'pressed'); // z-index:999;
		}
		this.dx = getMousePosition(e).x - this.xPos;
		this.dy = getMousePosition(e).y - this.yPos;
		
		document.onmousemove = function(e){
			if (!e) var e = window.event;
			
			_this.htmlObj.xPos = getMousePosition(e).x - _this.htmlObj.dx;
			_this.htmlObj.yPos = getMousePosition(e).y - _this.htmlObj.dy;
			_this.setPosition();
			selection.disable();
		}
		document.onmouseup = function(){
			document.onmousemove = null;
			selection.enable();
			if(typeof removeClass != 'undefined'){
				removeClass(_this.htmlObj, 'pressed');  // z-index:1;
			}
		}
	}
	this.htmlObj.onmouseover = function(){
		if(typeof addClass != 'undefined'){
			addClass(this, 'hover');
			addClass(_this.navLink, 'hover');
		}

	}
	this.htmlObj.onclick = function(){
		selection.disable();
	}
	// синхронизация подсветки с элементом навигации
	this.htmlObj.onmouseout = function(){
		if(typeof removeClass != 'undefined'){
			removeClass(this, 'hover');
			removeClass(_this.navLink, 'hover');
		}
	}
	this.htmlObj.ondblclick = function(){
		if(typeof removeClass != 'undefined'){
			removeClass(_this.navLink, 'hover');
		}
		_this.navLink.onclick();
		//selection.disable();
	}
}

// zoomation должна быть looped принудительно
Zoomation.prototype.initBaseParams = function() {
	Zoomation.superClass.initBaseParams.call(this);
	this.loop = true;
}

// Инициализация координат и позиционирование должно произойти только после загрузки и отрисовки анимации
Zoomation.prototype.onload = function(ajaxObject){
	Zoomation.superClass.onload.call(this, ajaxObject);
	this.initCoords();
	this.setPosition();
	if(typeof addClass != 'undefined' && typeof removeClass != 'undefined'){
		removeClass (this.htmlObj, 'unloaded');
		removeClass(this.navLink, 'loading');
		addClass(this.htmlObj, 'flashing');
	}

	var _this = this;
	setTimeout(function(){
			removeClass (_this.htmlObj, 'flashing');
			_this.start();
		}, 300);
}

// Задание изначальных координат анимации
Zoomation.prototype.initCoords = function(){
	var winWidth = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
	var selfWidth = this.htmlObj.offsetWidth;
	var selfHeight = this.htmlObj.offsetHeight;
	this.htmlObj.xPos = Math.random()*(winWidth - selfWidth - 250) + 200; 
	this.htmlObj.yPos = getPageY(this.navLink) - selfHeight/2;
}

// Позиционирование анимации
Zoomation.prototype.setPosition = function(){
	this.htmlObj.style.left = this.htmlObj.xPos + "px";
	this.htmlObj.style.top = this.htmlObj.yPos + "px";
}


// Функции запрета и разрешения выделения
var selection = {
	disable : function(){
		document.onselectstart = function(){
			return false;
		}
		document.getElementsByTagName("body")[0].focus();
	},

	enable : function(){
		document.onselectstart = function(){
			return true;
		}
	}
}

function dalert(msg){
	$('dalert').innerHTML += msg + '<br />';
}

