 /* daspoda:2009 */
var delay = 5;	// delay between fades
var useFlyout = 0;	// 1 or 0   use or not use mouse-over flyout
YUI({combine: true, timeout: 10000}).use('node','anim', 'datatype',function(Y) {
	function numeric(str){ 
		var strOut = new String(str); 
		return strOut.replace(/[^0-9]/g, ''); 
	}
	// get all images and put them in an array and remove from view
	var faderheight = numeric(Y.one('#imagefader').getStyle('height'));
	var faderwidth = numeric(Y.one('#imagefader').getStyle('width'));
	var images = new Array();
	var imgOffsetsX = new Array();
	var imgOffsetsY = new Array();
	var text = new Array();
	var contentNodes = Y.all('#imagefader .imgfade');
	
	contentNodes.each(function(ni){
		if (useFlyout){
			text[text.length] = ni.all('.imgtitle');
		}
		images[images.length] = ni.one('img').getAttribute('src');
		//var posy = -(numeric(ni.one('img').getStyle('height')) - faderheight)/2 + "px";
		var posy = "0px";
		var posx = "0px";
		//var posx = -(numeric(ni.one('img').getStyle('width')) - faderwidth)/2 + "px";
		imgOffsetsY[images.length-1] = posy;
		imgOffsetsX[images.length-1] = posx;
		ni.remove();
	});
	
	
	// add top and bottom div
	Y.one('#imagefader').append('<div class="imgfade bottomimg">');
	Y.one('#imagefader').append('<div class="imgfade topimg">');
	var top = Y.one('.topimg');
	var bottom = Y.one('.bottomimg');
	
	
	if (useFlyout){
		Y.one('#imagefader').append('<div class="textpopup">');
		Y.one('#imagefader .textpopup').append('<div class="textpopup-bg">');
		Y.one('#imagefader .textpopup').append('<div class="textpopup-txt">');
	
		var textbox = Y.one('.textpopup');
		var textbox_bg = textbox.one('.textpopup-bg');
		var textbox_txt = textbox.one('.textpopup-txt');
		var textboxtopposition = ( faderheight - numeric(textbox.getStyle('height')) );
		textbox.setStyle('top',textboxtopposition+'px');
		textbox.setStyle('opacity', '0');
		textbox_bg.setStyle('opacity', '0.3');
	}
	
	
	// go through frames in reverse that way it works with the last image loaded by html
	var imgNum = images.length - 1;
	top.setStyle('background', '#000 url('+images[imgNum]+') no-repeat ' + imgOffsetsX[imgNum] + ' ' + imgOffsetsY[imgNum]);
	if (useFlyout){
		textbox_txt.append(text[imgNum]);
	}
	bottom.setStyle('background', '#000 url('+images[imgNum]+') no-repeat ' + imgOffsetsX[imgNum] + ' ' + imgOffsetsY[imgNum]);
	
	// setup the delay function hack
	var wait = new Y.Anim({
		node:bottom,
		to: {opacity:1},
		duration: delay
	});
	
	// do the swap
	var switchPics = function(){
		imgNum = (imgNum-1);
		if (imgNum<0) {imgNum = images.length - 1};
		top.setStyle('background', '#000 url('+images[imgNum]+') no-repeat ' + imgOffsetsX[imgNum] + ' ' + imgOffsetsY[imgNum]);
		top.setStyle('left', -faderwidth + "px");
		
		if (useFlyout){
			textbox_txt.all('div').remove();
			textbox_txt.append(text[imgNum]);
		}
	};
	var switchBottom = function(){
		bottom.setStyle('background', '#000 url('+images[imgNum]+') no-repeat ' + imgOffsetsX[imgNum] + ' ' + imgOffsetsY[imgNum]);
		bottom.setStyle('left', "0px");
		anim.stop();
		wait.run();
		
	};
	
	var anim = new Y.Anim({
		node: top,
		from: {left: -(faderwidth * 0.5) + "px" , opacity: 0.2},
		to: {left:0 , opacity: 1},
		duration: 0.25,
		easing: Y.Easing.easeOut
	});
	
	
	// show/hide additional text
	if (useFlyout){
		var fadeout = new Y.Anim({
			node:textbox,
			to: { opacity:0, top:(textboxtopposition + 5) +'px' },
			duration: 0.1
		});
		var fadein = new Y.Anim({
			node:textbox,
			to: { opacity:0.9, top:(textboxtopposition +'px') },
			duration: 0.1
		});
	
		Y.one('#imagefader').on('mouseover', function(){fadein.run(); fadeout.stop();});
		Y.one('#imagefader').on('mouseout', function(){fadein.stop(); fadeout.run();});
	}
	
	// animate to next slide on click
	Y.one('#imagefader').on('click', function(){wait.stop(); anim.run();});
	
	anim.on('end', switchBottom);
	anim.on('start', switchPics);
	wait.on('end', function(){
		anim.run();
		if (useFlyout){
			textbox_txt.all('div').remove();
		}
	});
	wait.run();
});