// JavaScript Document

var Horoscope = new Class({

  initialize: function(container){
		this.container = container;
		this.selectBox = this.container.getElement('div.select');
		this.target = container.getElement('p');
		this.selected = this.selectBox.getElement('p').getElement('span');
		this.list = this.selectBox.getElement('ul');
		this.items = this.list.getElements('li');
		this.closed = true;
		
		this.selectBox.addEvent('click', $lambda(false));
		
		this.selected.addEvent('click', function(event){
			event.stop();
			this.toggle();
		}.bind(this));
		
		this.items.each(function(item){
		  item.addEvent('click', function(event){
			  event.stop();
				this.select(item);
			}.bind(this));
		}, this);
	},
	
	open: function(){
		this.selectBox.addClass('open');
		this.closed = false;
	},
	
	close: function(){
		this.selectBox.removeClass('open');
		this.closed = true;
	},
	
	toggle: function(){
		this.closed ? this.open() : this.close();
	},
	
	select: function(item){
		this.selected.set('class', '').addClass(item.get('class'));
		this.selected.set('html', item.get('html'));
		this.selected.getElement('span.content').destroy();
		this.target.set('html', item.getElement('span.content').get('html'));
		this.close();
	},
	
	isOpen: function(){
		return !this.closed;
	}

});

var Slideshow = new Class({

  Implements: Options,
	
	options: {
		wait: 6000,
		duration: 600,
		stopOnClick: false,
		slide: '.slide',
		toggles: '#toggles li'
	},
	
	initialize: function(container, options){
		this.setOptions(options);
		this.container = $(container);
		
		this.toggles = $$(this.options.toggles);
		this.slides = this.container.getElements(this.options.slide);
		this.count = this.slides.length;
		this.current = 0;
		
		this.slideFx = new Array();
		this.slides.each(function(slide, index){
		  this.slideFx[index] = new Fx.Tween(slide, { property: 'opacity', duration: this.options.duration, transition: Fx.Transitions.Quint.easeInOut, link: 'cancel' });
			if (index){
				this.slideFx[index].set(0);
			} else {
				this.slideFx[index].set(1);
				slide.removeClass('first');
			}
		}, this);
		
		this.toggles.each(function(toggle, index){
		  toggle.getElement('a').addEvent('click', function(event){
			  event.stop();
			  $clear(this.repeater);
				this.show(index);
				this.repeater = this.move.periodical(this.options.wait, this);
			}.bind(this));
		}, this);
		
		this.repeater = this.move.periodical(this.options.wait, this);
		
		
	},
	
	move: function(){
		this.animate((this.current + 1)%this.count);
	},
	
	show: function(index){
		this.slides[index].setStyle('z-index', 1);
		this.slides[this.current].setStyle('z-index', 0);
		this.toggles[this.current].removeClass('active');
		this.toggles[index].addClass('active');
		this.slideFx[index].set(1);
		this.slideFx[this.current].set(0);
		this.current = index;
	},
	
	animate: function(index){
		this.slides[index].setStyle('z-index', 1);
		this.slides[this.current].setStyle('z-index', 0);
		this.toggles[this.current].removeClass('active');
		this.toggles[index].addClass('active');
		this.slideFx[index].start(1).chain(function(){
		  this.slideFx[this.current].set(0);
		  this.current = index;
		}.bind(this));
	} 
	
});


// Main Object
var Maxizabava = {
	
	init: function(){
		
		if ($('block-horoscope')) {
		  var horoscope = new Horoscope($('block-horoscope'));
			$(document.body).addEvent('click', function(){
			  if (horoscope.isOpen()) horoscope.close();
			});
		}
		
		if ($('slideshow')){
			new Slideshow('slideshow');
		}
	}
	
};

// onDomReady script initialization
window.addEvent('domready', function(){
  Maxizabava.init();
});


// Přidat do oblíbených
function addBookmark(url,desc){
	var bookmarkurl      = url;
	var bookmarktitle    = desc;

	if (window.sidebar) { // Mozilla Firefox Bookmark
		window.sidebar.addPanel(bookmarktitle, bookmarkurl,"");
	} else if(document.all){ // IE Favourites
		window.external.AddFavorite(bookmarkurl,bookmarktitle);
	}
}


// Kontrola registračního formuláře
function kontrola(form){ 
	if(form.r_nick.value == ""){
		alert("Zadejte vaší přezdívku.");
		form.r_nick.focus(); 
		return false;  
	}else if(form.r_nick.value.length < 4){
		alert("Minimální délka přezdívky jsou 4 znaky.");
		form.r_nick.focus(); 
		return false;  		
	}else if(form.r_pass1.value == ""){
		alert("Zadejte Vaše heslo.");
		form.r_pass1.focus();
		return false;
	}else if(form.r_pass2.value == ""){
		alert("Zadejte prosím Váše heslo pro kontrolu ještě jednou.");
		form.r_pass2.focus();
		return false;
	}else if(form.r_pass1.value != form.r_pass2.value){
		alert("Zadaná hesla se musejí shodovat.");
		form.r_pass1.focus();
		return false;
	}else if(form.r_pass1.value.length < 6){
		alert("Minimální délka hesla je 6 znaků.");
		form.r_pass1.focus(); 
		return false;  			
	}else if(form.r_email.value == "" || form.r_email.value.indexOf('@')==-1 || form.r_email.value.indexOf('.')==-1){
		alert("Zadejte prosím Váš e-mail.");
		form.r_email.focus();
		return false;
	}else if(form.r_email.value.length < 6){
		alert("Neplatný email.");
		form.r_email.focus(); 
		return false;  	
	} else {
			form.submit();
	}
	return false;
}
