var Slideshow = new Class({

  Implements: [Events, Options],
	
	options: {
		slide: '.slide',
		width: 0,
		duration: 300,
		moveAfter: 5000,
		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'
		},
		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');
		}
		
		// set proper width
		this.position.setStyle('width', this.count*this.options.width);
		
		this.moveFx = new Fx.Tween(this.position, { property: 'left', duration: this.options.duration, link: 'chain', transition: Fx.Transitions.Quint.easeInOut });
		this.moveFx.set(0);
		
		// 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();
		
		
		
	},
	
	move: function(num){
		if (!$defined(num)) this.current = (++this.current%this.count);
		if ($defined(num))	this.current = num.limit(0, this.count-1);
		this.moveFx.start(-this.current*this.options.width);
		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 Passcal = {
	
	init: function(){
		
		// news slideshow
		if ($('uvod')){
			new Slideshow($('upoutavka').getElement('.inner'), $('slide-position'), {
			  width: 322,
				duration: 500,
				prevButton: $('nav-left'),
				nextButton: $('nav-right'),
				counter: $('nav-page')
			});
		}
		
		if ($('hp-upoutavka-wrapper')){
			$('hp-upoutavka-close').addEvent('click', function(event){
				event.stop();
				$('hp-upoutavka-wrapper').tween('opacity', '0');
			});
		}
		
	}
	
};

// Email.js version 5
var tld_ = new Array()
var topDom_ = 13;
var m_ = "mailto:";
var a_ = "@";
var d_ = ".";

function fcuk(name, dom, tl, params)
{
	var s = e(name,dom,tl);
	document.write('<a href="'+m_+s+params+'">'+s+'</a>');
}
function e(name, dom, tl)
{
	var s = name+a_;
	if (tl!=-2)
	{
		s+= dom;
		if (tl>=0)
			s+= d_+tld_[tl];
	}
	else
		s+= swapper(dom);
	return s;
}
function swapper(d)
{
	var s = "";
	for (var i=0; i<d.length; i+=2)
		if (i+1==d.length)
			s+= d.charAt(i)
		else
			s+= d.charAt(i+1)+d.charAt(i);
	return s.replace(/\?/g,'.');
}

// remooz
window.addEvent('load', function() {
 
	/**
	 * Some options for the large photos.
	 *
	 * The first argument is the argument for $$ (can be an array of elements or a selector)
	 */
 
	ReMooz.assign('.gallery a', {
		'origin': 'img',
		'shadow': 'onOpenEnd', // fx is faster because shadow appears after resize animation
		'resizeFactor': 0.8, // resize to maximum 80% of screen size
		'cutOut': false, // don't hide the original
		'opacityResize': 0.4, // opaque resize
		'dragging': false, // disable dragging
		'centered': true // resize to center of the screen, not relative to the source element
	});
 
	/**
	 * Note on "shadow": value can be true, onOpenEnd (appear after resize) and false, to disable shadow
	 * WebKit (Safari 3) uses (great looking) CSS shadows, so it ignores this option.
	 */
 
});


// http://davidwalsh.name/dwclickable-entire-block-clickable-mootools-12
// dwClickable: Entire Block Clickable Using MooTools 1.2

var dwClickables = new Class({
	
	//implements
	Implements: [Options],

	//options
	options: {
		elements: $$('div'),
		selectClass: '',
		anchorToSpan: false
	},
	
	//initialization
	initialize: function(options) {
		//set options
		this.setOptions(options);
		//set clickable
		this.doClickables();
	},
	
	//a method that does whatever you want
	doClickables: function() {
		
		//for all of the elements
		this.options.elements.each(function(el) {
			
			//get the href
			var anchor = el.getElements('a' + (this.options.selectClass ? '.' + this.options.selectClass : ''))[0];
			
			//if we found one
			if($defined(anchor)) {
				
				//add a click event to the item so it goes there when clicked. 
				this.setClick(el,anchor.get('href'));
				
				//modify anchor to span if necesssary
				if(this.options.anchorToSpan) {
					var span = new Element('span',{
						text: anchor.get('text')
					}).replaces(anchor);
				}
			}
			
		},this);
	},
	
	//ads the click event
	setClick: function(element,href) {
		
		element.addEvent('click', function() {
			window.location = href;
		});
	}
	
});


var pageScroll = {
	roll: function() { new SmoothScroll ({duration: 800,transition:Fx.Transitions.Expo.easeInOut}); }
};


window.addEvent('domready', function() {
	
	Passcal.init();
	
	var clix = new dwClickables({
		elements: $$('.clickable'),
		anchorToSpan: false
	});
	
	new SmoothScroll({ duration:700 }, window); //700 milliseconds to get there
	
});
