var menuItem = new Class({
	initialize: function(id){
		this.mainObj = $(id);
		this.currentEvent = null;
		this.headerMain = $('headerMain');
		this.get1stLVL();
		this.get2ndLVL();
		this.makeEvents();
		this.preloadedImgs = [];	
		this.show = true;	
	},
	get1stLVL: function(){
		this.LVL1_UL = this.mainObj.getElements('ul.sub')[0];
		this.LVL1 = this.LVL1_UL.getChildren();
	},
	get2ndLVL: function(){
		var _this = this;
		this.LVL2 = new Array();
		this.LVL2ANIM = new Array();
		this.LVL1.each(function(item,i){
			_this.LVL2[i] = item.getElements('div.carShort');
			_this.headerMain.adopt(_this.LVL2[i][0]);	
			if (_this.LVL2[i][0]) {
				_this.LVL2ANIM[i] = new Fx.Style(_this.LVL2[i][0].id, 'opacity', {
					duration: 100,
					initialize: function(){
							_this.LVL2[i].setStyles({'opacity':0});
					},
					onStart: function(){
						if (_this.LVL2[i].getStyle('display') == 'none') {
							_this.LVL2[i].setStyles({
								'display': 'block'
							})
						}
					},
					onComplete:function(){
						if (_this.LVL2[i][0].title == 1) {
							_this.LVL2[i][0].title = '';
							return;
						}
						if (_this.LVL2[i][0].title == '') {
							_this.LVL2[i][0].title = 1;
						}
					}
				});
			}
		})
		
	},
	makeEvents: function(){
		var _this = this;
		this.mainObj.addEvents({
			'mouseenter':function(){
				(function(){
					_this.showNode()
				}).delay(100);
				_this.setShow(true);
			},
			'mouseleave':function(){
				(function(){
					_this.hideNode()
				}).delay(100);
			}
		})
		this.LVL1.each(function(item,i){
			item.addEvents({
				'mouseenter':function(){
					(function(){
						_this.showShort(i)
					}).delay(100);
					_this.setShow(true);
					_this.currentEvent = i;
				},
				'mouseleave':function(){
					(function(){
						_this.hideShort(i);
					}).delay(100);
					_this.setShow(true);
					_this.currentEvent = null;		
				}
			})
			
		})
		this.LVL2.each(function(item,i){
			item[0].title = 1;
			item.addEvents({
				mouseenter: function(){
					_this.currentEvent = i;
					_this.setShow(true);
				},
				mouseleave: function(){
					(function(){
						_this.hideShort(i);
					}).delay(100);	
					_this.currentEvent = null;
					_this.setShow(false);	
				}
			})
		})	
	},
	showShort: function(iterator){
		var $this = this;
		if (this.LVL2[iterator][0].title == 1) {
			this.LVL2ANIM[iterator].stop();
			this.LVL2ANIM[iterator].start(0, 1);
			this.preloadImg(iterator);
		}
	},
	hideShort: function(iterator){
		var $this = this;
		if (this.currentEvent == iterator) {
			return;
		}
		if (this.currentEvent != iterator) {
			this.LVL2ANIM[iterator].stop();
			this.LVL2ANIM[iterator].start(1, 0);
			_this = this;
			(function(){
				if (_this.getShow() == false){
					_this.hideNode();
				}
			}).delay(100);
		}
	},
	showNode: function(){
		this.LVL1_UL.setStyles({'display':'block','opacity':'0.95'});
	},
	hideNode: function(){
		if (this.currentEvent == null){
			this.LVL1_UL.setStyles({'display': 'none'})
		}
	},
	preloadImg: function(i){
		if(!this.preloadedImgs[i]){
			var prelImg = this.LVL2[i].getElements('img.menuPrelImg')[0];
			prelImg[0].src = prelImg[0].title;
			this.preloadedImgs[i] = true;			
		}
	},
	getShow: function(){
		return this.show;
	},
	setShow: function(status){
		this.show = status;
	}
});

var submenu = new Class({
	initialize: function(id){
		this.mainObj = $(id);
		this.getSubs();
	},
	getSubs:function(){
		var $this = this;
		this.subs = [];
		this.activators = this.mainObj.getElements('div.menu_i');
		this.activators.each(function(item,i){
			if(item.getElements('div.submenu')[0]){
				$this.subs[i] = item.getElements('div.submenu')[0];
				var _this = $this;
				item.addEvents({
					'mouseenter':function(){
						_this.showSub(i);
					},
					'mouseleave':function(){
						_this.hideSub(i);
					}
				})
			}						
		})
		this.subs.each(function(item,i){
			if (item) {
				var it = item;
				item.setStyles({
					'display': 'block',
					'opacity': '0'
				});
				item.height = item.getStyle('height');
				item.setStyles({
					'height': '0',
					'overflow': 'hidden',
					'opacity': '0.9'
				});
				var it = item;
				item.subFx = new Fx.Styles(item, {duration: 500, transition: Fx.Transitions.linear,
					onStart: function(){
						if(this.from.height == 0){
							item.setStyles({'border-bottom':'3px solid #666'});
						}
					},
					onComplete: function(){
						if(this.from.height != 0){
							item.setStyles({'border':'0'})
						}
					}
				});
			}
		})
	},
	showSub:function(i){
		var $this = this;
		this.subs[i].subFx.stop();
		this.subs[i].subFx.start({
			'height': [0, (this.subs[i].height)],
			'opacity': [0, 0.9]
		});
	},
	hideSub:function(i){
		this.subs[i].subFx.stop();
		this.subs[i].subFx.start({
			'height': [(this.subs[i].height, 0)],
			'opacity': [0.9, 0]
		});
		this.subs[i].subFx.start(this.subs[i].getStyle('height'),0);
	}
});


