/**
Generate a 3 Button hover state object for an object that has a vertically stacked
background image sprite with the normal state on top, hover state in middle, and 
click state on bottom defined through its class to be used here. Note that the button's
class should already be applied to that DOM object before instantiating this object. That class should assign a background image in the normal state (with the top background image of the vertically oriented sprite displayed).
@params
	string button_class - the class of the button object
	string button_height - the height of the button object
**/
var Button3HoverStateManager = function(button_class,button_height){
	var current_background = $('.'+button_class).css('background');
	var current_background_image = $('.'+button_class).css('background-image');
	
	/**state of mouse & button interaction **/
	var _is_over = false;

	$('.'+button_class).mouseover(function(){
		_is_over = true;
		var background_string = current_background_image + ' 0 -' + button_height + 'px no-repeat';
		$(this).css('background',background_string);
	});
	
	$('.'+button_class).mouseout(function(){
		_is_over = false;
		var background_string = current_background_image + ' 0 0 no-repeat';
		$(this).css('background',background_string);
	});
	
	$('.'+button_class).mousedown(function(){
		var double_button_height = 2 * parseInt(button_height);
		var background_string = current_background_image + ' 0 -' + double_button_height + 'px no-repeat';
		$(this).css('background',background_string);
	});
	
	$('.'+button_class).mouseup(function(){
		if(_is_over){
			var background_string = current_background_image + ' 0 -' + button_height + 'px no-repeat';
			$(this).css('background',background_string);	
		}
		else{
			var background_string = current_background_image + ' 0 0 no-repeat';
			$(this).css('background',background_string);
		}
	});
}


/**
Generate a 3 Button hover state object for an object that has a vertically stacked
background image sprite with the normal state on top, hover state in middle, and 
click state on bottom defined through its class to be used here. Note that the button's
class should already be applied to that DOM object before instantiating this object. That class should assign a background image in the normal state (with the top background image of the vertically oriented sprite displayed).
@params
	string button_class - the class of the button object
	string button_height - the height of the button object
	int num_pulsating_images - the number of images in the vertically oriented sprite that are between the initial normal state and click state images
	int time_per_image - the time in milliseconds between bg transitions
**/
var Button3HoverStateWithPulsatingManager = function (button_class,button_height, bg_x_offset,num_pulsating_images,time_per_image){
	var that_button = this;
	var that_button_offset = this.bg_x_offset;
	var current_background = $('.'+button_class).css('background');
	var current_background_image = $('.'+button_class).css('background-image');
	
	var pulsate_is_on = false;
	
	/**state of mouse & button interaction **/
	var _is_over = false;
	
	/**pulsation interval object **/
	var pulsation_interval_object;
	
	function get_button_height_multiple(multiple){
		return button_height * multiple;
	}
	
	/**pulsation object states**/
	var current_position = 10;
	var current_button = $('.236x37HoverButtonRed');
	var pulsate_cycle_complete = false;
	var pulsate_cycle_midway = false;
	var position_changes_per_cycle = num_pulsating_images*2-1;
	
	/**pulsation object**/
	var IterateFunction = function iterate_bg_position(){
		//iterate
		var background_string = current_background_image + ' -' + bg_x_offset + 'px -' + get_button_height_multiple(current_position) + 'px no-repeat';
		current_button.css('background',background_string);
		//iterate position variable
		if(!pulsate_cycle_midway){
			if(current_position < num_pulsating_images){
				current_position += 1;	
			}
			else if(current_position == num_pulsating_images){
				if(!pulsate_cycle_midway){
					current_position -= 1;
					pulsate_cycle_midway = true;
				}
				else{
					
				}
			}
			else{alert('There was an error.');}
				
		}
		else{
			if(current_position > 1){
				current_position -= 1;	
			}
			else if(current_position == 1){
				if(pulsate_cycle_midway){
					current_position += 1;
					pulsate_cycle_midway = false;
				}
				else{
					
				}
			}
			else{alert('There was an error.');}

		}
		
	}
	

	
	
	$('.'+button_class).mouseover(function(){		
		_is_over = true;
		current_button = $(this);
		clearInterval(pulsation_interval_object);
		pulsation_interval_object = setInterval(IterateFunction,time_per_image);
	});
	
	$('.'+button_class).mouseout(function(){
		clearInterval(pulsation_interval_object);
		_is_over = false;
		
		
		/** restore pulsation object states **/
		current_position = 10;
		current_button;
		pulsate_cycle_complete = false;
		pulsate_cycle_midway = false;
		position_changes_per_cycle = num_pulsating_images*2-1;
		/** restore button css **/
		var background_string = current_background_image + ' -' + bg_x_offset + 'px 0 no-repeat';
		$(this).css('background',background_string);
	});
	
	$('.'+button_class).mousedown(function(){
		clearInterval(pulsation_interval_object);
		_is_over = true;
		
		/** restore pulsation object states **/
		current_position = 1;
		current_button;
		pulsate_cycle_complete = false;
		pulsate_cycle_midway = false;
		position_changes_per_cycle = num_pulsating_images*2-1;
		/** restore button css **/
		var the_click_bg_position = button_height * num_pulsating_images + parseInt(button_height);
		var background_string = current_background_image + ' -' + bg_x_offset + 'px -' + the_click_bg_position + 'px no-repeat';
		$(this).css('background',background_string);
	});
	
	$('.'+button_class).mouseup(function(){
		clearInterval(pulsation_interval_object);
		/** restore pulsation object states **/
		current_position = 1;
		current_button;
		pulsate_cycle_complete = false;
		pulsate_cycle_midway = false;
		position_changes_per_cycle = num_pulsating_images*2-1;
		
		if(_is_over){
			current_button = $(this);
			pulsation_interval_object = setInterval(IterateFunction,time_per_image);
			//var background_string = current_background_image + ' -' + bg_x_offset + 'px 0 no-repeat';
			//$(this).css('background',background_string);
		}
		else{
			var background_string = current_background_image + ' -' + bg_x_offset + 'px 0 no-repeat';
			$(this).css('background',background_string);
		}
	});
	
	
	
}


