
window.addEvent('domready', function() {
	if(Browser.Engine.name == 'trident' && Browser.Engine.version == 4) {
		topy = $('bottomcontainer').getPosition().y;
		$('topcontainer').setStyle('height',topy+'px');
	}
	setTransitions();
	initFirstLevelAction();
	initSecondLevelMenu();
	initThirdLevelMenu();
	initCurrentPath();
	initImageZoom();
});

function initCurrentPath() {
	current = $$('#mainmenu_bar .menu-current');
	snd_arrow = false;
	if(current.length == 1) {
		current[0].getParents().each(function(par) {
			if(par.hasClass('thirdlevelmenu') || par.hasClass('secondlevelmenu')) {
				par.setStyles({display: 'block', opacity:100});
				par.fade('show');
			}
			if(par.hasClass('hasThird')) {
				snd_arrow = par;
			}
			if(par.hasClass('firstlevelmenu')) {
				setHorArrow(par.getElement('a'));
			}
		});
		setRightArrow(current[0]);
	}
	if(snd_arrow)
		setRightArrow(snd_arrow);
	all_arrows = $$('#mainmenu_bar .arrow_right').extend($$('#firstlevel_arrow'));
	max_x = 0;
	the_y = 0;
	bottom = $('bottomcontainer');
	all_arrows.each(function(arr) {
		this_x = arr.getPosition(bottom).x;
		if(this_x > max_x) {
			max_x = this_x;
			the_y = arr.getPosition(bottom).y
		}
	});
	alignMainContainer(max_x,the_y);
}

function alignMainContainer(x,y) {
	if(!$('maincontainer'))
		return;
	x += 25;
	y = Math.abs(y) - 10; 
	$('maincontainer').setStyle('left',x+'px');
	$('maincontainer').setStyle('bottom',y+'px');
	mainc_height = $('maincontainer').getSize().y;
	mainc_tobody = $('maincontainer').getCoordinates('body').bottom;
	if(mainc_tobody - mainc_height < 10) { // mainc is to big, lets add a top value and make it scrollable
		$('maincontainer').setStyle('top','10px');
		if(Browser.Engine.name == 'trident' && Browser.Engine.version == 4) {
			$('maincontainer').setStyle('overflow','hidden');
			mctopy = $('bottomcontainer').getPosition().y - 40;
			$('maincontainer').setStyle('height',mctopy+'px');
		}
		$('maincontainer').setStyle('padding-right','30px');
		var mainc_scroll = new MooScroll({selector:'#maincontainer'});
	}
}

function initFirstLevelAction() {
	firstlevel = $$('#mainmenu_bar .firstlevelmenu');
	firstlevel.each(function(mp) {
		mp.getElement('a').addEvent('click',moveHorArrow);
		mp.getElement('a').addEvent('click',clearAllSubMenus);
	});
}

function moveHorArrow(e) {
	$('firstlevel_arrow').get('tween').cancel();
	$('firstlevel_arrow').tween('left',$('firstlevel_arrow').getPosition().x+'px',this.getPosition().x+'px');
}
function setHorArrow(el) {
	$('firstlevel_arrow').setStyle('left',el.getPosition().x+'px');
}

function initSecondLevelMenu() {
	secondlevel = $$('#mainmenu_bar .secondlevelmenu');
	secondlevel.each(function(sl,index) {
		sl.setStyle('opacity',0);
		sl_aChildren = sl.getChildren().filter(function(child,index) { return (child.get('tag') == 'a') || (child.hasClass('hasThird')); });
		h = (sl_aChildren.length * 17) + 32 - (index);
		sl.setStyle('top','-'+h+'px');
		sl.getParent().addEvent('click',toggleSecondLevel);
		sl.getChildren().each(function(item) {
			item.addEvent('click',function(e) { moveRightArrow(this); e.stopPropagation(); return toggleThirdLevel(this); });
		});
	});
}

function moveRightArrow(el) {
	pT = el.getParent();
	ra = false;
	pT.getChildren().each(function(ch) { if(ch.hasClass('arrow_right')) ra = ch; });
	if(!ra)
		return false;
	ra.setStyle('display','block');
	ra.tween('top',ra.getPosition(pT).y+'px',el.getPosition(pT).y+'px');
}
function setRightArrow(el) {
	pT = el.getParent();
	ra = false;
	pT.getChildren().each(function(ch) { if(ch.hasClass('arrow_right')) ra = ch; });
	if(!ra)
		return false;
	ra.setStyle('display','block');
	ra.setStyle('top',el.getPosition(pT).y+'px');
}

function toggleSecondLevel(e) {
	e.stopPropagation();
	sl = this.getElement('div');
	if(sl.getStyle('opacity') > 0) {
		sl.fade('out');	
	}
	else {
		sl.setStyles({ display: 'block', opacity: 0	});
		sl.setStyle('display','block');
		sl.getElements('.arrow_right').each(function(arrow) { arrow.setStyle('display','none'); });
		sl.fade('in');
	}
	return false;
}

function initThirdLevelMenu() {
	thirdlevel = $$('#mainmenu_bar .thirdlevelmenu');
	thirdlevel.each(function(tl,index) {
		tl.setStyle('opacity',0);
		h = (tl.getChildren().length -1) * 14 - (index*4);
		tl.setStyle('top','-'+h+'px');
		tl.getElements('a').each(function(atag) { atag.addEvent('click',function(e) { moveRightArrow(this); e.stopPropagation() }); });
	});
}

function toggleThirdLevel(theThird) {
	clearAllOtherThirdLevel(theThird);
	if(theThird.hasClass('hasThird')) {
		tl = theThird.getElement('div');
		if(tl.getStyle('opacity') > 0) {
			tl.fade('out');	
		}
		else {
			tl.getElements('.arrow_right').each(function(arrow) { arrow.setStyle('display','none'); });
			tl.setStyles({ display: 'block', opacity: 0	});
			tl.fade('in');
		}
		return false;
	}
	return true;
}

function clearAllOtherThirdLevel(theThird) {
	theThird.curM = true;
	thirdlevel = $$('#mainmenu_bar .hasThird');
	thirdlevel.each(function(tl,index) {
		if(!tl.curM) {
			tl.getElement('div').setStyles({ display: 'none', opacity: 0	});
		}
	});
	theThird.curM = false;
}

function clearAllSubMenus() {
	secondlevel = $$('#mainmenu_bar .secondlevelmenu');
	secondlevel.each(function(sl,index) {
		sl.setStyles({ display: 'none', opacity: 0	});
	});
	thirdlevel = $$('#mainmenu_bar .thirdlevelmenu');
	thirdlevel.each(function(tl,index) {
		tl.setStyles({ display: 'none', opacity: 0	});
	});
	arr_right = $$('#mainmenu_bar .arrow_right');
	arr_right.each(function(arr) {
		arr.setStyle('display','none');
	});
}




function setTransitions() {
	$('firstlevel_arrow').set('tween', {transition: Fx.Transitions.Quad.easeInOut});	
}

