//don't edit this file
var myFader = Class.create();
myFader.prototype = {
	initialize: function( img, windowSize )
	{
	  this.img = img;
	  this.windowSize = windowSize;
	},
	
	apply:  function( percent )
	{
	  var opacity = 100;
	
	  if ( percent <= this.windowSize )
		opacity = ( percent / this.windowSize ) * 100;
	  else if ( percent >= ( 100 - this.windowSize ) )
		opacity = ( ( 100 - percent ) / this.windowSize ) * 100;
	
	  this.img.set_opacity( opacity );
	}
};//end myFader.prototype;

//Animation ///////////////////
var Animation = Class.create();
Animation.prototype = {
//function Animation( am, img, seconds, effects )
	initialize: function( am, img, seconds, effects )
	{
	  this.img = img;
	  this.animationManager = am;
	  this.seconds = seconds;
	  this.effects = $A(effects);
	  this.startMS = 0;
	},

	//Animation.prototype.start = function()
	start : function()
	{
	  this.animationManager.add( this );
	  this.startMS = 0;
	
	//  this.img.hide();
	  //for( var e in this.effects )
	  this.effects.each(function(effect) 
	  {
		//this.effects[e].apply( 0 );
		effect.apply( 0 );
	  });
	//  this.img.show();
	},

	//Animation.prototype.animate = function()
	animate : function()
	{
	  var d = new Date();
	  if ( this.startMS == 0 )
		this.startMS = d.valueOf();
	
	  var p = (((d.valueOf()-this.startMS)/1000)/this.seconds)*100;
//	  for( var e in this.effects )
//		this.effects[e].apply( p );
	  this.effects.each(function(e) {e.apply(p)});
	},

	//Animation.prototype.done = function()
	done : function ()
	{
	  var d = new Date();
	  return ( ( d.valueOf() - this.startMS ) / 1000 ) > this.seconds;
	}
}

function am_idle (am) {
  if ( am.animations.length > 0 )
  {
	am.animations[0].animate();
	if ( am.animations[0].done() )
	  am.animations.shift();
	if ( am.animations.length == 0 )
	  am.on_finished();
  }
}

var AnimationManager = Class.create();
AnimationManager.prototype = {
	//function AnimationManager( speed )
	initialize: function (speed)
	{
	   this.animations = [];
	   var self = this;
	   window.setInterval( function() { self.idle(); }, speed );
//		new PeriodicalExecuter(am_idle(this), speed/1000);
	},

	//AnimationManager.prototype.add = function( anim )
	add : function (anim)
	{
	  this.animations.push( anim );
	},

	//AnimationManager.prototype.idle = function()
	idle : function()
	{
	  if ( this.animations.length > 0 )
	  {
		this.animations[0].animate();
		if ( this.animations[0].done() )
		  this.animations.shift();
		if ( this.animations.length == 0 )
		  this.on_finished();
	  }
	},

	//AnimationManager.prototype.on_finished = function()
	on_finished : function()
	{
	}
}
//ImageInfo /////////////////
var ImageInfo = Class.create();
ImageInfo.prototype = {
//function ImageInfo( src, width, height, htmlObj, captionObj )
initialize: function ( src, width, height, htmlObj, captionObj )
{
  this.src = src;
  this.width = width;
  this.height = height;
  this.current_width = width;
  this.current_height = height;

  this.htmlObj = htmlObj;
  this.htmlObj.src = this.src;
  this.caption = captionObj;
  this.htmlObj.width = this.current_width;
  this.htmlObj.height = this.current_height;
},

//ImageInfo.prototype.set_opacity = function( opacity )
set_opacity: function(opacity)
{
  this.htmlObj.style.MozOpacity = opacity / 100;
  this.caption.style.MozOpacity = opacity / 100;
  this.htmlObj.style.opacity = opacity / 100;
  this.caption.style.opacity = opacity / 100;
  this.htmlObj.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity='+opacity+')';
  this.caption.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity='+opacity+')';
  if (opacity <=10) {
	  this.htmlObj.style.display = "none";
	  this.caption.style.display = "none";
  } else {
	  this.htmlObj.style.display = "inline";
	  this.caption.style.display = "block";
  }
}
}
//grunt work

var g_animationManager = new AnimationManager( 50 );
var g_current_slide = 0;
var g_slides = [];

g_animationManager.on_finished = function()
{
  g_current_slide++;
  if ( g_current_slide >= g_slides.length )
    g_current_slide = 0;
  g_slides[ g_current_slide ].start();
}

function rnd( start, end )
{
  return ( Math.random() * ( end - start ) ) + start;
}

function load_slides( old_images )
{
//  var ic = document.getElementById( 'imgContainer' );
  var ic = $( 'imgContainer' );
  var images = $A(old_images);
  
  images.each( function(img) 
//  for( var i in images )
  {
    //var img = images[i];


var imgObj = document.createElement( 'img' );
    imgObj.style.position = 'relative';
    imgObj.style.left = '0px';
    imgObj.style.top = '0px';
	imgObj.style.display = 'none';
	var imgLink = document.createElement('a');
	imgLink.href = img.link_page;
	imgLink.appendChild(imgObj);
    ic.appendChild( imgLink );
	

	var captionObj = document.createElement('div');
	captionObj.className = 'caption';
	captionObj.style.display = 'none';
	var capLink = document.createElement('a');
	capLink.href = img.link_page;
	capLink.innerHTML = img.caption;
	captionObj.appendChild(capLink);
	ic.appendChild( captionObj );

    var ii = new ImageInfo( img.src, img.width, img.height, imgObj, captionObj );

    g_slides.push( 
      new Animation( g_animationManager, ii, slide_time,
		[ new myFader( ii, 3 ) ] )
    );
//  }
  });
}

function start_slides()
{
  g_slides[ g_current_slide ].start();
}
