jQuery(document).ready(function($)
{		
	/* Interior rollovers */
	$(".intRollover").mouseover(function(){
		iImageName = $(this).children().attr('src');
		if($(this).hasClass('red')){
			hImageName = iImageName.replace('_i.gif', '_red.gif');
		}
		else{
			hImageName = iImageName.replace('_i.gif', '_h.gif');
		}
		$(this).children().attr('src', hImageName);
	});
	
	$(".intRollover").mouseout(function(){
		iImageName = $(this).children().attr('src');
		if($(this).hasClass('red')){
			hImageName = iImageName.replace('_red.gif', '_i.gif');
		}
		else{
			hImageName = iImageName.replace('_h.gif', '_i.gif');
		}
		$(this).children().attr('src', hImageName);
	});
	
	$("#prevImage a").click(function(){
		z = $("#currentPos").html();
		z--;
		// switch to the next category
		if(Number(z) < 0){
			$("#intGallery > ul").hide("normal", function(){
				$("#intGallery").append('<div style="padding: 200px 0px 0px 70px"><img src="/public/wp-content/themes/ohioboy/images/ajax-loader.gif" border="0" height="24" width="24" /> Loading the next section! </div>');
			});						
			$("#intGalleryNav").load("/public/wp-content/themes/ohioboy/circular.php?category="+getPrevCategoryId(window.currentCategory)+"&action=nav", null, function(){
			
				// deactive current nav icon
				currentIconSrc = $(".active").children().attr('src');
				$(".active").parent().fadeOut("normal");
				$(".active").children().attr('src', currentIconSrc.replace('_a.gif', '_i.gif'));
				$(".active").parent().fadeIn("normal");
				$(".active").removeClass("active");														   
				
				//activate next icon
				catName = getPrevCategoryName(window.currentCategory);
				prevIconSrc = $("#int"+catName+"Icon > a > img").attr('src');
				$("#int"+catName+"Icon").fadeOut("normal");
				$("#int"+catName+"Icon > a > img").attr('src', prevIconSrc.replace('_i.gif', '_a.gif'));
				$("#int"+catName+"Icon").fadeIn("normal");
				$("#int"+catName+"Icon > a").addClass("active");
				
				// Swap out our title graphic
				prevTitleSrc = $("#intPageTitle > img").attr('src');
				$("#intPageTitle").fadeOut('normal');
				$("#intPageTitle > img").attr('src', prevTitleSrc.replace($("#intPageTitle > img").attr('alt'), getPrevCategoryName(window.currentCategory).toLowerCase()));
				$("#intPageTitle").fadeIn('normal');
				$("#intPageTitle > img").attr('alt', getPrevCategoryName(window.currentCategory).toLowerCase());
				
				// Update category image totals	
				$("#currentPos").html(String($("#intGalleryNav > ul").children().length - 1));
				$("#totalImages").html(String($("#intGalleryNav > ul").children().length - 1));
				
				//Set last item to current
				$("#intGalleryNav a:last").addClass("current");	
				captionID = $(".current").attr("id").replace('link_', '');
				$("#imageComment").html($("#"+captionID).html());
								
				//re-initialize scrollbar
				$(function(){$('#intGalleryNav').jScrollPane({showArrows:true, scrollbarWidth: 9});});
				
				// load the gallery once the navigation is complete
				$("#intGallery").load("/public/wp-content/themes/ohioboy/circular.php?category="+getPrevCategoryId(window.currentCategory)+"&action=gallery&back=1", null, function(){
					// reinitialize the slideviewer
					$("#intGallery").slideView(); 
				});
				
				// Switch our variable so we can move ahead again
				window.currentCategory = getPrevCategoryId(window.currentCategory);

				// switch graphics to red or aqua
				setCatColor();
			});						
		}
		else{
			$("#currentPos").html(String(z));
			var cnt = - (678*z);
			$("#intGallery").find("ul").animate({ left: cnt}, 750, "easeInOutExpo");
			$("#intGalleryNav a").removeClass("current");
			$("#intGalleryNav a:eq("+z+")").addClass("current");
			captionID = $(".current").attr("id").replace('link_', '');
			$("#imageComment").html($("#"+captionID).html());
		}
	});
	
	$("#nextImage a").click(function(){
		z = $("#currentPos").html();
		z++;	
		if(z > Number($("#totalImages").html())){			
			$("#intGallery > ul").hide("normal", function(){
				$("#intGallery").append('<div style="padding: 200px 0px 0px 70px"><img src="/public/wp-content/themes/ohioboy/images/ajax-loader.gif" border="0" height="24" width="24" /> Loading the next section! </div>');
			});						
			$("#intGalleryNav").load("/public/wp-content/themes/ohioboy/circular.php?category="+getNextCategoryId(window.currentCategory)+"&action=nav", null, function(){
			
				// deactive current nav icon
				currentIconSrc = $(".active").children().attr('src');
				$(".active").parent().fadeOut("normal");
				$(".active").children().attr('src', currentIconSrc.replace('_a.gif', '_i.gif'));
				$(".active").parent().fadeIn("normal");
				$(".active").removeClass("active");														   
				
				//activate next icon
				catName = getNextCategoryName(window.currentCategory);
				nextIconSrc = $("#int"+catName+"Icon > a > img").attr('src');
				$("#int"+catName+"Icon").fadeOut("normal");
				$("#int"+catName+"Icon > a > img").attr('src', nextIconSrc.replace('_i.gif', '_a.gif'));
				$("#int"+catName+"Icon").fadeIn("normal");
				$("#int"+catName+"Icon > a").addClass("active");
				
				// Swap out our title graphic
				nextTitleSrc = $("#intPageTitle > img").attr('src');
				$("#intPageTitle").fadeOut('normal');
				$("#intPageTitle > img").attr('src', nextTitleSrc.replace($("#intPageTitle > img").attr('alt'), getNextCategoryName(window.currentCategory).toLowerCase()));
				$("#intPageTitle").fadeIn('normal');
				$("#intPageTitle > img").attr('alt', getNextCategoryName(window.currentCategory).toLowerCase());
				
				// Update category image totals	
				$("#currentPos").html(String(0));
				$("#totalImages").html(String($("#intGalleryNav > ul").children().length - 1));																																
				
				//re-initialize scrollbar
				$(function(){$('#intGalleryNav').jScrollPane({showArrows:true, scrollbarWidth: 9});});
				
				$("#intGallery").load("/public/wp-content/themes/ohioboy/circular.php?category="+getNextCategoryId(window.currentCategory)+"&action=gallery", null, function(){
					// reinitialize the slideviewer
					$("#intGallery").slideView(); 
				});
				
				// Switch our variable so we can move ahead again
				window.currentCategory = getNextCategoryId(window.currentCategory);
				
				// switch graphics to red or aqua
				setCatColor();
			});						
		}
		else{
			$("#currentPos").html(String(z));
			var cnt = - (678*z);
			$("#intGallery").find("ul").animate({ left: cnt}, 750, "easeInOutExpo");
			$("#intGalleryNav a").removeClass("current");
			$("#intGalleryNav a:eq("+z+")").addClass("current");
			captionID = $(".current").attr("id").replace('link_', '');
			$("#imageComment").html($("#"+captionID).html());
		}
	});
	
});

