Fx.Shake = new Class({

	Extends: Fx.Tween,

	options: {
		times: 5
	},

	initialize: function(){
		this.parent.apply(this, arguments);
		if (this.options.property) {
			this.property = this.options.property;
			delete this.options.property;
		}
		this._start = this.start;
		this.start = this.shake;
		this.duration = this.options.duration;
		this.shakeChain = new Chain();
	},

	complete: function(){
		if (!this.shakeChain.$chain.length && this.stopTimer()) this.onComplete();
		else if (this.shakeChain.$chain.length) this.shakeChain.callChain();
		return this;
	},

	shake: function(property, distance, times){
		if (!this.check(arguments.callee, property, distance)) return this;
		var args = Array.link(arguments, {property: String.type, distance: $defined, times: $defined});
		property = this.property || args.property;
		times = args.times || this.options.times;
		this.stepDur = this.duration / (times + 1);
		this._getTransition = this.getTransition;
		this.origin = this.element.getStyle(property).toInt()||0;
		this.shakeChain.chain(
			function(){
				this.shakeChain.chain(
					function() {
							this.stopTimer();
							this.getTransition = this._getTransition;
							this.options.duration = this.stepDur / 2;
							//stage three, return to origin
							this._start(property, this.origin);
					}.bind(this)
				);
				this.getTransition = function(){
					return function(p){
						return (1 + Math.sin( times*p*Math.PI - Math.PI/2 )) / 2;
					}.bind(this);
				}.bind(this);
				this.stopTimer();
				this.options.duration = this.stepDur * times;
				//stage 2: offset to the other side using the shake transition
				this._start(property, this.origin - args.distance);
			}.bind(this)
		);
		this.options.duration = this.stepDur / 2;
		//stage 1: offset to one side
		return this._start(property, this.origin + args.distance);
	},

	onCancel: function(){
		this.parent();
		this.shakeChain.clearChain();
		return this;
	}

});

Element.Properties.shake = {

	set: function(options){
		var shake = this.retrieve('shake');
		if (shake) shake.cancel();
		return this.eliminate('shake').store('shake:options', $extend({link: 'cancel'}, options));
	},

	get: function(options){
		if (options || !this.retrieve('shake')){
			if (options || !this.retrieve('shake:options')) this.set('shake', options);
			this.store('shake', new Fx.Shake(this, this.retrieve('shake:options')));
		}
		return this.retrieve('shake');
	}

};

Element.implement({

	shake: function(property, distance, times, options){
		var args = Array.link(arguments, {property: String.type, distance: Number.type, times: Number.type, options: Object.type});
		if (args.options) this.set('shake', args.options);
		this.get('shake').start(args.property, args.distance, args.times);
		return this;
	}

});

var AutoGrow = new Class({

	Implements: [Options, Events],

	options: {
		interval: 250, // update interval in milliseconds
		margin: 18, // gap (in px) to maintain between last line of text and bottom of textarea
		minHeight: 125 // minimum height of textarea
	},

	initialize: function(textarea, options) {
		this.textarea = $(textarea);
		this.options.minHeight = textarea.clientHeight;
		this.setOptions(options);
		this.dummy =  new Element("div", {
			styles:	{
				"overflow-x" : "hidden",
				"position"   : "absolute",
				"top"        : 0,
				"left"       : "-9999px",
			}
		}).setStyles(this.textarea.getStyles("font-size", "font-family", "width", "line-height", "padding")).inject(document.body);
		this.resize.periodical(this.options.interval, this);
	},

	resize: function() {
		var html = this.textarea.get('value').replace(/\n|\r\n/g, '<br>X');
		if (this.dummy.get("html").toLowerCase() != html.toLowerCase()){
			this.dummy.set("html", html);
			var triggerHeight = this.dummy.getSize().y + this.options.margin;
			if (this.textarea.clientHeight != triggerHeight)
			{
				var newHeight = Math.max(this.options.minHeight, triggerHeight);
				this.textarea.tween("height", newHeight);
			}
		}
	}

});
	window.addEvent('domready',function(){
		var curbgpos = $('dummyicon').getStyle('background-position');

		if ($('kontakt')) {
			przygotujForme();
		};
		if ($('portfolio')) {
			portfolio();
		};

	$$('a.menuitem').each(function(el){
		var bgpos=el.rel;
		el.addEvent('mouseenter',function(el){
			$('dummyicon').morph({'background-position' : '0 '+ bgpos +'px'});
		});
		el.addEvent('mouseleave',function(el){
		
		$('dummyicon').morph({'background-position' : curbgpos});
		});
		el.addEvent('click',function(e) {
			_gaq.push(['_trackPageview', el.get('href')]);
			e.stop();
			$('dummyicon').morph({'background-position' : '0 '+ bgpos +'px'});
			$('dummyicon').setStyle('background-position', '0 '+ bgpos +'px');
			curbgpos='0 '+ bgpos +'px';
			$$('g:plusone').each(function(gplus){
				gplus.set('href',el.get('href'));
		console.log(el.get('href'));
			});
			new Request({ 
				url: el.get('href'),
				onRequest: function() { 
					$('body').fade('out');
				}, 
				onSuccess: function(res) { 
					$('body').set('html',res).fade('in');
					//robię magię
					if (el.get('href')== '/cv/kontakt/' || el.get('href')== '/cv/en/kontakt/') {
						przygotujForme();
					}
					if (el.get('href')== '/cv/portfolio/' || el.get('href')== '/cv/en/portfolio/') {
						portfolio();
					}
					gapi.plusone.go();
				} 
			}).send();
			History.push(el.get('href')); // Push the new URL
		});
		el.addEvent('contextmenu',function(e) { 
			e.stop(); 
			el.fireEvent('click',e);
		}); 
	});
	$('dummyicon').addEvent('click',function(){
		var m = $('dummyicon');
		var fx = new Fx.Tween(m,{
			duration: 150,
			onComplete: function(){ 
					m.setStyle('background-image','url(/templates/default/images/icos.php?'+ $time()+')');
					m.fade('in');
			}
		});
		fx.start('opacity',1,0);
	});
	$('dummyicon').addEvent('contextmenu',function(e) { 
		e.stop(); 
		$('dummyicon').fireEvent('click',e);
	}); 

});

