// JavaScript Document
Element.implement({
  makeResetable: function(){
		this.addEvent('focus', function(){
		  if (this.value == this.defaultValue) this.value = '';
		});
		
		this.addEvent('blur', function(){
		  if (this.value == ''){
				this.value = this.defaultValue;
			}
		});
		
		if (this.value == '') this.value = this.defaultValue;
	}
});

String.implement({
  
	getAnchor: function(){
		var hashIndex = this.indexOf('#');
		var anchor = '';
		if (hashIndex > -1){
			anchor = this.substr(hashIndex).replace('#', '');
		}
		return anchor;
	}
});


var Slideshow = new Class({

  Implements: [Events, Options],
	
	options: {
		slide: '.slide',
		width: 0,
		duration: 300,
		moveAfter: 7000,
		start: 0,
		prevButton: '',
		nextButton: '',
		counter: false,
		resume: true,
		counterTemplate: '{current}/{count}',
		paging: false,
		pagingClass: {
			prev: 'slide-link-prev',
			next: 'slide-link-next',
			page: 'slide-link',
			active: 'active'
		},
		mouseEnterStop: true,
		onMove: $empty()
	},
	
	initialize: function(container, positionWrapper, options){
    this.container = $(container);
    this.setOptions(options);
		this.position = $(positionWrapper);
		this.slides = this.container.getElements('.slide');
		this.count = this.slides.length;
		this.current = this.options.start;
		this.pages = new Array();
		
		// try to read slide width
		if ((this.options.width == 0) && (this.count > 0)){
			this.options.width = this.slides[0].getStyle('width');
		}
		
		// effects
		this.fx = new Array();
		this.slides.each(function(slide, index){
		  this.fx[index] = new Fx.Tween(slide, { property: 'opacity', link: 'cancel', duration: this.options.duration });
			if (index == this.options.start){
				this.fx[index].set(1);
			} else {
				this.fx[index].set(0);
				slide.removeClass('hide');
			}
		}, this);
		
		// generate paging if available
		if (this.options.paging){
			this.options.prevButton = new Element('a', {'class': this.options.pagingClass.prev, 'html': '&laquo;' });
			this.options.prevButton.inject(this.options.paging);
			for (var i = 1; i <= this.count; i++){
				this.pages[i-1] = new Element('a', {'class' : this.options.pagingClass.page, 'html': i }).inject(this.options.paging);
			}
			this.options.nextButton = new Element('a', {'class': this.options.pagingClass.next, 'html': '&raquo;' });
			this.options.nextButton.inject(this.options.paging);
			
			// add move events to pages
			this.pages.each(function(page, index){
			  page.addEvent('click', function(event){
				  event.stop();
  				this.repeater = $clear(this.repeater);
	  			this.move(index);
		  		if (this.options.resume) (this.repeater = this.move.periodical(this.options.moveAfter, this));
			 	}.bind(this));
			}, this);
		}
		
		// buttons
		if (this.options.prevButton){
			this.options.prevButton.addEvent('click', function(event){
			  event.stop();
				this.repeater = $clear(this.repeater);
				this.move(this.current - 1);
				if (this.options.resume) (this.repeater = this.move.periodical(this.options.moveAfter, this));
			}.bind(this));
		}
		
		if (this.options.nextButton){
			this.options.nextButton.addEvent('click', function(event){
			  event.stop();
				this.repeater = $clear(this.repeater);
				this.move(this.current + 1);
				if (this.options.resume) (this.repeater = this.move.periodical(this.options.moveAfter, this));
			}.bind(this));
		}
		
		this.repeater = this.move.periodical(this.options.moveAfter, this);
		this.update();
		
		// stop slideshow on mouseenter
		if (this.options.mouseEnterStop){
			this.container.addEvents({
			  'mouseenter': function(){ $clear(this.repeater); }.bind(this),
				'mouseleave': function(){ this.repeater = this.move.periodical(this.options.moveAfter, this); }.bind(this)
			});
		}
		
		
		
	},
	
	move: function(num){
  	var next = (num ? num.limit(0, this.count-1) : (this.current + 1)%(this.count));
		this.fx[this.current].start(0);
		this.fx[next].start(1);
		this.current = next;
		this.update();
		this.fireEvent('move');
	},
	
	update: function(){
		if (this.options.counter){
			this.options.counter.set('text', this.options.counterTemplate.substitute({ current: this.current+1, count: this.count }));
		}
		// hide buttons
		if (this.options.prevButton){
			this.current == 0 ? this.options.prevButton.setStyle('visibility', 'hidden') : this.options.prevButton.setStyle('visibility', 'visible');
		}
		
		if (this.options.nextButton){
			this.current == this.count - 1 ? this.options.nextButton.setStyle('visibility', 'hidden') : this.options.nextButton.setStyle('visibility', 'visible');
		}
		
		/*
		this.pages.each(function(page, index){
		  index == this.current ? page.addClass('active') : page.removeClass('active');
		}, this);
		*/
		
	}

});