/**
Generate a 3 Button hover state object for an object that has a vertically stacked
background image sprite with the normal state on top, hover state in middle, and 
click state on bottom defined through its class to be used here. Note that the button's
class should already be applied to that DOM object before instantiating this object. That class should assign a background image in the normal state (with the top background image of the vertically oriented sprite displayed).
@params
	string button_class - the class of the button object
	string button_height - the height of the button object
	int num_pulsating_images - the number of images in the vertically oriented sprite that are between the initial normal state and click state images
	int time_per_image - the time in milliseconds between bg transitions
**/
var Button3HoverStateWithPulsatingManager2 = function (button_class,button_height, bg_x_offset,num_pulsating_images,time_per_image){
	var that_button = this;
	var that_button_offset = this.bg_x_offset;
	var current_background = $('.'+button_class).css('background');
	var current_background_image = $('.'+button_class).css('background-image');
	
	var pulsate_is_on = false;
	var pulsate_lock_on = false;
	var pulsate_lock_off = false;
	
	
	/**state of mouse & button interaction **/
	var _is_over = false;
	var is_active = true;
	
	/**pulsation interval object **/
	var pulsation_interval_object;
	
	function get_button_height_multiple(multiple){
		return button_height * multiple;
	}
	
	/**pulsation object states**/
	var current_position = 10;
	var current_button = $('.236x37HoverButtonRed');
	var pulsate_cycle_complete = false;
	var pulsate_cycle_midway = false;
	var position_changes_per_cycle = num_pulsating_images*2-1;
	
	/**pulsation object**/
	var IterateFunction = function iterate_bg_position(){
		//iterate
		var background_string = current_background_image + ' -' + bg_x_offset + 'px -' + get_button_height_multiple(current_position) + 'px no-repeat';
		current_button.css('background',background_string);
		//iterate position variable
		if(!pulsate_cycle_midway){
			if(current_position < num_pulsating_images){
				current_position += 1;	
			}
			else if(current_position == num_pulsating_images){
				if(!pulsate_cycle_midway){
					current_position -= 1;
					pulsate_cycle_midway = true;
				}
				else{
					
				}
			}
			else{alert('There was an error.');}
				
		}
		else{
			if(current_position > 1){
				current_position -= 1;	
			}
			else if(current_position == 1){
				if(pulsate_cycle_midway){
					current_position += 1;
					pulsate_cycle_midway = false;
				}
				else{
					
				}
			}
			else{alert('There was an error.');}

		}
		
	}
	
	
	this.turn_pulsate_lock_on_on = function(){
		pulsate_lock_on = true;
	}
	
	this.turn_pulsate_lock_on_off = function(){
		pulsate_lock_on = false;
	}
	
	this.turn_pulsate_lock_off_on = function(){
		pulsate_lock_off = true;
	}
	
	this.turn_pulsate_lock_off_off = function(){
		pulsate_lock_off = false;
	}
	
	this.make_inactive = function(){
		is_active = false;
	}
	
	this.make_active = function(){
		is_active = true;
	}
	
	
	this.pulsate_on = function(){
		if(pulsate_lock_off == false){
			that_button.pulsate_is_on = true;
			
			/** restore pulsation object states **/
			that_button.current_position = 10;
			that_button.pulsate_cycle_complete = false;
			that_button.pulsate_cycle_midway = false;
			that_button.position_changes_per_cycle = num_pulsating_images*2-1;
			clearInterval(pulsation_interval_object);
			pulsation_interval_object = setInterval(IterateFunction,time_per_image);

		}
	}
	
	this.pulsate_off = function(){
		if(pulsate_lock_on == false){
			that_button.pulsate_is_on = false;
			that_button.current_button = $(".236x37HoverButtonRed");
			
			/** restore pulsation object states **/
			that_button.current_position = 10;
			that_button.pulsate_cycle_complete = false;
			that_button.pulsate_cycle_midway = false;
			that_button.position_changes_per_cycle = num_pulsating_images*2-1;
			clearInterval(pulsation_interval_object);
			var background_string = $('.'+button_class).css('background-image') + ' -0px 0 no-repeat';
			that_button.current_button.css('background',background_string);
		}
		
		
	}
	
	
	$('.'+button_class).mouseover(function(){		
		_is_over = true;
		current_button = $(this);
		that_button.pulsate_on();
		//clearInterval(pulsation_interval_object);
		//pulsation_interval_object = setInterval(IterateFunction,time_per_image);
	});
	
	$('.'+button_class).mouseout(function(){
		//clearInterval(pulsation_interval_object);
		_is_over = false;
		that_button.pulsate_off();
		
		/** restore pulsation object states **/
		current_position = 10;
		current_button;
		pulsate_cycle_complete = false;
		pulsate_cycle_midway = false;
		position_changes_per_cycle = num_pulsating_images*2-1;
		/** restore button css **/
		var background_string = current_background_image + ' -' + bg_x_offset + 'px 0 no-repeat';
		$(this).css('background',background_string);
	});
	
	$('.'+button_class).mousedown(function(){
		if(is_active){
			clearInterval(pulsation_interval_object);
			_is_over = true;
			
			/** restore pulsation object states **/
			current_position = 1;
			current_button;
			pulsate_cycle_complete = false;
			pulsate_cycle_midway = false;
			position_changes_per_cycle = num_pulsating_images*2-1;
			/** restore button css **/
			var the_click_bg_position = button_height * num_pulsating_images + parseInt(button_height);
			var background_string = current_background_image + ' -' + bg_x_offset + 'px -' + the_click_bg_position + 'px no-repeat';
			$(this).css('background',background_string);

		}
	});
	
	$('.'+button_class).mouseup(function(){
		if(is_active){
			clearInterval(pulsation_interval_object);
			/** restore pulsation object states **/
			current_position = 1;
			current_button;
			pulsate_cycle_complete = false;
			pulsate_cycle_midway = false;
			position_changes_per_cycle = num_pulsating_images*2-1;
			
			if(_is_over){
				if(!that_button.pulsate_is_on){
					that_button.pulsate_on();	
					current_button = $(this);
					//pulsation_interval_object = setInterval(IterateFunction,time_per_image);
					var background_string = current_background_image + ' -' + bg_x_offset + 'px 0 no-repeat';
					current_button.css('background',background_string);
					that_button.make_inactive();
					that_button.turn_pulsate_lock_off_on();

				}
				else{
					current_button = $(this);
					//pulsation_interval_object = setInterval(IterateFunction,time_per_image);
					var background_string = current_background_image + ' -' + bg_x_offset + 'px 0 no-repeat';
					current_button.css('background',background_string);
					that_button.make_inactive();
					that_button.turn_pulsate_lock_off_on();
				}
				
			}
			else{
				var background_string = current_background_image + ' -' + bg_x_offset + 'px 0 no-repeat';
				$(this).css('background',background_string);
			}
		}
			
	});
	
	
	
}
