// JavaScript Document

/*****************/
/* PREREQUISITES */
/*****************/
Number.implement({

  inRange: function(min, max){
		if (min <= this && this <= max) return true
	}

});

/*
---

script: Group.js

name: Group

description: Class for monitoring collections of events

license: MIT-style license

authors:
  - Valerio Proietti

requires:
  - Core/Events
  - /MooTools.More

provides: [Group]

...
*/

(function(){

this.Group = new Class({

	initialize: function(){
		this.instances = Array.flatten(arguments);
	},

	addEvent: function(type, fn){
		var instances = this.instances,
			len = instances.length,
			togo = len,
			args = new Array(len),
			self = this;

		instances.each(function(instance, i){
			instance.addEvent(type, function(){
				if (!args[i]) togo--;
				args[i] = arguments;
				if (!togo){
					fn.call(self, instances, instance, args);
					togo = len;
					args = new Array(len);
				}
			});
		});
	}

});

})();

    var Parallax = new Class({
    Implements: [Options, Events],
		options: {
			container: window,
			el: null,
			elInner: null,
			start: 0,
			elFaktor: -0.6,
			elInnerFaktor: 0.5
    },
    initialize: function(options) {
		this.setOptions(options);
		this.container = document.id(this.options.container);
		this.go();
    },
    go: function() {
		if(this.options.el) {
			var el = this.options.el;
			var elInner = this.options.elInner;
			var elFaktor = this.options.elFaktor;
			var elInnerFaktor = this.options.elInnerFaktor;
			var start = this.options.start;
			var stop = el.getCoordinates().top + el.getSize().y;
			
			this.container.addEvent('scroll', function(e) {
				var containerPos = this.getScroll().y;
				if((containerPos >= start) && (containerPos <= stop)) {
					var elPos = containerPos * elFaktor;
					var elInnerPos = containerPos * elInnerFaktor;
					el.setStyle('background-position', '0 ' + elPos + 'px');
					elInner.setStyle('background-position', '0 ' + elInnerPos + 'px');
			}
		});
		}
    }
    }); 



var DoubleSlider = new Class({

  Implements: [Options, Events],
	
	options: {
		knob: {
			min: '.knob-min',
			max: '.knob-max'
		},
		limit: {
			min: '.input-min',
			max: '.input-max'
		},
		output: {
			min: '.output-min',
			max: '.output-max'
		},
		range: '.range',
		loadedClass: 'loaded',
		diff: 100,
		stepSize: 500
	},
	
	initialize: function(container, options){
		var self = this;
		
		this.loaded = false;

    this.setOptions(options);
		this.container = document.id(container);
		this.range = this.container.getElement(this.options.range);
		
		this.knob = this.toElements(this.options.knob);
		this.knob.minText = this.knob.min.getElement('span');
		this.knob.maxText = this.knob.max.getElement('span');
		
		this.limit = this.toElements(this.options.limit);
		this.limit.minValue = this.limit.min.get('value').toInt();
		this.limit.maxValue = this.limit.max.get('value').toInt();
		
		this.output = this.toElements(this.options.output);
		
		this.start = {
			min: (this.output.min.value ? this.output.min.value.toInt() : this.limit.minValue),
			max: (this.output.max.value ? this.output.max.value.toInt() : this.limit.maxValue)
		};
		
		this.steps = Math.ceil((this.start.max - this.start.min)/this.options.stepSize);

		this.slider = {
			min: new Slider(this.range, this.knob.min, {
				range: [this.limit.minValue, this.limit.maxValue],
				offset: -5,
				steps: self.steps,
				snap: true,
			  onChange: function(step){
					var limitValue = this.getKnobValue('max')-this.options.diff;
					if (step > limitValue){
						this.slider.min.set(limitValue);
						step = limitValue;
					}
					this.setKnobValue('min', step);
					this.output.min.value = step;
				}.bind(this),
				onComplete: function(){
		      if (this.loaded) this.fireEvent('change', [this.getKnobValue('min'), this.getKnobValue('max')]);
				}.bind(this)
			}),
			max: new Slider(this.range, this.knob.max, {
				range: [this.limit.minValue, this.limit.maxValue],
				offset: -5,
				steps: self.steps,
				snap: true,
			  onChange: function(step){
					var limitValue = this.getKnobValue('min')+this.options.diff;
					if (step < limitValue){
						this.slider.max.set(limitValue);
						step = limitValue;
					}
					this.setKnobValue('max', step);
					this.output.max.value = step;
				}.bind(this),
				onComplete: function(){
		      if (this.loaded) this.fireEvent('change', [this.getKnobValue('min'), this.getKnobValue('max')]);
				}.bind(this)
			})
		}

    // default knob values
		this.setKnobValue('min', this.start.min);
		this.setKnobValue('max', this.start.max);
		this.slider.min.set(this.start.min);
		this.slider.max.set(this.start.max);
		
		this.loaded = true;
		
	},
	
	toElements: function(hash){
		var hash = new Hash(hash);
		hash.each(function(item, key){
		  hash.set(key, this.container.getElement(item));
		}, this);
		return hash;
	},
	
	setKnobValue: function(knobType, value){
		switch (knobType){
			case 'min':
			  this.knob.minText.set('text', value+' Kč');
			break;
			case 'max':
			  this.knob.maxText.set('text', value+' Kč');
			break;
		}
	},
	
	getKnobValue: function(knobType){
		switch (knobType){
			case 'min': 
			  return this.knob.minText.get('text').toInt();
			break;
			case 'max':
			  return this.knob.maxText.get('text').toInt();
			break;
		}
	}
	

});

