//on dom ready...
window.addEvent('domready', function() {
	$$('.models li a').each(function(el) {
		el.addEvent('click', function(event) {
			event.stop();
			
			var toUpdate = "#responseSlot_" + el.get('class') + " .howMany";
			//alert(toUpdate);
			
			var req = new Request({
				method: 'get',
				url: el.get('href'),
				data: {'do' : '1' },
				onRequest: function() {
						//alert('Request made. Please wait...');
					},
				onComplete: function(response) {
						//alert('Request completed successfully.' + response.split('|'));
						var howMany = response.split('|');
						document.getElement(toUpdate).set('text', howMany[1]);
						if (howMany[1] == 0) {
							document.getElement(toUpdate).getParent().set('class', 'disabled');
						} else {
							document.getElement(toUpdate).getParent().set('class', '');
						}
					}
			}).send();
		});
	});
	
	// ustal numer elementu do rozwiniÄ?cia w mainMenu
	var number = 0;
	var itemFounded = false;
	
	$$('#productsMenu ul.accordion').each(function(el) {
		var items = el.getElements('li');
		items.each(function(el) {
			if (el.getProperty('class') == 'selected')
				itemFounded = true;
		});
		if (itemFounded) return;
		else number++;
	});
	
	if (itemFounded == true) {
		var myAccordion = new Accordion($$('.toggler'), $$('.accordion'), {
			opacity: false,
			initialDisplayFx: true,
			alwaysHide: true,
			show: number
		});
	} else {
		var myAccordion = new Accordion($$('.toggler'), $$('.accordion'), {
			opacity: false,
			initialDisplayFx: false,
			alwaysHide: true,
			display: -1
		});
	}
	
	
//	var myAccordion = new Accordion($$('.toggler'), $$('.accordion'), {
//		display: -1,
//		alwaysHide: true
//	});
	
	$$('.toggler').setStyles({
		cursor: 'pointer'
	});
	
	var SimpleSlideshow = new Class({
		options: {
			showControls: true,
			showDuration: 5000,
			showTOC: true,
			tocWidth: 20,
			tocClass: 'toc',
			tocActiveClass: 'toc-active'
		},
		Implements: [Options,Events],
		initialize: function(container,elements,options) {
			//settings
			this.container = $(container);
			this.elements = $$(elements);
			this.currentIndex = 0;
			this.interval = '';
			if(this.options.showTOC) this.toc = [];
			
			//assign
			this.elements.each(function(el,i){
				if(this.options.showTOC) {
					this.toc.push(new Element('a',{
						text: i+1,
						href: '#',
						'class': this.options.tocClass + '' + (i == 0 ? ' ' + this.options.tocActiveClass : ''),
						events: {
							click: function(e) {
								if(e) e.stop();
								this.stop();
								this.show(i);
							}.bind(this)
						},
						styles: {
							left: ((i + 1) * (this.options.tocWidth + 10))
						}
					}).inject(this.container));
				}
				if(i > 0) el.set('opacity',0);
			},this);
			
			//next,previous links
			if(this.options.showControls) {
				//this.createControls();
				
			}
			//events
			this.container.addEvents({
				mouseenter: function() { this.stop(); }.bind(this),
				mouseleave: function() { this.start(); }.bind(this)
			});

		},
		show: function(to) {
			this.elements[this.currentIndex].fade('out');
			if(this.options.showTOC) this.toc[this.currentIndex].removeClass(this.options.tocActiveClass);
			this.elements[this.currentIndex = ($defined(to) ? to : (this.currentIndex < this.elements.length - 1 ? this.currentIndex+1 : 0))].fade('in');
			if(this.options.showTOC) this.toc[this.currentIndex].addClass(this.options.tocActiveClass);
		},
		start: function() {
			this.interval = this.show.bind(this).periodical(this.options.showDuration);
		},
		stop: function() {
			$clear(this.interval);
		},
		//"private"
		/*
		createControls: function() {
			var next = new Element('a',{
				href: '#',
				id: 'next',
				text: '>>',
				events: {
					click: function(e) {
						if(e) e.stop();
						this.stop(); 
						this.show();
					}.bind(this)
				}
			}).inject(this.container);
			var previous = new Element('a',{
				href: '#',
				id: 'previous',
				text: '<<',
				events: {
					click: function(e) {
						if(e) e.stop();
						this.stop(); 
						this.show(this.currentIndex != 0 ? this.currentIndex -1 : this.elements.length-1);
					}.bind(this)
				}
			}).inject(this.container);
		}
		*/
	});
	

	var slideshow = new SimpleSlideshow('slideshow-container','#slideshow-container a');
	slideshow.start();	
	
	
//	/* settings */
//	var showDuration = 5000;
//	var container = $('slider');
//	var images = container.getElements('a');
//	var currentIndex = 0;
//	var interval;
//	/* opacity and fade */
//	images.each(function(img,i){ 
//		if(i > 0) {
//			img.set('opacity',0);
//		}
//	});
//	/* worker */
//	var show = function() {
//		images[currentIndex].fade('out');
//		images[currentIndex = currentIndex < images.length - 1 ? currentIndex+1 : 0].fade('in');
//	};
//	/* start once the page is finished loading */
//	window.addEvent('load',function(){
//		interval = show.periodical(showDuration);
//	});
	
});