// BEGIN SLIDESHOW OBJECT
RandAccess_SS = {
	
	// FUNCTIONS WITHIN THIS OBJECT
	
	// resetDefaults : resets so we can call the function again with a different object.
	
	// init: sets up parameters that can't be gathered until after document.ready, populates the slideshow thumbnails and inserts the first slide
	
	// setTimer : timer to call the nextImg function
	
	// nextImg: determines the next image and loads it. This image is hidden and calls the function changeSlide onload.
	//   nextImg can take up to 2 arguments
	//   nextImg() assumes the direction is forward (or playing normally) and advances to the next slide in the series
	//   nextImg('reverse') shows the previous slide
	//   nextImg('random', 'X') allows random access to any slide where X is the image number */
	   
	// changeSlide : fades in the next div with our newly inserted image, hides the old div, flips out z-indexes, and updates all variables so it's ready for a new switch.
		
	// showLoading : displays #ss_loading div
	
	
	
		// vars :: that can be configured
		fade_speed : 500, // speed to crossfade images
		slideshow_delay : 5200, // display duration
		next_div : 'ss_2', // ids of the two crossfading divs
		curr_div : 'ss_1',
		
		
		// defaults updated on document.ready
		slidesObj : '',
		slide_amount : 1,  
		thumb_container_width : 0,
		timer_int : {},
		load_timer : {},
		curr_html : 0,
		next_html : 1,
		nav_html : '',
		playing : true,
		fade_lock : false,
		slider_position : 0,
		thumb_slide_lock : false,
		thumb_wrapper_width : 0,
		slide_count: 0,
		
		
		// reset the defaults 
		resetDefaults : function(){
			jQuery('#ss_loading').hide();
			
			/*jQuery("#thumb_next").unbind();
			jQuery("#thumb_prev").unbind();
			jQuery('#slide_next img').unbind();
			jQuery('#slide_prev img').unbind();
			jQuery("#pp_button").unbind(); 
			
			if(jQuery("#pp_button").hasClass('play') == true){
				jQuery("#pp_button").removeClass('play').addClass('pause');
			}*/
			clearTimeout(RandAccess_SS.timer_int)
			clearTimeout(RandAccess_SS.load_timer);
			
			RandAccess_SS.next_div = 'ss_2'; 
			RandAccess_SS.curr_div = 'ss_1';
			jQuery('#'+ RandAccess_SS.next_div).hide();
			jQuery('#'+ RandAccess_SS.curr_div).show();
			RandAccess_SS.slidesObj = '';
			RandAccess_SS.slide_amount = 1;  
			RandAccess_SS.timer_int = {};
			RandAccess_SS.load_timer = {};
			RandAccess_SS.curr_html = 0;
			RandAccess_SS.next_html = 1;
			RandAccess_SS.nav_html = '';
			RandAccess_SS.playing = true;
			RandAccess_SS.fade_lock = false;
		},
		
	
	
	init : function(slidesObject){
		
		
		RandAccess_SS.resetDefaults();
		RandAccess_SS.slidesObj = slidesObject; //set our slides object
		RandAccess_SS.slide_amount=RandAccess_SS.slidesObj.length;
		
		//get all slides and add nav
		
		for (i=0; i < RandAccess_SS.slide_amount; i++){ // populate the thumbnails
			RandAccess_SS.nav_html = RandAccess_SS.nav_html + '<a href="#" class="random_access ss_number" id="ss_nav_'+ i +'" slidenumber="'+ i +'">' + (parseInt(i)+1) + '</a>';
			//console.log(RandAccess_SS.nav_html)
		};
		
		var nav_prefix ='<a href="#" class="ss_arrow" id="l_arrow"><img src="images/banners/ss_l_arrow.gif" border="0" width="6" height="11" /></a>';
		var nav_suffix = '<a href="#" class="ss_arrow" id="r_arrow"><img src="images/banners/ss_r_arrow.gif" border="0" width="6" height="11" /></a>';
		//populate the nav container
		jQuery('#ss_nav').html(nav_prefix + RandAccess_SS.nav_html + nav_suffix)
		
		//attach clicks to links
		jQuery('#ss_nav .random_access').each(function(index) {
			jQuery(this).click(function(){
				if(!(RandAccess_SS.fade_lock)){ // if not currently locked out from animating
					clearTimeout(RandAccess_SS.timer_int)
					RandAccess_SS.playing = false;
					RandAccess_SS.nextImg('random', jQuery(this).attr('slidenumber'))
				}
			})
		})
		
		jQuery("#l_arrow").click(function(){
				if(!(RandAccess_SS.fade_lock)){ // if not currently locked out from animating
					clearTimeout(RandAccess_SS.timer_int)
					RandAccess_SS.playing = false;
					RandAccess_SS.nextImg('reverse')
				}
			})
		
		jQuery("#r_arrow").click(function(){
				if(!(RandAccess_SS.fade_lock)){ // if not currently locked out from animating
					clearTimeout(RandAccess_SS.timer_int)
					RandAccess_SS.playing = false;
					RandAccess_SS.nextImg()
				}
			})
		
		// set the first nav to current
		jQuery('#ss_nav_0').addClass('current');
		
		
		// load the inital html into the viewer area
		//jQuery('#ss_container #' + RandAccess_SS.curr_div).html(RandAccess_SS.slidesObj[RandAccess_SS.curr_html].ss_html);	
		// do the first load check
		//jQuery("#load_test").html('<img src="' + RandAccess_SS.slidesObj[RandAccess_SS.curr_html].load_check_img_url + '" onload="RandAccess_SS.setTimer();" />')
	
	},
	
	changeSlide : function(next_html, curr_html)
	{
		
		// hide the loading animation
		clearTimeout(RandAccess_SS.load_timer);
		jQuery('#ss_loading').hide();
		jQuery('#ss_nav a').removeClass('current');
		jQuery('#ss_nav_' + RandAccess_SS.next_html).addClass('current');
		
		
		jQuery("#" + RandAccess_SS.next_div).css('z-index', '940').fadeIn(RandAccess_SS.fade_speed, function(){
			jQuery("#" + RandAccess_SS.curr_div).hide();
			jQuery("#" + RandAccess_SS.next_div).css('z-index', '920')
				
			// switch our current and next variables
			// for images and the holder divs
			RandAccess_SS.curr_html = RandAccess_SS.next_html;
			var curr_switch_hold = RandAccess_SS.curr_div;
			RandAccess_SS.curr_div = RandAccess_SS.next_div
			RandAccess_SS.next_div = curr_switch_hold
			RandAccess_SS.fade_lock = false; // release the lock out
				
			if(RandAccess_SS.playing){ // if we are in playmode, reset the timer
				RandAccess_SS.setTimer()
			} else{
				//alert('kill timer')
				//jsut in case
				clearTimeout(RandAccess_SS.load_timer);
			}
		})
	},
		
	
	// nextImg ::
	// mode: reverse or random (or blank for normal increment).
	// if mode is random, new_slide should be set to the new slide number.
	nextImg : function(mode, new_slide) {
			// advance the count so we know when we've hit the first slide again so we can stop.
			RandAccess_SS.slide_count ++;
		
			RandAccess_SS.load_timer = setTimeout(RandAccess_SS.showLoading, 300)
			RandAccess_SS.fade_lock = true; // lock out while we load and fade
			
			if (mode == 'random'){
				RandAccess_SS.next_html = new_slide;
			}else if(mode == 'reverse'){
				(RandAccess_SS.curr_html == 0) ? RandAccess_SS.next_html = eval(RandAccess_SS.slide_amount) - 1 : RandAccess_SS.next_html = RandAccess_SS.curr_html - 1;
			} else {
				
				(RandAccess_SS.curr_html < (RandAccess_SS.slide_amount - 1)) ? RandAccess_SS.next_html = eval(RandAccess_SS.curr_html) + 1 : RandAccess_SS.next_html = 0;
			}
		jQuery('#ss_container #' + RandAccess_SS.next_div).html(RandAccess_SS.slidesObj[RandAccess_SS.next_html].ss_html);
		
		// load and do some sort of load checking to see that it's done
		// try throwing the image into a div that's off screen and check for width until image is loaded.
		
		// only run the onload script if we are in our firt iteration through the banners. We want to go back to 1 and stop
		
		//if(){
		RandAccess_SS.previewCheck()	
		
		jQuery("#load_test").html('<img src="' + RandAccess_SS.slidesObj[RandAccess_SS.next_html].load_check_img_url + '" onload="RandAccess_SS.changeSlide(' + RandAccess_SS.next_html + ' , ' + RandAccess_SS.curr_html + ');" />')
		//} else{ // just load without the 
			
		//}
		
		
		
		
		//RandAccess_SS.changeSlide(RandAccess_SS.next_html ,RandAccess_SS.curr_html);
	},
	
	previewCheck : function(){ // check to see if we've gone through the slides all once. If we have, stop the slideshow
	//alert(RandAccess_SS.slide_count)
	//alert(parseInt(RandAccess_SS.slide_amount))
	// we want the last image to be loaded, but we don't want to reset the timer on that image, so we will return true for the image load check, but set the playing variable to false if it's one less.
		//if(RandAccess_SS.slide_count <= parseInt(RandAccess_SS.slide_amount) ){
			if(RandAccess_SS.slide_count == parseInt(RandAccess_SS.slide_amount) || RandAccess_SS.playing == false){
				RandAccess_SS.playing = false;
				return false;
			} else {
				return true;
			}
		
	},
		
	setTimer : function()
	{
		
		RandAccess_SS.timer_int = setTimeout(RandAccess_SS.nextImg, RandAccess_SS.slideshow_delay); //
	}, 
	
	showLoading : function()
	{
		jQuery('#ss_loading').show();
	}
}
	
// END SLIDESHOW THUMBNAIL OBJECT
//	
	

	
	
	
	

jQuery(document).ready( function (){    
	//RandAccess_SS.init(slides1);			 
});