var Bubble = new Class({

 Implements: Options,
 
 options: {
	 element: 'div.bubble',
	 start: {	

	 },
	 end: {
	 },
	 fx: {
		 duration: 150,
		 link: 'cancel',
		 transition: Fx.Transitions.Quad.easeOut
	 }
 },
 
 initialize: function(container, options){
	 
	 this.container = document.id(container);
	 
	 this.setOptions(options);
	 
	 this.element = this.container.getElement(this.options.element);
	 this.element.setStyle('visibility', 'visible');
	 
	 this.fx = new Fx.Morph(this.element, this.options.fx);
	 this.fx.set(this.options.start);
	 
	 this.container.addEvents({
		  'mouseenter': this.fx.start.bind(this.fx, this.options.end),
			'mouseleave': this.fx.start.bind(this.fx, this.options.start)
	 });
	 
 }

});

var LinkPosition = new Class({
 	
	initialize: function(){
		var positionstep = 800;
		$$('a.ex').setStyle('top','-9999px');
			var h = Math.max(
				Math.max(document.body.scrollHeight, document.documentElement.scrollHeight),
				Math.max(document.body.offsetHeight, document.documentElement.offsetHeight),
				Math.max(document.body.clientHeight, document.documentElement.clientHeight)
				);
				
			var myheight = h-440;
			period = Math.floor(myheight/(positionstep + 500));
			
			if(period == 0) $$('a.boy1').setStyle('top','500px');
			for(i=1; i<=period; i++){

				if(i == 1){ 
					$$('a.boy'+i).setStyle('top','500px');
					if(myheight > 2500)
					$$('a.woman'+i).setStyle('top','1400px');
				}
				else
				{
					im = i-1;
					if((im*500)+585+ positionstep*i < h){
					$$('a.boy'+i).setStyle('top',(im*500)+positionstep*i+'px');
					}
					
					if((im*500)+700+620+positionstep*i < h){
					$$('a.woman'+i).setStyle('top',(im*500)+700+positionstep*i+'px');
					}
				}
				
			}
	}
});

