// JavaScript Document
Array.implement({

  random: function(){
		if (this.length != 2) return null;
		var min = this.min();
		var max = this.max();
		return (Math.random()*(max+1-min)).toInt()+min;
	}

});

var Fake3D = new Class({

  Implements: [Options, Events],
	
	options: {
		direction: 'all', // horizontal | vertical | all
		item: '.layer',
		source: null,
		baseZindex: 3,
		offset: 5
	},
	
	initialize: function(container, options){

		this.container = document.id(container);
		this.setOptions(options);
		
		this.source = (this.options.source ? document.id(this.options.source) : this.container);

    this.dimensions = window.getSize();
		
		this.items = this.container.getElements(this.options.item);
		
		this.itemData = new Array();
		
		this.items.each(function(item, index){
			var data = {
				position: {x: item.getStyle('left').toInt(), y: item.getStyle('top').toInt()},
				coeficient: item.getComputedStyle('z-index').toInt()/this.options.baseZindex
			}
			this.itemData[index] = data;
		}, this);
		
		this.source.addEvent('mousemove', function(event){
		  this.render(event.client);
		}.bind(this));
		
		this.center = this.getCenter();
		
	},
	
	getCenter: function(){
		var position = this.container.getPosition();
		var containerSize = this.container.getSize();
		var center = {
			x: (position.x + containerSize.x/2).toInt(),
			y: (position.y + containerSize.y/2).toInt()
		}
		
		return center;
	},
	
	reset: function(){
		this.dimensions = window.getSize();
		this.center = this.getCenter();
		return this;
	},
	
	render: function(coords){
		var coef = {
			x: (this.options.offset*(coords.x - this.center.x)/this.center.x),
			y: (this.options.offset*(coords.y - this.center.y)/this.center.y)
		}
		
		this.items.each(function(item, index){
		  item.setStyles({
			  top: this.itemData[index].position.y - coef.y*this.itemData[index].coeficient,
				left: this.itemData[index].position.x - coef.x*this.itemData[index].coeficient
			});
		}, this);
			
	}


});

var RadioGroup = new Class({

  Implements: [Events, Options],
	
	options: function(){
		//onChange: $empty(radio, index),
		//onCheck: $empty(radio, index),
		//onUncheck: $empty(radio, index)
	},
	
	initialize: function(radioGroup, options){
		this.group = $$(radioGroup);
		
		this.setOptions(options);
		
		this.current = false;
	  
		this.group.each(function(radio, index){
			var span = radio.getParent('span');
			span.addClass('ready');
		  var label = radio.getParent('label');
			label.addEvent('click', function(event){
			  event.preventDefault();
				this.check(index);
			}.bind(this));
		}, this);
		
		this.check();
		
		return this;
		
	},
	
	check: function(radioIndex){
		this.group.each(function(radio, index){
			if ($defined(radioIndex)) radio.checked = (radioIndex == index);
			var parent = radio.getParent('span');
		  if (radio.checked){
				parent.addClass('checked');
				this.fireEvent('check', [radio, index]);
				this.fireEvent('change', [radio, index]);
			} else {
				if (parent.hasClass('checked')){
					parent.removeClass('checked');
					this.fireEvent('uncheck', [radio, index]);
				}
			}
		}, this);
	},
	
	getElements: function(){
		return this.group;
	}

});

var Checkbox = new Class({

  Implements: [Events, Options],
	
	options: function(){
		//onChange: $empty(radio)
	},
	
	initialize: function(element, options){
		this.element = document.id(element);
		
		this.setOptions(options);
	  
		this.span = this.element.getParent('span');
    this.span.addClass('ready');
		this.label = this.element.getParent('label');
		this.label.addEvent('click', function(event){
			event.preventDefault();
			this.check();
		}.bind(this));
		
		this.check(true);
		
		return this;
		
	},
	
	check: function(onload){
		if (!onload) this.element.checked = !this.element.checked;
		this.checked = this.element.checked;
		this.element.checked ? this.span.addClass('checked') : this.span.removeClass('checked');
    if (!onload) this.fireEvent('change', this.element);
	},
	
	checked: false

});

