var selected 	 = 'selected'; 			  	 		//The css class to state the list is selected
var path		 = '.caption > ul > li';   	 		//Path to rotating object list
var stopper		 = '.slide';				 		// Rollover this div to stop intervals
var selectedpath = ''; 					  	 		//The path to add the selected class 
var appendtext	 = ''; 	 					 		//Text appended on interval
var appendpath	 = 'a'; 				  	 		//Path to append information//pictures too
var speed		 = 10000; 				  	 		//Speed to rotate
var image		 = '< < <';  	 				  	//Path too Image Path
var box			 = ['#slideshow > .content > .left > .text > .slide > .caption',68];					//Content Area Path, height
var images		 = ['img18','img13','img11','img12','img10']; 	//Images to be shown (id names)
var titles		 = [
					'RedHawk Survival Training Institute',
					'RedHawk Survival Training Institute',
					
					'RedHawk\'s Marine Training Courses',
					'RedHawk\'s Outdoor Training Courses',
					'RedHawk\'s Outdoor Adventures'
					];
var text		 = [
					'',
					'',
					'',
					''
					];
var titlespath	 = '< < > .content > h4 > a';
var textpath	 = '< < > .content > span';
var btns		 = '#slideshow > .content > .left > .text > .slide > .caption > .slides > li > a';
var linkpath	 = '';
var links		 = [
					'about.php?aboutus',
					'about.php?aboutus',
					'#',
					'#',
					'#',
					'courses.php?marine',
					'adventures.php?outdooradventures',
					'contact.php',
					'courses.php'
					];

//------------------ Do not edit anything below this line

var restart = 0;

$(document).ready(function(){
	rotate(speed);
	$(stopper).hover(function(){ 
							 if($(box[0]).length > 0){
								$(box[0]).animate({height:box[1]},1000); 
							 }
						   window.clearInterval(int);
						   int = 0;
						   if(restart != 0){
								window.clearTimeout(restart);
								restart = 0;
						   }
				},function(){
							
							 if($(box[0]).length > 0){
								hide();
							 }
							restart = window.setTimeout("restartRotate()",speed);
	});
	
	if($(btns).length > 0){
		$(btns).each(function(i){
			$(this).click(function(){
				var t 	= $(this).html();
				var div	= '';
				$(path).each(function(){ 
					if($(this).find('a').text().match(t)){
						$(this).attr('class',selected);
						div = $(this);
					}else{
						$(this).attr('class','');
						$(this).css('background',"url('images/module/images/slideshow/button.png') no-repeat");
					}
				});
				var setimg			= $(div).travel(image);
				rotation 			= i;
				if(titles.length > 0){
					var settitle	= $(div).travel(titlespath);
					var settext		= $(div).travel(textpath);
					$(settitle).text(titles[rotation]);
					$(settext).text(text[rotation]);
					$('#slideshow').find('h4').find('a').attr('href',links[rotation]);
				}
				
				$(setimg).attr('id',images[rotation]);
				
				if(appendtext.length > 0){
					var appendpoint = $(div).travel(appendpath);
					$(appendpoint).append(appendtext);
				}
				rotation++;
			});
		});
	}
	
});


function restartRotate(){ rotate(speed); }

var rotation = 0;
var int		 = 0;

function rotate(r){
	if(int == 0){
		int = window.setInterval('rotate()',r);
	}
	if($(path+selectedpath).length == 0){
		alert('There is an error in your Rotation Script');
		window.clearInterval(int);
	}
	$(path).each(function(i){
		var prev = rotation-1;
		var ps	 = $(path).length;
		if(prev == -1){
			prev = ps-1;	
		}
		if(rotation == ps){ 
			rotation = 0;
		}
		if(i == prev){
			if($(this).hasClass(selected)){
				$(this).attr('class','');
				$(this).find('a').css('background',"url('images/module/images/slideshow/button.png') no-repeat");
				if(appendtext.length > 0){
					var tag = appendtext.split('>');
					tag 	= tag[0].substr(1,tag[0].length);
					var appendpoint = $(this).travel(appendpath+' > '+tag);
					$(appendpoint).remove();
				}
			}
		}else
		if(i == rotation){
			$(this).attr('class',selected);
			$(this).find('a').css('background',"url('images/module/images/slideshow/hover.png') no-repeat");
				var setimg		= $(this).travel(image);
				if(titles.length > 0){
					var settitle	= $(this).travel(titlespath);
					var settext		= $(this).travel(textpath);
					$(settitle).text(titles[rotation]);
					$(settext).text(text[rotation]);
					$('#slideshow').find('h4').find('a').attr('href',links[rotation]);
				}
			$(setimg).attr('id',images[rotation]);
			if(appendtext.length > 0){
				var appendpoint = $(this).travel(appendpath);
				$(appendpoint).append(appendtext);
			}
			if($(box[0]).length > 0){
				$(box[0]).css('height',box[1]+'px');
				window.setTimeout("hide()",speed/10);
			}
		}
	});
	rotation++;
}

function hide(div){
	$(box[0]).animate({height:0},1000);
	
}

$.fn.travel = function(string){
	var div = $(this);
	var ft = string.split(" ");
	for(k=0; k<ft.length; k++){
		if(ft[k] == '<'){ div = $(div).parent(); }
		if(ft[k] == '>'){ 
			var kids  = div.children();
			var found = false;
			k = k+1;
			for(l=0; l<kids.length; l++){
				var re = /^([a-zA-Z])$/;
				if(ft[k].substring(0,1) == '.'){
						var nv = ft[k].substring(1,ft[k].length);
						if($(kids[l]).attr("class").toUpperCase() == nv.toUpperCase()){
							div = $(kids[l]);
							found = true;
						}
				}else 
				if(ft[k].substring(0,1) == '#'){
						var nv = ft[k].substring(1,ft[k].length);
						if($(kids[l]).attr("id").toUpperCase() == nv.toUpperCase()){
							div = $(kids[l]);	
							found = true;
						}
				}else
				if(re.test(ft[k].substring(0,1))){
					if($(kids[l]).tagName() == ft[k].toUpperCase()){
						div = $(kids[l]);	 
						found = true;
					}
				}else{
					alert(ft[k]+' is an unrecognizable entry');	
				}
			}
			if(!found){ alert("There was an error in your script at: '> "+ft[k]+"'"); }
		}	
	}
	return $(div);	
}

$.fn.tagName = function() {
    return this.get(0).tagName;
}
