// For preview animation
var previewInitInterval = 0;
var previewStepInterval = 30;

var stepTimer = null; // global, IE fix
lenta = { // Namespace
	// Properties
		// -> base
	marginLeft : 0, // px
	marginRightLimit : 0,
	lentaWidth : 0, // px
	contWidth :0, // px
	curMohth : 0,
	monthCount : 12, // [num]
	inited : false,
	frameObj : null,
	moveObj : null,
	lineObj : null,
		// -> scroll
	scrollObj : null,
	scrollPadding : 20, // px
	scrItemWidth : 0, // px
	scrWidth : 0, // px
	scrollLeft : 0, // px
	wasScrollLeft : 0,
		// -> scroll move
	moveInit : false,
	moveX0 : 0,
	moveX : 0,
	initTimeout : 150,
	stepTimeout : 10,
	lentaStep : 50, // px
	direction : false,
		// -> comments animation
	coms : [],
	isComShow : [],
	comTimers : [],
	comTimeout : 10,
	comSteps : 7,
	nowCommentShow : [],
	nowCommentHide : [],
		// -> hide
	fadeInterval : 50,
	fadeStep : 10,
	// Methods
	init : function() {
		if (!this.inited) {
			// Objects detect
			if(!(this.frameObj = $("lentaFrameId"))) {alert("lentaFrameId is not found"); return;}
			if(!(this.moveObj = $("lentaMoveId"))) {alert("lentaMoveId is not found"); return;}
			if(!(this.lineObj = $("lentaLineId"))) {alert("lentaLineId is not found"); return;}
			if (!(this.scrollObj = $("lentaScrollId"))) {alert("lentaScrollId is not found"); return;}
			// Add handlers
			addHandler(window, "resize", function() {lenta.scrollDefine();})
			addHandler(document, "mousemove", function(evt) {lenta.moveHandler(evt);})
			addHandler(document, "mouseup", lenta.clearMove);
			if (this.scrollObj = document.getElementById("lentaScrollId")) {
				addHandler(lenta.scrollObj, "mousedown", function(evt) {
					evt = evt || window.event;
					if(evt.preventDefault) evt.preventDefault();
					evt.returnValue = false;
					lenta.moveInit = true;
					lenta.moveX0 = defPosition(evt).x;
					lenta.wasScrollLeft = lenta.scrollLeft;
				});
			}
			this.elHoverInit();
			this.inited = true;
		}
		// Init
		this.scrollDefine();
		return false;
	},
	hide : function() {
		if (this.wasHide) return;
		this.wasHide = true;
		$('lenta-fade').style.display = "block";
		fadeOut('lenta-fade', 0, this.fadeStep, this.fadeInterval);
	},
	show : function() {
		if (!this.wasHide) return false;
		$('map-fade3').style.display = "block";
		fadeOut('map-fade3', 0, this.fadeStep, this.fadeInterval);
		return false;
	},
	elHoverInit : function() {
		var el, com;
		var len = window.allDataForLenta ? allDataForLenta.length : 100;
		for (var i = 0; i <= len; i++) {
			if ( el = $("elId_" + i) ) {
				el.onmouseover = lenta.elMouseOver;
				el.onmouseout = lenta.elMouseOut;
				el.index = i;
				//if (com = $("comId_" + i)) {
				//	com.h = com.offsetHeight;
				//	com.index = i;
				//	com.onmouseover = lenta.comMouseMove;
				//	com.onmouseout = lenta.comMouseMove;
				//	com.onmousemove = lenta.comMouseMove;
				//}
			}
		}
	},
	killFreezed : function(index) {
		for (var i = 0; i < this.isComShow.length; i++) {
			if (i == index) continue;
			if (this.isComShow[i] && !this.nowCommentShow[i] && !this.nowCommentHide[i]) {
				this.nowCommentHide[i] = true;
				comment = $("comId_" + i);
				this.comShow(i, comment.h, comment.h, 1);
			}
		}
	},
	elMouseOver : function() {
		var comment;
		fixClassName(this, "active");
		if (lenta.nowCommentShow[this.index] || lenta.nowCommentHide[this.index]) return;
		if (lenta.isComShow[this.index]) return;
		clearTimeout(lenta.comTimers[this.index]);
		lenta.comTimers[this.index] = setTimeout("lenta.comShow("+this.index+", 0, 10)", 100);
	},
	elMouseOut : function(evt) {
		evt = evt || window.event;
		fixClassName(this, 0, "active");
		if (/*lenta.nowCommentShow[this.index] || */lenta.nowCommentHide[this.index]) return;
		clearTimeout(lenta.comTimers[this.index]);
		lenta.isComShow[this.index] = false;
		lenta.nowCommentShow[this.index] = false;
		lenta.comTimers[this.index] = setTimeout("lenta.comShow("+this.index+", 10, 10, 1)", 50);
	},
	comMouseMove : function() {
		if (!lenta.nowCommentShow[this.index] && !lenta.nowCommentHide[this.index]) {
			clearTimeout(lenta.comTimers[this.index]);
			lenta.comTimers[this.index] = null;
		}
	},
	comShow : function(index, cur, limit, minus) {
		var wrap = $('elId_'+index).firstChild.nodeType == 1 ? $('elId_'+index).firstChild : $('elId_'+index).firstChild.nextSibling;
		var height = cur + (minus ? (-1)*limit : limit) / this.comSteps;
		
		if (minus) height = height < 0 ? 0 : height;
		else height = height < limit ? height : limit;
		
		var wrapY = 5 * height / limit;
		wrap.style.top = wrapY + "px";
		
		if (height == limit && typeof minus == "undefined") {
			this.isComShow[index] = true;
			this.nowCommentShow[index] = false;
			clearTimeout(this.comTimers[index]);
			this.comTimers[index] = null;
		} else if (height == 0 && minus) {
			this.isComShow[index] = false;
			this.nowCommentHide[index] = false;
			clearTimeout(this.comTimers[index]);
			this.comTimers[index] = null;
		} else {
			if (minus) this.nowCommentHide[index] = true;
			else this.nowCommentShow[index] = true;
			clearTimeout(this.comTimers[index]);
			this.comTimers[index] = setTimeout("lenta.comShow("+index+","+height+","+limit+(minus ? ", 1" : "")+")", this.comTimeout);
		}
	},
	clearMove : function() {
		lenta.moveInit = false;
		if (stepTimer != null) {
			clearInterval(stepTimer);
			stepTimer = null;
			if (lenta.direction == 'r') lenta.showNext();
			else lenta.showPrevious();
		}
	},
	showNext : function() {
		this.marginLeft -= this.lentaStep;
		this.marginLeft = this.marginLeft < this.marginRightLimit ? this.marginRightLimit : this.marginLeft;
		this.setMargin();
		return false;
	},
	showPrevious : function() {
		this.marginLeft += this.lentaStep;
		this.marginLeft = this.marginLeft >= 0 ? 0 : this.marginLeft;
		this.setMargin();
		return false;
	},
	setMargin : function() {
		this.moveObj.style.marginLeft = this.marginLeft + "px";
		this.scrollLeftDefine();
	},
	selectMonth : function(num) {
		this.curMohth = num;
		if (this.curMohth >= this.monthCount) return;
		var block;
		if (block = $("monthId_" + this.curMohth)) {
			this.marginLeft = (-1) * block.offsetLeft;
			this.setMargin();
		}
	},
	scrollDefine : function() {
		try {
			this.scrWidth = Math.floor(this.frameObj.offsetWidth) - 2 * this.scrollPadding;
			this.scrItemWidth = this.scrWidth / (this.lineObj.offsetWidth / this.frameObj.offsetWidth);
			this.scrollObj.style.width = this.scrItemWidth + "px";
			this.marginRightLimit = (-1) * (this.lineObj.offsetWidth - this.frameObj.offsetWidth);
			this.scrollLeftDefine()
		} catch(e) {}
	},
	scrollLeftDefine : function() {
		this.scrollLeft = Math.abs(this.marginLeft) / (this.lineObj.offsetWidth - this.frameObj.offsetWidth);
		this.scrollLeft = this.scrollPadding + this.scrollLeft * (this.scrWidth - this.scrItemWidth);
		this.scrollObj.style.left = this.scrollLeft + "px";
	},
	scrollInit : function(order) {
		this.direction = order;
		stepTimer = setInterval("lenta.scrollRepeat()", this.initTimeout);
	},
	scrollRepeat : function() {
		if (this.direction == 'r') this.showNext();
		else this.showPrevious();
		if (stepTimer != null) {
			clearInterval(stepTimer);
			stepTimer = null;
			stepTimer = setInterval("lenta.scrollRepeat()", this.stepTimeout);
		}
	},
	moveHandler : function(evt) {
		if (this.moveInit) {
			evt = evt || window.event;
			if (navigator.userAgent.toLowerCase().indexOf("gecko") == -1) {
				if(evt.preventDefault) evt.preventDefault();
				evt.returnValue = false;
			}
			this.moveX = defPosition(evt).x;
			var dX = this.moveX - this.moveX0;
			dX += this.wasScrollLeft;
			if (dX <= this.scrollPadding) scrollLeft = this.scrollPadding;
			else if (dX > this.scrWidth - this.scrItemWidth + this.scrollPadding) scrollLeft = this.scrWidth - this.scrItemWidth + this.scrollPadding;
			else scrollLeft = dX;
			this.marginLeft = (-1) * ((scrollLeft  - this.scrollPadding) / (this.scrWidth - this.scrItemWidth)) * (this.lineObj.offsetWidth - this.frameObj.offsetWidth);
			this.setMargin();
		}
	}
};

