var duration = 9000, transition = 1000, step = 50,
	currentForemostSlideshowImg = 0, slideshowCounter = 0, slideshowInterval,
	fadeStep = 50, fadeCounter = 0, fadeDelta = 0, fadeInterval, transitionStep = 50,
	transitionCounter = 0, transitionDelta = 0, transitionInterval, currentGalleryPic,
	currentForemostGalleryDiv, resetGalleryOnClose, galleryDivs = new Array(),
	IE6 = false /*@cc_on || @_jscript_version < 5.7 @*/;

//var debug = true;

function trace( txt ) {
	if( window.debug ) {
		if( window.opera ) opera.postError( txt );
		if( window.console ) console.log( txt );
	}
}

function addEvent( obj, evType, fn, useCapture){
	if ( obj.addEventListener ) {
		obj.addEventListener( evType, fn, useCapture );
		return true;
	}
	else if ( obj.attachEvent ) {
		var r = obj.attachEvent( "on" + evType, fn );
		return r;
	}
	else {
		alert( "Error: Eventhandler could not be attached." );
	}
	return false;
}

function addObject( obj, tagname ) {
	var elem = document.createElement( tagname );
	obj.appendChild( elem );
	return elem;
}

function addClass( obj, classname ) {
	obj.className += " " + classname;
}

function removeClass( obj, classname ) {
	obj.className = obj.className.replace( " " + classname, "" );
}

function setOpacity( obj, opacity ) {
	if( obj.style ) {
		if( obj.style.opacity != null ) {
			obj.style.opacity = ( opacity / 100 ).toPrecision( 2 ).toString();
		}
		else if( obj.style.filter != null ) {
			obj.style.filter = "alpha(opacity=" + opacity + ")";
		}
	}
}

function initSlideShow() {
	slideshowInterval = setInterval( onSlideshowTimer, step );
}

function onSlideshowTimer() {
	var obj;

	if( slideshowCounter == 0 ) {
		obj = document.getElementById( "slide" + currentForemostSlideshowImg );
		setOpacity( obj, 100 );
		currentForemostSlideshowImg ^= 1;
		nextSlideshowImage = ( nextSlideshowImage + 1 ) % slideshowImages.length;
		obj = document.getElementById( "slide" + currentForemostSlideshowImg );
		setOpacity( obj, 0 );
		obj.src = slideshowImagesPath + slideshowImages[ nextSlideshowImage ];
		obj.style.zIndex = 2;
		document.getElementById( "slide" + ( currentForemostSlideshowImg ^ 1 ) ).style.zIndex = 1;
	}
	else if( slideshowCounter > duration ) {
		obj = document.getElementById( "slide" + currentForemostSlideshowImg );
		setOpacity( obj, 100 * ( slideshowCounter - duration ) / transition );
	}
	slideshowCounter += step;
	if( slideshowCounter > duration + transition ) slideshowCounter = 0;
}

function initGallery( resetOnClose ) {
	var obj, obj2, obj3;
	resetGalleryOnClose = resetOnClose;
	obj = addObject( document.getElementById( "content" ), "div" );
	obj.id = "gallerycontainer";
	obj2 = addObject( obj, "a" );
	obj2.id = "prevpic";
	obj2.title = "previous";
	addEvent( obj2, "click", galleryPrevPage, false );
	obj2 = addObject( obj, "a" );
	obj2.id = "nextpic";
	obj2.title = "next";
	addEvent( obj2, "click", galleryNextPage, false );
	obj2 = addObject( obj, "a" );
	obj2.id = "closegallery";
	obj2.title = "close";
	addEvent( obj2, "click", galleryFadeOut, false );
	obj2 = addObject( obj, "div" );
	obj2.id = "gallery";
	galleryDivs.push( addObject( obj2, "div" ) );
	obj3 = addObject( galleryDivs[ 0 ], "img" );
	addEvent( obj3, "load", function() { galleryImageHasLoaded( 0 ) }, false );
	galleryDivs.push( addObject( obj2, "div" ) );
	obj3 = addObject( galleryDivs[ 1 ], "img" );
	addEvent( obj3, "load", function() { galleryImageHasLoaded( 1 ) }, false );
	galleryDivs.push( addObject( obj2, "div" ) );
	obj3 = addObject( galleryDivs[ 2 ], "img" );
	addEvent( obj3, "load", function() { galleryImageHasLoaded( 2 ) }, false );
	resetGalleryImages();
	if( window.debug ) {
		galleryDivs[ 0 ].style.width = "250px";
		galleryDivs[ 0 ].style.height = "150px";
		galleryDivs[ 0 ].style.left = "0px";
		galleryDivs[ 0 ].style.top = "30px";
		galleryDivs[ 1 ].style.width = "250px";
		galleryDivs[ 1 ].style.height = "150px";
		galleryDivs[ 1 ].style.left = "250px";
		galleryDivs[ 1 ].style.top = "130px";
		galleryDivs[ 2 ].style.width = "250px";
		galleryDivs[ 2 ].style.height = "150px";
		galleryDivs[ 2 ].style.left = "500px";
		galleryDivs[ 2 ].style.top = "230px";
	}
 }

