/*
 * 
 * 	@ SiteBakery
 * 	version 1.0 DEV Build 0914
 * 	
 * 	(c) Copyright C. Lahme 2007
 * 	
 * 	File: modules/rotation/js/rotation.js
 * 	C-Flags: 
 * 	
/*--------------------------------------------------------------------------*/

var ImageRotation = Class.create();

ImageRotation.prototype = {

	initialize: function(myUrl) {
		
		this.imageArray = new Array();
		this.imageDir = '';
		this.imageId = -1;
		this.delay = 1;
		this.duration = 2;
		this.period = 7;
		this.selectedFilter = -1;
		
		this.request = new Ajax.Request(myUrl, {
			onComplete: function(transport) {
				if (200 != transport.status) 
					alert(transport.status);

				try {
					this.myEval = transport.responseText.evalJSON(true);
				} catch(e) {
					alert(e);
					return;
				}
				
				rotation.imageArray = this.myEval.imageArray;
				rotation.filters = this.myEval.filters;
				rotation.imageDir = this.myEval.imageDir;
				rotation.start();
	
			}	
		});
		
	},
	
	prepare: function(backId, frameId, frontId) {
		
		this.divback = $(backId);
		this.divback.innerHTML = '';
		
		this.divfront = document.createElement('div');
		this.divfront.setAttribute('id',frontId);
		this.divfront.innerHTML = '';
		
		this.divframe = document.createElement('div');
		this.divframe.setAttribute('id',frameId);
		this.divframe.innerHTML = '';
		
		this.divframe.appendChild(this.divfront);
		this.divback.appendChild(this.divframe);
		
		this.divfront = $(frontId);
		this.divfront.toggle();
		
		this.divframe = $(frameId);
		this.divframe.toggle();
		
	},
	
	start: function() {
		
		this.event = new PeriodicalExecuter(this.nextItem.bind(this), this.period);
		this.nextItem();
		
	},
	
	nextItem: function() {
		
		if(this.imageArray.length == 0)
			return;

		this.selectedFilter++;
		if(this.selectedFilter >= this.filters.length)
			this.selectedFilter = 0;

		this.filter(this.filters[this.selectedFilter]);
		
	},
	
	filter: function(filterType) {
		
		if(this.divfront.visible() && this.divframe.visible()) {	
		
			switch(filterType) {
				case "Appear":
				case "BlindDown":
				case "BlindToRight":
				case "RollDown":
				case "SlideDown":
					this.divback.style.background = 'url(' + this.imageDir + this.imageArray[this.imageId] + ')';
					this.divframe.hide();
					this.divfront.hide();
					break;
												
				case "Fade":
					new Effect.Fade(this.divframe.id, {queue: 'front', delay: this.delay, duration: this.duration});
					new Effect.Fade(this.divfront.id, {delay: this.delay, duration: this.duration});
					break;
					
				default:
				
			}
		
		} else {
			
			switch(filterType) {
				case "BlindFromRight":
				case "BlindUp":
				case "SlideUp":
				case "RollUp":
				case "SwitchOff":
				case "DropOut":
				case "Fold":
					this.divfront.style.background = 'url(' + this.imageDir + this.imageArray[this.imageId] + ')';				
					this.divfront.show();
					this.divframe.show();
					break;
					
			}
			
		}
		
		this.imageId++;
		if(this.imageId >= this.imageArray.length)
			this.imageId=0;
				
		switch(filterType) {
			case "Appear":
				this.divfront.style.background = 'url(' + this.imageDir + this.imageArray[this.imageId] + ')';
				this.divfront.show();
				new Effect.Appear(this.divframe.id, {delay: this.delay, duration: this.duration});
				break;

			case "Fade":
				this.divfront.style.background = 'url(' + this.imageDir + this.imageArray[this.imageId] + ')';
				this.divfront.show();
				new Effect.Appear(this.divframe.id, {delay: this.delay+this.duration, duration: this.duration});
				break;

			case "BlindDown":
				this.divfront.style.background = 'url(' + this.imageDir + this.imageArray[this.imageId] + ')';
				this.divfront.show();
				new Effect.BlindDown(this.divframe.id, {delay: this.delay, duration: this.duration*1.2});
				break;
			
			case "BlindFromRight":
				this.divback.style.background = 'url(' + this.imageDir + this.imageArray[this.imageId] + ')';
				new Effect.BlindFromRight(this.divframe.id, {delay: this.delay, duration: this.duration});
				new Effect.Fade(this.divframe.id, {delay: this.delay, duration: this.duration*1.2});
				break;
			
			case "BlindToRight":
				this.divfront.style.background = 'url(' + this.imageDir + this.imageArray[this.imageId] + ')';
				this.divfront.show();
				new Effect.BlindToRight(this.divframe.id, {queue: 'front', delay: this.delay, duration: this.duration*1.2});
				new Effect.Appear(this.divframe.id, {delay: this.delay, duration: this.duration*1.2});
				break;
			
			case "BlindUp":
				this.divback.style.background = 'url(' + this.imageDir + this.imageArray[this.imageId] + ')';
				new Effect.BlindUp(this.divframe.id, {delay: this.delay, duration: this.duration});
				break;
				
			case "RollDown":
				this.divfront.style.background = 'url(' + this.imageDir + this.imageArray[this.imageId] + ')';
				new Effect.BlindDown(this.divframe.id, {queue: 'front', delay: this.delay - 0.5, duration: this.duration - this.delay});			
				new Effect.Appear(this.divfront.id, {delay: this.delay, duration: this.duration*1.2});
				break;
			
			case "RollUp":
				this.divback.style.background = 'url(' + this.imageDir + this.imageArray[this.imageId] + ')';
				new Effect.BlindUp(this.divfront.id, {queue: 'front', delay: this.delay, duration: this.duration});
				new Effect.Fade(this.divframe.id, {delay: this.delay + 0.5, duration: this.duration});
				new Effect.BlindUp(this.divframe.id, {delay: this.delay + 0.5, duration: this.duration - 0.5});
				break;
				
			case "SlideDown":
				this.divfront.style.background = 'url(' + this.imageDir + this.imageArray[this.imageId] + ')';
				this.divfront.show();
				new Effect.SlideDown(this.divframe.id, {delay: this.delay, duration: this.duration});
				break;
			
			case "SlideUp":
				this.divback.style.background = 'url(' + this.imageDir + this.imageArray[this.imageId] + ')';
				new Effect.SlideUp(this.divframe.id, {delay: this.delay, duration: this.duration});
				break;
				
			case "SwitchOff":
				this.divback.style.background = 'url(' + this.imageDir + this.imageArray[this.imageId] + ')';
				new Effect.SwitchOff(this.divframe.id, {delay: this.delay, duration: this.duration});
				break;
				
			case "DropOut":
				this.divback.style.background = 'url(' + this.imageDir + this.imageArray[this.imageId] + ')';
				new Effect.DropOut(this.divframe.id, {delay: this.delay, duration: this.duration});
				break;
				
			case "Fold":
				this.divback.style.background = 'url(' + this.imageDir + this.imageArray[this.imageId] + ')';
				new Effect.Fold(this.divframe.id, {delay: this.delay, duration: this.duration});
				break;
			
			
		}
		
	}
	
}

Effect.BlindToRight = function(element) {
  element = $(element);
  var oldWidth = Element.getStyle(element, 'width');
  var elementDimensions = Element.getDimensions(element);
  return new Effect.Scale(element, 100, 
    Object.extend({ scaleContent: false, 
      scaleY: false,
      scaleFrom: 0,
      scaleMode: {originalHeight: elementDimensions.height, originalWidth: elementDimensions.width},
      restoreAfterFinish: true,
      afterSetup: function(effect) { with(Element) {
        makeClipping(effect.element);
        setStyle(effect.element, {width: '0px'});
        show(effect.element); 
      }},  
      afterFinishInternal: function(effect) { with(Element) {
        undoClipping(effect.element);
        setStyle(effect.element, {width: oldWidth});
      }}
    }, arguments[1] || {})
  );
}

Effect.BlindFromRight = function(element) {
  element = $(element);
  Element.makeClipping(element);
  return new Effect.Scale(element, 0, 
    Object.extend({ scaleContent: false, 
      scaleY: false, 
      restoreAfterFinish: true,
      afterFinishInternal: function(effect) { with(Element) {
        [hide, undoClipping].call(effect.element); }} 
    }, arguments[1] || {})
  );
}