var oldIE = (Browser.Engine.trident && Browser.Engine.version < 5);

var GiantLink = {
	
	init: function(){
		this.container = $('giant');
		this.anchor = this.container.getElement('a');
		this.logo = this.container.getElement('img').setStyle('cursor', 'pointer');
		this.logo.addEvent('click', function(){
		  window.location = this.anchor.get('href');
		}.bind(this));
																		
	}
 };

var PNGFix = new Class({

  initialize: function(img){
		this.img = $(img);
    this.src = this.img.get('src');
		this.img.set('src', '/img/blank.gif');
		this.img.style.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="'+this.src+'", sizingMethod="image")';
	}
	
});

var Clouds = {
	
	options: {
		position: [0, 50],
		speed: [3, 10],
		images: [
		  'cloud-1',
			'cloud-2',
			'cloud-3',
			'cloud-4'
		],
		delay: 2000,
		count: 10,
		zindex: [0, 2, 4, 6]
	},
	
	init: function(){
		
		this.container = new Element('div', {'id': 'cloud-container' }).inject(document.body);
		
		this.stack = new Array(); // cloud stack
		
		for (var i = 0; i<this.options.count; i++){
			this.createCloud(true);
			//this.createCloud.delay(i*this.options.delay, this);
		}
		
	},
	
	createCloud: function(onload){

		var cloud = new Cloud(this.container, {
			position: this.options.position.random(),
			speed: this.options.speed.random(),
			zindex: this.options.zindex.getRandom(),
			image: this.options.images.getRandom(),
			randomStart: !!onload,
			onComplete: function(cloud){
				this.stack.erase(cloud);
				this.createCloud();
			}.bind(this),
			onStart: function(cloud){
				this.stack.push(cloud);
			}.bind(this)
		});

	},
	
	pause: function(){
		this.stack.each(function(cloud){
		  cloud.pause();
		});
	},
	
	resume: function(){
		this.stack.each(function(cloud){
		  cloud.resume();
		});
	}
	
 };

var Cloud = new Class({

  Implements: [Events, Options],
	
	options: {
		position: 150,  // in pixels from top of the viewport
		speed: 10,       // 1-10, 10 fastest
		image: 'cloud-1',
		transition: 'linear',
		width: 238,
		randomStart: true
	},
	
	initialize: function(container, options){
		
		this.container = document.id(container);
		
		this.setOptions(options);

		this.size = this.container.getSize();
    this.cloud = new Element('div', {
		  'class': 'cloud {image}'.substitute({image: this.options.image}),
			'styles': {
				'bottom': this.options.position,
				'left': 0
			}
		});
		
		this.startPosition = -this.options.width;
		if (this.options.randomStart){
			this.startPosition = (this.size.x*2/3*Math.random()).toInt();
		}

		this.fx = new Fx.Tween(this.cloud, {
		  property: 'left',
			duration: ((this.size.x-this.startPosition)*200/this.options.speed).toInt(),
			link: 'cancel',
			transition: this.options.transition
		}).set(this.startPosition);
		
		this.cloud.inject(this.container);
		
		this.fireEvent('start', this);
		
		this.fx.start(this.size.x).chain(function(){ this.fireEvent('complete', this); }.bind(this));
		
		return this;
		
	},
	
	percent: function(number, percent){
		return number*percent/100
	},
	
	pause: function(){
		this.fx.pause();
	},
	
	resume: function(){
		this.fx.resume();
	}

});

