/************************************************************************/

/* GENERIC HELP FUNCTIONS */

function myRandom(min, max) {
	return Math.floor(Math.random() * (max - min + 1) + min);
}

function pprint(val) {
	$('pprintArea').setHTML(val);
}

/************************************************************************/

/* Slider Initialization*/

var loading_finished = false;
var is_being_moved = new Array();
var slider_positions = new Array(0,0,0,0,0,0,0);

slider_1_behavior = 1;
slider_2_behavior = 1;
slider_3_behavior = 1;
slider_4_behavior = 1;
slider_5_behavior = 1;
slider_6_behavior = 1;

window.addEvent('domready', function() {

	/* Slider 1 */
	var mySlide1 = new Slider($('slider1'), $('knob1'), {
		steps: 480,
		onComplete: function(step){
			slider_positions[1] = step;
			if (loading_finished)
				slider1Moved(step);
		}
	}).set(480);

	/* Slider 2 */
	var mySlide2 = new Slider($('slider2'), $('knob2'), {
		steps: 480,
		onComplete: function(step){
			slider_positions[2] = step;
			if (loading_finished)
				slider2Moved(step);
		}
	}).set(480);

	/* Slider 3 */
	var mySlide3 = new Slider($('slider3'), $('knob3'), {
		steps: 480,
		onComplete: function(step){
			slider_positions[3] = step;
			if (loading_finished)
				slider3Moved(step);
			}
	}).set(0);

	/* Slider 4 */
	var mySlide4 = new Slider($('slider4'), $('knob4'), {
		steps: 480,
		onComplete: function(step){
			slider_positions[4] = step;
			if (loading_finished)
				slider4Moved(step);
		}
	}).set(0);

	/* Slider 5 */
	var mySlide5 = new Slider($('slider5'), $('knob5'), {
		steps: 480,
		mode: 'vertical',
		onComplete: function(step){
			slider_positions[5] = step;
			if (loading_finished)
				slider5Moved(step);
		}
	}).set(480);

	/* Slider 6 */
	var mySlide6 = new Slider($('slider6'), $('knob6'), {
		steps: 480,
		mode: 'vertical',
		onComplete: function(step){
			slider_positions[6] = step;
			if (loading_finished)
				slider6Moved(step);
		}
	}).set(480);

	if ( ! (window.ie) )
		loading_finished = true;

	morph("pop", "cover", 0, 0, 6000, 0, 0, 0.0, 1.0, 3000 ); // 6000 & 3000

});

/*
 * This function applies mootools effects on an element
 * the element is faded in and moved from start position to end position
 * next the element is faded out
 *
 * The element must be positioned absolutely in the css for this to work
 *
 * elm_name :               the name of the element to move
 * start_top, start_left :  the initial starting position
 * fade_in_duration :       the duration of the moving and fade in effect
 * end_top, end_left :      the final position
 * min_opacity :            the minimum opacity that is reached in fade out
 * max_opacity :            the maximum opacity that is reached in fade in
 * fade_out_duration :      the duration the fade out effect
 */
function move_and_fade (elm_name, start_top, start_left, fade_in_duration, end_top, end_left, min_opacity, max_opacity, fade_out_duration) {

	if (! is_being_moved[elm_name] == true) {

		is_being_moved[elm_name] = true;

		var fade_in_to_end = new Fx.Styles(elm_name, {duration: fade_in_duration,transition: Fx.Transitions.linear});

		fade_in_to_end.start({
			'top': [start_top, end_top],
			'left': [start_left, end_left],
			'opacity': max_opacity

		}).chain(function(){
			var fade_out = new Fx.Styles(elm_name, {duration: fade_out_duration,transition: Fx.Transitions.linear});

			fade_out.start({
				'opacity': min_opacity

			}).chain(function(){
				is_being_moved[elm_name] = false;
			});
		});
	}

}

