/* global timer id so it can be shut off on user interaction */
var timer_id;

/* duration of slide display */
var rotator_slide_duration = 7000;

/* duration of slide transition */
var rotator_slide_transition = 500;

/* returns the width of one slide, which is also the width of the entire viewable area */
function get_width() {
	return parseInt($('div#rotator_slides div.rotator_slide').width());
}

/* returns the width of one slide, which is also the width of the entire viewable area */
function get_height() {
	return parseInt($('div#rotator_slides div.rotator_slide').height());
}

/* returns a count of the number of slides total */ 
function count_slides() {
	return parseInt($("div#rotator_slides div.rotator_slide").length);
}

/* returns the current slide selected */
function get_current_slide() {
	// log('get_current_slide');
	var margin = parseInt($("div#rotator_content div#rotator_slides").css('margin-left').replace(/\D/g, ""));
 	return margin / get_width();
}

function trytostopvids() {
	// log('trytostopvids');
	try { slide_f_out(get_current_slide()); } catch(err) { }
	try { slidechange() } catch(err) {}
	// try { stop(document.getElementById("player" + (get_current_slide()+1))); } catch(err) { }
}

/* go to a specific slide */
function slide_to(next_slide) {

	// log('slide_to');
	var new_margin = 0 - (get_width() * next_slide);
	var control = $("div#rotator_control_content");
	var nth_child = next_slide + 1;
	var control_link = $("div#rotator_slides").children(":nth-child(" + nth_child + ")").find('.rotator_slide_link');
		cslide = get_current_slide();
    	var out_function = "slide_f_out(" + (cslide) + "); ";
    	var in_function = "slide_" + (next_slide+1) + "_in();";  
    	/* try to run the out function for this slide */
		// log(out_function);
    	try { eval(out_function); } catch(err) { }
    	try { vimeofinish('vplayer'+cslide) } catch(err) { }
		try { slidechange() } catch(err) { log(err); }
	
	/* set the margin of the div containing the slides */	
	$("div#rotator_content div#rotator_slides").animate({marginLeft: new_margin + 'px'}, rotator_slide_transition);

    	/* try to run the in function for the next slide */
    	try { eval(in_function); } catch(err) { }  
	
	/* change the link in the control area */
	$("div#rotator_control_link").html(control_link.html());
	
	/* change the navigation item to reflect the current status */
	control.find('div').removeClass('rotator_control_current');
	control.find('div#rotator_control_content_' + parseInt(next_slide)).addClass('rotator_control_current');	
	
}

/* go to the next slide */
function slide_next() {
	// log('slide_next');
	var current = get_current_slide();
	if (current < (count_slides()-1)) slide_to(current + 1);
	else slide_to(0);
}

/* go to the previous slide */
function slide_previous() {
	
	// log('slide_previous');
	var current = get_current_slide();
	if (current == 0) slide_to(count_slides()-1);
	else slide_to(current-1);
}

/* go to the next slide and stop rotating */
function slide_next_click() {
	
	// log('slide_next_click');
	/* cancel the automatic sliding */
	clearInterval(timer_id);
	
	slide_next();
	
}

/* go to the previous slide and stop rotating */
function slide_previous_click() {
	
	// log('slide_previous_click');
	/* cancel the automatic sliding */
	clearInterval(timer_id);
	
	slide_previous();
	
}

/* start automatic slide show */
function slide_automatic(caller) {
	// log('slide_automatic');
	// log('CALL:'+caller);
	timer_id = setTimeout("slide_automatic('self')", rotator_slide_duration);
	slide_next();
}

/* go to a selected slide. this function is bound to the navigation divs */
function slide(event) {
	
	// log('slide');
	/* cancel the automatic sliding */
	clearInterval(timer_id);	
	
	/* go to the specific slide */
	slide_to(parseInt($(this).attr('id').replace(/\D/g, "")));
	
}

/* start the rotator */
function init_rotator() {

	// log('init_rotator');
	/* set the width of the slide container */ 
	$("div#rotator_slides").width((get_width() * count_slides()) + count_slides());
		
	/* create the navigation */	
	var nav = $('div#rotator_control_content');
	
	/* add the left arrow */
	var left_arrow = $("<img/>")
		.attr('src', 'http://www.colum.edu/images/slider-arrow-left.png')
		.bind('click', slide_previous_click);
	nav.append(left_arrow);
	
	
	for (var n = 0; n < count_slides(); n++) {
		
		var tmp_element = null;
		var tmp_id = 'rotator_control_content_' + n;
		
		tmp_element = $("<div>")
			.attr('id', tmp_id)
			.bind('click', slide)
			.html('&nbsp;' + (n+1) + '&nbsp;');
			
		if (n == 0) {
			tmp_element.addClass('rotator_control_current');
		}
		
		nav.append(tmp_element);
	
	}
	
	/* add the right arrow */
	var right_arrow = $("<img/>")
		.attr('src', 'http://www.colum.edu/images/slider-arrow-right.png')
		.bind('click', slide_next_click);
	nav.append(right_arrow);


	/* fade in the nav */
	nav.fadeIn();
	
	/* set the first slide */
	slide_to(0);
	
	/* start auto rotation */
	timer_id = setInterval("slide_next()", rotator_slide_duration);
	
}

$(window).load(function() {
	init_rotator();	
});


function log(logdata) {
	// console.log(logdata);
	$('#messages').append(logdata+' ');
	// alert(logdata);
}
