$(function()
{
	$("#topGlass").show().css({top: 85, left: "150%"}).animate({left: 703}, 1250, "easeOutBounce");
	$(".sf-menu li:last").css("border","0px");
	
	$(".sf-menu li").hover(function()
	{
		if (!$(this).data("hovered"))
		{
			$(this).data("hovered", 1);
			
			$(this).animate({backgroundColor: "#768F11"}, "normal", function() { $(this).animate({backgroundColor: "#032F19"}, "normal"); });
			//$(this).stop().effect("highlight", {times: 2, distance: 5, }, "normal");
		}
	}, function()
	{
		$(this).data("hovered",0);
		$(this).stop().css("background-color","transparent");	
	});	
	
		
	swapSlide(3,"banner","bannerContent","cycled", 5000, 1500);
	
	var sPath = window.location.pathname;
	var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
	
	//only load if on index page
	/*
	if(sPage == "index.php" || sPage == "")
	{
		
	}
	*/
	switch (sPage)
	{
		case "sports_club_mug_club_gilford_nh.php":
			swapSlide(4,"club_slideshow","club_slide","clubcycle",7000,3000,0);
			break;
		case "giving_back_to_the_nh_community.php":
			swapSlide(4,"giving_back_slideshow","giving_back_slide","gbcycle",7000,3000,0);
		break;
		case "gilford_nh_function_room_for_private_parties_and_weddings.php":
			swapSlide(5,"private_slideshow","private_slide","ppcycle",7000,3000, 0);
		break;
		case "events_in_lakes_region_gilford_nh.php":
			swapSlide(4,"club_slideshow","club_slide","clubcycle",7000,3000, 0);
		break;
		case "events_in_lakes_region_gilford_nh.php":
			swapSlide(4,"event_slideshow","events_slide","eventcycle",7000,3000, 0);
		break;
		default:
			swapSlide(1,"homeTop","homeTop_slide","gogo",7000,3000, 0);
		break;
	}
	
	
	
	
	function swapSlide(ssIndex,pDiv, sDiv, cDiv, speed, fade, curIdx)
	{
		var headerCount = 0;		
		
		$.ajax({
			url: "index.ajax?action=" + ssIndex,
			cache: false,
			dataType: 'json',
			success: function(output)
			{
				$(document.body).data("slideCount" + ssIndex, output.length).data("slides" + ssIndex, output);								
				doOrderedSwap(ssIndex, pDiv,sDiv,cDiv,speed, fade, curIdx);						
			},
			error: function(XMLHttpRequest, textStatus, errorThrown)
			{
				alert(errorThrown);	
			}
		});	
	}
	
	//This first random, then ordered swap is parameterized for multiple showcases
	function doOrderedSwap(ssIndex, pDiv, sDiv, cDiv, speed, fade, curIdx)
	{
		var slideCount = $(document.body).data("slideCount" + ssIndex);
		
		var slides = $(document.body).data("slides" + ssIndex);
	
		var idx = Math.floor(Math.random() * (slideCount));
		
		$("div." + sDiv).addClass(cDiv);
		
		var iElem = document.createElement("div");
		iElem.className = sDiv;
		
		var newIdx = idx;
		
		var currImg = $("div." + sDiv).css("background-image");
		//var currImg = $("<div class='" + sDiv + "'></div>");
		
		
		var currentPosIdx = 999;
		var currentIdx = idx;
		
		/*  
		//What I need here is to know if the value on the background image is present: url(...), if true, start incrementing the slides.
		//if it is empty, then the incremental portion of this script should be skipped to allow the random selection of the first slide to occur.
		//will come back to this later.
		
		if ($("div." + sDiv).css("background-image").get(0))
		if ($("div." + sDiv).children("background-image").get(0))
		{
			//$("#"+pDiv).append(currImg);
			currentPosIdx = currImg.indexOf("_") + 1;
			currentIdx = currImg.substr(currentPosIdx, 1);
			if(Number(currentIdx) >= 0 && Number(currentIdx) < slideCount)
			{
				(currentIdx == slideCount - 1)? newIdx = 0 : newIdx = Number(currentIdx) + 1;
			}
		}
		*/
		
		try  //i know, iknow... :(
		{
			//if( currImg.toLower() != 'none' && currImg.toLower() != 'nan')
			
			if( currImg.indexOf("_"))
			{
			 	currentPosIdx = currImg.indexOf("_") + 1;
				currentIdx = currImg.substr(currentPosIdx, 1);
				
			}
			
		}
		catch(error){curIdx = newIdx};
		//On the first cycle, the length of currImg is always none or NaN(depending on browser. Therefore, this conditional catches all calls except for the first.
		if(Number(currentIdx) >= 0 && Number(currentIdx) < slideCount)
		{		
			//After the initial random selection of available slides, cycle through the rest in and ordered fashion. 
			(currentIdx == slideCount - 1)? newIdx = 0 : newIdx = curIdx + 1;
		}

		
		$(iElem).css({
			display: "none",
			backgroundImage: "url('" + slides[newIdx] + "')"					
		}).appendTo("#" + pDiv).fadeIn(fade, function() { 
			$("div." + cDiv).remove();
			setTimeout(function(){doOrderedSwap(ssIndex, pDiv, sDiv, cDiv, speed, fade, newIdx)}, speed);
		});	
	}
});