/*
 * This function applies mootools effects on an element
 * the element is faded in and moved from start position to end position
 * next the element is moved back to the start position and faded out
 *
 * The element must be positioned absolutely in the css for this to work
 *
 * elm_name :               the name of the element to move
 * start_top, start_left :  the initial starting position
 * fade_in_duration :       the duration of the moving and fade in effect
 * end_top, end_left :      the final position
 * min_opacity :            the minimum opacity that is reached in fade out
 * max_opacity :            the maximum opacity that is reached in fade in
 * fade_out_duration :      the duration the fade out effect
 */
function pop_and_fade (elm_name, start_top, start_left, fade_in_duration, end_top, end_left, min_opacity, max_opacity, fade_out_duration) {

	if (! is_being_moved[elm_name] == true) {

		is_being_moved[elm_name] = true;

		var fade_in_to_end = new Fx.Styles(elm_name, {duration: fade_in_duration,transition: Fx.Transitions.linear});

		fade_in_to_end.start({
			'top': [start_top, end_top],
			'left': [start_left, end_left],
			'opacity': max_opacity

		}).chain(function(){
			var fade_out = new Fx.Styles(elm_name, {duration: fade_out_duration,transition: Fx.Transitions.linear});

			fade_out.start({
				'top': [end_top, start_top],
				'left': [end_left, start_left],
				'opacity': min_opacity

			}).chain(function(){
				is_being_moved[elm_name] = false;
			});
		});
	}

}

/*
 * This function applies mootools effects on an element
 * the possible actions are MOVE and POP
 * see the individual funtions above for details
 *
 * The element must be positioned absolutely in the css for this to work
 *
 * action_name :            the name of the action to perform (move or pop)
 * elm_name :               the name of the element to move
 * start_top, start_left :  the initial starting position
 * fade_in_duration :       the duration of the moving and fade in effect
 * end_top, end_left :      the final position
 * min_opacity :            the minimum opacity that is reached in fade out
 * max_opacity :            the maximum opacity that is reached in fade in
 * fade_out_duration :      the duration the fade out effect
 */
function morph (action_name, elm_name, start_top, start_left, fade_in_duration, end_top, end_left, min_opacity, max_opacity, fade_out_duration) {

	if (action_name == "move") {
		move_and_fade(elm_name, start_top, start_left, fade_in_duration, end_top, end_left, min_opacity, max_opacity, fade_out_duration);
	} else if (action_name == "pop") {
		pop_and_fade(elm_name, start_top, start_left, fade_in_duration, end_top, end_left, min_opacity, max_opacity, fade_out_duration);
	}
}

/************************************************************************/

/* Slider Actions */
/*
function depends_on_slider_position(step) {

	if ( (step > -1) && (step < 15) ) {
		morph("pop", "obj1", 50, 0, 2000, 50, 400, 0.0, 1.0, 1000 );
	}
	else if ( (step > 400) && (step < 430) ) {
		morph("move", "obj1", 50, 0, 2000, 50, 400, 0.0, 1.0, 1000 );
	}
}

function each_time_different(step) {

	if (slider_2_behavior == 1) {
		morph("move", "obj2", 50, -50, 1500, 350, -50, 0.0, 1.0, 1500 );
	} else if (slider_2_behavior == 2) {
		morph("pop", "obj2", 50, -50, 1500, 350, -50, 0.0, 1.0, 1500 );
	}

	slider_2_behavior++;
	if (slider_2_behavior == 3)
		slider_2_behavior = 1;
}
*/

function slider1Moved(step) {

	if ( (step > -1) && (step < 15) ) {
		morph("move", "foglia3", 450, 590, 3000, 460, 593, 0.0, 0.0, 4000 );
		morph("move", "foglia", 488, 575, 800, 490, 577, 0.0, 1.0, 4000 );
		morph("move", "foglia2", 474, 630, 2500, 480, 633, 0.0, 1.0, 4000 );
	}
	else if ( (step > 367) && (step < 481) ) {
		morph("move", "foglia3", 450, 590, 3000, 460, 593, 1.0, 1.0, 4000 );
	}

}

