var islider = new Class({
	Implements: Options, /* Помогает переопределять опции в новом объекте при создании экземпляра класса */
	options: {
		slideContainer: 'items',
		directButtons: 'play_buttons',
		slideNumbers: '',
		duration: 500,
		hDirect: 1, /* [Горизонтальный слайдинг если hDirect = 1, вертикальный слайдинг, если vDirect = 0] */
		itemNum: 0,
		slideTimer: 3000,
		isPlaying: 0, /* [Если установлено isPlaying = 1, то слайдер начинает проигрывать сразу, если isPlaying = 0, то только по нажатии кнопки "Play"] */
		parentAnimeHeight: 0
	},
	initialize: function(directButtons, slideContainer, options){
		this.setOptions(options);
		this.itemNum = this.options.itemNum;
		this.duration = this.options.duration;
		this.isPlaying = this.options.isPlaying;
		this.isPaused = this.options.isPaused;
		this.parentAnimeHeight = this.options.parentAnimeHeight;
		var hDirect = this.hDirect = this.options.hDirect;
		if(slideContainer) this.panes = $(slideContainer); else $(this.options.slideContainer);
		if(directButtons) this.buttons = $(directButtons).getChildren(); else if(this.buttons =  $(this.options.directButtons).getChildren());
		/* [По клику на цифре перемещаем слайдинг] *//*
		if(slideNumbers) this.slideNumbers = $(slideNumbers).getChildren(); else $(this.options.slideNumbers).getChildren();
		var currentPageIndex = this.options.itemNum;
		this.slideNumbers.each(function(item, index) { 
			item.addEvent('click', function() {
				if(this.iTimer) {
					this.iTimer = $clear(this.iTimer);
				}
				if(currentPageIndex > index) {
					this.sliding(0, index);
				} else {
					this.sliding(1, index);
				}
				currentPageIndex = index;
			}.bind(this));
		}.bind(this));
		*/
		if(this.buttons[0]) {
			this.buttons.each(function(item, index) {
				if(item.get('id') == 'previous') item.addEvent('click', function() {this.previous();}.bind(this));
				if(item.get('id') == 'next') item.addEvent('click', function() {this.next();}.bind(this));
				if(item.get('id') == 'play') item.addEvent('click', function() {this.play();}.bind(this));
				if(item.get('id') == 'pause') item.addEvent('click', function() {this.pause();}.bind(this));
			}.bind(this));
		}

		if((this.elements = ((pane = this.panes.getChildren()).length)) > 1) {
			
			this.pane = pane;
			this.pane.each(function(item, index) {
				if(index == 0) {
					if(this.parentAnimeHeight) {
						item.getParent().setStyles({
							height: item.getHeight()
						});
					}
					item.setStyles({
						left: '0px',
						top: '0px'
					});
				} else {
					if(hDirect == 1) {
						item.setStyles({
							left: -1*item.getWidth() + 'px'
						});
					} else if(hDirect == 0) {
						item.setStyles({
							top: -1*item.getParent().getHeight() + 'px'
						});
					}
				}
			});
		}
		if(this.isPlaying == 1) {
			this.iTimer = this.play.periodical(this.options.slideTimer, this);
		}
		window.addEvent('resize', function() {
			this.recalculateWidth();
		}.bind(this));
		
	//	window.addEvent('resize',});		
	},
	previous: function() {
		this.isPlaying = 0;
		if(this.iTimer) {
			this.iTimer = $clear(this.iTimer);
		}
		this.sliding(0);
	},
	pause: function() {
		this.isPlaying = 0;
		if(this.iTimer) {
			this.iTimer = $clear(this.iTimer);
		}
	},
	sliding: function(direction, itemId) {
		this.curPane = this.pane[this.itemNum]; /* [Старая панель - текущая] */
		var curPane = this.curPane;
		var curPageIndex = this.itemNum;
		if(direction == 1 && !itemId) {
			if(this.itemNum < (this.elements - 1)) {
				this.itemNum++;
			} else {
				this.itemNum = 0;
			}
		} else if(direction == 0 && !itemId && itemId != 0 ) {
			if(this.itemNum > 0) {
				this.itemNum--;
			} else {
				this.itemNum = this.elements - 1;
			}
		} else {
			if(itemId != this.itemNum) {
				this.itemNum = itemId;
			}
			curPageIndex = itemId;
		}
		this.newPane = this.pane[this.itemNum]; /* [новая панель - всплывающая] */
		var newPane = this.newPane;

		itemIn = new Fx.Morph(this.newPane, {
			wait: true,
			duration: this.duration,
			transition: Fx.Transitions.Cubic.easeIn,
			link: "chain"
		});
		itemOut = new Fx.Morph(this.curPane, {
			wait: true,
			duration: this.duration,
			transition: Fx.Transitions.Cubic.easeIn,
			link: "chain"
		});
		if(this.parentAnimeHeight) {
			itemParent = new Fx.Morph(this.curPane.getParent(), {
				wait: true,
				duration: this.duration,
				transition: Fx.Transitions.Cubic.easeIn,
				link: "chain"
			});
			
			//alert(this.pane[this.itemNum].getHeight());
			
			itemParent.start({ /* [ Меняем размер родителя] */
				'height': [this.curPane.getHeight(), this.newPane.getHeight()]
			});
		}

		if(this.hDirect == 1) {
			if(direction == 1 || direction == 2) {
				itemIn.start({ /* [ Выдвигаем новый элемент] */
					'left': [newPane.getWidth(), 0]
				});
				
				itemOut.start({ /* [ Задвигаем старый элемент] */
					'left': [0, -1*curPane.getWidth()]
				});
			} else if(direction == 0) {

				itemIn.start({ /* [ Выдвигаем новый элемент] */
					'left': [-1*newPane.getWidth(), 0]
				});
				
				itemOut.start({ /* [ Задвигаем старый элемент] */
					'left': [0, curPane.getWidth()]
				});
			}
		} else if(this.hDirect == 0) {
			if(direction == 1 || direction == 2) {
				itemIn.start({ /* [ Выдвигаем новый элемент] */
					'top': [this.newPane.getParent().getHeight(), 0]
				});
				
				itemOut.start({ /* [ Задвигаем старый элемент] */
					'top': [0, -1*this.curPane.getParent().getHeight()]
				});
			} else if(direction == 0) {
				this.pane.each(function(item, index) { /* [Убираем с глаз долой, вниз, все что вылазит из-под] */
					if(newPane.getHeight() > curPane.getHeight()) {
						if(curPageIndex != index) {
							item.setStyles({
								top: newPane.getHeight() + 'px'
							});
						}
					}
				});
				itemOut.start({ /* [ Задвигаем старый элемент] */
					'top': [0, this.newPane.getHeight()]
				});
				itemIn.start({ /* [ Выдвигаем новый элемент] */
					'top': [-1*this.newPane.getHeight(), 0]
				});

			}
		}
	},
	play: function() { /* [direction - направление перемещения слайдера] */
		if(this.isPlaying == 0){
			this.isPlaying = 1;
			this.direction = 1;
			this.iTimer = this.sliding.periodical(this.options.slideTimer, this, 0);
		}
	},
	next: function() {
		this.isPlaying = 0;
		if(this.iTimer) {
			this.iTimer = $clear(this.iTimer);
		}
		this.sliding(1);
	},
	recalculateWidth: function() {
		this.curPane = this.pane[this.itemNum];
		
		if(this.itemNum - 1 < 0) {
			this.prevPane = this.pane[this.elements - 1];
		} else {
			this.prevPane = this.pane[this.itemNum - 1];
		}
		if(this.itemNum + 1 > this.elements) {
			this.nextPane = this.pane[0];
		} else {
			this.nextPane = this.pane[this.itemNum + 1];
		}
		if(this.hDirect == 1) {
			//alert(this.prevPane.getWidth() + " - " + this.prevPane.getParent().getWidth());
			this.prevPane.setStyles({
				left: -1*this.prevPane.getParent().getWidth() + 'px',
				width: this.prevPane.getParent().getWidth() + 'px'
			});
			this.nextPane.setStyles({
				left: -1*this.nextPane.getParent().getWidth() + 'px',
				width: this.nextPane.getParent().getWidth() + 'px'
			});			
		} else if(this.hDirect == 0) {
			this.prevPane.setStyles({
				top: -1*this.prevPane.getHeight() + 'px',
				width: this.curPane.getWidth() + 'px'
			});
			this.nextPane.setStyles({
				top: -1*this.nextPane.getHeight() + 'px',
				width: this.curPane.getWidth() + 'px'
			});	
		}
		if(this.parentAnimeHeight){
			this.curPane.getParent().setStyles({
				height: this.curPane.getHeight() + 'px'
			});
		}
		if(this.iTimer) {
			this.iTimer = $clear(this.iTimer);
			if(this.isPlaying == 0) this.isPlaying = 1;
			this.iTimer = this.sliding.periodical(this.options.slideTimer, this, 1);			
		}		
		
	}
});
