/*  Author: John Weber, weber2@ius.edu, Indiana University Southeast
	Description:  Carousel for the new Liveoncampus site.  Please do not resuse this code without the permissions of IU Southeast.  */

var number_of_movies = 5;			// number of movies in the carosel
var movie_large_width = 360;		// width in pixels of original picture
var movie_large_height = 270;		// height in pixels of original picture
var car_width = 979;				// carousel width in pixels
var car_height = 270;				// carousel height in pixels
var movie_width_min = 50;  			// lowest percent size the movie should go to.
var movie_width_max = 100;  		// highest percent size the movie should go to.


/* variables for functions - do not modify */
var stop_car = 0;
var movie_center_pos = (car_width)/number_of_movies;
var new_zindex = 20	;
var current_mov_detail = "";
var last_selected = 'mov_2';
var percent_of_thumnail = ((movie_large_width/car_width) - (1 / number_of_movies));

function getcords(e){
		  mouseX = Event.pointerX(e) - $('carousel').cumulativeOffset()[0];
		  mouseY = Event.pointerY(e) - $('carousel').cumulativeOffset()[1] ;

	if( mouseX > 0 && mouseX < car_width && mouseY > 0 && mouseY < car_height && stop_car == 0){
		 update_car(mouseX);
	}

}

function update_car(mouseX){
	
	var move_left = - mouseX/4 + 25;

	for(i=1; i <= number_of_movies; i++){
		
		var div_name = 'mov_' + i ;
		var div_name_red = 'mov_' + i + '_red' ;
		
		var div_pos = $(div_name + '_div').positionedOffset()[0] + (movie_large_width / 2);
		var mouse_ratio = (Math.abs(mouseX - div_pos ) / car_width);

		var div_ratio = Math.abs((( mouse_ratio) * 100) - 100);
		
		//div_ratio = div_ratio + ((div_ratio - 100)/100);  //adjust the rate of decease/increase in size
		
		if(div_ratio > movie_width_max)
			div_ratio = movie_width_max;
			
		if(div_ratio < movie_width_min)
			div_ratio = movie_width_min;
		
		if(div_name == last_selected)
			div_ratio = 100;
		
		div_ratio_pixel_width = (div_ratio/100) * movie_large_width;
		div_ratio_pixel_height = (div_ratio/100) * movie_large_height;
		

		if(div_name == last_selected)
			$(div_name + '_div').style.top = '0px';
		else
			$(div_name + '_div').style.top = (Math.abs(div_ratio - 100) - 5) + 'px';
		
	
		$(div_name).style.width = div_ratio_pixel_width + 'px';
		$(div_name).style.height = div_ratio_pixel_height + 'px';
		$(div_name_red).style.width = div_ratio_pixel_width + 'px';
		$(div_name_red).style.height = div_ratio_pixel_height + 'px';
		
		move_left = move_left + movie_center_pos;
		$(div_name + '_div').style.left = move_left - (div_ratio_pixel_width/2)  + 'px';
		
		
		
	}
	
}

function fade_in(mov_id){
	var div_name_in = mov_id;
	var div_name_out = last_selected;
	var div_div_name = mov_id + '_div';	
	var mov_desc_in = mov_id + "_desc";
	var mov_desc_out = last_selected + "_desc";
	var id_num = mov_id.replace('mov_','');

	if (mov_id != last_selected){
		
		new_zindex = new_zindex + 1;
		$(div_div_name).style.zIndex = new_zindex;
		
		for(n=id_num; n <= number_of_movies; n++){
			var z_div_name = 'mov_' + n + '_div';
			$(z_div_name).style.zIndex = new_zindex - Math.abs(n - id_num) ;
		}
		
		for(p=id_num; p >= 1; p--){
			var z_div_name = 'mov_' + p + '_div';
			$(z_div_name).style.zIndex = new_zindex - Math.abs(p - id_num) ;
		}
		
		new Effect.Opacity(div_name_in, { from: 0.5, to: 1, duration: 0.3,  queue: { position: 'end', scope: 'q_' + div_name_in } });	
		new Effect.Appear(mov_desc_in, { from: 0.0, to: 1, duration: 0.5,  queue: { position: 'end', scope: 'q_desc' + div_name_in }, delay:.5 });	
		
		$(mov_desc_in).style.top = '200px';
		$(mov_desc_out).style.top = '1000px';

		new Effect.Opacity(div_name_out, { from: 1, to: 0.5, duration: 0.3,  queue: { position: 'end', scope: 'q_' + div_name_out } });
		new Effect.Opacity(mov_desc_out, { from: 1, to: 0.0, duration: 0,  queue: { position: 'end', scope: 'q_desc' + div_name_out } });	
		last_selected = mov_id;
	}
}

function show_detail(mov_id){
	stop_car = 1;
	var mov_container = mov_id + "_container";
	current_mov_detail = mov_container;
	
	var url = '/housing/modules/hp_modules/' + mov_id + '.cfm';
	var params = '';
	var ajax = new Ajax.Updater(
						{success: mov_container},
						url,
						{method: 'get'});

	$$('.mov_container').each(Element.hide);
	
	Effect.Appear('black_cover', { duration: .5,  queue: { position: 'end', scope: 'showdetail' } });
	Effect.Appear(mov_container, { duration: .5,  queue: { position: 'end', scope: 'showdetail' } });
}


function hide_detail(){
	stop_car = 0;
	$(current_mov_detail).innerHTML = '';
	Effect.Fade('black_cover', { duration: .5 });
}


function init_page(){
	start_mouse = 525;
	start_mov = 'mov_3';
	
	for(i=1; i <= number_of_movies; i++){
		var mov_id = "mov_" + i;
		new Effect.Opacity(mov_id, { from: 1, to: 0.5, duration: 0.1,  queue: { position: 'end', scope: 'q_' + mov_id } });	
	}
	
	$$('.mov_desc').each(Element.hide);
	$('black_cover').style.top = '-3px';
	Element.hide('black_cover');
	
	fade_in(start_mov);
	update_car(start_mouse);
	update_car(start_mouse);
	update_car(start_mouse);
	update_car(start_mouse);
	update_car(start_mouse);
}