/* Javascript Document
*
*	Copyright Correlation Inc.
*	Correlation Inc. is a Corporation in the State of New York, US.
*	all rights reserved
*/

// alert('bibly boo');

// depends on
// crlw_ajax
// crlw_setTransparency
// crlw_animTransition
(function(){
	
	if(!window.crlw){
		window.crlw = {};
	}
	
	// slideshows list
	window.crlw.slideshows = {};
	
	//<(
	window.crlw.slideshows.builder = function(args_arr,slideshow_JSId){
		// testing code
		this.slideshow_JSId = slideshow_JSId;
		this.contentInfo = [];
		this.contentInfo['A'] = [];
		this.contentInfo['A']['cont_HTMLId'] = args_arr['A']['cont_HTMLId'];
		this.contentInfo['A']['image_HTMLId'] = args_arr['A']['image_HTMLId'];
		this.contentInfo['A']['caption_HTMLId'] = args_arr['B']['caption_HTMLId'];
		this.contentInfo['A']['cont_ref'] = document.getElementById(args_arr['A']['cont_HTMLId']);
		this.contentInfo['A']['image_ref'] = document.getElementById(args_arr['A']['image_HTMLId']);
		this.contentInfo['A']['caption_ref'] = document.getElementById(args_arr['A']['caption_HTMLId']);
		this.contentInfo['B'] = [];
		this.contentInfo['B']['cont_HTMLId'] = args_arr['B']['cont_HTMLId'];
		this.contentInfo['B']['image_HTMLId'] = args_arr['B']['image_HTMLId'];
		this.contentInfo['B']['caption_HTMLId'] = args_arr['B']['caption_HTMLId'];
		this.contentInfo['B']['cont_ref'] = document.getElementById(args_arr['B']['cont_HTMLId']);
		this.contentInfo['B']['image_ref'] = document.getElementById(args_arr['B']['image_HTMLId']);
		this.contentInfo['B']['caption_ref'] = document.getElementById(args_arr['B']['caption_HTMLId']);
		this.contentInfo['images'] = [];
		this.contentInfo['images'] = args_arr['images'];
		this.fade_time = args_arr['fade_time'];
		this.transition_time = args_arr['transition_time'];
		this.timeout = null;
		this.currentLetter = 'A';
		this.oddLetter = 'B';
		this.currentSlide = 0;
		this.nextSlide = 0;
		this.auto_rotate = args_arr['auto_rotate'];
		this.thumb_images = args_arr['thumb_images'];
		if(args_arr['thumb_HTMLId_str']){
			this.thumb_HTMLId_str = args_arr['thumb_HTMLId_str'];
		}
		this.open_transition = false;
		this.waiting_transition = false;
		this.waitingSlide = null;
	}
	window.crlw.slideshows.list = new Array();
	//)>
	
	//<(
	window.crlw.slideshows.setNewSlideshow = function(args_arr){

		var master = this;
		var slideshow_JSId = (master.list.length + 1);
		var newSlideshow = new master.builder(args_arr,slideshow_JSId);
		master.list[slideshow_JSId] = newSlideshow;


		// set up transition listeners if not autorotate
		if(newSlideshow.thumb_images == true){
			// s
			for(i=0; i < newSlideshow.contentInfo.images.length;i++){
				var image_JSId = newSlideshow.contentInfo.images[i].image_JSId;
				var thumb_ref = document.getElementById(newSlideshow.thumb_HTMLId_str + image_JSId);
				thumb_ref.image_JSId = image_JSId;
				thumb_ref.slideshow_JSId = slideshow_JSId;
				thumb_ref.onclick = function(){
					window.crlw.slideshows.list[this.slideshow_JSId].goToSlide(this.slideshow_JSId,this.image_JSId);
				};
			}
			
		}


		master.list[slideshow_JSId].start(slideshow_JSId);
		return slideshow_JSId;
	};
	//)>

	//<(
	window.crlw.slideshows.builder.prototype.start = function(slideshow_JSId){

		master = window.crlw.slideshows.list[slideshow_JSId];
		// testing code
		//alert('starting');
		if(master.auto_rotate){
			master.startNextRotation(slideshow_JSId);
			//alert('starting');
		}
	};
	//)>
	
	//<(
	window.crlw.slideshows.builder.prototype.runTransition = function(slideshow_JSId){
		master = window.crlw.slideshows.list[slideshow_JSId];

		// testing code
		// alert('fading:' + master.contentInfo.images[master.nextSlide].src);

		// make slide transparent

		// cancel a future transition if currently scheduled
		// this only happens if this is a manually triggered change
		// rather than a scheduled one
		clearTimeout(master.timeout);
		master.open_transition = true;
        // fade out current
        window.crlw.animTrans.set(
            master.contentInfo[master.currentLetter].cont_HTMLId,
            'Opacity',
            '100',
            '0',
            master.fade_time,
            'master.finishTransition(' + master.slideshow_JSId  +')'
        );

	};
	//)>
	
	//<(
	window.crlw.slideshows.builder.prototype.finishTransition = function(slideshow_JSId){
		master = window.crlw.slideshows.list[slideshow_JSId];
		// alert('preping next slide');

		// switch active side
		if(master.currentLetter == 'A'){
			master.currentLetter = 'B';
			master.oddLetter = 'A';
		}else{
			master.currentLetter = 'A';
			master.oddLetter = 'B';
		}

		// set new as relative absolutes 
		//master.contentInfo[master.oddLetter].cont_ref.style.position = 'absolute';
		//master.contentInfo[master.currentLetter].cont_ref.style.position = 'relative';
		master.contentInfo[master.oddLetter].cont_ref.style.zIndex = 1;
		master.contentInfo[master.currentLetter].cont_ref.style.zIndex = 2;
		// redo transparency for back
		window.crlw.setTransparency(master.contentInfo[master.oddLetter].cont_HTMLId,100);

		master.currentSlide = master.nextSlide;

		master.open_transition = false;

		if(master.auto_rotate){
			master.startNextRotation(slideshow_JSId)
		}
	}
	//)>

	//<(
	window.crlw.slideshows.builder.prototype.startNextRotation = function(slideshow_JSId){



		master = window.crlw.slideshows.list[slideshow_JSId];

		master.incrementNextSlide(slideshow_JSId);

		//testing code
		// alert('starting rotation to: ' + master.contentInfo.images[master.nextSlide].src);


		// load next slide
		master.loadSlide(slideshow_JSId);


		// set slide transition timeout
		var timout_time = master.transition_time * 1000;
		master.timeout = window.setTimeout('window.crlw.slideshows.list[' + master.slideshow_JSId  +'].goToSlide(' + master.slideshow_JSId  +','+ master.nextSlide +')',timout_time);

	}
	//)>

	//<(
	window.crlw.slideshows.builder.prototype.incrementNextSlide = function(slideshow_JSId){
		master = window.crlw.slideshows.list[slideshow_JSId];
		master.nextSlide++;
		if(master.nextSlide >= master.contentInfo.images.length){
			master.nextSlide = 0;
		}
	}
	//)>

	//<(
	window.crlw.slideshows.builder.prototype.loadSlide = function(slideshow_JSId){

		master = window.crlw.slideshows.list[slideshow_JSId];
		// alert('prepping next slide: ' + master.nextSlide);
		//alert(master.contentInfo.images.length);
		// set src of image

		master.contentInfo[master.oddLetter].image_ref.nextSlide = master.nextSlide;
		master.contentInfo[master.oddLetter].image_ref.slideshow_JSId = slideshow_JSId;
		master.contentInfo[master.oddLetter].image_ref.src = 'images/' + master.contentInfo['images'][master.nextSlide].src;
		master.contentInfo[master.oddLetter].caption_ref.innerHTML = master.contentInfo['images'][master.nextSlide].caption;

		//alert(master.nextSlide + '  ' + master.contentInfo.images[master.nextSlide].src);
		// set caption content
			// ajax request
		// set slide transition timeout
	}
	//)>

	//<(
	window.crlw.slideshows.builder.prototype.goToSlide = function(slideshow_JSId,slideNumber){
		master = window.crlw.slideshows.list[slideshow_JSId];
		// testing code
		// alert('go to slide' + slideNumber);

		// if slide transition is in progress
		if(!master.open_transition){
			master.waiting_transition = true;
			master.waitingSlide = slideNumber;
			master.showWaitingSlide(slideshow_JSId,slideNumber);
		}else{
		// wait till it is done
			if(!master.waiting_transition){
				master.waiting_transition = true;
				master.waitingSlide = slideNumber;
				// set interval to check back
				master.waiting_interval = window.setInterval('window.crlw.slideshows.list[' + slideshow_JSId  +'].showWaitingSlide(' + slideshow_JSId + ')',100);
			}else{
			// overwrite already waiting transition if exists
				master.waitingSlide = slideNumber;
			}
		}
	}
	//)>

	//<(
	window.crlw.slideshows.builder.prototype.showWaitingSlide = function(slideshow_JSId){
		master = window.crlw.slideshows.list[slideshow_JSId];
		// testing code
		// alert('go to slide' + slideNumber);

		// if slide transition is in progress
		// wait till it is done
		if(!master.open_transition){
			master.nextSlide = master.waitingSlide;

			// load slide behind 
			master.loadSlide(slideshow_JSId);

			// run transition have it wait till loaded
			master.runTransition(slideshow_JSId);
			master.waiting_transition = false;
			master.waitingSlide = null;
			if(master.waiting_interval){
				clearInterval(master.waiting_interval);
			}
		}
	}
	//)>
})();

