/**************************************************************
	Script		  : SlideShow
	Version		  : 1.3 (CS)
	Authors		  : Samuel Birch
	Website	     : http://www.phatfusion.net/slideshow/
	Licence		  : Open Source MIT Licence
	Modifications : Claude Schneider
**************************************************************/

var SlideShow = new Class({

   //these are only defaults, over-ridden by any options when creating object 
   getOptions: function(){
		return {
			effect: 'fade', //fade|wipe|slide|random
			duration: 1000,
			fade_gap: 100,
			transition: Fx.Transitions.linear,
			//transition: Fx.Transitions.Quad.easeOut,
			direction: 'right', //top|right|bottom|left|random
			color: false,
			wait: 5000,
			loop: false,
			thumbnailCls: 'outline',
			loadingCls: 'loading',
			onClick: false
		};
	},

	initialize: function(container, images, options)
   {
		this.setOptions(this.getOptions(), options);

		this.container = $(container);
		// CS-removed: setStyles({ position: 'relative',
      this.container.setStyles({
         position: 'relative',
			overflow: 'hidden'
		});
		if(this.options.onClick){
			this.container.addEvent('click', function(){
				this.options.onClick(this.imageLoaded);
			}.bind(this));
		}

		this.imagesHolder = new Element('div').setStyles({
			position: 'absolute',
			overflow: 'hidden',
			top: this.container.getStyle('height'),
			left: 0,
			width: '0px',
			height: '0px',
			display: 'inline'
		}).inject(this.container); //CS was display none

      if($type(images) == 'string')
      {
			var imageList = [];
			$$('.'+images).each(function(el)
         {
            imageList.push(el.href);
				el.inject(this.imagesHolder);
			},this);
			this.images = imageList;
		}
      else
      {
			this.images = images;
		}
		this.images_requested = new Array();
		first_image_loaded = false;

		this.oldImage = new Element('div').setStyles({
			position: 'absolute',
			overflow: 'hidden',
			top: 0,
			left: 0,
			opacity: 0,
			width: this.container.getStyle('width'),
			height: this.container.getStyle('height')
		}).inject(this.container);

		this.newImage = this.oldImage.clone();
		this.newImage.inject(this.container);

		this.timer = 0;
		this.image = -1;
		this.imageLoaded = 0;
		this.stopped = true;
		this.started = false;
		this.animating = false;
	},

   preload_images: function(i,preloading_sub_page)
   {
      if(i == null)   i = 0;
      if(this.images_requested[i] == null)
      {
         this.images_requested[i] = true;
         preload_img = new Asset.image(this.images[i],
         {
            id: this.images[i],
            onload: function()
            {
               if(this.container.get('id') == "home_slideshow")
               {
                  //update_home_pb();
               }
               if(preloading_sub_page == new_sub_page)
               {
                  if(i == 0)
                  {
                     //loading_div.setStyle('display','none');
                  }
                  var newimg = new Element('img',{'src':this.images[i],'id':this.images[i]});
                  //top_margin = (newimg.height/2).toInt();
                  /*if(top_margin < 240)
                  {
                     newimg.setStyle('margin-top',240-top_margin);
                  }*/
                  newimg.inject(this.imagesHolder);
                  if(i < (this.images.length-1))
                  {
                     this.preload_images(i+1,preloading_sub_page);
                  }
               }
            }.bind(this)
         });
      }
      else
      {
         if(i < (this.images.length-1))
         {
            this.preload_images(i+1,preloading_sub_page);
         }
      }
   },

   load: function(){
		$clear(this.timer);
		//CS-removed this.loading.setStyle('display','block');
		this.image++;
		var img = this.images[this.image];
		delete this.imageObj;

      doLoad = true;

      this.imagesHolder.getElements('img').each(function(el)
      {
         var src = this.images[this.image];
			if(el.src.contains(src))
         {
            this.imageObj = el;
				doLoad = false;
				this.add = false;
            this.show();
			}
		},this);

		if(doLoad)
      {
         if(this.images_requested[this.image] == null)
         {
            this.images_requested[this.image] = true;
            this.add = true;
            this.imageObj = new Asset.image(img,
            {
               id: img, onload: function()
               {
                  this.show();
                  //loading_div.setStyle('display','none');
               }.bind(this)
            });
   		}
   		else
   		{
            this.image--;
            this.load.delay(500,this);
   		}
		}
	},

	show: function(add)
   {
      if(this.add)
      {
			//CS-replaced was: this.imageObj.injectInside(this.imagesHolder);
			//top_margin = (this.imageObj.height/2).toInt();
         /*if(top_margin < 240)
         {
            this.imageObj.setStyle('margin-top',240-top_margin);
         }*/
         this.imageObj.inject(this.imagesHolder);
		}

		this.newImage.setStyles({
			zIndex: 1,
			opacity: 0
		});
		var img = this.newImage.getElement('img');
      if(img)
      {
         //CS-replaced: img.replaceWith(this.imageObj.clone());
         this.imageObj.clone().replaces(img);
		}
      else
      {
			var obj = this.imageObj.clone();
			//CS-replaced: obj.injectInside(this.newImage);
			obj.inject(this.newImage);
		}
		this.imageLoaded = this.image;
		//CS-removed: this.loading.setStyle('display','none');
		
		//CS this.effect();
		this.fade();
	},

	wait: function(){
      this.timer = this.load.delay(this.options.wait,this);
	},

	play: function(num){
		if(this.stopped){
			if(num > -1){this.image = num-1};
			if(this.image < this.images.length){
				this.stopped = false;
				if(this.started){
					this.next();
				}else{

               this.load();
				}
				this.started = true;
			}
		}
	},

	stop: function(){
		$clear(this.timer);
		this.stopped = true;
	},

	next: function(wait)
   {
   //TODO - allow user to skip quickly while still animating
		var doNext = true;
		if(wait && this.stopped){
			doNext = false;
		}
		if(this.animating){
			//doNext = false;
		}
		if(doNext){
			this.cloneImage();
			$clear(this.timer);
			if(this.image < this.images.length-1){
				if(wait){
               this.wait();
				}else{
					this.load();
				}
			}else{
				if(this.options.loop){
					this.image = -1;
					if(wait){
						this.wait();
					}else{
						this.load();
					}
				}else{
					this.stopped = true;
				}
			}
		}
	},

	previous: function(){
		if(this.imageLoaded == 0){
			this.image = this.images.length-2;
		}else{
			this.image = this.imageLoaded-2;
		}
		this.next();
	},

	cloneImage: function(){
		var img = this.oldImage.getElement('img');
		if(img){
			//CS img.replaceWith(this.imageObj.clone());
			this.imageObj.clone().replaces(img);
		}else{
			var obj = this.imageObj.clone();
			obj.inject(this.oldImage);
		}

		this.oldImage.setStyles({
			zIndex: 0,
			top: 0,
			left: 0,
			opacity: 1
		});

		this.newImage.setStyles({opacity:0});
	},

	/*CS effect: function(){
		this.animating = true;

      this.effectObj = this.newImage.effects({
			duration: this.options.duration,
			transition: this.options.transition
		});
		this.fade();
	},*/

	setup: function(dir){
		if(dir == 'top'){
			this.top = -this.container.getStyle('height').toInt();
			this.left = 0;
			this.topOut = this.container.getStyle('height').toInt();
			this.leftOut = 0;

		}else if(dir == 'right'){
			this.top = 0;
			this.left = this.container.getStyle('width').toInt();
			this.topOut = 0;
			this.leftOut = -this.container.getStyle('width').toInt();

		}else if(dir == 'bottom'){
			this.top = this.container.getStyle('height').toInt();
			this.left = 0;
			this.topOut = -this.container.getStyle('height').toInt();
			this.leftOut = 0;

		}else if(dir == 'left'){
			this.top = 0;
			this.left = -this.container.getStyle('width').toInt();
			this.topOut = 0;
			this.leftOut = this.container.getStyle('width').toInt();

		}else{
			this.top = 0;
			this.left = 0;
			this.topOut = 0;
			this.leftOut = 0;
		}
	},

	/*fade: function(){
		this.effectObj = new Fx.Morph(this.newImage,{
         duration: this.options.duration,
         transition: this.options.transition
         });
      this.effectObj.start({
			opacity: [0,1]
		});
		this.resetAnimation.delay(this.options.duration+90,this);
		if(!this.stopped){
		this.next.delay(this.options.duration+100,this,true);
		}
	},*/

	fade: function()
   {
      this.animating = true;

		this.effectObj2 = new Fx.Morph(this.oldImage,{
         //duration: this.options.duration,
         duration: this.options.duration,
         transition: this.options.transition
         //transition: Fx.Transitions.Quint.easeOut
         });
      this.effectObj2.start({
			opacity: [1,0]
		});

      this.effectObj = new Fx.Morph(this.newImage,{
         //duration: this.options.duration,
         duration: this.options.duration,
         transition: this.options.transition
         //transition: Fx.Transitions.Quint.easeOut
         });

      /*this.effectObj.start({
			opacity: [0,1]
		});*/
		(function(){this.effectObj.start({opacity: [0,1]})}.bind(this)).delay(this.options.fade_gap);

		//this.resetAnimation.delay(this.options.duration+90,this);
		this.resetAnimation.delay(this.options.duration+this.options.fade_gap+90,this);
		if(!this.stopped)
      {
   		//this.next.delay(this.options.duration+100,this,true);
         this.next.delay(this.options.duration+this.options.fade_gap+100,this,true);
		}
	},

	resetAnimation: function()
   {
		this.animating = false;
	}

});
SlideShow.implement(new Options, new Events);
//SlideShow.implement(new Events);


