/* 
 * A workaround for IE issues in mootools 1.2.1
 * - Recreates FX.Scroll() but utilises 1.2.0's getPosition/getOffset routines.
 */

Fx.Scroll2 = new Class({
 
    'Extends': Fx.Scroll,
    'styleString': Element.getComputedStyle,
    'styleNumber': function(element, style) {
        return this.styleString(element, style).toInt() || 0;
    },
    'borderBox': function(element) {
        return this.styleString(element, '-moz-box-sizing') == 'border-box';
    },
    'topBorder': function(element) {
        return this.styleNumber(element, 'border-top-width');
    },
    'leftBorder': function(element) {
        return this.styleNumber(element, 'border-left-width');
    },
    'isBody': function(element) {
        return (/^(?:body|html)$/i).test(element.tagName);
    }, 
    'toElement': function(el) {
        var offset   = {x: 0, y: 0};
        var element  = $(el);
       
        if (this.isBody(element)) {
            return offset;
        }
        var scroll = element.getScrolls();
        while (element && !this.isBody(element)){
            offset.x += element.offsetLeft;
            offset.y += element.offsetTop;
           
            if (Browser.Engine.gecko){
                if (!this.borderBox(element)){
                    offset.x += this.leftBorder(element);
                    offset.y += this.topBorder(element);
                }
                var parent = element.parentNode;
                if (parent && this.styleString(parent, 'overflow') != 'visible'){
                    offset.x += this.leftBorder(parent);
                    offset.y += this.topBorder(parent);
                }
            } else if (Browser.Engine.trident || Browser.Engine.webkit){
                offset.x += this.leftBorder(element);
                offset.y += this.topBorder(element);
			}

            element = element.offsetParent;
            if (Browser.Engine.trident) {
                while (element && !element.currentStyle.hasLayout) {
                    element = element.offsetParent;
                }
            }
        }
        if (Browser.Engine.gecko && !this.borderBox(element)){
            offset.x -= this.leftBorder(element);
            offset.y -= this.topBorder(element);
        }
       
        var relative = this.element;
        var relativePosition = (relative && (relative = $(relative))) ? relative.getPosition() : {x: 0, y: 0};
        var position = {x: offset.x - scroll.x, y: offset.y - scroll.y};
       
        return this.start(position.x - relativePosition.x, position.y - relativePosition.y);
    }
});

var fmcScrollTo = new Class({
	Implements: Options,
	options: {
		container: document.body,
		slides: [],
		startIndex: 0,
		wrap: true,
		duration:1500,
		onShow: Class.empty //Mootools 1.2: $empty
	},
	initialize: function(options){
		//this.options = 
		this.setOptions(options);
		this.startIndex = this.options.startIndex;
		this.scroll = new Fx.Scroll2(this.options.container, {
			wait: false,
			duration: this.options.duration,
			offset: {'x': 0, 'y': 0}
			//transition: Fx.Transitions.Expo.easeOut
		});
	},
	scrollToEl: function(itemToScrollTo)
	{
		this.startIndex = itemToScrollTo;
		this.scroll.toElement(this.options.slides[itemToScrollTo]);
	}
});

var fadeImages = {
	actual:0,
	prev:false,
	on:false,
	init:function(options)
	{
		this.on=true;
		this.options = options;
		this.elms = options.elements;
		this.container = options.container;
		this.elms.each(function(el, index)
		{
			el.setStyles({'position':'absolute', opacity:0});
		});
		this.newImage = this.elms[this.actual]; 
		this.oldImage = this.elms[this.prev];
		this.fadeInOut();
		this.timer = this.fadeInOut.periodical(7000, this);
	}
	, fadeInOut:function()
	{
		this.newImage.set('tween', {duration: 'long'});
		this.newImage.tween('opacity', 1);
		if(this.oldImage)
		{
			this.oldImage.set('tween', {duration: 'long'});
			this.oldImage.fade(0);
		}
		this.prev = this.actual;
		this.actual++;
		if(this.actual ==this.elms.length)
		{
			this.actual = 0;
		}
		this.newImage = this.elms[this.actual];
		this.oldImage = this.elms[this.prev];
	}
};

var MakeScrollbar = new Class({
	Implements: Options,
	content:null,
	ignoreMouse:false,
	id:'',
	top:100,
	scrollbar:null,
	handle:null,
	horizontal:false,
	slider:null,
	offsetX:0,
	initialize:function(options){
		if(options.content) { this.content = options.content; } else { return false; }
		if(options.ignoreMouse) this.ignoreMouse = options.ignoreMouse;
		(options.id) ? this.id = options.id : this.id = 'scrollbar_' + (100 * random());
		if(options.top) this.top = options.top;
		if(options.offsetX) this.offsetX = options.offsetX;
		var that = this;
		this.scrollbar = new Element('div',{id:this.id, 'class':'scrollbar'}).inject(this.content, 'after');
		this.content.setStyles({ overflow:'hidden'});//width:(content.getSize().x-15),
		if(options.pos != 'css') {
			this.scrollbar.setStyles(
			{
				top:this.top,
				left:this.content.getPosition().x+ this.content.getSize().x+this.offsetX,
				height:this.content.getSize().y,
				position:'absolute'
			});
		}
		this.top = new Element('div', {'class':'scr-top'}).inject(this.scrollbar);
		this.bottom = new Element('div', {'class':'scr-bottom'}).inject(this.scrollbar);
		this.handle = new Element('div',{'class':'scr-scrubber'}).inject(this.scrollbar);
		this.steps = this.countSteps();
		this.slider = new Slider(this.scrollbar, this.handle, {	
			steps: this.steps,
			mode: (this.horizontal?'horizontal':'vertical'),
			onChange: function(step){
				// Scrolls the content element in x or y direction.
				var x = (that.horizontal?step:0);
				var y = (that.horizontal?0:step);
				that.content.scrollTo(x,y);
			}
		}).set(0);
		if( !(this.ignoreMouse) ){
			// Scroll the content element when the mousewheel is used within the 
			// content or the scrollbar element.
			$$(this.content, this.scrollbar).addEvent('mousewheel', function(e){
				e = new Event(e).stop();
				var step = that.slider.step - e.wheel * 30;
				that.slider.set(step);
			});
		}
		// Stops the handle dragging process when the mouse leaves the document body.
		//$(document.body).addEvent('mouseleave',function(){that.slider.drag.stop()});
	},
	countSteps:function()
	{
		var nb = (this.horizontal?(this.content.getScrollSize().x - this.content.getSize().x):(this.content.getScrollSize().y - this.content.getSize().y))
		return nb;
	},
	changeSteps:function()
	{
		this.slider.steps = this.countSteps();
		if(this.countSteps() <= 0)
		{
			this.scrollbar.setStyle('visibility','hidden');
		}else{
			this.scrollbar.setStyle('visibility','visible');
		}
	}
});


