// For pices in mecanizados.
var my_glider;
//List of elements
var listOfelements = new Array('1','1_2','1_3','1_4','1_5','1_6','1_7','2', '2_2','2_3',
                        '2_4', '3','4','5','6_1','6_2','6_3', '6_4', '6_5','6_6','6_7', '6_8','6_9',
                        '6_10','6_11','6_12');
//init glider.
function initGlider(my_glider){
    //var my_glider ;
    if(typeof(my_glider)== 'undefined' ){
       my_glider=new Glider('my-glider','../../images/piezas/piezas_','.jpg','centro','imgSection',listOfelements, {duration:0.5});
    }
    return my_glider;
}


Glider = Class.create();
Object.extend(Object.extend(Glider.prototype, Abstract.prototype), {
	initialize: function(wrapper,rootImage,imageType,idImage,classSection,listOfElements, options){
	    this.scrolling  = false;

	    this.wrapper    = $(wrapper);

	    this.scroller   = this.wrapper.down('div.scroller');

	    this.sections   = this.wrapper.getElementsBySelector('div.section');

	    this.options    = Object.extend({ duration: 1.0, frequency: 3 }, options || {});

	    this.sections.each( function(section, index) {
	      section._index = index;
	    });

	    this.events = {
	      click: this.click.bind(this)
	    };
        //For top image
        this.idImage    = $(idImage);
        this.rootImage  = rootImage;
        this.imageType  = imageType;
        this.contador = 0;
        this.lista =    listOfElements;
        //For small images :
        this.classSection = classSection;

        //console.log('this.image :'+idImage);
        this.changeFlags(wrapper+'Left');
        this.changeFlags(wrapper+'Right');
        this.changeFlags(wrapper+'topLeft');
        this.changeFlags(wrapper+'topRight');


	    this.addObservers();


			if(this.options.initialSection) this.moveTo(this.options.initialSection, this.scroller, { duration:this.options.duration });  // initialSection should be the id of the section you want to show up on load
			if(this.options.autoGlide) this.start();

	  },

  changeFlags:function(thisFlag){
  //alert(thisFlag);
  $(thisFlag).observe('mouseover', function(event){
        //$(thisFlag).setStyle({height:'102px',width:'19px',backgroundImage:'\'url(../images/'+thisFlag+'_on.png)\''});
        $(thisFlag).removeClassName(thisFlag);
        $(thisFlag).addClassName(thisFlag+'On');
        //alert(thisFlag+'On');
  });

  $(thisFlag).observe('mouseout', function(event){
        if($(thisFlag).hasClassName(thisFlag+'On')){
            //alert('has On');
           $(thisFlag).removeClassName(thisFlag+'On');
        }
        if($(thisFlag).hasClassName(thisFlag+'Click')){
           $(thisFlag).removeClassName(thisFlag+'Click');
        }
        if($(thisFlag).hasClassName(thisFlag+'Blur')){
           $(thisFlag).removeClassName(thisFlag+'Blur');
        }
        $(thisFlag).addClassName(thisFlag);
        //alert(thisFlag);
  });

  $(thisFlag).observe('blur', function(event){
        $(thisFlag).removeClassName(thisFlag);
        $(thisFlag).addClassName(thisFlag+'Blur');
        //alert(thisFlag+'Blur');
  });

},

  addObservers: function() {
    //console.log('this.image en obser:'+this.idImage.src);
    //console.log('ruta :'+this.rootImage+this.lista[this.contador]+this.imageType);
    var controls = this.wrapper.getElementsBySelector('div.controls a');
    controls.invoke('observe', 'click', this.events.click);
    // For top part:
    this.idImage.src=this.rootImage+this.lista[this.contador]+this.imageType;
    //For load images
    var items = $$('img');
    //console.log('images items:');
    //console.log(items[0]);
    //console.log('classSection:'+this.classSection);
    items.each(function(node) {
        //console.log('node.parentNode.tagName.toUpperCase(): '+node.parentNode.tagName.toUpperCase());
        //console.log('node.className.toUpperCase():'+node.className.toUpperCase());
        if (node.parentNode.tagName.toUpperCase() == 'DIV' && node.className.toUpperCase() =='IMGSECTION' )   {
            //console.log('classname:'+node.id);
            //console.log('classSection'+this.classSection);
            node.observe('click',function(event){
                event = event || window.event;
                var element = event.target || event.srcElement;
	            //console.log(element.id);
                //console.log('src:'+my_glider.rootImage+element.id+my_glider.imageType);
                my_glider.idImage.src=my_glider.rootImage+element.id+my_glider.imageType;
                my_glider.setNewPosition(element.id);

            });
            node.observe('mouseover',function(){
                node.addClassName('onImgSection');
                //console.log('onmouseover:'+node.className);
            });
            node.observe('mouseout',function(){
                node.removeClassName('onImgSection');
                //console.log('onmouseout'+node.className);
            });


        }

    });
  },
  click: function(event) {
		this.stop();
    var element = Event.findElement(event, 'a');
    if (this.scrolling) this.scrolling.cancel();

    this.moveTo(element.href.split("#")[1], this.scroller, { duration:this.options.duration });
    Event.stop(event);
  },

	moveTo: function(element, container, options){
			this.current = $(element);

			Position.prepare();
	    var containerOffset = Position.cumulativeOffset(container),
	     elementOffset = Position.cumulativeOffset($(element));

		  this.scrolling 	= new Effect.SmoothScroll(container,
				{duration:options.duration, x:(elementOffset[0]-containerOffset[0]), y:(elementOffset[1]-containerOffset[1])});
		  return false;
		},

  next: function(){
    //console.log('current:'+this.current);
    if (this.current) {
      var currentIndex = this.current._index;
      //console.log('currentIndex:'+currentIndex);
      var nextIndex = (this.sections.length - 1 == currentIndex) ? 0 : currentIndex + 1;
    } else var nextIndex = 1;
    //console.log('nextIndex:'+nextIndex);
    this.moveTo(this.sections[nextIndex], this.scroller, {
      duration: this.options.duration
    });
  },



  previous: function(){
    if (this.current) {
      var currentIndex = this.current._index;
      var prevIndex = (currentIndex == 0) ? this.sections.length - 1 :
       currentIndex - 1;
    } else var prevIndex = this.sections.length - 1;

    this.moveTo(this.sections[prevIndex], this.scroller, {
      duration: this.options.duration
    });
  },

  nextTop: function(){
        this.contador+=1;
        if (this.contador ==this.lista.length){
            this.contador = this.lista.length-1;
        }else{
            if (this.contador < 0) this.contador = 0;
        }
        this.idImage.src=this.rootImage+this.lista[this.contador]+this.imageType;


  },

  previousTop: function(){
    //console.log('Contador :'+this.contador+':::Lista pos :'+this.lista[this.contador] );
    this.contador-=1;
    if (this.contador < 0) this.contador = 0;

    this.idImage.src=this.rootImage+this.lista[this.contador]+this.imageType;

  },


  clickTop: function(){
    //console.log('Click : Contador :'+this.contador+':::Lista pos :'+this.lista[this.contador] );
    showImageGroup(this.lista[this.contador]);return false;

  },

    setNewPosition: function(pos){
        this.contador = this.lista.indexOf(pos);

    },
	stop: function()
	{
		clearTimeout(this.timer);
	},

	start: function()
	{
		this.periodicallyUpdate();
	},





	periodicallyUpdate: function()
	{
		if (this.timer != null) {
			clearTimeout(this.timer);
			this.next();
		}
		this.timer = setTimeout(this.periodicallyUpdate.bind(this), this.options.frequency*1000);
	}

});