function przygotujForme() {
	;
	var thisColor = '#999', restingPosition = 2;
	$$('form#kontakt .slider label').each(function(label){
		var labelColor= label.get('rel');
		label.setStyles({
			color: (labelColor ? labelColor : thisColor),
			position: 'absolute',
			top: restingPosition,
			left: 25,
			display: 'inline',
			'z-index': 99
		});
		var input = label.getNext('input');
		var width = label.getSize().y;
		var move = width + restingPosition;
		if(input.get('value') !== '') {
			label.morph({'top':0 - move,'left':0,'color':thisColor});
		}
		input.addEvents({
			focus: function() {
				if(input.get('value') == '') {
					label.morph({'top':0 - move,'left':0,'color':thisColor});
				}
				else {
					label.setStyles({'top':0 - move,'left':0,'color':thisColor});
				}
			},
			blur: function() {
				if(input.get('value') == ''){
					label.morph({'top':restingPosition,'left':25,'color':labelColor});
				}
			}
		});
	});
	
		$(document.body).getElements('textarea').each(function(el) {
			new AutoGrow(el);
		});
	$$('#kontakt .image').each(function (el) {
		el.addEvents({
			'click' : function(e) {
				e.stop();
				var jest = 0;
				$$('input').each( function(inp) {
					if (inp.get('value') !== '' ) {
							jest = 1;
					} else {
						if (inp.get('id') !== 'telefon' && inp.get('type') !=='image') {
							jest = 0;
						}
					}		
					if(el.get('id')=='clean_it') inp.set('value','').fireEvent('blur'); 
				});
				if(el.get('id')=='clean_it') {$('tresc').set('value','').fireEvent('blur');}
				if(el.get('id')=='send_it'){
					if($('tresc').get('value') == '') {jest = 0;}
					if (jest == 1) {
						wyslijForme();
					} else {
						$('send_it').shake('margin-right',5,3);
					}
				}
			},
			'mouseenter': function(){
				el.fade('in');
			},
			'mouseleave': function(){
				el.fade('0.4');
			},
		}).setStyle('opacity','0.4');
	});

};
function wyslijForme() {
	$('kontakt').set('send', {
		url: '/mailme.php',
		method: 'post',
		onComplete: function(response) {
			if (response =='ok') {
			$('body').chain( $('body').fade('out'), $('body').set('html','<h3>Dziękuję za kontakt</h3>'), $('body').fade('in'));
			} else {
				reload();
				$('captcha').set('value','');
				JSON.decode(response).each(function(el){
					if($(el)!== 'undefined') {
						$(el).highlight('#f00');
					}
				});
			}
		}
	});
$('kontakt').send()
}
function portfolio() {
	$$('ul.portfolio li span.img').each(function(el){
		var clone = el.clone().set({
			styles: {
				opacity: '0.1'
			},
			events: {
				mouseenter: function(){ this.fade('in') },
				mouseleave: function(){ this.fade('0.1') }
			}
		}).fade('0.1').addClass('over_icon');
		el.grab(clone);
	});
}
function reload() {
	var dummy = $time() + $random(0, 100);
	document.getElementById('captcha_img').src="/captcha/captcha.php?"+dummy;
};

