var scrollToOpts = {duration: 1000, offset: {top: -250}};
jQuery(function($) {
	$('.dv').defaultValue();
	$('form').submit(function() { $(this).find('.empty').val('');});
	$('.canvas').each(function() {
		var self = this;
		var $tg = $('<a>', {class: 'toggle', href: '#', html: 'więcej', type: 'show-more', rel: $(this).parents('li').find('h3:first').text() }).click(function(e) {
			e.preventDefault();
			_gaq.push(['_trackEvent', $(this).attr('type'), $(this).attr('rel')]);
			$(self).addClass('expanded').find('img').not(':first-child').slideDown();
			$(this).remove();
		});
		if ($(this).children().size() > 1) $(this).after($tg);
	});
	$(window).load(function() {
		$('#works .canvas a[href]').each(function() {
			var self = this;
			var $i = $('<img>', {src: $(this).attr('href')}).load(function() {
				$(self).append($i);
				$i.unwrap();
			});
		});
	});
	$('#works .canvas img').live('click', function() {
		var $s = $(this).next('img:visible:first');
		var $nx = $(this).parents('li').next().find('img:visible:first');
		var $f = $('#works .canvas:first img:first');
		var $el = $s.size() ? $s : $nx.size() ? $nx : $f;
		$.scrollTo($el, scrollToOpts);
		_gaq.push(['_trackEvent', 'next-image', $(this).attr('src')]);
	});
	$('#navi a').each(function() {
		var self = this;
		$(this).click(function() {
			$.scrollTo($(this).attr('href'), {duration: 1000, onAfter: function() {
				_gaq.push(['_trackEvent', 'navi', $(self).attr('href')]);
			}});
		});
	});
	$.localScroll.hash();
});
