var rotating_image = '';
function image_controls(image_dir, image_numbers, delay){ if(parseInt(image_numbers) > 1){
	//CHANGE DELAY INTO SECONDS
	var delay = (parseInt(delay) * 1000);
	
	//DEBUG
	debug = document.getElementById('debug');
	
	//TARGET DIV HOLDER
	var div_holder = document.getElementById('image_rotate_holder');
	div_holder.scrollLeft = "0";
			
	//SET ROTATE POSITION
	var rotate_position = (parseInt(div_holder.scrollLeft / parseInt(div_holder.style.width)));
				
	//GET IMAGE COORDINATES
	var image_top = find_position_top(div_holder);
	var image_bottom = (parseInt(image_top) + parseInt(div_holder.style.height));
	var image_left = find_position_left(div_holder);
	var image_right = (parseInt(image_left) + parseInt(div_holder.style.width));
			
	//CREATE IMAGE HOLDERS & CONTROLS
	var control_position = (parseInt(image_right) - (parseInt(image_numbers) * 30) - 10);
	for(i = 0; i < image_numbers; i++){ 
		//CREATE SLIDE CONTROLS
		var slide_controls = document.createElement('div');
		slide_controls.id = 'image_controls_'+i;
		if(i == 0){ slide_controls.style.backgroundImage = "url('"+image_dir+"home-rotator-advanced-button-on.png')"; } else { slide_controls.style.backgroundImage = "url('"+image_dir+"home-rotator-advanced-button-off.png')"; }
		slide_controls.style.position = 'absolute';
		slide_controls.style.top = (parseInt(image_bottom) - 40)+'px';
		slide_controls.style.left = control_position+'px';
		slide_controls.className = 'slide-control hover';
		slide_controls.setAttribute("onclick", "slide_click('"+i+"', '"+image_dir+"')");
		slide_controls.innerHTML = "<!-- -->";
		div_holder.parentNode.appendChild(slide_controls);
		control_position = (parseInt(control_position) + 30);
	}	
	
	//START NORMAL SLIDE
	rotating_image = setTimeout(function(){ rotate_slide(); }, delay);
	
	function rotate_slide(){
		//SET ROTATE POSITION
		var rotate_position = (parseInt(div_holder.scrollLeft / parseInt(div_holder.style.width)));
	
		//CHECK FOR NEXT POSITION
		var next_position = (parseInt(rotate_position) + 1);
		if(next_position >= image_numbers){ 
			rotate_position = (parseInt(image_numbers) - 1);
			next_position = '0'; 
			
			//GET BACKGROUND POSITIONS
			var starting_position = (parseInt(div_holder.style.width) * rotate_position);
			var ending_position = (parseInt(div_holder.style.width) * next_position);
			var slide_position = starting_position;
					
			//GET MOTION POINTS
			var first_motion = (parseInt(starting_position) - 100);
			var final_motion = (parseInt(ending_position)  + 150);
			
			//BEGIN SLIDE
			reverse_slide();
	
			//UPDATE CONTROLS
			document.getElementById('image_controls_'+rotate_position).style.backgroundImage = "url('"+image_dir+"home-rotator-advanced-button-off.png')"; 
			document.getElementById('image_controls_'+next_position).style.backgroundImage = "url('"+image_dir+"home-rotator-advanced-button-on.png')";
						
			//UPDATE CURRENT POSITION
			rotate_position = '0';
		} else {				
			//GET BACKGROUND POSITIONS
			var starting_position = (parseInt(div_holder.style.width) * rotate_position);
			var ending_position = (parseInt(div_holder.style.width) * next_position);
			var slide_position = starting_position;
			
			//GET MOTION POINTS
			var first_motion = (parseInt(starting_position) + 100);
			var final_motion = (parseInt(ending_position)  - 150);
			
			//BEGIN SLIDE
			normal_slide();
						
			//UPDATE CONTROLS
			document.getElementById('image_controls_'+rotate_position).style.backgroundImage = "url('"+image_dir+"home-rotator-advanced-button-off.png')"; 
			document.getElementById('image_controls_'+next_position).style.backgroundImage = "url('"+image_dir+"home-rotator-advanced-button-on.png')";
						
			//UPDATE CURRENT POSITION
			rotate_position++;
		}
		
		//SET TIMEOUT
		rotating_image = setTimeout(rotate_slide, delay);
		
		function normal_slide(){
			//INCRIMENT STARTING POSITION
			slide_position = (parseInt(slide_position) + 2);
			if(slide_position >= final_motion){ slide_position = (parseInt(slide_position) + 1); } else if(slide_position >= first_motion){ slide_position = (parseInt(slide_position) + 15); }
			if(slide_position <= ending_position){
				div_holder.scrollLeft = slide_position;
				var slider = setTimeout(normal_slide, '2');
			} else { clearTimeout(slider); div_holder.scrollLeft = ending_position; }
		}
			
		function reverse_slide(){
			//INCRIMENT STARTING POSITION
			slide_position = (parseInt(slide_position) - 4);
			if(slide_position <= final_motion){ slide_position = (parseInt(slide_position) - 1); } else if(slide_position >= first_motion){ slide_position = (parseInt(slide_position) - 15); } else if(((parseInt(slide_position) - parseInt(ending_position)) > parseInt(div_holder.style.width))){ slide_position = (parseInt(slide_position) - 25); }
			if(slide_position >= ending_position){
				div_holder.scrollLeft = slide_position;
				var slider = setTimeout(reverse_slide, '2');
			} else { clearTimeout(slider); div_holder.scrollLeft = ending_position; }
		}
				
	}
	
	function slide_right(){
		//CLEAR TIMEOUT
		clearTimeout(rotating_image);
		
		//SET ROTATE POSITION
		var rotate_position = (parseInt(div_holder.scrollLeft / parseInt(div_holder.style.width)));
		
		//CHECK FOR NEXT POSITION
		var next_position = (parseInt(rotate_position) + 1);
		if(next_position >= image_numbers){ 
			rotate_position = (parseInt(image_numbers) - 1);
			next_position = '0'; 
			
			//GET BACKGROUND POSITIONS
			var starting_position = (parseInt(div_holder.style.width) * rotate_position);
			var ending_position = (parseInt(div_holder.style.width) * next_position);
			var slide_position = starting_position;
			
			//GET MOTION POINTS
			var first_motion = (parseInt(starting_position) - 100);
			var final_motion = (parseInt(ending_position)  + 150);
			
			//BEGIN SLIDE
			reverse_slide();
	
			//UPDATE CONTROLS
			document.getElementById('image_controls_'+rotate_position).style.backgroundImage = "url('"+image_dir+"home-rotator-advanced-button-off.png')"; 
			document.getElementById('image_controls_'+next_position).style.backgroundImage = "url('"+image_dir+"home-rotator-advanced-button-on.png')";
						
			//UPDATE CURRENT POSITION
			rotate_position = '0';
		} else {				
			//GET BACKGROUND POSITIONS
			var starting_position = (parseInt(div_holder.style.width) * rotate_position);
			var ending_position = (parseInt(div_holder.style.width) * next_position);
			var slide_position = starting_position;
			
			//GET MOTION POINTS
			var first_motion = (parseInt(starting_position) + 100);
			var final_motion = (parseInt(ending_position)  - 150);
			
			//BEGIN SLIDE
			normal_slide();
						
			//UPDATE CONTROLS
			document.getElementById('image_controls_'+rotate_position).style.backgroundImage = "url('"+image_dir+"home-rotator-advanced-button-off.png')"; 
			document.getElementById('image_controls_'+next_position).style.backgroundImage = "url('"+image_dir+"home-rotator-advanced-button-on.png')";
						
			//UPDATE CURRENT POSITION
			rotate_position++;
		}
		
		function normal_slide(){
			//INCRIMENT STARTING POSITION
			slide_position = (parseInt(slide_position) + 2);
			if(slide_position >= final_motion){ slide_position = (parseInt(slide_position) + 1); } else if(slide_position >= first_motion){ slide_position = (parseInt(slide_position) + 15); }
			if(slide_position <= ending_position){
				div_holder.scrollLeft = slide_position;
				var slider = setTimeout(normal_slide, '2');
			} else { clearTimeout(slider); div_holder.scrollLeft = ending_position; }
		}
		
		function reverse_slide(){
			//INCRIMENT STARTING POSITION
			slide_position = (parseInt(slide_position) - 4);
			if(slide_position <= final_motion){ slide_position = (parseInt(slide_position) - 1); } else if(slide_position >= first_motion){ slide_position = (parseInt(slide_position) - 15); } else if(((parseInt(slide_position) - parseInt(ending_position)) > parseInt(div_holder.style.width))){ slide_position = (parseInt(slide_position) - 25); }
			if(slide_position >= ending_position){
				div_holder.scrollLeft = slide_position;
				var slider = setTimeout(reverse_slide, '2');
			} else { clearTimeout(slider); div_holder.scrollLeft = ending_position; }
		}			
		
	}
	
	function slide_left(){
		//CLEAR TIMEOUT
		clearTimeout(rotating_image);
		
		//SET ROTATE POSITION
		var rotate_position = (parseInt(div_holder.scrollLeft / parseInt(div_holder.style.width)));
	
		//CHECK FOR NEXT POSITION
		var next_position = (parseInt(rotate_position) - 1);
		if(next_position < 0){ 
			rotate_position = 0;
			next_position = (parseInt(image_numbers) - 1); 
								
			//GET BACKGROUND POSITIONS
			var starting_position = (parseInt(div_holder.style.width) * rotate_position);
			var ending_position = (parseInt(div_holder.style.width) * next_position);
			var slide_position = starting_position;
						
			//GET MOTION POINTS
			var first_motion = (parseInt(starting_position) + 50);
			var final_motion = (parseInt(ending_position)  - 50);
						
			//BEGIN SLIDE
			reverse_slide();
	
			//UPDATE CONTROLS
			document.getElementById('image_controls_'+rotate_position).style.backgroundImage = "url('"+image_dir+"home-rotator-advanced-button-off.png')"; 
			document.getElementById('image_controls_'+next_position).style.backgroundImage = "url('"+image_dir+"home-rotator-advanced-button-on.png')";
						
			//UPDATE CURRENT POSITION
			rotate_position = (parseInt(image_numbers) - 1);
		} else {	
			
			//GET BACKGROUND POSITIONS
			var starting_position = (parseInt(div_holder.style.width) * rotate_position);
			var ending_position = (parseInt(div_holder.style.width) * next_position);
			var slide_position = starting_position;
						
			//GET MOTION POINTS
			var first_motion = (parseInt(starting_position) - 100);
			var final_motion = (parseInt(ending_position)  + 150);
					
			//BEGIN SLIDE
			normal_slide();
						
			//UPDATE CONTROLS
			document.getElementById('image_controls_'+rotate_position).style.backgroundImage = "url('"+image_dir+"home-rotator-advanced-button-off.png')"; 
			document.getElementById('image_controls_'+next_position).style.backgroundImage = "url('"+image_dir+"home-rotator-advanced-button-on.png')";
			
			//UPDATE CURRENT POSITION
			rotate_position--;
		}
				
		function normal_slide(){
			//INCRIMENT STARTING POSITION
			slide_position = (parseInt(slide_position) - 4);
			if(slide_position <= final_motion){ slide_position = (parseInt(slide_position) - 1); } else if(slide_position >= first_motion){ slide_position = (parseInt(slide_position) - 15); }
			if(slide_position >= ending_position){
				div_holder.scrollLeft = slide_position;
				var slider = setTimeout(normal_slide, '2');
			} else { clearTimeout(slider); div_holder.scrollLeft = ending_position; }
		}
			
		function reverse_slide(){
			//INCRIMENT STARTING POSITION
			slide_position = (parseInt(slide_position) + 4);
			if(slide_position >= final_motion){ slide_position = (parseInt(slide_position) + 1); } else if(slide_position <= first_motion){ slide_position = (parseInt(slide_position) + 15); } else if(((parseInt(ending_position) - parseInt(slide_position)) > parseInt(div_holder.style.width))){ slide_position = (parseInt(slide_position) + 25); }
			if(slide_position <= ending_position){
				div_holder.scrollLeft = slide_position;
				var slider = setTimeout(reverse_slide, '2');
			} else { clearTimeout(slider); div_holder.scrollLeft = ending_position; }
		}		
	}
} }

