/*******************
* authour: Patrick patrick@avalade.com
* version: 1.10 calculation of width and height trigger only after the image is loaded
               (in order to make sure the width and height are correct)
********************/
var tSlide = new Class({
	Implements:[Options],
	options:{className: '.sblock', duration: 500, elPosition: 'left', opacity: 0.85},
	initialize:function(A){
		this.setOptions(A);
		this.slides = $$(this.options.className);
		this.totalSlides = this.slides.length;
		this.imagesLoaded = 0;
		
		for(var i = 0; i < this.totalSlides; i++)
		{
			if(this.slides[i].getChildren().length == 2)
			{
				this.slides[i].getChildren()[1].setStyle('display', 'none');
				this.slides[i].setStyles({'visibility': 'hidden'});
			}
		}
		
		if(this.totalSlides > 0)
		{
			var imgSetUp = (function(el){
				el.setup();
			}).pass(this);
			//this.setup();
			if(Browser.Engine.trident) // IE (sometimes cannot trigger window load event)
			{
				var _timer = setInterval(function() {    
					if (/loaded|complete/.test(document.readyState)) {      
						clearInterval(_timer);      
						imgSetUp();
				    }  
				}, 10);
			}
			else
			{
				window.addEvent('load', function(){
					imgSetUp();
				});
			}
		}
	},
	
	setup: function(){
		for(var i = 0; i < this.totalSlides; i++)
		{
			if(this.slides[i].getChildren().length == 2)
			{	
				var childrenCount = this.slides[i].getChildren().length;

				this.slides[i].setStyles({'visibility': 'visible', position: 'relative', overflow: 'hidden', width: (this.slides[i].getChildren()[0].getSize().x == 0 ? 'auto' : this.slides[i].getChildren()[0].getSize().x+'px'), height: (this.slides[i].getChildren()[0].getSize().y == 0 ? 'auto' : this.slides[i].getChildren()[0].getSize().y+'px')});
					
				if(this.options.elPosition == 'left' || this.options.elPosition == 'right')
				{
					var fromValue = (this.options.elPosition == 'left' ? -this.slides[i].getSize().x : this.slides[i].getSize().x);
					this.slides[i].getChildren()[1].setStyles({position: 'absolute', top: 0, left: fromValue, display: 'block', opacity: this.options.opacity, 'display': 'block'});
				}
				else if(this.options.elPosition == 'top' || this.options.elPosition == 'bottom')
				{
					var fromValue = (this.options.elPosition == 'top' ? -this.slides[i].getSize().y : this.slides[i].getSize().y);
					this.slides[i].getChildren()[1].setStyles({position: 'absolute', top: fromValue, left: 0, display: 'block', opacity: this.options.opacity, 'display': 'block'});
				}
				
				//var mouseOverBlock = new Element('div', {'name': 'mvOver', styles: {width: this.slides[i].getSize().x+'px', height: this.slides[i].getSize().y == 0 ? ('auto' : this.slides[i].getSize().y+'px'), position: 'absolute', top: '0', left: '0', 'z-index': '99', display: 'block', 'opacity': '0.0001', 'background-color': '#FFF'}});

				var toValue = ((this.options.elPosition == 'left' || this.options.elPosition == 'right') ? this.slides[i].getSize().x - this.slides[i].getChildren()[1].getSize().x : this.slides[i].getSize().y - this.slides[i].getChildren()[1].getSize().y );
				
				if(toValue < 0)
					toValue = 0;
								
				this.slides[i].toggleSliding = {
					flag: 0,
					from: fromValue,
					to: toValue,
					pos: (this.options.elPosition == 'left' || this.options.elPosition == 'right') ? 'left' : 'top',
					fn: new Fx.Tween(this.slides[i].getChildren()[1], {
						duration: this.options.duration
					})
				};
				
				this.slides[i].addEvent('mouseover', this.toggleSlide.pass('over', this.slides[i]));
				this.slides[i].addEvent('mouseleave', this.toggleSlide.pass('leave', this.slides[i]));
			}
		}
	},
	toggleSlide: function(type){
		if(type == 'over' && this.toggleSliding.flag == 0){
			this.toggleSliding.fn.slideIn(this.toggleSliding.pos, this.toggleSliding.to);
			this.toggleSliding.flag = 1;
		}else if(type != 'over' && this.toggleSliding.flag == 1){
			this.toggleSliding.fn.slideOut(this.toggleSliding.pos, this.toggleSliding.from);
			this.toggleSliding.flag = 0;
		}
	}
});

Element.implement({
	toggleSliding: {}
});

Fx.Tween.implement({
	slideIn: function(p, to){
		this.cancel();
		this.start(p, to);
	},
	slideOut: function(p, from){
		this.cancel();
		this.start(p, from);
	}
});
