var _g = {
	delay: 400,
	fx: {
		fps: 70,
		duration: 400,
		transition: Fx.Transitions.Expo.easeInOut,
		wait: false
	},
	step: {
		current: 0,
		offset: 100
	},
	containers: [],
	menu: {
		divs: [],
		names: [],
		digits: []
	},
	side: {
		width: {
			min: 40,	max: 220,	error:10
		}
	},
	size: {
		offset: 20
	}
};
Element.implement({
	correctPoleSize: function(){
		var S = this.getElement('.box').getSize();
		this.hasClass('pole')?this.setStyles({
			'width': S.x-S.x%2,
			'height': S.y-S.y%2
		}):'';
		return this;
	},
	convert_digit_to_png: function(n){
		this.empty().title = n;
		var results = n.toString().match(/\d/g);
		if ($chk(results))	results.each(function(result){
			 this.set('html', this.get('html')+'<u id="d'+result+'" class="d"><b></b></u>')
		}.bind(this))
		return this;
	}
})
function i_max(i) {
	return (i==_g.step.max && _g.step.current==_g.step.min);
}
function i_min(i) {
	return (i==_g.step.min && _g.step.current==_g.step.max);
}
function pose(_c){
	// _c = container
	var _location = returnLocation(_c.i)
	if ((_location == _c.location && _location != 'center')) {
		_c.dom.setStyles(getCoords(_c.dom, _c.location))
	} else {
		elementsDisplay();
		_c.location!='center' && _location!='center'?_c.dom.setStyles(getCoords(_c.dom, _location)):_c.morph.start(_location=='center'&&_c.location!='center'?getCoords(_c.dom, _c.location, _location):getCoords(_c.dom, _location)).chain(function(){
			arrowsDisplay();
		});
		_c.location = _location;
	}
	return this;
};
function return_left(obj, location){
	return (location=='center'?(_g.size.x/2-obj.getSize().x/2-12):(location=='right'?_g.size.x+_g.step.offset:-(obj.getSize().x+_g.step.offset)));
}
function getCoords(obj, location_start, location_end ) {
	return {
		'left': $defined(location_end)?[return_left(obj, location_start), return_left(obj, location_end)]:return_left(obj, location_start),
		'top': Math.max(_g.size.offset, (_g.size.y/2-obj.getSize().y/2+18) )
	};
}
function returnLocation(i) {
	var loca = 'center';
	if ((i>_g.step.current && !i_max(i)) || i_min(i)) {
		loca = 'right'
	}
	if ((i<_g.step.current && !i_min(i)) || i_max(i)) {
		loca = 'left'
	}
	return loca;
}
function elementsDisplay() {
	var in_limit = _g.step.current>=_g.step.min,
		not_cover = _g.step.current!=0;
	if (in_limit) {
		_g.side.arrows.left.set('class', 'step_arrow '+_g.menu.names[(_g.step.current==_g.step.min)?(_g.step.max-_g.step.min):(_g.step.current-_g.step.min-1)]);
		_g.side.arrows.right.set('class', 'step_arrow '+_g.menu.names[(_g.step.current==_g.step.max)?0:(_g.step.current-_g.step.min+1)]);
		document.body.set('class', _g.menu.names[_g.step.current-_g.step.min]);
	}
	_g.step.current!=1?_g.logo.morph.start({'left': !not_cover?-_g.logo.width:35}):'';
	_g.cartouche.morph.start({'right': !in_limit?-_g.cartouche.width:-2})
	_g.side.arrows.morph.start({
		'0': {
			'margin-left': !in_limit?20:180
		},
		'1': {
			'margin-right': !not_cover?20:180
		}
	}).chain(function(){
		if (!in_limit) {
			_g.side.arrows.left.set('class', 'step_arrow');
			_g.side.arrows.right.set('class', 'step_arrow');
		}
	})
}
function arrowsDisplay() {
}
function scrollHide(c) {
	c!=_g.step.current?_g.scroller.boxes[_g.step.current].setStyle('display', 'none'):'';
}
function scrollDisplay() {
	_g.scroller.boxes[_g.step.current].setStyles({
		'visibility': ( (_g.size.y-_g.size.offset)<_g.containers[_g.step.current].dom.getHeight() )?'visible':'hidden'//,		'height': _g.size.y-_g.size.offset*2
	})
}
function animateNewStep(n){
	if (_g.step.current!=n) {
		_g.step.current = n;
		_g.list.convert_digit_to_png(_g.containers[n].list_number+1)
		window.fireEvent('resize');
	}
}

