function flip_menu2(id) {
	if(!this.prev) this.prev = menu2_default;

	ax.dom.$('menu2_' + this.prev).addClass('hidden');
	ax.dom.$('menu2_' + id).removeClass('hidden');
	
	this.prev = id;
}

function flip_menu2_default() {
	flip_menu2(menu2_default);
 }
 
function gallery_scroll_to_before() {
	ax.dom.$('gallery-cc').addClass('onBefore')
//	document.getElementById('gallery-cc').className = 'ribbon-cc onBefore'
}

function gallery_scroll_to_after() {
	ax.dom.$('gallery-cc').removeClass('onBefore')
//	document.getElementById('gallery-cc').className = 'ribbon-cc onAfter'
}

function gallery_scroll_to_info(n) {
	ax.dom.$('gallery-cc').addClass('onInfo')
	ax.smooth({obj:ax.dom.$('gallery-text'+n).style, param:'left', fora: text_transition_speed, from: 758, to:200, add:'px', transition: sine_transition})
	current_gallery_text = n
}

function gallery_scroll_to_less(n) {
	if(!ax.dom.$('gallery-cc').hasClass('onInfo')) return

	ax.smooth({obj:ax.dom.$('gallery-text'+n).style, param:'left', fora: text_transition_speed, from: 200, to:758, add:'px', transition: sine_transition});
	setTimeout(function(){
		ax.dom.$('gallery-cc').removeClass('onInfo')
		current_gallery_text = null
	}, text_transition_speed)
}

function sine_transition(x) {
	return (Math.PI / 2) * Math.sin(x * Math.PI)
}

function ribbon_to(n, id) {
	if (ax.flag['flip']) return;

	gallery_scroll_to_less(current_gallery_text)
	window.location.hash = id
	
	setTimeout(function(n){
		// if already current, don't move
		var button = ax.dom.$('gallery_button'+n)
		if (button.hasClass('current')) return;

		// the new gallery item
		var gallery = ax.dom.$('gallery_' + n)
		gallery.inside(ax.dom.$('gallery-cc'))
		gallery.after(ax.dom.$('gallery-cc').findElement('firstChild'))
		
		// make a flip
		ax.smooth({
			obj: ax.dom.$('gallery-cc').style,
			param: 'marginTop',
			fora: gallery_transition_speed,
			from: 0,
			to: -380,
			add: 'px',
			transition: sine_transition,
			flag: 'flip'
		})
		
		// normalize gallery area after making a flip
		setTimeout(function(gallery){
			galleryCC = ax.dom.$('gallery-cc');
			galleryCC.findElement('firstChild').inside(galleryCC)
			galleryCC.style.marginTop = '0px'
		}.bind(null, [gallery]), gallery_transition_speed)
		
		// mark current thumbnail
		ax.dom.$('scrollarea1').descendants('BUTTON').each(
			function(el) {ax.dom.removeClass('current', el)}
		)
		button.addClass('current')

		// scroll thumbnails
		set_scrollx("scrollarea1")
		if(button.offsetLeft < scrollx_element_width*5) scrollx_r(scrollx_element_width)
		if(button.offsetLeft > scrollx_element_width) 	scrollx_l(scrollx_element_width)

	}.bind(null, [n]), current_gallery_text ? (text_transition_speed * 1.2) : 0)
	
//	setTimeout(function(n){
//		ax.dom.$('scrollarea1').descendants('BUTTON').each(
//			function(el) {ax.dom.removeClass('current', el)}
//		)
//		ax.dom.$('gallery_button'+n).addClass('current')
//		ax.smooth({obj:ax.dom.$('gallery-cc').style, param:'marginTop', fora:500, to:-380*(n-1), add:'px', transition: sine_transition})
//	}.bind(null, [n]), current_gallery_text ? 500 : 0)
}

	current_gallery = 0;
	current_gallery_text = null;

	scrollx_move_flag=0;
	scrollx_element = null;
	scrollx_buttonL = null;
	scrollx_buttonR = null;
	scrollx_def_speed = 12;	
	scrollx_speed = scrollx_def_speed;
	scrollx_delay = 20;
	scrollx_element_width = 132;
	text_transition_speed = 400
	gallery_transition_speed = 400

function set_scrollx(el) {
	scrollx_element = document.getElementById(el);
	scrollx_buttonL = document.getElementById(el + '_buttonL');
	scrollx_buttonR = document.getElementById(el + '_buttonR');
	
	scrollx_w1 = scrollx_element.firstChild.offsetWidth - scrollx_element.parentNode.offsetWidth;
	if(!scrollx_element.style.marginLeft) scrollx_element.style.marginLeft = '0px';
}

function scrollx_l(width) {
	ax.dom.removeClass('inactive', scrollx_buttonL);

	newv = parseInt(scrollx_element.style.marginLeft);
	if(-newv >= scrollx_w1) {
		ax.dom.addClass('inactive', scrollx_buttonR);
		scrollx_element.style.marginLeft = '-' + scrollx_w1+'px';
		return;
	} 

	if(width > 0) {
		setTimeout("scrollx_l("+(width-scrollx_speed)+")", scrollx_delay);
		if(scrollx_element.offsetHeight > 60) 	scrollx_element.style.marginLeft = newv-scrollx_speed+'px';
	}
}

function scrollx_r(width) {
	ax.dom.removeClass('inactive', scrollx_buttonR);

	newv = parseInt(scrollx_element.style.marginLeft);
	if(newv >= -1) {
		scrollx_element.style.marginLeft = 0;
			ax.dom.addClass('inactive', scrollx_buttonL);
		return;
	} 

	if(width > 0) {
		setTimeout("scrollx_r("+(width-scrollx_speed)+")", scrollx_delay);
		if(newv<=0)	scrollx_element.style.marginLeft = newv+scrollx_speed+'px';
	}
}

function init(){
	if (typeof hashToN == 'object') {
		id = window.location.hash.slice(1)
		n = hashToN[id]
		if (n) {
			set_scrollx("scrollarea1")
			ribbon_to(n, id)
			scrollx_l((n-3)*132)
		}
	}
}

ax.event.listen(window, 'load', init)
