/* returns the width of the div containing all the slides */
function get_total_width() {
	return parseInt($("div#rotator_slides").width());
}

/* 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() {
	var margin = parseInt($("div#rotator_content div#rotator_slides").css('margin-left').replace(/\D/g, ""));
 	return margin / get_width();
}

/* go to a specific slide */
function slide_to(next_slide) {
	
	var new_margin = 0 - (get_width() * next_slide);
	var control = $("div#rotator_control_content");
	$("div#rotator_content div#rotator_slides").animate({marginLeft: new_margin + 'px'}, rotator_slide_transition);
	
	/* change the navigation item to reflect the current status */

	if (rotator_use_numbers) {		
		control.find('a').css(rotator_control_of_state_css);
		control.find('a#slide-' + next_slide).css(rotator_control_on_state_css);
	} else {
		control.find('a').find('img').attr('src', rotator_off_state_img);
		control.find('a#slide-' + next_slide).find('img').attr('src', rotator_on_state_img);
	}
	
}

/* go to the next slide */
function 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() {
	var current = get_current_slide();
	if (current > 0) slide_to(current - 1);
	else slide_to(count_slides()-1);	
}

/* start automatic slide show */
function slide_automatic() {
	if (rotator_auto_rotate) {
		setTimeout("slide_automatic()", rotator_slide_duration);
		slide_next();
	}
}

/* go to a slide when selected */
function slide(event) {
	
	/* cancel the automatic sliding */
	rotator_auto_rotate = false;
	
	slide_to(parseInt($(this).attr('id').replace(/\D/g, "")));	
}

function init_rotator() {
			
	/* set the size and CSS specifics of the rotator container */
	$("div#rotator_container").css({
		'width': rotator_width + 'px',
		'height': (rotator_height + rotator_control_height) + 'px'
	})
	.css(rotator_css);
	
	/* set the size of the slides container */
	$("div#rotator_content").css({
		'width': rotator_width + "px",
		'height': rotator_height + "px"
	});
	
	/* set the size of the slides themselves */
	$("div#rotator_container div#rotator_content div#rotator_slides div.rotator_slide").css({
		'width': rotator_width + "px",
		'height': rotator_height + "px"
	});

	/* set the height and width of the controls area */
	$("div#rotator_control").css({
		'width': rotator_width + "px",
		'height': rotator_control_height + "px"		
	})
	.css(rotator_control_css);
	
	/* set the width of the div holding all the slides.  plus a pixel for every slide so everything will fit on one line */
	var slide_container_width = get_width() * $('div#rotator_slides').children().size();
	$('div#rotator_slides').css('width', slide_container_width + $('div#rotator_slides').children().size());
	
	/* set the location of the right and left arrows */	
	if (rotator_next_img && rotator_previous_img) {
	
		var previous = $("div#rotator_previous_button");
		var next = $("div#rotator_next_button");	
	
		previous.css('height', get_height());
		previous.append($("<img/>").attr('src', rotator_previous_img));
		previous.find('img').hide();
		previous.bind('mouseover', function() {$(this).find('img').css('display', 'block');})
			.bind('mouseout', function() {$(this).find('img').css('display', 'none');})
			.bind('click', slide_previous);
		
		next.css('height', get_height());
		next.append($("<img/>").attr('src', rotator_next_img));
		next.find('img').hide();
		next.bind('mouseover', function() {$(this).find('img').css('display', 'block');})
			.bind('mouseout', function() {$(this).find('img').css('display', 'none');})
			.bind('click', slide_next);
	
		var div_width = parseInt($("div#rotator_previous_button").find('img').width());
		var img_height = parseInt($("div#rotator_previous_button").find('img').height());		
		var next_left = get_width() - div_width;
	
		previous.css({
			'width': div_width + "px",
			'top': '0px',
			'left': '0px'
		});
		
		next.css({
			'width': div_width + "px",
			'top': '0px',
			'left': next_left + 'px'
		});	
	
		var top_margin = get_height() / 2 - img_height / 2;
		previous.find('img').css('margin-top', top_margin + 'px');
		next.find('img').css('margin-top', top_margin + 'px');
		
	}
		
	/* create the navigation */	
	var nav = $('div#rotator_control_content');
	
	for (var n = 0; n < count_slides(); n++) {
		
		var tmp_element = null;
		var tmp_id = 'slide-' + n;		
		
		tmp_element = $("<a>")
			.attr({
				'id': tmp_id,
				'href': '#'})
			.bind('click', slide);
		
		if (rotator_use_numbers) {
			
			tmp_element.text(n + 1);
			if (n == 0) tmp_element.css(rotator_control_on_state_css);
			
		} else {
			
			var button = $("<img>").attr('src', (n == 0 ? rotator_on_state_img : rotator_off_state_img));
			tmp_element.append(button);
						
		}
		
		nav.append(tmp_element);
	
	}

	/* fade in the nav */
	nav.fadeIn();
	
	/* start auto rotation */
	if (rotator_auto_rotate) {
		timer_id = setTimeout("slide_automatic()", rotator_slide_duration);
	}
	
}

$(window).load(function() {

	init_rotator();
	
});
