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);
	
	//TARGET DIV HOLDER
	var div_holder = document.getElementById('image_rotate_holder');
	div_holder.style.overflow = 'hidden';
	div_holder.style.backgroundPosition = '0px 0px';
	
	//SET ROTATE POSITION
	var rotate_position = (parseInt(div_holder.style.backgroundPosition.split(" ")[0].replace("-", "").replace("px", "") / 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));
	
	//RIGHT ARROW
	var right_arrow = document.createElement('img');
	right_arrow.src = image_dir+'right-arrow.png';
	right_arrow.className = 'hover';
	right_arrow.style.zIndex = '100';
	right_arrow.style.position = 'absolute';
	right_arrow.style.top = (parseInt(image_top) + (parseInt(div_holder.offsetHeight) / 2))+'px';
	right_arrow.style.left = (parseInt(image_right) - 40)+'px';
	right_arrow.onclick = function(){ slide_right(); };
	div_holder.parentNode.appendChild(right_arrow);
	
	//LEFT ARROW
	var left_arrow = document.createElement('img');
	left_arrow.src = image_dir+'left-arrow.png';
	left_arrow.className = 'hover';
	left_arrow.style.zIndex = '100';
	left_arrow.style.position = 'absolute';
	left_arrow.style.top = (parseInt(image_top) + (parseInt(div_holder.offsetHeight) / 2))+'px';
	left_arrow.style.left = image_left+'px';
	left_arrow.onclick = function(){ slide_left(); };
	div_holder.parentNode.appendChild(left_arrow);
		
	//CREATE IMAGE HOLDERS & CONTROLS
	var control_position = ((parseInt(image_left) + (parseInt(image_numbers) * 25)) - 10);
	for(i = 0; i < image_numbers; i++){ 
		//CREATE SLIDE CONTROLS
		var slide_controls = document.createElement('div');
		slide_controls.id = 'image_controls_'+i;
		slide_controls.style.width = '25px';
		slide_controls.style.height = '25px';
		if(i == 0){ slide_controls.style.backgroundImage = "url('"+image_dir+"slide-on.jpg')"; } else { slide_controls.style.backgroundImage = "url('"+image_dir+"slide-off.jpg')"; }
		slide_controls.style.position = 'absolute';
		slide_controls.style.top = (parseInt(image_bottom) - 30)+'px';
		slide_controls.style.left = control_position+'px';
		slide_controls.className = 'slide-control hover';
		slide_controls.innerHTML = "<a href=\"javascript:void slide_click('"+i+"', '"+image_dir+"');\" style=\"display:block;\" class=\"slide-control\">"+(parseInt(i) + 1)+"</a>";
		div_holder.parentNode.appendChild(slide_controls);
		control_position = (parseInt(control_position) + 27);
	}	
	
	//START NORMAL SLIDE
	rotating_image = setTimeout(function(){ rotate_slide(); }, delay);
	
	function rotate_slide(){
		//SET ROTATE POSITION
		var rotate_position = (parseInt(div_holder.style.backgroundPosition.split(" ")[0].replace("-", "").replace("px", "") / 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;
					
			//BEGIN SLIDE
			reverse_slide();
	
			//UPDATE CONTROLS
			document.getElementById('image_controls_'+rotate_position).style.backgroundImage = "url('"+image_dir+"slide-off.jpg')"; 
			document.getElementById('image_controls_'+next_position).style.backgroundImage = "url('"+image_dir+"slide-on.jpg')";
			document.getElementById('image_rotate_holder').setAttribute('onclick', "window.location='"+links_array[next_position]+"'");
			if(links_array[next_position] == '#'){ document.getElementById('image_rotate_holder').className = ''; } else { document.getElementById('image_rotate_holder').className = 'hover'; }
						
			//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;
			
			//BEGIN SLIDE
			normal_slide();
						
			//UPDATE CONTROLS
			document.getElementById('image_controls_'+rotate_position).style.backgroundImage = "url('"+image_dir+"slide-off.jpg')"; 
			document.getElementById('image_controls_'+next_position).style.backgroundImage = "url('"+image_dir+"slide-on.jpg')";
			document.getElementById('image_rotate_holder').setAttribute('onclick', "window.location='"+links_array[next_position]+"'");
			if(links_array[next_position] == '#'){ document.getElementById('image_rotate_holder').className = ''; } else { document.getElementById('image_rotate_holder').className = 'hover'; }
						
			//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 >= (parseInt(starting_position) + 25)){ slide_position = (parseInt(slide_position) + 75); }
			if(slide_position <= ending_position){
				div_holder.style.backgroundPosition = "-"+slide_position+"px 0px";
				var slider = setTimeout(normal_slide, '1');
			} else { clearTimeout(slider); div_holder.style.backgroundPosition = "-"+ending_position+"px 0px"; }
		}
					
		function reverse_slide(){
			//INCRIMENT STARTING POSITION
			slide_position = (parseInt(slide_position) - 2);
			if(slide_position <= (parseInt(starting_position) - 25)){ slide_position = (parseInt(slide_position) - 75); }
			if(slide_position >= ending_position){
				div_holder.style.backgroundPosition = "-"+slide_position+"px 0px";
				var slider = setTimeout(reverse_slide, '1');
			} else { clearTimeout(slider); div_holder.style.backgroundPosition = "-"+ending_position+"px 0px"; }
		}
	}
	
	function slide_right(){
		//CLEAR TIMEOUT
		clearTimeout(rotating_image);
		
		//SET ROTATE POSITION
		var rotate_position = (parseInt(div_holder.style.backgroundPosition.split(" ")[0].replace("-", "").replace("px", "") / 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;
					
			//BEGIN SLIDE
			reverse_slide();
	
			//UPDATE CONTROLS
			document.getElementById('image_controls_'+rotate_position).style.backgroundImage = "url('"+image_dir+"slide-off.jpg')"; 
			document.getElementById('image_controls_'+next_position).style.backgroundImage = "url('"+image_dir+"slide-on.jpg')";
			document.getElementById('image_rotate_holder').setAttribute('onclick', "window.location='"+links_array[next_position]+"'");
			if(links_array[next_position] == '#'){ document.getElementById('image_rotate_holder').className = ''; } else { document.getElementById('image_rotate_holder').className = 'hover'; }
						
			//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;
			
			//BEGIN SLIDE
			normal_slide();
						
			//UPDATE CONTROLS
			document.getElementById('image_controls_'+rotate_position).style.backgroundImage = "url('"+image_dir+"slide-off.jpg')"; 
			document.getElementById('image_controls_'+next_position).style.backgroundImage = "url('"+image_dir+"slide-on.jpg')";
			document.getElementById('image_rotate_holder').setAttribute('onclick', "window.location='"+links_array[next_position]+"'");
			if(links_array[next_position] == '#'){ document.getElementById('image_rotate_holder').className = ''; } else { document.getElementById('image_rotate_holder').className = 'hover'; }
						
			//UPDATE CURRENT POSITION
			rotate_position++;
		}
		
		function normal_slide(){
			//INCRIMENT STARTING POSITION
			slide_position = (parseInt(slide_position) + 2);
			if(slide_position >= (parseInt(starting_position) + 25)){ slide_position = (parseInt(slide_position) + 75); }
			if(slide_position <= ending_position){
				div_holder.style.backgroundPosition = "-"+slide_position+"px 0px";
				var slider = setTimeout(normal_slide, '2');
			} else { clearTimeout(slider); div_holder.style.backgroundPosition = "-"+ending_position+"px 0px"; }
		}
					
		function reverse_slide(){
			//INCRIMENT STARTING POSITION
			slide_position = (parseInt(slide_position) - 2);
			if(slide_position <= (parseInt(starting_position) - 25)){ slide_position = (parseInt(slide_position) - 75); }
			if(slide_position >= ending_position){
				div_holder.style.backgroundPosition = "-"+slide_position+"px 0px";
				var slider = setTimeout(reverse_slide, '2');
			} else { clearTimeout(slider); div_holder.style.backgroundPosition = "-"+ending_position+"px 0px"; }
		}
	}
	
	function slide_left(){
		//CLEAR TIMEOUT
		clearTimeout(rotating_image);
		
		//SET ROTATE POSITION
		var rotate_position = (parseInt(div_holder.style.backgroundPosition.split(" ")[0].replace("-", "").replace("px", "") / 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;
			
			//BEGIN SLIDE
			reverse_slide();
	
			//UPDATE CONTROLS
			document.getElementById('image_controls_'+rotate_position).style.backgroundImage = "url('"+image_dir+"slide-off.jpg')"; 
			document.getElementById('image_controls_'+next_position).style.backgroundImage = "url('"+image_dir+"slide-on.jpg')";
			document.getElementById('image_rotate_holder').setAttribute('onclick', "window.location='"+links_array[next_position]+"'");
			if(links_array[next_position] == '#'){ document.getElementById('image_rotate_holder').className = ''; } else { document.getElementById('image_rotate_holder').className = 'hover'; }
						
			//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;
			
			//BEGIN SLIDE
			normal_slide();
						
			//UPDATE CONTROLS
			document.getElementById('image_controls_'+rotate_position).style.backgroundImage = "url('"+image_dir+"slide-off.jpg')"; 
			document.getElementById('image_controls_'+next_position).style.backgroundImage = "url('"+image_dir+"slide-on.jpg')";
			document.getElementById('image_rotate_holder').setAttribute('onclick', "window.location='"+links_array[next_position]+"'");	
			if(links_array[next_position] == '#'){ document.getElementById('image_rotate_holder').className = ''; } else { document.getElementById('image_rotate_holder').className = 'hover'; }		
			
			//UPDATE CURRENT POSITION
			rotate_position--;
		}
		
		function normal_slide(){
			//INCRIMENT STARTING POSITION
			slide_position = (parseInt(slide_position) - 2);
			if(slide_position <= (parseInt(starting_position) - 25)){ slide_position = (parseInt(slide_position) - 75); }
			if(slide_position >= ending_position){
				div_holder.style.backgroundPosition = "-"+slide_position+"px 0px";
				var slider = setTimeout(normal_slide, '2');
			} else { clearTimeout(slider); div_holder.style.backgroundPosition = "-"+ending_position+"px 0px"; }
		}
		
		function reverse_slide(){
			//INCRIMENT STARTING POSITION
			slide_position = (parseInt(slide_position) + 2);
			if(slide_position >= (parseInt(starting_position) + 25)){ slide_position = (parseInt(slide_position) + 75); }
			if(slide_position <= ending_position){
				div_holder.style.backgroundPosition = "-"+slide_position+"px 0px";
				var slider = setTimeout(reverse_slide, '2');
			} else { clearTimeout(slider); div_holder.style.backgroundPosition = "-"+ending_position+"px 0px"; }
		}		
	}
} }

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.style.backgroundPosition.split(" ")[0].replace("-", "").replace("px", "") / 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();
	} else {
		//BEGIN SLIDE
		reverse_slide();
	}
	
	//UPDATE CONTROLS
	document.getElementById('image_controls_'+current_position).style.backgroundImage = "url('"+image_dir+"slide-off.jpg')"; 
	document.getElementById('image_controls_'+clicked_slide).style.backgroundImage = "url('"+image_dir+"slide-on.jpg')";
	document.getElementById('image_rotate_holder').setAttribute('onclick', "window.location='"+links_array[clicked_slide]+"'");
	if(links_array[next_position] == '#'){ document.getElementById('image_rotate_holder').className = ''; } else { document.getElementById('image_rotate_holder').className = 'hover'; }
		
	function normal_slide(){
		//INCRIMENT STARTING POSITION
		slide_position = (parseInt(slide_position) - 2);
		if(slide_position <= (parseInt(starting_position) - 25)){ slide_position = (parseInt(slide_position) - 75); }
		if(slide_position >= ending_position){
			div_holder.style.backgroundPosition = "-"+slide_position+"px 0px";
			var slider = setTimeout(normal_slide, '2');
		} else { clearTimeout(slider); div_holder.style.backgroundPosition = "-"+ending_position+"px 0px"; }
	}
		
	function reverse_slide(){
		//INCRIMENT STARTING POSITION
		slide_position = (parseInt(slide_position) + 2);
		if(slide_position >= (parseInt(starting_position) + 25)){ slide_position = (parseInt(slide_position) + 75); }
		if(slide_position <= ending_position){
			div_holder.style.backgroundPosition = "-"+slide_position+"px 0px";
			var slider = setTimeout(reverse_slide, '2');
		} else { clearTimeout(slider); div_holder.style.backgroundPosition = "-"+ending_position+"px 0px"; }
	}
		
}

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);
} 