var Countdown = new Class({

  Implements: Options,
	
	options: {
		to: '2012-02-14 09:00:00'
	},
	
	initialize: function(options){
		
		this.hourcontainer = document.id('hou');
		this.mincontainer = document.id('min');
		this.seccontainer = document.id('sec');
		
		this.setOptions(options);
		
		this.time = {
			current: new Date(),
			to: new Date.parse(this.options.to).getTime()
		}
		
		this.repeater = this.count.periodical(42, this);

	},
	

	count: function(){
		var diff = this.time.to - new Date().getTime();
		this.hours = Math.floor(diff/(1000*60*60));	
		tmp = (diff-this.hours*1000*60*60)
		this.mins = Math.floor(tmp/(1000*60));	
		tmp = (diff-(this.hours*1000*60*60+this.mins*1000*60))
		this.sec = Math.floor(tmp/(1000));	
		
		this.hourcontainer.set('text', this.hours);
		this.mincontainer.set('text', this.mins);
		this.seccontainer.set('text', this.sec);

	},
	
	format: function(int){
		var formated = new Array();
		while (int > 0){
			var num = (int%1000).toString();
			int = (int/1000).toInt();
      if (int > 0) num = num.pad(3, '0', 'left');
		  formated.push(num);
    }
		return formated.reverse().join(' ');
	}

});

/***************/
/* MAIN SCRIPT */
/***************/

// Global object
var Zazitky = {};

// Main object
Zazitky.Darky = {
	
	init: function(){
	
		this.container = document.id('content');
		
		var myParallax = new Parallax({el: $('outer'),elInner: $('inner')});
		
		if (document.id('categories')) Zazitky.Darky.Categories.init();
		if (document.id('f-countdown')) new Countdown();
		new LinkPosition();
		
		this.categorySelect = document.id('gift_type');
		if (this.categorySelect){
			this.categorySelect.addEvent('change', function(){
			  if (this.value) window.location = this.value; 
			});
		}
		
		if (document.id('filter')){
		$$('div.type').each(function(element){
			var options = element.getElements('div.options');	
				options.addEvent('click', function(){
						$$('.sitetype').setStyle('display','block');
					});
					
			var listoptions = element.getElements('div.sitetype');
			listoptions.addEvent('mouseleave', function(){
						$$('.sitetype').setStyle('display','none');
					});
		});
		}
		
		
		if (document.id('certifikat-range')){
		var arr = new Array("","12","52","92","132","172","212","252","292","332","372","412","452","492","532","572","612","652","692");
		var price = new Array("","500","1000","1500","2000","2500","3000","3500","4000","4500","5000","6000","7000","8000","9000","10000","15000","20000","25000");
		
		$$('div.range').each(function(element){
		act = parseInt($$('.actual').get('value'));
		$$('.actual-price b').set('text',price[act]+' Kč');
		$$('.actual-price').setStyle('left',arr[act]+'px');
		
			var ran = element.getElements('span.pr');	
				ran.addEvent('click', function(){
						i = this.get('id').replace("p","");
					
						$$('.actual-price').setStyle('left',arr[i]+'px');
						$$('.actual-price b').set('text',price[i]+' Kč');
						$$('.actual').set('value',i);
						Zazitky.Darky.filterItems("0", price[i]);
						new LinkPosition();
					});
					
		var next = element.getElements('span.next');	
				next.addEvent('click', function(){
				act = parseInt($$('.actual').get('value'));
				if(act < 18){
					$$('.actual-price').setStyle('left',arr[act+1]+'px')
					$$('.actual-price b').set('text',price[act+1]+' Kč')
					$$('.actual').set('value',act+1)
					Zazitky.Darky.filterItems("0", price[i]);
					new LinkPosition();
				}
				});
		
		var prev = element.getElements('span.prev');	
		prev.addEvent('click', function(){
				act = parseInt($$('.actual').get('value'));
				if(act > 1){
					$$('.actual-price').setStyle('left',arr[act-1]+'px')
					$$('.actual-price b').set('text',price[act-1]+' Kč')
					$$('.actual').set('value',act-1)
					Zazitky.Darky.filterItems("0", price[i]);
					new LinkPosition();
				}
				});
					
			
		});
		}
		
		
		this.emptyPage = document.id('empty-page');
		if (this.emptyPage) this.emptyPage.hide();
		
		
		this.prices = new Array();
		this.items = new Array();
		
		$$('#item .item').each(function(item, index){
			this.items[index] = new Zazitky.Darky.Item(item);
			this.prices.push(this.items[index].getPrice());
		}, this);
		
		if (document.id('price-range')){
			
			if (this.items.length > 1){
				this.input = {
					min: this.container.getElement('input.input-min'),
					max: this.container.getElement('input.input-max')
				}
				
				this.output = {
					min: this.container.getElement('input.output-min'),
					max: this.container.getElement('input.output-max')
				}
				
				this.input.min.value = this.output.min.value = (this.prices.min()/100).toInt()*100;
				this.input.max.value = this.output.max.value = Math.ceil(this.prices.max()/100).toInt()*100;
				
				new DoubleSlider('price-range',{
					diff: 500,
					onChange: function(min, max){
						this.filterItems(min, max);
					}.bind(this)
				});
			} else {
				document.id('price-range').hide();
			}
		}
		
		
		//active category
		$$('div.categories').each(function(element){
			var box = element.getElements('div.cat-box');	
			box.each(function(box, index){
			box.addEvent('mouseenter', function(){
					$$('div.cat-box').setStyle('opacity','0.5')
					$$(this).setStyle('opacity','1')
					
					});
					
			box.addEvent('mouseleave', function(){
					$$('div.cat-box').setStyle('opacity','1')
					});
			});
		
		});


		

	},
	
	filterItems: function(min, max){
		var visibleItems = 0;
		var group = new Group(this.items);
		group.addEvent('complete', function(){
			new LinkPosition();
		});
		this.items.each(function(item){
			if (item.price.inRange(min, max)){
				visibleItems++;
				item.show();

			} else {
				item.hide();

			}
		});
		visibleItems == 0 ? this.emptyPage.show() : this.emptyPage.hide();

	}

	
};

