// JavaScript Document
var Slideshow = new Class({
	
	Implements: Options,
	
	options: {
    boxSelector: '#top-box .box',
    toggleSelector: '#switcher .toggle',
    fxDuration: 500,
    changeSlideDuration: 10000,
		start: 0
	},
 
	initialize: function(options){
		
		this.setOptions(options);
		
		this.slides = $$(this.options.boxSelector);
		
		this.previous = 0;
		this.current = 0;
		this.count = this.slides.length;
		this.repeater = null;
		
		// exit if only one slide available
		if (this.count == 0) return null;
		
		// slides
		this.slides.each(function(slide, index){
		  var fx = new Fx.Tween(slide, { property: 'opacity', duration: this.options.fxDuration, link: 'cancel' });
		  index == this.options.start ? fx.set(1) : fx.set(0);
			slide.addEvents({
			  'hide': fx.start.bind(fx, 0),
			  'show': fx.start.bind(fx, 1)
			});
		}, this);
		
		// toggles
		this.toggles = $$(this.options.toggleSelector);
		this.toggles.setStyle('cursor', 'pointer');
		this.toggles.each(function(toggle, index){
		  toggle.addEvents({
			  'mouseenter': function(){
					$clear(this.repeater);
					this.show(index);
				}.bind(this),
				'mouseleave': function(){
					this.repeater = this.animate.periodical(this.options.changeSlideDuration, this);
				}.bind(this)
			});
		}, this);
		
	  // initialize starting slide
		this.toggles[this.options.start].addClass('active');
		
		// run
		this.repeater = this.animate.periodical(this.options.changeSlideDuration, this);
	},
	
	show: function(index){
		this.current = index;
		if (this.current != this.previous){
			this.toggles[this.previous].removeClass('active');
			this.toggles[this.current].addClass('active');
			this.slides[this.previous].fireEvent('hide');
			this.slides[this.current].fireEvent('show');
			this.previous = this.current;
		}
	},
	
	animate: function(){
		this.show((this.previous + 1)%this.count);
	}
	
});

var AutoPin = new Class({

  Implements: Options,
		
	options: {
		offset: 20
	},
	
	initialize: function(element, options){
		this.element = document.id(element);
		this.setOptions(options);
		
		this.ie6 = (Browser.Engine.trident && Browser.Engine.version == 4);
		
		this.top = this.element.getPosition().y - this.options.offset;
		
		this.body = document.id(document.body);
		
		this.scrolled = this.body.getScroll().y;
		
		if (this.isPinnable()) this.check();
		
		// scroll event
		window.addEvent('scroll', this.check.bind(this));
  },
	
	check: function(){
		if (this.ie6){
			this.element.setStyle('margin-top', Math.max(0, this.body.getScroll().y - this.top));
		} else {
			this.isPinnable() ? this.pin() : this.unpin();
		}
	},
	
	isPinnable: function(){
		return (this.body.getScroll().y - this.top > 0 );
	},
	
	pin: function(){
		this.element.setStyles({
			'position': 'fixed',
			'width': 221,
			'top': this.options.offset
		});
	},
	
	unpin: function(){
		this.element.setStyles({
		  'position': 'static',
			'width': 'auto',
			'top': 'auto'
		});
	}

});

var Clickable = new Class({

  initialize: function(element){
		this.element = document.id(element);
		this.anchor = this.element.getElement('a');
		if (!this.anchor) return null;
		
		this.element.setStyle('cursor', 'pointer');
		this.element.addEvent('click', function(event){
		  if (event.target != this.anchor){
				window.location = this.anchor.get('href');
			}
		}.bind(this));
	}

});

var Help = {
	
	init: function(){
		
		this.width = 250;
		
		this.helpDesc = document.id('inline-help-desc').addClass('active');
		
		this.offsetParent = this.helpDesc.getOffsetParent();
		
		this.links = $$('a[rel=help]');
		
		this.links.each(function(element){

      var helpContent = document.id(element.get('href').slice(1));
			if (helpContent.hasClass('dont-close')) this.addCloseButton(helpContent);
			
  		var img = element.getElement('img');
			
			element.setStyle('cursor', 'help');
		  element.addEvents({
			  'click': function(event){ event.preventDefault(); },
			  'mouseenter': function(){
					helpContent.addClass('active');
					img.set('src', '/img/help-off.gif');
					// helpContent position
					var coords = helpContent.getCoordinates();
					var position = element.getPosition(this.offsetParent);
					helpContent.setStyles({
						'left' : position.x - coords.width,
						'top' : position.y - coords.height + 18
					});
				}.bind(this),
				'mouseleave': function(){
					if (!helpContent.hasClass('dont-close')){ helpContent.removeClass('active'); }
					img.set('src', '/img/help.gif');
				}
			});
		}, this);

  },
	
	addCloseButton: function(helpContent){
		var closeButton = new Element('span', {
			'title': 'Zavřít',
		  'styles': {
				'display'    : 'block',
				'width'      : '12px',
				'height'     : '12px',
				'background' : 'url(/img/help-close-button.gif) no-repeat left top',
				'position'   : 'absolute',
				'top'        : '3px',
				'right'      : '3px',
				'cursor'     : 'pointer'
			},
			'events': {
				'click': function(){ helpContent.removeClass('active'); },
				'mouseenter': function(){ this.setStyle('background-position','left bottom'); },
				'mouseleave': function(){ this.setStyle('background-position','left top'); }
			}
		}).inject(helpContent);
	}
	
 };

