// BEGIN SLIDESHOW OBJECT

SlideShow = {
		fade_speed : 500, // speed to crossfade images
		slideshow_delay : 4600, // display duration
		next_div : 'ss_2', // ids of the two crossfading divs
		curr_div : 'ss_1',
		context : 'work_detail',
		
		// 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,
		img_dir : '/files/images/',
		play_state : 'play',
		cat_list_nav_position : {},
		resize_prefix : '/thumbnailgen.php?w=577&im=',
		
		
		
		// reset the defaults 
		resetDefaults : function(){
				$('#ss_loading, #fs_ss_loading').hide();
			
			clearTimeout(SlideShow.timer_int);
			//console.log('reset')
			clearTimeout(SlideShow.load_timer);
			
			SlideShow.next_div = 'ss_2'; 
			SlideShow.curr_div = 'ss_1';
			$('#'+ SlideShow.next_div).hide();
			$('#'+ SlideShow.curr_div).show();
			SlideShow.slidesObj = '';
			SlideShow.slide_amount = 1;  
			SlideShow.timer_int = {};
			SlideShow.load_timer = {};
			SlideShow.curr_html = 0;
			SlideShow.next_html = 1;
			SlideShow.nav_html = '';
			SlideShow.playing = true;
			SlideShow.fade_lock = false;
			SlideShow.img_dir = '/files/images/';
			SlideShow.context = 'work_detail';
			SlideShow.play_state = 'play';
			SlideShow.cat_list_nav_position = {};
			
		},
		
	
	
	init : function(slidesObject, context, curr_slide){ // context = home, fullscreen, work_detail, or modal
		
	
		SlideShow.resetDefaults();
		SlideShow.slidesObj = eval(slidesObject); //set our slides object
		SlideShow.slide_amount=SlideShow.slidesObj.length;
		SlideShow.context = context
		SlideShow.play_state = GlobalSlideShow.play_state;
		if(curr_slide){
			SlideShow.curr_html = curr_slide;
		}
		
		// if this is a work detial page
		if(SlideShow.context == 'work_detail'){
			var category_list ='';
			var last_category = ''
			//var cat_list_nav_position = {};
			var cat_list_nav_count = -1;
			//get all slides and add nav
			for (i in SlideShow.slidesObj){ // populate side links
				if(last_category != SlideShow.slidesObj[i].category){
					cat_list_nav_count++
					SlideShow.cat_list_nav_position[i] = cat_list_nav_count;
					category_list = category_list + '<li><a href="#" slidenumber="'+ i +'" highlight_pos="' + cat_list_nav_count + '">' + SlideShow.slidesObj[i].category + '</a></li>'
					last_category = SlideShow.slidesObj[i].category
				} else {
					SlideShow.cat_list_nav_position[i] = cat_list_nav_count;
				}
			};
			$("#twoCol_right").append('<ul id="category_list">' + category_list + '</ul>');
			
			// highlight the nav state for the slide we are on
			$('#category_list a[highlight_pos="' + SlideShow.cat_list_nav_position[SlideShow.curr_html] + '"]').addClass('current');	
			//attach clicks to links
			$('#category_list a').each(function(index) {
				$(this).click(function(e){
					e.preventDefault();
					$('#category_list a').removeClass('current')
					$(this).addClass('current')
					if(!(SlideShow.fade_lock)){ // if not currently locked out from animating
						clearTimeout(SlideShow.timer_int)
						SlideShow.nextImg('random', $(this).attr('slidenumber'))
					}
				})
			})
			
			
			// load the initil image
			$('#ss_container #' + SlideShow.curr_div).html('<img src = "' + SlideShow.resize_prefix + SlideShow.slidesObj[SlideShow.curr_html].slide_img + '" onload="SlideShow.setTimer();" />');
			// add the count
			$("#ss_count").html((parseInt(SlideShow.curr_html) + 1) + ' / ' + SlideShow.slide_amount)
		
		}
		
		if(SlideShow.context == 'fullscreen'){
			// set the play pause button state
			SlideShow.setPlayPauseButtonState();
		//alert(SlideShow.img_dir)
			$('#fs_ss #fs_' + SlideShow.curr_div).html('<img src = "' + SlideShow.img_dir + SlideShow.slidesObj[SlideShow.curr_html].slide_img + '" onload="SlideShow.setTimer(true);" />');
			// add the count
			$("#fs_ss_count").html((parseInt(SlideShow.curr_html) + 1) + ' / ' + SlideShow.slide_amount)
			$("#fs_ss_caption").html(SlideShow.slidesObj[SlideShow.curr_html].category)
			/// since we are reinitializing when we go to a full screen mode, we need to rebuild the relations of the images to the side nav.
			
			var category_list ='';
			var last_category = ''
			var cat_list_nav_count = -1;
			for (i in SlideShow.slidesObj){ // populate side links
				if(last_category != SlideShow.slidesObj[i].category){
					cat_list_nav_count++
					SlideShow.cat_list_nav_position[i] = cat_list_nav_count;
					last_category = SlideShow.slidesObj[i].category
				} else {
					SlideShow.cat_list_nav_position[i] = cat_list_nav_count;
				}
			}
		};
		
		
		if(SlideShow.context == 'modal'){
			// load the initil image
			$('#ss_container #' + SlideShow.curr_div).html('<img src = "' + SlideShow.resize_prefix + SlideShow.slidesObj[SlideShow.curr_html].slide_img + '" onload="SlideShow.setTimer();" />');
			$("#ss_count").html((parseInt(SlideShow.curr_html) + 1) + ' / ' + SlideShow.slide_amount)
			$("#ss_caption").html(SlideShow.slidesObj[SlideShow.curr_html].caption)
		}
		
		if(SlideShow.context == 'home'){
			$('#ss_container').hide();
			SlideShow.play_state = 'pause';
			$('#ss_container #' + SlideShow.curr_div).html('<img src = "' + SlideShow.slidesObj[SlideShow.curr_html].slide_img + '" onload="SlideShow.fadeInBanner();"/>');
			$('#desc').html(SlideShow.slidesObj[SlideShow.curr_html].caption)
			$('#desc a').append(' <span class="more">More ></span>');
			
			
			
			
			
			
		}
		
		$("#ss_nav .prev, #fs_controls .prev").click(function(e){
				e.preventDefault();
				if(!(SlideShow.fade_lock)){ // if not currently locked out from animating
					clearTimeout(SlideShow.timer_int)
					SlideShow.nextImg('reverse')
				}
			})
		
		$("#ss_nav .next, #fs_controls .next").click(function(e){
				e.preventDefault();
				if(!(SlideShow.fade_lock)){ // if not currently locked out from animating
					clearTimeout(SlideShow.timer_int)
					SlideShow.nextImg()
				}
			})
		
		
		if(SlideShow.context == 'work_detail'){
			$('#playpause').click(function(e){
				e.preventDefault()
				//console.log('click')
				var span_el = $(this).find('span');
				if( $(span_el).hasClass('pause') ){
					if(!(SlideShow.fade_lock)){ // if not currently locked out from animating
						$('#playpause').html('<span class="play"></span> Play');
						SlideShow.setPlayState('pause')
					}
				} else {
					if(!(SlideShow.fade_lock)){ // if not currently locked out from animating
						$('#playpause').html('<span class="pause"></span> Pause');
						SlideShow.setPlayState('play')
					}
				}
			});
		}
		
		if(SlideShow.context == 'fullscreen'){
			$('#fs_play_pause').click(function(e){
			e.preventDefault()
			   if($(this).hasClass('pause') ){
					if(!(SlideShow.fade_lock)){ // if not currently locked out from animating
						$(this).removeClass('pause').addClass('play');
						SlideShow.setPlayState('pause')   
					}
			   } else {
				  
				  if(!(SlideShow.fade_lock)){ // if not currently locked out from animating
						$(this).removeClass('play').addClass('pause');
						SlideShow.setPlayState('play')
						//GlobalSlideShow.play_state = 'play';
					}
				 }
			});
		}
	
	},
	
	setPlayState : function(state){
		//console.log('running funtion setPlayState')
		SlideShow.playPause(state)
		SlideShow.play_state = state;
		GlobalSlideShow.play_state = state;
	},
	
	changeSlide : function(next_html, curr_html, slide_type)
	{
		clearTimeout(SlideShow.timer_int);  
		
		// make sure we have our play pause buttons displaying correctly.
		SlideShow.setPlayPauseButtonState();
		clearTimeout(SlideShow.load_timer);
		$('#ss_loading, #fs_ss_loading').hide();
		
		if(SlideShow.context == 'work_detail'){
			//alert(SlideShow.next_html)
			$("#ss_count").html((parseInt(SlideShow.next_html) + 1) + ' / ' + SlideShow.slide_amount)
			$('#category_list a').removeClass('current')
			$('#category_list a[highlight_pos="' + SlideShow.cat_list_nav_position[SlideShow.next_html] + '"]').addClass('current');
			
		}
		if(SlideShow.context == 'modal'){
		
			$("#ss_caption").html(SlideShow.slidesObj[SlideShow.next_html].caption)
			$("#ss_count").html((parseInt(SlideShow.next_html) + 1) + ' / ' + SlideShow.slide_amount)
		}
		
		if(SlideShow.context == 'fullscreen'){
			$("#fs_ss_caption").html(SlideShow.slidesObj[SlideShow.next_html].category)
			$("#fs_ss_count").html((parseInt(SlideShow.next_html) + 1) + ' / ' + SlideShow.slide_amount)
		}
		
		
		// if we're on the homepage, change out the caption
		if(SlideShow.context == 'home'){
			var quick_fade = (SlideShow.fade_speed / 2)
			$("#desc").fadeOut(quick_fade, function(){
				$("#desc").html(SlideShow.slidesObj[next_html].caption)	
				$('#desc a').append(' <span class="more">More ></span>');
				$("#desc").fadeIn(quick_fade)
				
				// call back to show description
				SlideInterval.endIntialSlide()
				SlideInterval.showDesc();
				
			})
			
				
		}
		
		var id_prefix = '';	
			var zindex1 = 100;
			var zindex2 = 101;
				
		if(SlideShow.context == 'fullscreen'){
		 	id_prefix = 'fs_'	
			zindex1 = 1000;
			zindex2 = 1001;
		} 
		
		if(slide_type){
				if (slide_type == SlideShow.context){
				
			$("#" + id_prefix + SlideShow.next_div).css('z-index', zindex2).fadeIn(SlideShow.fade_speed, function(){
				$("#" + id_prefix + SlideShow.curr_div).hide();
				$("#" + id_prefix + SlideShow.next_div).css('z-index', zindex1)
			
				SlideShow.curr_html = SlideShow.next_html;
				GlobalSlideShow.current_slide = SlideShow.curr_html;
				var curr_switch_hold = SlideShow.curr_div;
				SlideShow.curr_div = SlideShow.next_div
				SlideShow.next_div = curr_switch_hold
				SlideShow.fade_lock = false; // release the lock out
					
					if(SlideShow.play_state == 'play'){ // if we are in playmode, reset the timer
						SlideShow.setTimer()
					}
				})
		
			}
		}
	},
	
	playPause : function (command){
			//console.log('running funtion playpause')
		//console.log(command)
		if(command == 'pause'){
			clearInterval(SlideShow.timer_int)
		} else if(command == "play") {
			SlideShow.nextImg()
		}
		
	},
	
	setPlayPauseButtonState : function(){
			//alert(SlideShow.play_state)
			if(SlideShow.play_state == 'play'){
				// for fullscreen
				$('#fs_play_pause').removeClass('play').addClass('pause');
				$('#playpause').html('<span class="pause"></span> Pause');
			
			} else {
				// for fullscreen
				$('#fs_play_pause').removeClass('pause').addClass('play');
				$('#playpause').html('<span class="play"></span> Play');
			}
			
		},
		
	
	// 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) {
			
			//console.log(mode);
			SlideShow.setPlayPauseButtonState();
		SlideShow.load_timer = setTimeout(SlideShow.showLoading, 150)
			SlideShow.fade_lock = true; // lock out while we load and fade
			
			if (mode == 'random'){
				SlideShow.next_html = new_slide;
			}else if(mode == 'reverse'){
				(SlideShow.curr_html == 0) ? SlideShow.next_html = eval(SlideShow.slide_amount) - 1 : SlideShow.next_html = SlideShow.curr_html - 1;
			} else {
				
				(SlideShow.curr_html < (SlideShow.slide_amount - 1)) ? SlideShow.next_html = eval(SlideShow.curr_html) + 1 : SlideShow.next_html = 0;
			}
			
			
			
				if((SlideShow.context == 'modal') || (SlideShow.context == 'work_detail')){
			
		$('#ss_container #' + SlideShow.next_div).html('<img src = "' + SlideShow.resize_prefix +  SlideShow.slidesObj[SlideShow.next_html].slide_img + '" onload="SlideShow.changeSlide(' + SlideShow.next_html + ' , ' + SlideShow.curr_html + ', \''+ SlideShow.context +'\' );" />');
				} else if(SlideShow.context == 'fullscreen'){
				$('#fs_ss #fs_' + SlideShow.next_div).html('<img src = "' + SlideShow.img_dir + SlideShow.slidesObj[SlideShow.next_html].slide_img + '" onload="SlideShow.changeSlide(' + SlideShow.next_html + ' , ' + SlideShow.curr_html + ', \''+ SlideShow.context +'\' );" />');
				} else {
		$('#ss_container #' + SlideShow.next_div).html('<img src = "' + SlideShow.slidesObj[SlideShow.next_html].slide_img + '" onload="SlideShow.changeSlide(' + SlideShow.next_html + ' , ' + SlideShow.curr_html + ', \''+ SlideShow.context +'\' );" />');			
				}
		
	},
	
	fadeInBanner : function(){
		$('#ss_container').fadeIn('slow')
	},
		
	setTimer : function()
	{
		
		if ( SlideShow.play_state == 'play') {
			SlideShow.timer_int = setTimeout(SlideShow.nextImg, SlideShow.slideshow_delay); //
		}
	}, 
	
	showLoading : function()
	{
		$('#ss_loading, #fs_ss_loading').show();
	}
}
