//sqrt-smooth-0.0.1.js
//abc-0.0.9.js
//cookie.js

function Message(settings) {	
	this.editContainer	= null;
	this.msgContainer	= null;
	this.msgWrapper		= null;
	
	this.settings = {
		editContainerId		: 'msg-edit-container',
		msgContainerId		: 'msg-container',
		applyButtonId		: 'msg-btn-apply',
		mode				: 'default',
		showMode			: ['always', 'once', 'once-per-visit']
	};
	
	this.assignSettings(settings);
	
	if(this.inCms() && !this.isEditMode()) {		
		return false;
	}		
	
	this.init();
}

Message.prototype.init = function(args) {	
	this.msgContainer = ABC.$(this.settings.msgContainerId);	
	
	if(!this.isEditMode()) {
		this.initFunctions();
		this.contentTransfer();
		this.addHandlers();
		this.show();
	} else {		
		this.initEditContainer();
	}
}

Message.prototype.assignSettings = function(settings) {
	var type = null;
	
	for(var s in settings) {
		type = typeof this.settings[s];
		if( type != 'undefined' ) {
			if(type == 'string') {
				this.settings[s] = settings[s];
			} else if(type == 'object' && ABC.inArray(this.settings[s], settings[s])) {
				this.settings[s] = settings[s];
			}
		}
	}	
}



Message.prototype.addHandlers = function() {
	var inputs = this.msgContainer.getElementsByTagName('*');
	var inpLen = inputs.length;
	var obj = this;
	
	for(var i = 0; i < inpLen; i++) {
		if( (/\bclose\b/).test(inputs[i].className) ) {
			ABC.addEvent(inputs[i], 'click',
				function(evt) {
					Cookie.set('closemessage', 'yes', null, null, null, null);
					obj.doClose(obj);
					ABC.cancelDefaultEvent(evt);
				}, false);
			
			return true;
		}
	}
	return false;
}

Message.prototype.applyChanges = function(obj) {
	if( (obj.msgContainer = ABC.$(obj.settings.msgContainerId)) && (obj.editContainer = ABC.$(obj.settings.editContainerId)) ) {
		
		obj.msgContainer.innerHTML = '';
		//obj.msgContainer.appendChild(this.getPaddingImg());
		obj.msgContainer.innerHTML += obj.editContainer.innerHTML;
		alert('Содержиоме было скопировано в блок.\nСохраните страницу, чтобы изменения вступили в силу.');
	} else {
		alert('Ошибка! Возможно, отсутствует блок.\nПопробуйте вставить блок из меню "Вставить блок".');
	}	
}

Message.prototype.initEditContainer = function() {	
	var obj = this;	
	this.editContainer	= ABC.$(this.settings.editContainerId);	
	
	if(this.contentReverseCopy(this)) {
		this.applyButton = ABC.$(this.settings.applyButtonId);		
		ABC.addEvent(this.applyButton, 'click', function(evt){obj.applyChanges(obj); /*ABC.cancelDefaultEvent(evt);*/}, false);	
	}
}


Message.prototype.contentReverseCopy = function(obj) {
	if(!obj.editContainer) {
		alert('Ошибка! Блок для редактирования не найден.');	
		return false;
	}	
	
	obj.editContainer.innerHTML = obj.msgContainer.innerHTML;	
	return true;
}

Message.prototype.getPaddingImg = function() {
	var padImg = ABC.create('img');
	ABC.attr(padImg, {width: '1', height: '1'});
	ABC.css(padImg, {display: 'block', clear: 'both'});
	return padImg;
}

Message.prototype.contentTransfer = function() {
	this.msgWrapper = ABC.create('div');
	ABC.css(this.msgWrapper, {position: 'absolute', right: '0px', top: '0px'});
	
	
	//this.msgWrapper.appendChild(this.getPaddingImg());
	
	this.msgWrapper.innerHTML = this.msgContainer.innerHTML;	
	this.msgContainer.innerHTML = '';
	
	ABC.css(this.msgContainer, {height: '1px', position: 'relative', overflow: 'hidden'});
	this.msgContainer.appendChild(this.msgWrapper);	
	ABC.css(this.msgContainer, {width: this.msgWrapper.offsetWidth + 'px'});
}

Message.prototype.show = function() {
	if(this.settings.showMode == 'once') {
		this.showOnce();
	} else if(this.settings.showMode == 'once-per-visit') {
		this.showOncePerVisit();
	} else {
		this.doOpen(this);
	}	
	
	//alert(Cookie.test());
}

Message.prototype.showOncePerVisit = function() {
	/*ABC.addEvent(document.body, 'unload', function(){
		Cookie.unset('endvisit', null, null);
	}, false);*/	
	
	if(!Cookie.get('closemessage')) {
		this.doOpen(this);
	}	
}

Message.prototype.showOnce = function() {
	if(!Cookie.get('showmessage')) {
		this.doOpen(this);
		Cookie.set('showmessage', 'yes', null, null, null, null);
	}
}

Message.prototype.isEditMode = function() {	
	return (this.settings.mode == 'edit');	
}

Message.prototype.inCms = function() {	
	return !!parent.document.getElementById('templateframe');
}

Message.prototype.initFunctions = function() {
	var obj = this;
	
	this.setHeight = function(newHeight) {		
		if(newHeight <= 0) {
			ABC.css(obj.msgContainer, {'height': '1px'});			
			return false;
		} else if(newHeight >= obj.msgWrapper.offsetHeight) {
			ABC.css(obj.msgContainer, {'height': obj.msgWrapper.offsetHeight+'px'});			
			return false;
		}
		
		ABC.css(obj.msgContainer, {'height': newHeight+'px'});
		return true;			
	}
}

Message.prototype.doOpen = function(obj) {
	obj.sqrtAnim = obj.sqrtAnim || new sqrtSmooth(1, obj.msgWrapper.offsetHeight, 20, obj.setHeight);
	obj.sqrtAnim._do(1, obj.msgWrapper.offsetHeight, 20, obj.setHeight);
}

Message.prototype.doClose = function(obj) {
	obj.sqrtAnim = obj.sqrtAnim || new sqrtSmooth(obj.msgWrapper.offsetHeight, 1, 20, obj.setHeight);
	obj.sqrtAnim._do(parseInt(obj.msgContainer.style.height), 1, 20, obj.setHeight);
}