// Main object - loader
var Fine = {
	
	init: function(){
		
		// id loader
		if (document.id('page-homepage')) new Slideshow();
		if (document.id('inline-help-desc')) Help.init();
		if (document.id('page-faq')) Fine.Faq.init();
		if (document.id('page-pricelist')) Fine.Pricelist.init();
		if (document.id('page-program-list')) Fine.ProgramList.init();
		if (document.id('tools')) Fine.Languages.init();
		
		// remooz
		this.imageLinks = $$('div.pics a, p.thumbs a, #middle-column a.remooz, #project-gallery a[href$=jpg], #project-gallery a[href$=png]');
		if (this.imageLinks.length > 0) this.zoom(this.imageLinks);
		
		// movable block
		this.fixedBlocks =  $$('#right-column div.auto-pin, #middle-column div.countries-column ul');
		if (this.fixedBlocks.length > 0){
		  this.fixedBlocks.each(function(block){
			  new AutoPin(block);
			});
		}
		
		// smooth scroll
		this.smoothscroll = new Fx.SmoothScroll({ duration: 800, transition: 'expo:in:out', links: 'a[rel!=help]' });
		
		// clickable elements
		$$('#title, #maintenance-program').each(function(element){ new Clickable(element); });
		
		// forms
		Fine.Form.init();
		
		// global sliders
		this.slideBlocks = $$('div.slide-block');
		this.slideToggles = $$('a.slide-toggle, p.slide-toggle');
		this.slideToggles.each(function(toggle, index){
		  if (this.slideBlocks[index]){
				this.slideBlocks[index].set('slide', { duration: 'short', link: 'cancel' }).slide('hide');
				toggle.addEvent('click', function(event){
				  event.preventDefault();
					this.slideBlocks[index].slide('toggle');
				}.bind(this));
			}
		}, this);
		
	},
	
	zoom: function(imageLinks){
		imageLinks.each(function(imageLink){
		  new ReMooz(imageLink, {
			  'origin': 'img',
				'shadow': 'onOpen',
				'resizeFactor': 0.9,
				'cutOut': false,
				'opacityResize': 0,
				'dragging': true,
				'centered': true
			});
		});
	}
	
 };

Fine.Faq = {
	
	init: function(){
		
		this.container = document.id('middle-column');
		
		this.url = new URI();
		this.anchor = this.url.get('fragment');
		
		this.start = 0;
		
		this.activeItem = null;
		
		
		if (this.anchor) this.activeItem = document.id(this.anchor);
		
		if (this.activeItem) this.start = this.container.getElements('dt').indexOf(this.activeItem);
		
	  new Fx.Accordion('dt', 'dd', {
			show: this.start,
			opacity: false,
			onActive: function(toggle, element){
				toggle.tween('background-color', '#EEEEEE');
			}.bind(this),
			onBackground: function(toggle, element){
				toggle.tween('background-color', '#FFFFFF');
			}
		});
		
		this.container.addClass('ready');

	}
	
 };
 
Fine.Pricelist = {
	
	init: function(){
		
		this.container = document.id('middle-column');
		
		this.container.getElements('span.toggler').setStyle('cursor','pointer');
		this.container.getElements('span.toggler em').setStyle('visibility', 'visible');

    new Fx.Accordion(this.container.getElements('span.toggler'), this.container.getElements('div.fieldset'), {
			show: -1,
			opacity: false,
		  alwaysHide: true,
			onActive: function(toggler, element){
				toggler.getElement('em').tween('opacity', 0);
				toggler.getElement('img').setProperty('src','/obchod/img/toggler-close.gif');
			},
			onBackground: function(toggler, element){
				toggler.getElement('em').tween('opacity', 1);
				toggler.getElement('img').setProperty('src','/obchod/img/toggler.gif');
			}
		});
		
		this.container.addClass('ready');

	}
	
 };
 
Fine.ProgramList = {
	
	init: function(){
		
		$$('#middle-column-content .toggler').addEvent('click', function(event){ event.preventDefault(); });
		
		new Fx.Accordion('#middle-column-content .toggler', '#middle-column-content .slide', {
			opacity: false,
			show: -1,
			alwaysHide: true
  	});
		
	}
 };
 
Fine.Languages = {
	
	init: function(){
		this.langs = document.id('tools').getElements('li');
		
		this.langs.each(function(lang){
	    var bubble = lang.getElement('p');
	    var anchor = lang.getElement('em');
			var link = lang.getElement('a');
			if (link){
				anchor.addEvent('click', function(){
				  window.location = link.getProperty('href');
				});
			}
			lang.addEvents({
			  'mouseenter': function(){
					bubble.setStyle('display', 'block');
				},
				'mouseleave': function(){
					bubble.setStyle('display', 'none');
				}
			});
			bubble.setStyles({
			  'padding-top': 14,
				'top': 12,
				'background-position' : 'left 2px'
			});
	  });
	}
	
 };

Fine.Form = {

  init: function(){
		
		if (document.id('support-form')) Fine.Form.Support.init();
		
	}

 };
 
Fine.Form.Support = {
	
	init: function(){
		this.form = document.id('support-form');
		
		// antispam field
		this.anti = new Element('input', {
		  'type' : 'hidden',
			'name' : 'anti',
			'value' : 'fine'
		}).inject(this.form);
		
		// slides
		this.slides = this.form.getElements('div.form-slide');
		this.toggles = this.form.getElements('a.form-toggle');
		if (this.slides.length == this.toggles.length){
			this.toggles.each(function(toggle, index){
			  var slideFx = new Fx.Slide(this.slides[index], { duration: 300, link: 'cancel' }).hide();
				toggle.addEvent('click', function(event){
				  event.preventDefault();
					if (toggle.hasClass('open')){
						slideFx.slideOut();
						toggle.set('html', '?').removeClass('open');
					} else {
						slideFx.slideIn();
						toggle.set('html', '&times;').addClass('open');
					}
				});
			}, this);
		}
		
	}
 };

window.addEvent('domready', function(){

  Fine.init();

});