/*
Dependency of image list in the slideshow page:
eg: var ImageIlist = ['1020x610_imageA1.jpg','1020x610_imageA2.jpg']
*/
jQuery(document).ready(function() {
		$('#thubnailList li').hide();
        $('#thubnailList li').eq(0).show();
var slideShowObj = {};
		slideShowObj.slidesList = $('#mainImgList');
		slideShowObj.slideHeight = $('li img',slideShowObj.slidesList).eq(0).height();
		slideShowObj.slidesThumbList = $('#thubnailList');
		slideShowObj.MainDescList = $('#MainDescList');
		slideShowObj.MainImgSlideRightNav = $('#mainImgRightNav');
		slideShowObj.MainImgSlideLeftNav = $('#mainImgLeftNav');
		slideShowObj.slidesThumbListLeftNav = $('.leftAction');
		slideShowObj.slidesThumbListRightNav = $('.rightAction');
		slideShowObj.MainDescListItemHeight = $('#MainDescList li').eq(0).height();
		slideShowObj.autoScroll = 1; // is autoscroll enabled
		slideShowObj.maxSlides = ImageIlist.length; // number of slides 
		slideShowObj.slidesThumbList.containerWidth = 132 ;
		slideShowObj.options = {};
		slideShowObj.controls = {};	
		slideShowObj.slideAction = {};
		slideShowObj.options.slideDuration = 7000;
		slideShowObj.options.autoPlayResumeDelay = 14000;
		slideShowObj.options.slideTransitionDuration = 600;
		var autoScrollPlay;
		var navHovered = 0;
		var imgHovered = 0;
		
		// select the currently active item
		slideShowObj.selectSlide = function(indexArg){
				$('li',slideShowObj.slidesThumbList).removeClass('selected');
				$('li',slideShowObj.slidesThumbList).eq(indexArg).addClass('selected');
		}
		// Build stage : stage
	slideShowObj.slideAction.buildStage = function(indexArg){
		$('#mainImgList li:first').css({'width':'1020px','height':'610px','position':'absolute','left':'0','top':'0','z-index':'1','display':'block'});
		$('#mainImgList li:last').css({'width':'1020px','height':'610px','position':'absolute','left':'0','top':'0','z-index':'2','display':'block'});
		//appear Slide
		$('#mainImgList li:first').html('<img src="'+ImageIlist[indexArg]+'" alt=""  />');
		$('#mainImgList li:last').fadeOut(slideShowObj.options.slideTransitionDuration,function(){
			//$(this).css({'z-index':($(this).css('z-index')-2)})
			$(this).html('');
			$(this).detach().prependTo('#mainImgList');
		});
		
		}	
		// set the currently active Main Description box
		slideShowObj.setCurrentMainDescList = function(indexArg){
				$('li',slideShowObj.MainDescList).hide();
				$('li',slideShowObj.MainDescList).eq(indexArg).show();
		}
		
		// set the currently active Thubnail Item
		slideShowObj.setCurrentThumbList = function(indexArg){
				$('li',slideShowObj.slidesThumbList).hide();
				$('li',slideShowObj.slidesThumbList).eq(indexArg).show();
		}
		
		// return Current Slide Index
		slideShowObj.getCurrentSlideIndex = function(){
				return $('li:visible',slideShowObj.MainDescList).index()
		}
		// Initial show Nav buttons
		slideShowObj.showInitNavButton = function(buttonElem){
				$(buttonElem).fadeTo('slow',0.6);
				var t = setTimeout(function(){
				if ((navHovered == 0) && (imgHovered == 0)){
					$(buttonElem).fadeTo('slow',0);
				}
				
				},4000);
				clearTimeout('t');
			}

		
		// appear option
		slideShowObj.slideAction.appear = function(indexArg){
			$('li',slideShowObj.slidesList).hide();
			slideShowObj.slideAction.buildStage(indexArg);
			slideShowObj.selectSlide(indexArg); // set the selected Thubnail
			slideShowObj.setCurrentMainDescList(indexArg) // set the Main Description Colmn value 
			slideShowObj.setCurrentThumbList(indexArg) // set the Thumb Colmn value 
		}
		
			
	
		
		// show next slide action
		slideShowObj.controls.next = function(){
		if(slideShowObj.getCurrentSlideIndex()<(slideShowObj.maxSlides-1)){
		slideShowObj.slideAction.appear(slideShowObj.getCurrentSlideIndex()+1); 
		}else{
		slideShowObj.slideAction.appear(0); 
		}
		return false;
		}
		// show Prev slide action
		slideShowObj.controls.prev = function(){
		if(slideShowObj.getCurrentSlideIndex()> 0){
		slideShowObj.slideAction.appear(slideShowObj.getCurrentSlideIndex()-1); 
		}else{
		slideShowObj.slideAction.appear(slideShowObj.maxSlides-1); 
		}
		return false;
		}
		//resume auto play
		slideShowObj.controls.resumeAutoPlay = function(){
			autoScrollPlay = setInterval(function() { slideShowObj.controls.next(); }, slideShowObj.options.slideDuration);
			}
		slideShowObj.controls.resumeAutoPlayTimer = function(){
			var uniqueTimer = setTimeout(function(){clearTimeout(uniqueTimer);clearInterval (autoScrollPlay);slideShowObj.controls.resumeAutoPlay();},slideShowObj.options.autoPlayResumeDelay);
		} 
		// set Nav click behavior
		$(slideShowObj.MainImgSlideRightNav).click(function(){
			slideShowObj.controls.next();
			clearInterval (autoScrollPlay);// stop auto play
			slideShowObj.controls.resumeAutoPlayTimer(); // start resume function
		})
		$(slideShowObj.MainImgSlideLeftNav).click(function(){
			slideShowObj.controls.prev();
			clearInterval (autoScrollPlay); // stop auto play
			slideShowObj.controls.resumeAutoPlayTimer(); // start resume function
		})
			// show/Hide the Nav buttons
		if(slideShowObj.maxSlides>1){	
			
			// Initial show up of Img Navs
			slideShowObj.showInitNavButton(slideShowObj.MainImgSlideLeftNav);
			slideShowObj.showInitNavButton(slideShowObj.MainImgSlideRightNav);
		
			// auto scroll action
			if(slideShowObj.autoScroll){
			var autoScrollPlay = setInterval(function() { slideShowObj.controls.next(); }, slideShowObj.options.slideDuration);
			}
		} // end if
	
	// build stage
	slideShowObj.slideAction.buildStage(0);
				if(slideShowObj.maxSlides>1){	
					$('#MainDesc').hover(function(){
						navHovered = 1;
					},function(){
						navHovered = 0;
					})
					
					$('#mainImgRightNav,#mainImgLeftNav').hover(function(){
						navHovered = 1;
						$(this).fadeTo(10,0.8);
					},function(){
						navHovered = 0;
						$(this).fadeTo(10,0.6);
					})
					
					$('#mainImgList li').hover(
						function(e){
						imgHovered = 1;
							$(slideShowObj.MainImgSlideLeftNav).fadeTo(200,0.6);
							$(slideShowObj.MainImgSlideRightNav).fadeTo(200,0.6);
						return true;
						}, 
						function(e){
						imgHovered = 0;
							var hideTimeout = setTimeout(function(){
							if((imgHovered!=1) && (navHovered!=1)){
								$(slideShowObj.MainImgSlideLeftNav).fadeTo(200,0);
								$(slideShowObj.MainImgSlideRightNav).fadeTo(200,0)
								clearTimeout(hideTimeout);
							}
							},3000)
							
						return true;
						}
					)
				}

	
		
}); // doc ready
$.fn.preload = function() {
    this.each(function(){
        $('<img/>')[0].src = this;
	});
}