var Slider = new Class({
											 
	Implements: [Events, Options],
	
	options: {
		onOpen: $empty(),
		onClose: $empty(),
		duration: 400
	},

  initialize: function(toggler, slide, options){
		this.setOptions(options);
		this.toggler = toggler;
		this.slide = slide;
		
		this.slideFx = new Fx.Slide(this.slide, { duration: this.options.duration, link: 'cancel' });
		this.slideFx.hide();
		this.open = false;
		
		this.toggler.addEvent('click', function(event){
		  event.stopPropagation();
			if (this.open){
				this.slideFx.slideOut();
				this.open = false;
				this.fireEvent('close', this.toggler);
			} else {
				this.slideFx.slideIn();
				this.open = true;
				this.fireEvent('open', this.toggler);
			}
		}.bind(this));
		
		this.toggler.setStyle('cursor', 'pointer');
		
		return this;
	},
	
	isOpen: function(){
		return this.open;
	}
	
});

var TableHighlighter = new Class({
																 
  initialize: function(table){
		this.table = $(table);
		this.disabled = false;
		
		this.resetRows();
		
		if (this.rows.length == 0) return false;
		
		this.cols = this.rows[0].getElements('th').length;
		
		// default highlighted column
		this.anchor = document.URL.getAnchor();
		this.anchors = this.anchor.split('-');
		this.anchor = this.anchors[0];
		this.trial = this.anchors[1];
		if (this.anchors[0] != ''){
			this.col = $('column-'+this.anchors[0]);
			if ($defined(this.col)){
				var index = this.rows[0].getElements('th').indexOf(this.col);
				if (index > 0){
					this.defaultIndex = index;
					this.disabled = true;
				}
			}
		}
		
		// trial
		//if (this.anchor != 'trial' && !$defined(this.trial)){
//			$$('tr.trial').each(function(row){
//			  row.destroy();
//				this.resetRows();
//      }, this);
//		}
//		if (this.anchor == 'trial' || $defined(this.trial)){
//			$$('tr.order').getElements('a').flatten().each(function(link){
//			  link.set('href', link.get('href') + '_trial');
//			});
//		}
		
		this.cells = new Array();
		this.rows.each(function(row){
		  var cellsInRow = row.getElements('th, td');
			this.cells.include(cellsInRow);
			if (!this.disabled){
				cellsInRow.each(function(cell, index){
					cell.addEvent('mouseenter', function(){
						this.highlightColumn(index);
					}.bind(this));
				}, this);
			}
		}, this);
		this.cells = this.cells.flatten();
		
		if (!this.disabled){
			this.table.addEvent('mouseleave', function(){
				this.cells.each(function(cell) { cell.removeClass('hover'); });
			}.bind(this));
		}
		
		if (this.disabled){
			this.highlightColumn(this.defaultIndex);
		}
		
		// expand buttons
		this.expandButtons = this.table.getElements('a[class^=expand-]');
		if (this.expandButtons.length > 0){
			this.expandButtons.each(function(button){
			  var type = button.get('class').replace('expand-','');
				var rows = this.table.getElements('tr.common-'+type);
				if (rows.length > 0){
					rows.setStyle('display', 'none');
					button.addEvent('click', function(event){
					  event.stop();
						if (Browser.Engine.trident){
							rows.setStyle('display', 'block');
						} else {
							rows.setStyle('display', 'table-row');
						}
						button.getParent('td').set('html', '&nbsp;');
					});
				}
			}, this);
		}
		
	},
	
	highlightColumn: function(index){
		this.cells.each(function(cell) { cell.removeClass('hover'); });
		if (index > 0){
			this.rows.each(function(row){
				row.getElements('th, td')[index].addClass('hover');
			});
		}
	},
	
	resetRows: function(){
		this.rows = this.table.getElements('tr');
		this.rows = this.rows.filter(function(row, index){
			return (row.getElements('td, th').length > 2);
		});
	}

});