window.addEvent('domready', function(){
	
//	GLOBAL Start
	$('logo').addEvent('mousedown', function(e){
		e.stop();
		animateNewStep(0);
	})
	_g.logo = {};
	_g.logo.img = $('logo');
	_g.logo.width = _g.logo.img.getStyle('width').toInt();
	_g.logo.morph = new Fx.Morph(_g.logo.img, _g.fx);
	_g.logo.label = _g.logo.img.getFirst().getChildren()[1];
	_g.logo.pager = _g.logo.img.getLast();
	_g.logo.pages = [];
	
	_g.cartouche = {};
	_g.cartouche.img = $('cartouche');
	_g.cartouche.width = _g.cartouche.img.getStyle('width').toInt();
	_g.cartouche.morph = new Fx.Morph(_g.cartouche.img, _g.fx);
	_g.cartouche.label = $('label').getFirst();
	
	_g.containers = $$('div.container').map(function(item, i){
		return {
			dom: item,
			i: i,
			morph: new Fx.Morph(item, _g.fx),
			lists: item.getChildren(),
			list_number: 0,
			list_morph: new Fx.Elements(item.getChildren(), _g.fx)
		};
	});

	_g.side.sides = $$('div.side');
	_g.side.attr = [];
	_g.side.morph = _g.side.sides.map(function(item){
		return new Fx.Morph(item.getFirst(), _g.fx);
	})
	
	_g.side.arrows = {};
	_g.side.arrows.left = $$('.left .step_arrow');
	_g.side.arrows.right = $$('.right .step_arrow');
	_g.side.arrows.both = $A(_g.side.arrows.left).extend($A(_g.side.arrows.right));
	_g.side.arrows.morph = new Fx.Elements(_g.side.arrows.both, _g.fx);
	
	refreshSomeGlobals();
	function refreshSomeGlobals() {
		_g.size = $H(_g.size).extend(window.getSize());
		_g.side.sides.map(function(side, i){
			_g.side.attr[i] = {};
			_g.side.attr[i].right = side.hasClass('right');
			_g.side.attr[i].body = {};
			_g.side.attr[i].body.dom = side.getFirst();
			_g.side.attr[i].body.width = _g.side.attr[i].body.dom.getWidth();
			_g.side.attr[i].width = side.getWidth();
		});
	}
//	GLOBAL End

	function showContainer(_c) {
		_c.dom.getElements('.pole').each(function(div){
			div.correctPoleSize();
		})
		_c.dom.setStyles(getCoords(_c.dom, _c.location));
		_c.location=='center'?_c.dom.removeClass('hidden'):_c.dom.addClass('hidden');
	}
	arrowsDisplay();
	elementsDisplay();
	_g.menu.divs = $$('div.menu div');
	_g.menu.divs.each(function(menu, i){
		_g.menu.names[i] = menu.id;
	})
	
	_g.containers.each(function(container, i){
		container.location = returnLocation(i);
		if (container.dom.hasClass('gallery')) {
			!$defined(_g.step.min)?_g.step.min=i:'';
			_g.step.max = i;
			var tables = container.dom.getChildren();
			tables.shift();
			tables.setStyles({
				'display': 'none'
			});
			_g.logo.pager.addEvent('mousedown', function(e){
				e.stop();
			}).grab(new Element('div', {
				'class': 'page '+_g.menu.names[i-_g.step.min]
			}).set('text', container.dom.getChildren().length))
		}
		showContainer(container);
//		(i>=_g.step.min && i<=_g.step.max)?:'';
		_g.logo.pages[i-_g.step.min]=_g.logo.pager.getLast();
		window.addEvent('resize', function(){
			refreshSomeGlobals()
			pose(container);
		})
	})
	_g.lists = $('lists').set('html', _g.logo.pager.get('html')).getChildren().each(function(page, i){
		page.convert_digit_to_png(page.get('text'))
	})
	_g.list = $('list').convert_digit_to_png(1)

	
	_g.menu.divs.each(function(menu, i){
		menu.addEvent('mousedown', function(){
			animateNewStep(i+_g.step.min);
		})
		menu.getElement('.digit').convert_digit_to_png(_g.logo.pages[i].get('text'))

	})
	$('author').addEvent('mousedown', function(){
		animateNewStep(1);
	})
	_g.logo.pager.getChildren().each(function(page, i){
		var limit = page.get('text').toInt()
		page.empty();
		if (limit>1) {
			for (i=1;i<=limit;i++) {
				page.grab(new Element('div', {'class': 'number'}).grab(new Element('p', {'class': 'fright'}).set('text', i).convert_digit_to_png(i)).addEvent('mousedown', function(e){
					if ((_g.step.current>=_g.step.min) && (_g.step.current <= _g.step.max)) {
						var step_c =  _g.step.current,
							container = _g.containers[step_c], 
							list_n = container.list_number,
							last_n = -2,
							title_n = this.getFirst().title-1;
						if (list_n!=title_n) {
							_g.list.convert_digit_to_png(title_n+1)
							container.lists[list_n].setStyle('display', 'none');
							container.lists[title_n].setStyle('display', 'block');
							pose(_g.containers[_g.step.current])
//							window.fireEvent('resize');
/*							function chain_morph(first_n, second_n){
								if ($defined(container.lists[first_n])) {
									container.lists[first_n].setStyles({
										'display': 'none',
										'position': 'absolute'
									});
								}
								container.lists[second_n].setStyles({
									'position': 'relative'
								});							
							}
							chain_morph(last_n, list_n)
							container.lists[title_n].setStyles({
								'display': 'block',
								'position': 'absolute'
							});
							container.list_morph.start(JSON.decode(
								'{"'+list_n+'": {"top": '+container.lists[list_n].getHeight()+'}, "'+title_n+'": {"top": -'+container.lists[list_n].getHeight()+'}}'
							)).chain(function(){
								chain_morph(list_n, title_n)
								container.last_number = list_n;
								window.fireEvent('resize');
							})
							container.last_number = list_n;
*/							container.list_number = title_n;

						} 
					}
				}))
			}
		}
	})



	document.addEvent('keydown', function(e){
		if (e.key == 'right' && e.control) {
			_g.side.arrows.right.fireEvent('mousedown');
			e.stop()
		}
		if (e.key == 'left' && e.control) {
			_g.side.arrows.left.fireEvent('mousedown');
			e.stop()
		}
		if (e.code == 9) {
			e.stop()
		}
	})


//	TEST					________________________________________________________________________________________________________________________	TEST
	
	
	_g.side.arrows.left.addEvent('mousedown', function(e){
		if (e) e.stop();
		animateNewStep(_g.step.current==_g.step.min?_g.step.max:Math.max(_g.step.min, _g.step.current-1));
		return false;
	})
	_g.side.arrows.right.addEvent('mousedown', function(e){
		if (e) e.stop();
		animateNewStep(_g.step.current==_g.step.max?_g.step.min:Math.min(_g.step.max, _g.step.current+1));
		return false;
	})
	
})
