/************		CUSTOMIZATION		************/

var koScaleAmt = 150; // percentage of zoom in ( set to 100 for no zoom )
var koZoomPanRatio = 100; // percentage of zooming vs panning ( 100 = full zoom, 0 = no zoom )
var koTimeFade = 2.0; // duration of fade in seconds
var koTimeBetween = 4.0; // duration between fades in seconds
var koshowelement = 'koshow' // id of element holding slides

/**************************************************/

function startKoShow() {
	if ($(koshowelement)){
		var i = 1;
		slideArray = $(koshowelement).childElements();
		slideArray.each(function(slide) {
			slide.absolutize();
			slide.setOpacity(0);
			slide.style.zIndex = i;
			i = i + 1;
		});
			slideArray[0].setOpacity(1);
			zoomPanKoShow(slideArray[0],koTimeBetween+koTimeFade,0);
			nextKoShow(0);
	}
}

function nextKoShow(i){
	var next = i + 1;
	if (next >= slideArray.length)
		next = 0;
	new Effect.Morph(slideArray[i], {style:'opacity:0', duration:koTimeFade, delay:koTimeBetween, transition: Effect.Transitions.linear});
	new Effect.Morph(slideArray[next], {style:'opacity:1', duration:koTimeFade, delay:koTimeBetween, transition: Effect.Transitions.linear, afterFinish:function(){
			slideArray[i].setOpacity(0);
			slideArray[i].style.zIndex = slideArray[i].style.zIndex+slideArray.length;
			nextKoShow(next);
		}
	});
	zoomPanKoShow(slideArray[next],koTimeBetween+(koTimeFade*2),koTimeBetween);
}

function zoomPanKoShow(slide,duration,delay){
 if (koScaleAmt > 100){
		var orgWidth = slide.getWidth();
		var orgHeight = slide.getHeight();
		
		var farX = (orgWidth * koScaleAmt/100) - orgWidth;
		var farY = (orgHeight * koScaleAmt/100) - orgHeight;
		
		var zoomWidth = orgWidth * (koScaleAmt/100);
		var zoomHeight = orgHeight * (koScaleAmt/100);
		
		var morphWidth = zoomWidth - ((zoomWidth - orgWidth) * koZoomPanRatio/100);
		var morphHeight = zoomHeight - ((zoomHeight - orgHeight) * koZoomPanRatio/100);
		
		var panToX = -RandRange(0,farX);
		var panToY = -RandRange(0,farY);

		if( RandRange(0,1)){ // zoom out
			slide.setStyle({top:panToY+'px', left:panToX+'px', height:zoomHeight+'px', width:zoomWidth+'px'});
			new Effect.Morph(slide, {style:'left:0px; top:0px; width:'+morphWidth+'px; height:'+morphHeight+'px;' ,duration:duration, delay:delay, transition: Effect.Transitions.linear, afterFinish:function(){
				slide.setStyle({top:'0px', left:'0px', height:orgHeight+'px', width:orgWidth+'px'});
			}});
		} else { // zoom in
			slide.setStyle({top:'0px', left:'0px', height:morphHeight+'px', width:morphWidth+'px'});
			new Effect.Morph(slide, {style:'left:'+panToX+'px; top:'+panToY+'px; width:'+zoomWidth+'px; height:'+zoomHeight+'px;' ,duration:duration, delay:delay, transition: Effect.Transitions.linear, afterFinish:function(){
				slide.setStyle({top:'0px', left:'0px', height:orgHeight+'px', width:orgWidth+'px'});
			}});
		}
	}
}

function RandRange( intFrom, intTo, intSeed ){
	intFrom = Math.floor( intFrom );
	intTo = Math.floor( intTo );
	return(
		Math.floor(
			intFrom + 
			(
				(intTo - intFrom + 1) * 
				Math.random( 
					(intSeed != null) ? intSeed : 0 
					)
			))
		);
}

var slideArray = new Array();
window.onload = startKoShow;