function slide_click(clicked_slide, image_dir){
	//CLEAR TIMEOUT
	clearTimeout(rotating_image);
	
	//DETERMINE POSITION BASED ON CURRENT POSITION
	var div_holder = document.getElementById('image_rotate_holder');
	var current_position = (parseInt(div_holder.scrollLeft / parseInt(div_holder.style.width)));
		
	//GET POSITIONS
	var starting_position = (parseInt(div_holder.style.width) * current_position);
	var ending_position = (parseInt(div_holder.style.width) * clicked_slide);
	var slide_position = starting_position;
				
	//CHECK FOR MOVE FORWARD OR MOVE BACKWARDS
	if(clicked_slide > current_position){
		//BEGIN SLIDE
		normal_slide();	
		
		//GET MOTION POINTS
		var first_motion = (parseInt(starting_position) + 100);
		var final_motion = (parseInt(ending_position)  - 150);
		
	} else {
		//BEGIN SLIDE
		reverse_slide();	
		
		//GET MOTION POINTS
		var first_motion = (parseInt(starting_position) - 100);
		var final_motion = (parseInt(ending_position)  + 150);
	}
	
	//UPDATE CONTROLS
	document.getElementById('image_controls_'+current_position).style.backgroundImage = "url('"+image_dir+"home-rotator-advanced-button-off.png')"; 
	document.getElementById('image_controls_'+clicked_slide).style.backgroundImage = "url('"+image_dir+"home-rotator-advanced-button-on.png')";
		
	function normal_slide(){
		//INCRIMENT STARTING POSITION
		slide_position = (parseInt(slide_position) + 2);
		if(slide_position >= final_motion){ slide_position = (parseInt(slide_position) + 1); } else if(slide_position >= first_motion){ slide_position = (parseInt(slide_position) + 15); }
		if(slide_position <= ending_position){
			div_holder.scrollLeft = slide_position;
			var slider = setTimeout(normal_slide, '2');
		} else { clearTimeout(slider); div_holder.scrollLeft = ending_position; }
	}
		
	function reverse_slide(){
		//INCRIMENT STARTING POSITION
		slide_position = (parseInt(slide_position) - 2);
		if(slide_position <= final_motion){ slide_position = (parseInt(slide_position) - 1); } else if(slide_position <= first_motion){ slide_position = (parseInt(slide_position) - 15); } else if(((parseInt(ending_position) - parseInt(slide_position)) > parseInt(div_holder.style.width))){ slide_position = (parseInt(slide_position) - 25); }
		if(slide_position >= ending_position){
			div_holder.scrollLeft = slide_position;
			var slider = setTimeout(reverse_slide, '2');
		} else { clearTimeout(slider); div_holder.scrollLeft = ending_position; }		
	}
		
}