Zazitky.Darky.Item = new Class({
															 
	Implements: [Events, Options],
	
	options: {
		bubble: {
			element: '.more',
			start: {
        'bottom': -40,
				'opacity': 0
			},
			end: {
				'bottom': -40,
				'opacity': 1
			},
			fx: {
				duration: 150,
				link: 'cancel',
				transition: Fx.Transitions.Quad.easeOut
			}
		}
	},

  initialize: function(container){
	    var self = this;
		
		this.container = document.id(container);
	
		this.link = this.container.getElement('a').get('href');
				
		this.container.setStyle('cursor', 'pointer');
		
		this.price = this.container.getElement('p.price strong').get('text').replace('Kc','').replace(' ','').toInt();

    // item events
		this.container.addEvent('click', function(event){ 
			if (event.target.get('tag') != 'a') window.open(this.link); //window.location = this.link;
		}.bind(this));
		
		// bubble
		new Bubble(this.container, this.options.bubble);
		
		this.fx = new Fx.Tween(this.container, {
			property: 'opacity',
			duration: 200,
			link: 'cancel'
		});
		this.fx.set(1);
		
	},
	
	getPrice: function(){
		return this.price;
	},
	
	show: function(){
		var self = this;
		this.container.show();
		this.fx.start(1).chain(function(){ self.fireEvent('complete'); });
		
	},
	
	hide: function(){
		var self = this;
		this.fx.start(0).chain(function(){ self.container.hide(); self.fireEvent('complete'); });
		
	}

});

Zazitky.Darky.Categories = {
	
	init: function(){
		this.container = document.id('categories');
		
		this.items = this.container.getElements('div');
		
		this.items.each(function(item){
		  var bubble = item.getElement('div.bubble');
			if (bubble){
				var top = bubble.getStyle('top').toInt();
				
				new Bubble(item, {
					element: 'div.bubble',
					start: {
						top: top-10,
						opacity: 0
						
					},
					end: {
						top: top,
						opacity: 1
					}
				});
			}
		});
	}
 };



window.addEvent('domready', function(){

  Zazitky.Darky.init();

});

 
 