// ****************************** Map ******************************

function showStatNews(id,a) {
	try {
		$('iframeId').style.display = 'none';
		for (var i = 0; i <= 6; i++ ) {
			$('ntype'+i).style.display = 'none';
			$('lsn_'+i).style.fontWeight = 'normal';
		}
		$('ntype'+id).style.display = 'block';
		$('lsn_'+id).style.fontWeight = 'bold';
		$('lsnh').innerHTML = a.innerHTML;
	} catch(e) {}
	return false;
}

function showVideo(show) {
	$('videoContainerId').style.display = show ? 'block' : 'none';
	$('contPageId').style.display = show ? 'none' : 'block';
	return false;
}

// Zoooooooooooooooooooooom
function fontZoom(_class,a, idx) {
	document.body.className = _class;
	var links = a.parentNode.getElementsByTagName('a');
	for (var i = 0; i < links.length; i++) links[i].getElementsByTagName('img')[0].setAttribute('src', '/fileadmin/site/images/fs'+(i+1)+'.gif');
	a.getElementsByTagName('img')[0].setAttribute('src', '/fileadmin/site/images/fs'+idx+'_a.gif');
	if (_class == ''){
		_class = 'normal';
	}
	var today = new Date();
	_class = 'size_'+_class+'_'+today.getUTCSeconds();
	ajax_send(_class,'text_size.php');
	return false;
}
// Zoooooooooooooooooooooom
function fontZoom2(_class,a, idx) {
	if (_class == 'normal'){
	} else {
		document.body.className = _class;
	}
	a = document.getElementById(a);
	var links = a.parentNode.getElementsByTagName('a');
	for (var i = 0; i < links.length; i++) links[i].getElementsByTagName('img')[0].setAttribute('src', '/fileadmin/site/images/fs'+(i+1)+'.gif');
	document.getElementById('font_'+idx).getElementsByTagName('img')[0].setAttribute('src', '/fileadmin/site/images/fs'+idx+'_a.gif');
	//ajax_send('size_'+_class+'','text_size.php');
	return false;
}

