String.prototype.normalize = Number.prototype.normalize = function(prcsn){
	return (+(parseFloat(this.valueOf()).toFixed(prcsn || 2))) || 0;
};
// Array Remove - By John Resig (MIT Licensed)
Array.prototype.remove = function(from, to) {
	var rest = this.slice((to || from) + 1 || this.length);
	this.length = from < 0 ? this.length + from : from;
	return this.push.apply(this, rest);
};
$(function(){
	var url = document.location.pathname,
	language = url.match(/\/([a-zA-Z]{2})\/(.*)/) ? RegExp.$1 : null,
	section = RegExp.$2 || null;
	$('#content').addClass(language);
	$('#main_nav a[href$=' + section + ']').add('#languages a[href*=' + language + ']').addClass('active');

	var links = $('#works_nav a');
	links.click(function(){
		var $this = $(this)
		if(!$this.is('.active')) {
			var container = $('div.media'),
				flashvars = {}, 
				id = $(this).attr('href').match(/(\d{1,2})/) ? RegExp.$1 : null,
				description = $($this.attr('href'));

			links.removeClass('active');
			$this.addClass('active');
			flashvars['xmlPath'] = "../xml.php?s=" + id;
			container.empty().append("<div id='gallery' />");
			swfobject.embedSWF("../swf/main.swf", "gallery", "546", "293", "9.0.0","", flashvars);
			description.siblings().hide().end().show();
		}
		return false;
	}).slice(0,1).click();
	$('form').bind('submit', function(){
		var 	$this = $(this),
			container = $this.parent();
		container.animate({
			opacity: .3
		}, 800).addClass('loading');
		$this.block({
			overlayCSS: {
				backgroundColor: 'transparent'
			}
			, message: null
		});
		$.post($(this).attr('action'), $(this).serialize(), function(response){
			container.animate({
				opacity: 1
			}, 800, function(){
				$this.unblock();
				container.removeClass('loading');
				$('.error.message', container).remove();
				if(!response.result) {
					$('.error', container).removeClass('error');
					$.each(response, function(k, v){
						if(!(typeof v === 'boolean' && v) && v.hasOwnProperty(language)) {
							$('label[for=' + k + ']', container).addClass('error').after($("<span class='error message' />").text(v[language]));
						}
					});
				} else {
					$this.block({
						overlayCSS: {
							backgroundColor: "transparent"
						}
						, css: {
							cursor: 'default'
							, border: '2px solid #ccc'
							, padding: "2em 4em"
						}
						, message: "<p>" + response.message[language] + "</p>"
					});
					_gaq.push(['_trackPageview', '/wyslij-formularz-kontaktowy.html']);
				}
			});
		}, "json");
		return false;
	});
});

