jQuery(function($) {
	var myTransSpeed = 500;
	var myTimeout = 5000;
	var mySpeed = 75; // px/s
	$('a.zoom-in[href]').nyroModal();
	$('input.date').datepicker({
		showAnim: 'slideDown'
	});

	$('a[href].scroll').parent().localScroll();


	$(window).load(function() {
		$('.slide-gallery').each(function() {
			$(this).find('.slide-canvas').each(function() {
				var self = this;
//				var $nav = $('<div>', {class: 'slide-navi'});

//				IE8-hack
				var $nav = $('<div>');
				$nav.attr('class', 'slide-navi');
//				end of IE8-hack

				$(self).before($nav);

				$(self).find('a[href]').each(function() {
					$(this).after( $('<img>', {src: $(this).attr('href') }) );
				}).remove();

				$(self).addClass('cycle').cycle({
					delay: 3500,
					containerResize: false,
					speed: myTransSpeed,
					timeout: myTimeout,
					fx: 'fade',
					pager: $nav,
					fit: true,
					before: function(curr, next, opts, doForward) {
						$(this).find(':visible').stop(true, true);
						var longerDimension = parseInt( [$(next).width(), $(next).height()].sort()[0] );
						// 1000px height & speed=100px/s <=> 10 s timeout
						// 200px height & speed=100px/s <=> 2s timeout
						opts.timeout = longerDimension/mySpeed * 1000;
					},
					after: function(curr, next, opts, doForward) {
						var me = this;
						$(me).css({top: '0px', left: '0px'}).animate({
							top: '-' + ($(me).height() - $(me).parent().innerHeight()) + 'px',
							left: '-' + ($(me).width() - $(me).parent().innerWidth()) + 'px'
						}, {
							duration: opts.timeout - myTransSpeed,
							easing: 'linear'
						});
					}
				});
			});
			var $tabs = $(this).find('.tabs').tabs({
				show: function(e, ui) {
					var $current = $(ui.panel).find('.slide-canvas');
					$('.slide-canvas').not($current).cycle('pause');
					var me = $current.cycle('prev').cycle('next').cycle('resume');
					Cufon.refresh($(ui.tab).parents('.ui-tabs-navi'));
				}
			});
		});
	});
	$('.event[rel][type]').click(function() {
		_gaq.push(['_trackEvent', $(this).attr('type'), $(this).attr('rel')]);
	});
});
