$(document).ready(function() {
	//console.log("$('.slideshow-masthead div').length: "+$('.slideshow-masthead div').length);
	if($('.slideshow-masthead div').length > 1){//check for more than one masthead div...
		//call our function and pass in the user defined values...		
		var mastheadSlideShow = {
				easingFn : "easeOutExpo",
				speedIn : 2.5,
				delay : 5,
				speedOut : .25
			};
		
		doSlideShowMasthead(mastheadSlideShow);
	}

	function doSlideShowMasthead(usrObj){
		var navWidth = 0;//initialise a variable to store the width of the navigation...
		//add the nav element to the masthead element.
		$('.slideshow-masthead').append('<ul class="slideshow-masthead-nav"></ul>');
		var thisSlideshow = {obj:$('.slideshow-masthead'), divs:$('.slideshow-masthead').find("div")};
		thisSlideshow.obj.find('div').each(function(e){
			$(this).parent().find('.slideshow-masthead-nav').append('<li><a href="#" /></li>');
			$(this).css({"opacity":0}).hide();//hides all the divs
			if(e == 0){ $(this).eq(0).show(); }//show the first div
			navWidth += 25;
		});
		doSlide(thisSlideshow.obj, 0, usrObj);
		$('.slideshow-masthead-nav').css({"width":navWidth+20});
		//
		$('.slideshow-masthead li a').css({"cursor":"pointer"}).click(function(event){ //console.log("doSlide click");
			//alert("click?");
			$(this).parent().parent().parent().find("div").hide().stop(true);//stops the existing animation, and the animation queue...
			$(this).parent().parent().find("li").removeClass("active");//remove the active class from ALL of the "links"
			$(this).parent().addClass("active");//add the active class to this link
			//
			var obj = $(this).parent().parent().parent();//get the current object this link resides within.
			var thisNum = $(this).parent().index();//get this "link" number...
			//
			$('.slideshow-masthead div:eq('+thisNum+')').css({"opacity":0}).show().animate({
				opacity: 1
			}, usrObj.speedIn, function() {// Animation complete. console.log("click complete");
				doSlide(obj, thisNum, usrObj);//do the next one...
			});
			event.preventDefault();
		}).hover(
		  function () {
			$(this).parent().addClass("over");
		  },
		  function () {
			$(this).parent().removeClass("over");
		  }
		);
		//
	}
	function doSlide(obj, count, usrObj){ //console.log("doSlide ******** count: "+count);
		var speedIn = secToMilli(usrObj.speedIn);
		var speedOut = secToMilli(usrObj.speedOut);
		var delay = secToMilli(usrObj.delay);
		var ease = usrObj.easingFn;
		var divs = obj.find("div");
		//push the current div in front (one > total num) of the others (that are pushed back).
		divs.css({"z-index":-1}).eq(count).css( {"opacity":0, "z-index":(divs.length+1)} );
		//push the masthead navigation in front of the current div.
		//obj.find('.slideshow-masthead-nav').css( {"z-index":( divs.eq(count).css("z-index")+1 ) } );
		//
		if(count < divs.length){//while the count is less than the length of our divs...
			obj.find("li").removeClass("active");//remove the active class from ALL of the "links"
			obj.find('.slideshow-masthead-nav li:eq('+count+')').addClass("active");
			//
			obj.find('div:eq('+count+')').show().animate({//animate the div...
				opacity: 1
			},{
				duration:speedIn,
				easing:ease,
				queue:false,
				step: function(now, fx) {
					var data = fx.elem.id + ' ' + fx.prop + ': ' + now;
				},
				complete: function(){ //console.log("complete");
					count += 1;//crank count up by one (we want to animate the next div) ...
					//
					$(this).delay(delay).animate({//animate the div...
						opacity: 0
					},{
						duration:speedOut,
						easing:ease,
						queue:true,
						step: function(now, fx) {//var data = fx.elem.id + ' ' + fx.prop + ': ' + now;//console.log(data);
							var t = setTimeout ( function(){ /* */ }, speedOut/2);
						},
						complete: function(){
							doSlide(obj, count, usrObj);//do the next div...
						},
						specialEasing:{
							opacity:ease
						}
					});//animate complete finished...
				},
				specialEasing:{
					opacity:ease
				}
			});
		} else { //restart...console.log("restart");
			count = 0;
			doSlide(obj, count, usrObj);//do the next one...
		}
	}
});
// JavaScript Document
function myRandom(minVal, maxVal) {
	do {
		var r = Math.random();
		// Keep picking a number until it is not 1.
	} while (r == 1);
	return minVal+Math.floor(r*(maxVal+1-minVal));
}
function secToMilli(num) { return num*1000; };