var transition_speed = 600;

var currentSubMenu = false;
var currentSubMenuAnnotation = false;
var currentIndexMenuWindow_id = false;
var currentIndexMenuAnnotation_id = [];

function closeSubMenu() {
	if(currentSubMenu) currentSubMenu.addClass('hidden');
//	ax.dom.$('subMenu' + id).addClass('hidden');
}

function openSubMenu(id) {
	if(currentSubMenu) {
		closeSubMenu();
	}
	currentSubMenu = ax.dom.$('subMenu' + id)
	if(currentSubMenu) {
		currentSubMenu.removeClass('hidden');
	}
//	currentSubMenu.listen('mouseleave', 'closeSubMenu()');
	ax.dom.$('main_menu').listen('mouseleave', 'closeSubMenu()');
}

function showSubMenuAnnotation(sid) {
	if(currentSubMenuAnnotation) {
		currentSubMenuAnnotation.addClass('hidden');
	}
	currentSubMenuAnnotation = ax.dom.$('subMenuAnnotation' + sid)
	currentSubMenuAnnotation.removeClass('hidden');
}

function showIndexMenuWindow(sid) {
	if(currentIndexMenuWindow_id) {
		ax.dom.$('indexMenuWindow' + currentIndexMenuWindow_id).addClass('hidden');
		ax.dom.$('indexMenuTab' + currentIndexMenuWindow_id).removeClass('current');
	}
	currentIndexMenuWindow_id = sid;
	ax.dom.$('indexMenuWindow' + currentIndexMenuWindow_id).removeClass('hidden');
	ax.dom.$('indexMenuTab' + currentIndexMenuWindow_id).addClass('current');
}

function showIndexMenuAnnotation(id, sid) {
	ax.dom.$('indexMenuAnnotation' + id).addClass('hidden');
	if(currentIndexMenuAnnotation_id[id]) {
		ax.dom.$('indexMenuAnnotation' + currentIndexMenuAnnotation_id[id]).addClass('hidden');
	}
	currentIndexMenuAnnotation_id[id] = sid;
	ax.dom.$('indexMenuAnnotation' + currentIndexMenuAnnotation_id[id]).removeClass('hidden');
}


function setHidden(el) {
	if(el && el.className) ax.dom.addClass('hidden', el)
}

function showNewsFeed(group, id) {
	feed = ax.dom.$('newsFeed' + id);
	ax.array.each(setHidden, feed.parentNode.childNodes)
	feed.removeClass('hidden');
	
	tabs = ax.dom.$('newsFeedTabs').childNodes;
	for (i in tabs) {
		if (tabs[i].className && tabs[i].getAttribute('feedGroupId')==group)
			ax.dom.removeClass('newsHeaderCurrent', tabs[i])
	}
	ax.dom.$('newsFeedTab' + id).addClass('newsHeaderCurrent');
}


function sine_transition(x) {
	return (Math.PI / 2) * Math.sin(x * Math.PI)
}

function showNextTeaser() {
	scrollTeaserTo(-990)
}

function showPrevTeaser() {
	scrollTeaserTo(990)
}

function scrollTeaserTo(howmuch) {
	if (ax.flag['flip']) return;
	
	var obj = ax.dom.$('scrollBlock_teasers');
	var row = ax.dom.$('scrollBlock_row');
	var current = obj.style.marginLeft.replace(/[^\d|\-]/g,'');
	current = parseInt(current) || 0
	var from = current;

//	ax.dom.$('showPrevTeaser').removeClass('hidden');
//	ax.dom.$('showNextTeaser').removeClass('hidden');
//	if (current+howmuch == 0) {
//		ax.dom.$(row.lastChild).before(row.firstChild);
//		ax.dom.$('showPrevTeaser').addClass('hidden');
//	}
//	if (-(current+howmuch*2) == obj.offsetWidth) {

//		ax.dom.$('showNextTeaser').addClass('hidden');
//	}

	// слишком назад
	if (current+howmuch > 0) {
		obj.style.marginLeft = -howmuch + 'px';
		ax.dom.$(row.lastChild).before(row.firstChild);
		from = current-howmuch;
	}

	// слишком вперед	
	if (-(current+howmuch) >= obj.offsetWidth) {
		obj.style.marginLeft = (current - howmuch) + 'px';
		ax.dom.$(row.firstChild).after(row.lastChild);
		from = current-howmuch;
	}

	ax.smooth({
		obj: obj.style,
		param: 'marginLeft',
		fora: transition_speed,
		from: from,
		to: from+howmuch,
		add: 'px',
		transition: sine_transition,
		flag: 'flip'
	})
}

function goTeaser(url) {
	if (ax.flag['flip']) return;
	
	document.location = url;
}


function init() {
	initWheel();
}

function initWheel() {
    //adding the event listerner for Mozilla
    if(window.addEventListener)
        document.getElementById('scrollBlock_teasers').addEventListener('DOMMouseScroll', wheel, false);
 
    //for IE/OPERA etc
	document.getElementById('scrollBlock_teasers').onmousewheel = wheel;
}

function wheel(event)
{
	var delta = 0;
	
	if (!event) /* For IE. */
		event = window.event;
	
	if (event.wheelDelta) { /* IE/Opera. */
		delta = event.wheelDelta/120;
	} else if (event.detail) { /** Mozilla case. */
		delta = -event.detail/3;
	}
	
	if (delta<0) showNextTeaser();
	if (delta>0) showPrevTeaser();
                
	if (event.preventDefault) event.preventDefault(); // good browsers
	event.returnValue = false; // IE 7
}
