/*
(function(){

var read = function(option, element){
	return (option) ? ($type(option) == 'function' ? option(element) : element.get(option)) : '';
};
*/
this.slideGallery2 = new Class({

    options: {
      prefix: 'slider',
      sliderGallery: null,
      sliderMask: null,
      slider: null,
      sliderLeft: null,
      sliderRight: null,
      sliderPager: null,
      sliderPagerSelector:'span',
      sliderInfo: null,
      sliderObjects: null,
      orientation: 'h',
      positionParm: null,
      ObjectsSizes: new Array(),
      fxTween: null,
      fxOptions: {},
      currentObject: 0,
      objectSelector: 'a',
      objectsCount: 0,
      objectsPerSlide: 1,
      objectsPerMask: 5,
      infoPattern: '',
      loop: false,
      infiniteLoop: false,
      periodical: 0,
      timer: null,
      autoPlayDirection: 'right',
      sliderWidth: 0
      },

  	initialize: function(prefix,options){
  		if ( prefix ){
        this.options.prefix = prefix;
        }
  	  if ( options ){
         for ( var key in options ) {
      		this.options[key] = options[key];
      		}
        }

      if ( !this.options.sliderGallery ) this.options.sliderGallery = $(this.options.prefix+'_gallery');
    	if ( !this.options.sliderGallery ) return;
      if ( !this.options.sliderMask ) this.options.sliderMask = $(this.options.prefix+'_mask');
    	if ( !this.options.slider ) this.options.slider = $(this.options.prefix);
    	if ( !this.options.sliderLeft ) this.options.sliderLeft = $(this.options.prefix+'_left');
    	if ( !this.options.sliderRight ) this.options.sliderRight = $(this.options.prefix+'_right');
    	if ( !this.options.sliderPager ) this.options.sliderPager = $(this.options.prefix+'_pager');
      if ( !this.options.fxTween ) this.options.fxTween = new Fx.Tween(this.options.slider,this.options.fxOptions);
      if ( !this.options.positionParm ) this.options.positionParm = ( this.options.orientation == 'v' ) ? 'marginTop' : 'marginLeft' ;
      if ( !this.options.sliderInfo ) this.options.sliderInfo = $(this.options.prefix+'_info');
      if ( this.options.objectsPerSlide > this.options.objectsPerMask ) this.options.objectsPerSlide = this.options.objectsPerMask;
      
      this.calculateObjectSizes();
      
      if ( this.options.infiniteLoop && ( ( this.options.objectsPerMask + this.options.objectsPerSlide ) > this.options.sliderObjects.length || this.options.sliderPager ) ){
        this.options.infiniteLoop = false;
        }
      if ( this.options.infiniteLoop ){
        this.options.fxTween.addEvent('complete',this.infiniteLoopRight.bind(this));  
        this.options.loop = true;
        }
      
      this.options.slider.setStyle(((this.options.orientation=='v')?'height':'width'),this.options.sliderWidth+'px');
      if ( this.options.sliderLeft ){
        this.options.sliderLeft.addEvent('click',this.slideLeft.bind(this));
        }
      if ( this.options.sliderRight ){
        this.options.sliderRight.addEvent('click',this.slideRight.bind(this));
        }

      if ( this.options.sliderPager ){
        var links = this.options.sliderPager.getElements( this.options.sliderPagerSelector );
        if ( links ){
          for ( var i = 0 ; i < links.length ; i++ ){
            links[i].addEvent('click',this.slideTo.bind(this,[i]));
            }
          }
        }

      if ( this.options.periodical ){
        this.options.loop = true;
        this.startAutoplay();
        this.options.sliderGallery.addEvents({'mouseleave':this.startAutoplay.bind(this),'mouseenter':this.stopAutoplay.bind(this)});
        }

      this.hideLinks();
      this.parseInfo();
      },

    calculateObjectSizes: function(){
      if ( !this.options.sliderObjects ){
        this.options.sliderObjects = this.options.slider.getElements(this.options.objectSelector);
        }
      if ( this.options.sliderObjects ){
        this.options.sliderWidth = 0;
        this.options.objectsCount = this.options.sliderObjects.length;
        for ( i = 0 ; i < this.options.objectsCount ; i++ ){          
          var objectSize = this.options.sliderObjects[i].retrieve('objectSize');
          if ( !objectSize ){
            objectSize = this.sliderWidth( this.options.sliderObjects[i] );
            this.options.sliderObjects[i].store('objectSize',objectSize);
            }
          this.options.ObjectsSizes[i] = objectSize;
          this.options.sliderWidth += this.options.ObjectsSizes[i];
          }
        }
      },   

    sliderWidth: function(el){
      var size = el.getSize();
     	if ( this.options.orientation == 'v' ){
     		var styles = el.getStyles('marginTop','marginBottom','paddingTop','paddingBottom','borderTopWidth','borderBottomWidth');
     		sliderWidth = size.y.toInt() + styles.marginTop.toInt() + styles.marginBottom.toInt();
     		}
      else {
      	var styles = el.getStyles('marginLeft','marginRight','paddingLeft','paddingRight','borderLeftWidth','borderRightWidth');
	      sliderWidth = size.x.toInt() + styles.marginLeft.toInt() + styles.marginRight.toInt();
      	}
      return sliderWidth;
      },

    slideLeft: function(){
      if ( !this.options.fxTween.check() ) return false;
      if (  this.options.infiniteLoop ) this.infiniteLoopLeft();
      
      var slideStep = 0;

      for ( var i = 0 ; i < this.options.objectsPerSlide && this.options.currentObject > 0 ; i++ ){
        if ( this.options.currentObject > 0 ){
          slideStep += this.options.ObjectsSizes[this.options.currentObject];
          this.options.currentObject--;
          }
        }

      var currentMargin = this.options.slider.getStyle(this.options.positionParm).toInt();

      if ( slideStep ){
        this.options.fxTween.start(this.options.positionParm,currentMargin+'px',(currentMargin+slideStep)+'px');
        }
			else if ( this.options.loop ){
				this.options.currentObject = ( this.options.objectsCount >= this.options.objectsPerMask ) ? this.options.objectsCount - this.options.objectsPerMask : this.options.objectsCount - 1;
				var slideStep = 0;
				for(var i = 0; i < this.options.currentObject; i++ ){
					slideStep += this.options.ObjectsSizes[i];
					}
				this.options.fxTween.start(this.options.positionParm,currentMargin+'px',(currentMargin-slideStep)+'px');
				}

      this.hideLinks();
      this.parseInfo();
      },

    slideRight: function(){
      if ( !this.options.fxTween.check() ) return false;

      var slideStep = 0;
      for ( var i = 0 ; i < this.options.objectsPerSlide && this.options.currentObject < this.options.objectsCount ; i++ ){
        if ( this.options.currentObject < this.options.objectsCount - this.options.objectsPerMask ){
          slideStep += this.options.ObjectsSizes[this.options.currentObject];
          this.options.currentObject++;
          }
        }

      var currentMargin = this.options.slider.getStyle(this.options.positionParm).toInt();

      if ( slideStep ){
        this.options.fxTween.start(this.options.positionParm,currentMargin+'px',(currentMargin-slideStep)+'px');
        }
      else if ( this.options.loop ){
        this.options.currentObject = 0;
        this.options.fxTween.start(this.options.positionParm,currentMargin+'px','0px');
        }
      
      this.hideLinks();
      this.parseInfo();
      },

      slideTo: function(item){
        if ( !this.options.fxTween.check() ) return false;

        var newMargin = 0;

        for ( var i = 0 ; i < item ; i++ ){
          newMargin -= this.options.ObjectsSizes[i];
          }

        this.options.currentObject = item;

        var currentMargin = this.options.slider.getStyle(this.options.positionParm).toInt();
        if ( newMargin != currentMargin ){
          this.options.fxTween.start(this.options.positionParm,currentMargin+'px',newMargin+'px');
          }
        this.hideLinks();
        },

      hideLinks: function(){
        if ( !this.options.loop ){
          if ( this.options.sliderLeft ){
            if ( this.options.currentObject == 0 ){
              this.options.sliderLeft.addClass('hide');
              }
            else if ( this.options.currentObject > 0 ){
              this.options.sliderLeft.removeClass('hide');
              }
            }
          if ( this.options.sliderRight ){
            if ( this.options.currentObject < this.options.objectsCount - this.options.objectsPerMask ){
              this.options.sliderRight.removeClass('hide');
              }
            else {
              this.options.sliderRight.addClass('hide');
              }
            }
          }
        if ( this.options.sliderPager ){
          var links = this.options.sliderPager.getElements( this.options.sliderPagerSelector );
          links.removeClass('active');
          links[ this.options.currentObject ].addClass('active');
          }
      },

      parseInfo: function(){
      	if ( this.options.sliderInfo && this.options.infoPattern ){
      		var infoString = this.options.objectsCount ? this.options.infoPattern : '';
      		if ( this.options.objectsCount ){
            infoString = infoString.replace('{object_from}', this.options.currentObject+1 );
        		var object_to = this.options.currentObject + this.options.objectsPerMask;
        		if ( object_to > this.options.objectsCount  ){
        			object_to = this.options.objectsCount;
        			}
        		infoString = infoString.replace('{object_to}', object_to );
        		infoString = infoString.replace('{object_count}', this.options.objectsCount );            
            }
      		this.options.sliderInfo.set('html',infoString);
      		}
      	},

      startAutoplay: function(){
        if ( !this.options.timer ){
          this.options.timer = ( this.options.autoPlayDirection == 'left' ) ? this.slideLeft.periodical(this.options.periodical,this) : this.slideRight.periodical(this.options.periodical,this); ;
          }
        },

      stopAutoplay: function(){
        if ( this.options.timer ){
          clearInterval(this.options.timer);
          this.options.timer = null;
          }
        },
        
      infiniteLoopRight: function(){
        if ( this.options.infiniteLoop && this.options.currentObject == this.options.objectsPerSlide ){          
          for ( i = 0 ; i < this.options.objectsPerSlide ; i++ ){
            this.options.sliderObjects[i].inject(this.options.slider,'bottom');
            }
          this.options.slider.setStyle('marginLeft','0px');
          this.options.sliderObjects = null; 
          this.options.currentObject = 0;
          this.calculateObjectSizes();
          }
        },
      
      infiniteLoopLeft: function(){
        if ( this.options.infiniteLoop && this.options.currentObject == 0 ){
          var marginLeft = 0;
          for ( z = 0 , i = this.options.sliderObjects.length - 1 ; z < this.options.objectsPerSlide ; z++ , i++ ){
            marginLeft += this.options.ObjectsSizes[i];
            this.options.sliderObjects[i].inject(this.options.slider,'top');
            }
          this.options.slider.setStyle('marginLeft','-'+marginLeft+'px'); 
          this.options.currentObject = this.options.objectsPerSlide;
          this.options.sliderObjects = null;
          this.calculateObjectSizes();
          }
        } 

  });
/*
})();
*/
/*
window.addEvent('domready', function(){
  var sg = new slideGallery( 'slider' , {objectsPerSlide:1} );
  });
*/