var Gallery = new Class({
	options: { 
		page_width: 100,
		slideClass: 'slide',
		contentId: 'gallCont',
		handlers: true, // określa czy mamy guziki przewiń w lewo, przewiń w prawo
		handlerLw: 'scrollLw', // id przewiń w lewo
		handlerRg: 'scrollRg', // id przewiń w prawo
		index: false, // określa czy mamy spis stron
		indexId: 'pageIndex',
		indexClassA: 'indexA',
		indexClassI: 'indexI'
	},
	initialize: function(gall_id,options){
		this.setOptions(options);
		this.mainObj = $(gall_id);
		this.scroll = new Fx.Scroll(gall_id);
		this.currentSlide = 0;
		this.findContent();

	},
	findContent: function(){
		var $this = this;
		this.slides = [];
		this.slides = $$('.'+this.options.slideClass);
			var galLength = this.slides.length * this.options.page_width;
		this.contentDiv = $(this.options.contentId);
		this.contentDiv.setStyles({
			'width': galLength
		});
		if(this.options.handlers){
			var _this = $this;
			this.handlerLw = $(this.options.handlerLw).addEvent('click',function(){
				if (!_this.options.index) {
					_this.scrollLw();
				}
				if(_this.options.index){
					var iter = _this.currentSlide-1;
					if(_this.currentSlide-1<0) iter = (_this.indexHandlers.length-1);
					_this.indexHandlers[iter].fireEvent('click');
				}
			})
			this.handlerRg = $(this.options.handlerRg).addEvent('click',function(){
				if (!_this.options.index) {
					_this.scrollRg();
				}
				if(_this.options.index){
					var iter = _this.currentSlide+1;
					if(_this.currentSlide+1>=_this.indexHandlers.length) iter = 0;
					_this.indexHandlers[iter].fireEvent('click');
				}
			})
		}
		this.indexHandlers = [];
		if(this.options.index){
			this.indexContent = $(this.options.indexId);
			this.indexHandlers = this.indexContent.getChildren();
			this.indexHandlers.each(function(item,i){
				item.addEvent('click',function(){
					$this.gallScroll($this.slides[i]);
					$this.lastActive.className = $this.options.indexClassI;
					this.className = $this.options.indexClassA;
					$this.lastActive = this;
					$this.currentSlide = i;
				})
			})
			this.lastActive = this.indexContent.getChildren()[0];
		}
	},
	gallScroll: function(element){
		this.scroll.toElement(element);		
	},
	scrollLw: function(){
		if(this.currentSlide-1 >= 0){
			this.gallScroll(this.slides[this.currentSlide-1]);
			this.currentSlide --;
			return;
		}
		else{
			this.gallScroll(this.slides[this.slides.length-1]);
			this.currentSlide = this.slides.length - 1;
		}
	},
	scrollRg: function(){
		if(this.currentSlide+1 < this.slides.length){
			this.gallScroll(this.slides[this.currentSlide+1]);
			this.currentSlide ++;
			return;
		}
		else{
			this.gallScroll(this.slides[0]);
			this.currentSlide = 0;
		}
	}
});
Gallery.implement(new Options);

var kAccordion = new Class({
	options: {
		tabHeight: 'auto',
		handlerClass: 'accHandler_i' ,
		handlerClassA: 'accHandler_a',
		boxClass:'accContent' 
	},
	initialize: function(id, options){
		var $this = this;
		this.setOptions(options);
		this.mainObj = $(id);
		if (location.href) {
			var anchor = location.href.substr(location.href.lastIndexOf('#') + 1).replace("tab=", '');
			if (anchor) {
				this.startTab = parseInt(anchor);
			}
			if (!this.startTab) {
				this.startTab = 0;
			}
		}
		this.acc = new Accordion('h3.'+this.options.handlerClass, 'div.'+this.options.boxClass, {
			opacity:false,
			alwaysHide: true,
			display: $this.startTab,
			onActive: function(toggler, element){
				toggler.className = $this.options.handlerClassA;
				this.options.alwaysHide = false;
			},
			onBackground: function(toggler, element){
				toggler.className = $this.options.handlerClass;
			}
		},this.mainObj);
		this.setBoxes();
	},
	setBoxes: function(){
		var $this = this;
		this.boxes = $(this.mainObj).getElements('div.'+this.options.boxClass+'');
		this.boxes.each(function(item){
			item.setStyles({'height':$this.options.tabHeight})
		});
	}
});
kAccordion.implement(new Options);

var tabs = new Class({
	initialize: function(id, width){
		this.mainObj = $(id);
		this.width = width;
		this.elements();
		this.events();
	},
	elements: function(){
		this.tabMenu = this.mainObj.getElements('div.tabMenu')[0];
		this.tabMenu.setStyles({'width':this.width});
		this.buttons = this.tabMenu.getChildren();
		this.content = this.mainObj.getElements('div.tabContent');
		this.lastButton = buttons[0];
		this.lastContent = content[0];
		if(this.buttons.length != this.content.length ) alert('błędna konfiguracja tabs')
	},
	events: function(){
		var $this = this;
		this.buttons.each(function(item,i){
			item.addEvent('click',function(){
				if (this.className != 'a') {
					$this.lastContent.setStyles({
						'display': 'none'
					});
					$this.content[i].setStyles({
						'display': 'block'
					});
					$this.lastContent = $this.content[i];
					this.className = 'a';
					$this.lastContent.className = 'i';
					$this.lastButton = this;
				}
			})
		})
	}	
});
