function slideHorizontal(){
    this.link = new Array();
    this.box = new Array();
    this.time_out = null;
    this.pos_actual = null;
    this.pos_alcanzar = null;
    this.start = null;
    this.pinchado_inno = false;
    this.pinchado_clie = false;
    this.pinchado_cali = false;
	this.pinchado_est = false;
    var _this = this;

    this.anyadirEvento = function(elemento,evento,funcion){
	if(elemento.addEventListener)return elemento.addEventListener(evento,funcion,false);
        if(elemento.attachEvent)return elemento.attachEvent("on"+evento,funcion);
    }
    this.eliminarEvento = function(evento){
        if(window.event){
            window.event.cancelBubble=true;
            window.event.returnValue=false;
            return
        }
        if(evento.preventDefault&&evento.stopPropagation){
            evento.preventDefault();
            evento.stopPropagation();
        }
    }
    this.scrollHorizontal = function(){
        body = document.body;
        var docu = document.documentElement;
        if(body && body.scrollLeft){
            return body.scrollLeft;
        }
        if(docu && docu.scrollLeft){
            return docu.scrollLeft;
        }
        if(window.pageXOffset){
            return window.pageXOffset;
        }
        return 0
    }
    this.moverScroll = function(){
        _this.time_out = setTimeout(function(){
            if(!_this.nextStep()){
                clearTimeout(_this.time_out);
                _this.rellamada(1);
                return;
            }
            _this.rellamada(_this.nextStep());
            _this.moverScroll();
        },13)
    }
    this.init = function(){_this.anyadirEvento(window,"load",_this.load)}
    this.load = function(){
        _this.eliminarEvento(this);
        var menu = document.getElementById('menu').getElementsByTagName('a');
        _this.link.push(document.getElementById('sub_menu').getElementsByTagName('a').item(4));
        for(var i = 0; i < menu.length; i++){
            if(menu[i].href.match(/#trabajos/)){
                _this.link.push(menu[i]);
                _this.link.push(document.getElementById('sub_menu').getElementsByTagName('a').item(2));
            }
            else{
                _this.link.push(menu[i]);
            }
        }
        var new_comm = document.getElementById('new-comunication');
        _this.link.push(new_comm);
        var caja = document.getElementById('wrapper').childNodes;
        for(i = 0; i < caja.length; i++){
            if(caja[i].id != undefined && caja[i].id != ""){
                _this.box.push(caja[i]);
            }
        }
        for(i = 0; i < _this.link.length; i++){
            _this.link[i].indice = i;
            _this.anyadirEvento(_this.link[i], "click", _this.eliminarEvento);
            _this.link[i].onclick = function(){
                _this.eliminarEvento(this);
                if(this.href.match(/#innovacion/)){
                    if(!_this.pinchado_inno){
                        var slide1 = new slideTransparencia('slide_innovacion');
                        slide1.load();
                        slide1 = null;
                        _this.pinchado_inno = true;
                    }
                }
                if(this.href.match(/#clientes/)){
                    if(!_this.pinchado_clie){
                        var slide2 = new slideTransparencia('slide_clientes');
                        slide2.load();
                        slide2 = null;
                        _this.pinchado_clie = true;
                    }
                }
                if(this.href.match(/#calidad/)){
                    if(!_this.pinchado_cali){
                        var slide3 = new slideTransparencia('slide_calidad');
                        slide3.load();
                        slide3 = null;
                        _this.pinchado_cali = true;
                    }
                }
		if(this.href.match(/#estudio/)){
                    if(!_this.pinchado_est){
                        var slide4 = new slideTransparencia('slide_estudio');
                        slide4.load();
                        slide4 = null;
                        _this.pinchado_est = true;
                    }
                }
                if (this.indice == 9) { // Nuevo comunicación == rehabilitacion
                    this.indice = 6;
                }
                clearTimeout(_this.time_out);
                _this.pos_actual = _this.scrollHorizontal();
                _this.pos_alcanzar = _this.box[this.indice].offsetLeft - 125;
                _this.start = new Date().getTime();
                _this.moverScroll();
            }
        }
    }
    this.rellamada = function(p){
        var mov_scroll = _this.pos_actual + ((_this.pos_alcanzar - _this.pos_actual)*p);
        window.scrollTo(mov_scroll, 0);
    }
    this.nextStep = function(){
        var now = new Date().getTime();
        if((now - _this.start)>1000){
            return false;
        }
        return _this.easing((now - _this.start+.001)/1000);
    }
    this.easing = function(p){
        return (Math.pow((p-1), 3) +1);
    }
}
var slide_horizontal = new slideHorizontal();
slide_horizontal.init();
slide_horizontal = null;