function set_category(id, name, color){
	this.id = id;
	this.name = name;
	this.color = color;
}

function catIsRed(currentId){
	currentIndex = findCategoryIndex(currentId);
	if(window.categories[currentIndex].color == 'red'){
		return true;
	}
	return false;
}

function setCatColor(){
	if(catIsRed(window.currentCategory)){
		$("#prevImage > a").addClass("red");
		$("#nextImage > a").addClass("red");
		imageName = $("#intOhioboy > img").attr('src');
		$("#intOhioboy > img").attr('src', imageName.replace('_aqua.gif', '_red.gif'));
	}
	else{
		$("#prevImage > a").removeClass("red");
		$("#nextImage > a").removeClass("red");
		imageName = $("#intOhioboy > img").attr('src');
		$("#intOhioboy > img").attr('src', imageName.replace('_red.gif', '_aqua.gif'));
	}	
}

function findCategoryIndex(id){
	for(i = 0; i < window.categories.length; i++){
		if(window.categories[i].id == id){
			return i;	
		}
	}
}

function getPrevCategoryId(currentId){
	currentIndex = findCategoryIndex(currentId);
	if(currentIndex == 0){
		prevIndex = window.categories.length - 1;
	}
	else{
		prevIndex = currentIndex - 1;	
	}
	return window.categories[prevIndex].id;
}

function getPrevCategoryName(currentId){
	currentIndex = findCategoryIndex(currentId);
	if(currentIndex == 0){
		prevIndex = window.categories.length - 1;
	}
	else{
		prevIndex = currentIndex - 1;	
	}
	return window.categories[prevIndex].name;
}

function getNextCategoryId(currentId){
	currentIndex = findCategoryIndex(currentId);
	if(currentIndex == window.categories.length - 1){
		nextIndex = 0;
	}
	else{
		nextIndex = currentIndex + 1;	
	}
	return window.categories[nextIndex].id;
}

function getNextCategoryName(currentId){
	currentIndex = findCategoryIndex(currentId);
	if(currentIndex == window.categories.length - 1){
		nextIndex = 0;
	}
	else{
		nextIndex = currentIndex + 1;	
	}
	return window.categories[nextIndex].name;
}