function find_position_left(obj) {
	var curleft = 0;
	if(obj.offsetParent){
		curleft = obj.offsetLeft
		while (obj = obj.offsetParent){
			curleft += obj.offsetLeft
		}
	}
	
	return (curleft);
}
 
function find_position_top(obj) {
	var curtop = 0;
	if(obj.offsetParent){
		curtop = obj.offsetTop
		while (obj = obj.offsetParent){
			curtop += obj.offsetTop
		}
	}
	
	return (curtop);
} 

function launch_player(video_url){
	
	//CREATE UNDERLAY
	var underlay = document.createElement('div');
	underlay.id = 'underlay';
	underlay.style.zIndex = '400';
	underlay.style.width = (parseInt(document.body.offsetWidth) + 0)+'px';		
	underlay.style.height = (parseInt(document.body.offsetHeight) + 0)+'px';
	underlay.style.position = 'fixed';
	underlay.style.top = '0px';
	underlay.style.left = '0px';
	underlay.style.backgroundColor = '#000';
	underlay.className = 'transparent';
	document.body.appendChild(underlay);
		
	//CREATE OVERLAY
	var overlay = document.createElement('div');
	overlay.id = 'overlay';
	overlay.style.zIndex = '500';
	overlay.style.width = '785px';
	overlay.style.height = '512px';
	overlay.style.position = 'fixed';
	overlay.style.top = '150px';
	overlay.style.left = ((parseInt(document.body.offsetWidth) / 2) - 390)+'px';
	overlay.style.backgroundImage = "url('images/home-rotator-video-background.png')";
	overlay.style.backgroundRepeat = 'no-repeat';
	overlay.innerHTML = '';
	document.body.appendChild(overlay);
		
	//CREATE CLOSE
	var close_button = document.createElement('div');
	close_button.id = 'close_button';
	close_button.className = 'home-rotator-video-close'
	close_button.innerHTML = '<a href="javascript:void close_player();" title="Close This Window"><img src="images/home-rotator-advanced-video-close.png" /></a>';
	document.getElementById('overlay').appendChild(close_button);
		
	//CREATE VIDEO PLAYER 
	var player = document.createElement('a');
	player.id = 'player';
	player.setAttribute('href', video_url);
	player.className = 'home-rotator-video-player';
	document.getElementById('overlay').appendChild(player);
		
	//TRIGGER PLAYER	
	flowplayer("player", "manager/flowplayer/flowplayer-3.2.7.swf");
		
}

function close_player(){

	//REMOVE OBJECTS
	document.getElementById('close_button').parentNode.removeChild(document.getElementById('close_button'));	
	document.getElementById('player').parentNode.removeChild(document.getElementById('player'));	
	document.getElementById('overlay').parentNode.removeChild(document.getElementById('overlay'));
	document.getElementById('underlay').parentNode.removeChild(document.getElementById('underlay'));
	
}
