/*
* Scholarship
* @Author: Alexander Gavazov
* @Site: www.studio.bg
*/


var Scholarship = function(steps, texts) {
	this.steps = steps;
	this.texts = texts;

	this.lastActive = null;
	this.textTopDestination = null;

	this.setBehaviour();
}


Scholarship.prototype.setBehaviour = function() {
	this.steps.select('a').each(function(node) {
		var cloning = node.cloneNode(true);
		node.parentNode.appendChild(cloning);
		cloning.addClassName('active');
		cloning.setOpacity(0);

		cloning.observe('mouseover', this.fade.bind(this, cloning, .5));
		cloning.observe('mouseout', this.fade.bind(this, cloning, 0));

		node.observe('click', Event.stop);
		cloning.observe('click', Event.stop);

		node.observe('click', this.setActive.bind(this, cloning));
		cloning.observe('click', this.setActive.bind(this, cloning));
	}.bind(this));

	this.textTopDestination = this.texts.getHeight();

	this.texts.select('.text_step').each(function(node) {
		node.style.top = this.textTopDestination + 'px';
		node.setOpacity(0);
	}.bind(this));
}


Scholarship.prototype.fade = function(node, opacityTo) {
	if (node.isActive) {
		return;
	}

	if (node.fadeEffect) {
		node.fadeEffect.cancel();
	}

	node.fadeEffect = new Effect.Opacity(node, {
		to: opacityTo,
		duration: .7
	});
}


Scholarship.prototype.moveText = function(node, top) {
	// Check FF2
	if (navigator && navigator.userAgent && navigator.userAgent.search(/firefox\/2..*/gi) != -1) {
		node.style.top = 0 + 'px';
		return;
	}

	if (node.Effect) {
		node.Effect.cancel();
	}

	node.Effect = new Effect.Move(node, {
		y: top,
		transition: Effect.Transitions.sinoidal,
		duration: .5,
		mode: 'absolute'
	});
}


Scholarship.prototype.setActive = function(node) {
	// Hide last items
	if (this.lastActive) {
		// Hide menu
		this.lastActive.isActive = false;
		this.fade(this.lastActive, 0);


		// Hide text
		var old = $(this.lastActive.href.replace(/.*#/gi, ''));
		this.moveText(old, this.textTopDestination);
		this.fade(old, 0);
	}


	// Show current menu
	this.fade(node, 1);
	node.isActive = true;


	// Show current text
	var current = $(node.href.replace(/.*#/gi, ''));
	this.moveText(current, 0);
	this.fade(current, 1);


	// Set last used item
	this.lastActive = node;
}