Effect.SmoothScroll = Class.create();
Object.extend(Object.extend(Effect.SmoothScroll.prototype, Effect.Base.prototype), {
  initialize: function(element) {
    this.element = $(element);
    var options = Object.extend({
      x:    0,
      y:    0,
      mode: 'absolute'
    } , arguments[1] || {}  );
    this.start(options);
  },
  setup: function() {
    if (this.options.continuous && !this.element._ext ) {
      this.element.cleanWhitespace();
      this.element._ext=true;
      this.element.appendChild(this.element.firstChild);
    }

    this.originalLeft=this.element.scrollLeft;
    this.originalTop=this.element.scrollTop;

    if(this.options.mode == 'absolute') {
      this.options.x -= this.originalLeft;
      this.options.y -= this.originalTop;
    }
  },
  update: function(position) {
    this.element.scrollLeft = this.options.x * position + this.originalLeft;
    this.element.scrollTop  = this.options.y * position + this.originalTop;
  }
});


function showImageGroup(element){

     switch(element){
        case "1":    return GB_showImageSet(grouppieces1,1);
        case "1_2":  return GB_showImageSet(grouppieces1_2,1);
        case "1_3":  return GB_showImageSet(grouppieces1_3,1);
        case "1_4":  return GB_showImageSet(grouppieces1_4,1);
        case "1_5":  return GB_showImageSet(grouppieces1_5,1);
        case "1_6":  return GB_showImageSet(grouppieces1_6,1);
        case "1_7":  return GB_showImageSet(grouppieces1_7,1);
        case "2":    return GB_showImageSet(grouppieces2,1);
        case "2_2":  return GB_showImageSet(grouppieces2_2,1);
        case "2_3":  return GB_showImageSet(grouppieces2_3,1);
        case "2_4":  return GB_showImageSet(grouppieces2_4,1);
        case "3":  return GB_showImageSet(grouppieces3,1);
        case "4":  return GB_showImageSet(grouppieces4,1);
        case "5":  return GB_showImageSet(grouppieces5,1);
        case "6_1":  return GB_showImageSet(grouppieces6_1,1);
        case "6_2":  return GB_showImageSet(grouppieces6_2,1);
        case "6_3":  return GB_showImageSet(grouppieces6_3,1);
        case "6_4":  return GB_showImageSet(grouppieces6_4,1);
        case "6_5":  return GB_showImageSet(grouppieces6_5,1);
        case "6_6":  return GB_showImageSet(grouppieces6_6,1);
        case "6_7":  return GB_showImageSet(grouppieces6_7,1);
        case "6_8":  return GB_showImageSet(grouppieces6_8,1);
        case "6_9":  return GB_showImageSet(grouppieces6_9,1);
        case "6_10":  return GB_showImageSet(grouppieces6_10,1);
        case "6_11":  return GB_showImageSet(grouppieces6_11,1);
        case "6_12":  return GB_showImageSet(grouppieces6_12,1);

     }

};
