var DevonStudio = {
	start: function() {
		this.addGoogleStats();
		this.morphBackground();
		this.ajaxifyForm();
		this.setMail();
	},
	
	morphBackground: function() {
		var colors = [
		'273434', '342732',	'343327',
		'273433',	'342727',	'342734',
		'2e3427',	'27342e',	'0e171a'
		];
		
		var i = 0;
		function morph( color ) {
			$('html').animate({
				'backgroundColor': '#' + color,
				'color': '#' + color
			}, 7500, function() {
				morph( colors[ i = ( i >= ( colors.length - 1 ) ) ? 0 : ( i + 1 ) ] )
			});
		}
		
		morph( colors[i] );
	},
	
	addGoogleStats: function() {
		var GAID = 'UA-165199-4';
		var head = document.getElementsByTagName('head')[0];
		var scr = document.createElement('script');
		scr.type = 'text/javascript';
		scr.src = (location.protocol == 'http:' ? 'http://www.' : 'https://ssl.') + 'google-analytics.com/'+'urchin.js';
		var once = 0;
			function runMe() { 
				if (scr.readyState == 'loading' || urchinTracker == undefined || once) { return; }
				clearInterval(timer);
				once++;
				_uacct = GAID;
				urchinTracker();
			}
		var timer = setInterval(runMe, 2000);
		scr['onload'] = runMe;
		scr['onreadystatechange'] = runMe;
		head.appendChild(scr); 
	},
	
	ajaxifyForm: function() {
		function validateFields() {
			return true;
		}
		
		$('form').bind('submit', function(e) {
			var self = this;
			if (validateFields(this)) {
				var cover = document.createElement('div');
				var messg = document.createElement('span');				
				$(messg).addClass('wait').append('Wysyłanie wiadomości…');
				$(cover).addClass('cover').append(messg);
				$(this).append(cover);
				$.ajax({
					type: 'POST',
					url: 'ajax.php',
					data: $(self).serialize() + '&ajax=true',
					dataType: 'xml',
					success: function(data) {
					
						var status = 'NOTOK';
						var confirmation = 'Wystąpił błąd. :(';
						var node = data.getElementsByTagName('status')[0];
						
						if (node) {
							if (node.firstChild) {
								status = node.firstChild.nodeValue;
								confirmation = data.getElementsByTagName('confirmation')[0].firstChild.nodeValue;
							}
						}
						
						$(messg).addClass(status.toLowerCase()).removeClass('wait').text(confirmation);
						
						var timer = setInterval(function() {
							$(cover).fadeOut(1000, function() {
								$(this).remove(); 
								$(self).find('input:first').focus()
								if (status == 'OK') {
									self.reset()
								}
							});
							clearInterval(timer);
						}, 1500);					
						
					}
				});
			} else {
				alert('Źle wypełniony formularz');
			}
			e.preventDefault();
		});
	},
	
	setMail: function() {
		$('#mailme').each(function() {
			$this = $(this);
			$this.attr('href', $this.attr('href').replace('malpa', '@').replace('kropka', '.'));
			$this.text($this.text().replace('malpa', '@').replace('kropka', '.'));
		});
	}
}

$(function() {
	DevonStudio.start();
});