function slider2Moved(step) {

	morph("pop", "pupazzo", 310, 552, 500, 290, 694, 0.0, 1.0, 1500 );

	/* this is for detecting the position of the slider, it always needs 2 values -from and to- */
	if ( (step > -1) && (step < 40) ) {
		morph("move", "for_jesters",195, 520, 1000, 190, 525, 0.0,  1.0, 4000 );
	} else if ( (step > 450) && (step < 481) ) {
		morph("move", "toinvest",195, 520, 1000, 190, 525, 0.0, 1.0, 4000 );
	}

}

function slider3Moved(step) {

	if ( (step > 450) && (step < 481) ) {

		morph("move", "domanda", 345, 600, 1000, 345, 600, 0.0, 0.0, 1000 );

	}
	else if ( (step > -1) && (step < 240) ) {

		morph("move", "domanda", 345, 600, 1000, 345, 600, 1.0, 1.0, 1000 );

	}

	if (slider_3_behavior == 2) {

		morph("move", "squalo", 420,-400, 4000, 420, 400, 1.0, 1.0, 1000 );

	} else if (slider_3_behavior == 3) {

		morph("move", "squalo", 420,400, 100, 200, 500, 0.0, 1.0, 200 );
		morph("move", "soldi", -50,850, 500, -70, 870, 0.0, 1.0, 200 );

	} else if (slider_3_behavior == 4) {

		// return shark & money to initial position
        morph("move", "squalo", 420,-400, 500, 420, -400, 1.0, 0.0, 500 );
		morph("move", "soldi", -50,850, 500, -50, 850, 1.0, 0.0, 500 );
		slider_3_behavior = 1;
		
	}

	slider_3_behavior++;
	if (slider_3_behavior == 5)
		slider_3_behavior = 1;

	var step6 = slider_positions[6];
	if ( ( step6 >= 0 ) && ( step6 <= 10 ) && ( step >=  0) && ( step <= 30 ) ){
		morph("move","fake", 345, 425, 2800, 345, 425, 0.0, 1.0, 4000 );
		morph("move","kappa", 345, 425, 800, 345, 425, 0.0, 1.0, 4000 );
	}

}

function slider4Moved(step) {

	morph("move","willbe", 0, 700, 2500, 0, 710, 0.0, 1.0, 5500 );

}

function slider5Moved(step) {

	if ( (step >= 100) && (step <= 480) ) {

		if (slider_2_behavior == 1) {
			morph("move","palline", 0, 0, 500, 0, -25, 1.0, 1.0, 1500 );
		} else if (slider_2_behavior == 3) {
			morph("move","palline", 0, -25, 500, 0, 0, 1.0, 1.0, 1500 );
			slider_2_behavior = 0;
		}
		slider_2_behavior++;

	}

	morph("move","king", 388, 475, 800, 390, 477, 0.0, 1.0, 4000 );

}

function slider6Moved(step) {

	if (slider_6_behavior == 1) {

		morph("move", "movethis", 15, 480, 1500, 10, 470, 0.0, 0.0, 1500 );

	} else if (slider_6_behavior == 2) {

		morph("move","look",260, 750, 2000, 265, 745, 0.0, 1.0, 1500 );

	} else if (slider_6_behavior == 6) {
		// restore messages and recommense loop
		morph("move", "movethis", 15, 480, 500, 15, 480, 1.0, 0.0, 500 );
        slider_6_behavior = 0;
	}
	
	slider_6_behavior++;

	var step3 = slider_positions[3];
	if ( ( step >= 0 ) && ( step <= 10 ) && ( step3 >=  0) && ( step3 <= 30 ) ){
		morph("move","fake", 345, 425, 2800, 345, 425, 0.0, 1.0, 4000 );
		morph("move","kappa", 345, 425, 800, 345, 425, 0.0, 1.0, 4000 );
	}
}