function resetGalleryImages() {
	galleryDivs[ 0 ].firstChild.src = "/images/web/ajax-loader.gif";
	galleryDivs[ 1 ].firstChild.src = "/images/web/ajax-loader.gif";
	galleryDivs[ 2 ].firstChild.src = "/images/web/ajax-loader.gif";
}

function loadGalleryImages( index ) {
	currentGalleryPic = index;
	currentForemostGalleryDiv = 0;
	galleryDivs[ 0 ].style.zIndex = 11;
	galleryDivs[ 0 ].firstChild.src = galleryImages[ index ];
	galleryDivs[ 1 ].style.zIndex = 10;
	galleryDivs[ 1 ].firstChild.src = galleryImages[ ( index + 1 ) % galleryImages.length ];
	galleryDivs[ 2 ].style.zIndex = 9;
	galleryDivs[ 2 ].firstChild.src = galleryImages[ ( index + galleryImages.length - 1 ) % galleryImages.length ];
}

function galleryImageHasLoaded( i ) {
	galleryDivs[ i ].className = ( galleryDivs[ i ].firstChild.src.indexOf( "ajax-loader.gif" ) > -1 ? "loading" : "galleryimg" );
}

function galleryFadeIn() {
	if( fadeDelta != 0 ) return;
	addClass( document.getElementById( "gallerycontainer" ), "disp" );
	addClass( document.getElementById( "gallery" ), "disp" );
	fadeDelta = 1;
	fadeInterval = setInterval( onFadeTimer, fadeStep );
}

function galleryFadeOut() {
	if( fadeDelta != 0 ) return;
	fadeDelta = -1;
	fadeInterval = setInterval( onFadeTimer, fadeStep );
}

function onFadeTimer() {
	var gallery = document.getElementById( "gallery" );
	fadeCounter += fadeDelta;
	setOpacity( document.getElementById( "gallerycontainer" ), 20 * fadeCounter );
	if( fadeCounter < 1 ) {
		removeClass( gallery, "disp" );
		removeClass(document.getElementById( "prevpic" ), "disp" );
		removeClass(document.getElementById( "nextpic" ), "disp" );
		removeClass(document.getElementById( "closegallery" ), "disp" );
		removeClass(	document.getElementById( "gallerycontainer" ), "disp" );
		clearInterval( fadeInterval );
		fadeDelta = 0;
		if( resetGalleryOnClose ) resetGalleryImages();
	}
	else if( fadeCounter > 4 ) {
		addClass( gallery, "disp" );
		addClass( document.getElementById( "prevpic" ), "disp" );
		addClass( document.getElementById( "nextpic" ), "disp" );
		addClass( document.getElementById( "closegallery" ), "disp" );
		clearInterval( fadeInterval );
		fadeDelta = 0;
	}
}

function galleryPrevPage() {
	if( transitionDelta != 0 ) return;
	transitionDelta = -1;
	transitionInterval = setInterval( onTransitionTimer, transitionStep );
}

function galleryNextPage() {
	if( transitionDelta != 0 ) return;
	transitionDelta = 1;
	transitionInterval = setInterval( onTransitionTimer, transitionStep );
}

function onTransitionTimer() {
	if( transitionCounter == 0 ) {
		var obj = galleryDivs[ currentForemostGalleryDiv ];
		obj.style.zIndex = 10;
		setOpacity( obj, 100 );
		currentForemostGalleryDiv = ( currentForemostGalleryDiv + transitionDelta + 3 ) % 3;
		currentGalleryPic = ( currentGalleryPic + transitionDelta + galleryImages.length ) % galleryImages.length;
		obj = galleryDivs[ currentForemostGalleryDiv ];
		setOpacity( obj, 0 );
		obj.style.zIndex = 11;
		obj = galleryDivs[ ( currentForemostGalleryDiv + transitionDelta + 3 ) % 3 ];
		obj.style.zIndex = 9;
		obj.firstChild.src = "/images/web/ajax-loader.gif";
	}
	else if( transitionCounter == 1 ) {
		galleryDivs[ ( currentForemostGalleryDiv + transitionDelta + 3 ) % 3 ].firstChild.src = galleryImages[ ( currentGalleryPic + transitionDelta + galleryImages.length ) % galleryImages.length ];
	}
	else if( transitionCounter < 7 ) {
		obj = galleryDivs[ currentForemostGalleryDiv ];
		setOpacity( obj, 20 * ( transitionCounter - 1 ) );
	}
	transitionCounter++;
	if( transitionCounter > 6 ) {
		transitionCounter = 0;
		transitionDelta = 0;
		clearInterval( transitionInterval );
	}
}

addEvent( window, "load", initSlideShow, false );