var FormSupport = {
	
	init: function(){
		
		this.form = $('form-support');
		
		new Slider($('toggle'), $('form-support-date'), { duration: 200 });
		
		this.dateInput = $('lday');
		
		new vlaDatePicker(this.dateInput, {
			separator: '.',
			weekDayLabels: ['Po', 'Út', 'St', 'Čt', 'Pá', 'So', 'Ne'],
			monthLabels: ['Leden', 'Únor', 'Březen', 'Duben', 'Květen', 'Červen', 'Červenec', 'Srpen', 'Září', 'Říjen', 'Listopad', 'Prosinec'],
			monthSmallLabels: ['I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX', 'X', 'XI', 'XII'],
			startMonday: true,
			leadingZero: false,
			alignX: 'center',
			alignY: 'bottom', 
			offset: {
				y: 3
			} 
		});
		
		$('lname').makeResetable();
		$('lcontact').makeResetable();
		
	}
	
};

var Tabs = {
	
	init: function(){
		this.container = $('tab-box');
		this.tabs = this.container.getElements('.tab-set li a');
		this.panels = this.container.getElements('.panel');
		
		this.tabs.each(function(tab, index){
		  tab.addEvent('click', function(event){
			  event.stop();
				this.show(tab, index);
			}.bind(this));
		}, this);
		
		this.show(this.tabs[0], 0);
	},
	
	show: function(tab, index){
		this.panels.setStyle('display', 'none');
		this.panels[index].setStyle('display', 'block');
		tab.getParent('ul').getElements('li').removeClass('on');
		tab.getParent('li').addClass('on');
	}
	
};

var Antispam = new Class({

  initialize: function(form){
		this.form = $(form);
	  this.form.getElement('noscript').destroy();
		new Element('input', { 'type': 'hidden', 'value': 'gauzy', 'name': 'anti' }).inject(this.form);
	}
	
});

var Gauzy = {
	
	init: function(){
		
		// news slideshow
		if ($('news')){
			new Slideshow($('news').getElement('.slide-wrapper'), $('slide-position'), {
			  width: 235,
				duration: 500,
				prevButton: $('nav-left'),
				nextButton: $('nav-right'),
				counter: $('nav-page')
			});
		}
		
		// guides
		if ($('guides')){
			new Slideshow($('guides'), $('guides-position'),{
			  width: 650,
				duration: 500,
				moveAfter: 2500,
				paging: $('slide-link'),
				resume: false
			});
		}
		
		// togglers and sliders
		var toggles = $$('#main-content .toggle');
		var slides = $$('#main-content .toggle-desc');
	  if (toggles.length == slides.length){
			toggles.each(function(toggle, index){
			  new Slider(toggles[index], slides[index], {
				  onOpen: function(toggler){
						toggler.addClass('minus');
					},
					onClose: function(toggler){
						toggler.removeClass('minus');
					}
				});
			});
		}
		
		// vocabulary
		if ($('block-slovnicek')){
			$$('#block-slovnicek dt').setStyle('cursor', 'pointer');
			new Accordion($$('#block-slovnicek dt'), $$('#block-slovnicek dd'),{
			  show: -1,
				alwaysHide: true
			});
		}
		
		// support block
		if ($('form-support')) FormSupport.init();
		// tabs
		if ($('tab-box')) Tabs.init();
		
		// clickable
		$$('.clickable').each(function(block){
		  var link = block.getElement('a');
		  block.setStyle('cursor', 'pointer');
			block.addEvent('click', function(event){
			  window.location = link.get('href');
			});
			link.addEvent('click', function(event){
			  event.stopPropagation();
			});
		});
		
		$$('table.comparsion').each(function(table){
		  new TableHighlighter(table);
		});
		
		if ($('contact-form')) new Antispam('contact-form');
		if ($('support-form')) new Antispam('support-form');
		if ($('form-support')) new Antispam('form-support');
		
		
	}
	
};

window.addEvent('domready', function(){
  Gauzy.init();
});