var Nntj =  {
	
	init: function(){
		
		// giant
		GiantLink.init();
		
		// target=_blank replacement
		$$('a._blank').each(function(anchor){
		  anchor.set('target', '_blank');
		});
				
		/*
		// png fix
		if (oldIE){
			$$('img[src$=png]').each(function(img){
				new PNGFix(img);
			});
		}
		*/
		
		// clickable blocks
		$$('.clickable').each(function(item){
		  item.setStyle('cursor', 'pointer');
			var anchor = item.getElement('a');
			if (anchor){
				item.addEvent('click', function(){
				  window.location = anchor.get('href');
				});
				anchor.addEvent('click', function(event){
				  event.stopPropagation();
				});
			}
			
		});
		
		// boxy
		$$('.box').each(function(item){
		  item.addEvent('mouseover', function(event){
				  item.getElement('h2').addClass('active');
			});
			item.addEvent('mouseout', function(event){
				  item.getElement('h2').removeClass('active');
			});
		});
		
		
		this.anchors = $$('.anchors');
		if (this.anchors.length > 0){
			new Fx.SmoothScroll({
			  links: '.anchors a',
				transition: Fx.Transitions.Quint.easeInOut,
				duration: 1000
			});
		}
		
		// ReMooz
		$$('a.remooz').each(function(element){
		  new ReMooz(element, {
          centered: true,
					opacityResize: 0,
					cutOut: false,
          origin: element.getElement('img')
      });
		});
		
		//  Form loader
		Nntj.Form.init();
		
		// Clouds
		Clouds.init();
		
		// 3D
		if (document.id('homepage')){
			if (Browser.Engine.trident && Browser.Engine.version <= 6){
				new Fake3D('hp-deti', {
					source: document,
					offset: 5
				});
			} else {
				new Fake3D('hp-deti', {
					source: window,
					offset: 5
				});
			}
		}
		
	}
	
 };

Nntj.Form = {

  init: function(){
		
		this.forms = document.getElements('form');

		this.forms.each(function(form){
			var formID = form.get('id');
			if (formID){
				var objectTitle = formID.replace('form-','').camelCase().capitalize();
				if (Nntj.Form[objectTitle]) Nntj.Form[objectTitle].init();
			}
		});
		
	}

 };
 
Nntj.Form.Questionary = {
	
	init: function(){
		
		this.form = document.id('form-questionary');
		
		this.fieldsets = this.form.getElements('fieldset');
		
		this.fieldsets.each(function(fieldset, index){
		  var radios = fieldset.getElements('input[type=radio]');
			var checkboxes = fieldset.getElements('input[type=checkbox]');
			var options = {};

      switch (index){
				case 3: 
				  options = {
						onChange: function(radio, radioIndex){
              var el = $('lessons_valid');
              el.value = 0; // For use in validation
							if (radioIndex == 0){
								this.fieldsets[4].show();
								this.fieldsets[5].hide();
                el.name = 'lessons_3'; // For use in validation
							} else {
								this.fieldsets[4].hide();
								this.fieldsets[5].show();
                el.name = 'lessons_2'; // For use in validation
							}
							this.renumber(4);
						}.bind(this)
					}
				break;
				
			}
			if (radios.length){
				new RadioGroup(radios, options);
			}
			checkboxes.each(function(checkbox){
			  new Checkbox(checkbox);
			});
		
		}, this);

	},
	
	renumber: function(start){
		var idIndex = 1;
		this.fieldsets.each(function(fieldset, index){
		  if (fieldset.get('id') != 'adresa'){
				if (fieldset.getStyle('display') == 'none'){
					fieldset.set('id', '')
				} else {
					fieldset.set('id', 'otazka'+idIndex);
					idIndex++;
				}
			}
		});
	}
}


Nntj.Form.Poll = {
	
	init: function(){
		
		this.form = document.id('form-poll');
		
		this.radioGroup = new RadioGroup(this.form.getElements('input[type=radio]'));
	}
 };



window.addEvent('domready', function(){

  Nntj.init();
	
	if(document.id('box-stan')) {
		var container = document.id('box-stan');
		
		container.addEvent('mouseenter', function(event){
		  container.getElement('h2').addClass('active');
		});
		container.addEvent('mouseleave', function(event){
		  container.getElement('h2').removeClass('active');
		});
	}
	
	if(document.id('pritt-flash')) {
		new Swiff('/flash/prittel-banner-215x45.swf', {
				id: 'pritt-flash-banner',
				width: 240,
				height: 70,
				container: 'pritt-flash',
				params: {
						wmode: 'opaque',
						bgcolor: '#ffffff'
				}
		});

	}

});

