$(document).ready(function() {
	
	$('#patroni').each(function(){
		
		$(this).find('a').click(function() { return false;})
		
		var html = '';
		$(this).find('.img').each(function(index) {
			if (index < 3) {
				html += '<div class="img">' + $(this).html() +  '</div>';
			}
		});
		$(this).find('.gallery2').append(html)
		
		
		if ($(this).find('.img').length > 3) {
			$(this).find('.gallery2').data('i', 0);
			$(this).find('.gallery2').queue(nextStep);
		}
		
	});
	
	$('#sponsorzy').each(function(){
		
		$('#footer').width(980);
		
		$(this).width(560);
		
		var html = '';
		$(this).find('.img').each(function(index) {
			if (index < 5) {
				html += '<div class="img">' + $(this).html() +  '</div>';
			}
		});
		$(this).find('.gallery2').append(html)
		
		
		$(this).find('a').click(function() { return false;})
		
		if ($(this).find('.img').length > 5) {
			$(this).find('.gallery2').data('i', 0);
			$(this).find('.gallery2').queue(nextStep);
		}
	});
	
	$('#content .txt').html($('#content .txt').html().split(' www.').join(' http://www.'));
	//jQuery('#content .txt').linkify();
	$('body').append('<div id="pasek">&nbsp;</div>');
	$(window).resize(function() {
		$('#pasek').css('width', ($(window).width() - 975)/2);
		if ($(window).width() < 1502) {
			$('#para').width(1+486 - ((1502 - $(window).width())/2));
		} else {
			$('#para').width(1+486);
		}
	});
	$(window).resize();
	
	$('#ss').click(function() {  startSS(); return false; });
	
	$('#header').append('<div id="loading">&nbsp;</div>');
	$('#para').append('<div id="sound">&nbsp;</div>');
	
	$('#sound').flash({swf:rootURL + '/' + prefix + 'uploads/sound.swf', width:40, height:20, wmode: 'transparent' });
	
	makeAjax();
	
	stuffLoaded();
	
	$('#loading').hide();
	
	makeGallery();
	
});

$.address/*.state('/').init(function(event) {
                
})*/.change(function(event) {

	return 
            	
	//$('#content .txt').load(rootURL + event.path  +  /*+ '?showtemplate=false'*/  '/ #content.txt', stuffLoaded);
	
	//$('#loading').show();
	
	$('#menu li').removeClass('currentpage')
	$('#menu li').each(function() {
		if ($(this).find('a').attr('href').split(rootURL).join('') == (event.path)) {
			$(this).addClass('currentpage')
		}
	})
	

});

var rootURL = 'http://www.balpolski.co.uk';
var prefix = '';

function makeAjax() {

	   //$('#menu a').address();  
/*
	$('#menu a').click(function() {
		$.address.value($(this).attr('href').split(rootURL).join(''));  
		//$('#content').load($(this).attr('href') + ' #content .inside', stuffLoaded)
		return false;
	});
*/
	
}

function formatTitle(title, currentArray, currentIndex, currentOpts) {
    return '<div id="tip7-title">' + (title && title.length ? '<b>' + title.replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1' + '<br/>' + '$2') + '</b>' : '' ) + '<br/> ' + (currentIndex + 1) + ' / ' + currentArray.length + '</div>';
}

function makeGallery() {

$(document).ready(function() {
	$("a.group").fancybox({
		'speedIn':		300,
		'speedOut':	300,
		'overlayColor':	'#000',
		'overlayOpacity':	0.7,
		'autoScale':	false,
		onClosed : onFBClosed,
		'titlePosition' 		: 'inside',
		'titleFormat'		: formatTitle
	});
});

}

function stuffLoaded() {

	return 
	
	$('#loading').hide();

	makeGallery();
	
	if ($('.gallery').length ) {
	
		$('a.dir').click(function() {
		
		
			$.address.value($(this).attr('href').split(rootURL).join(''));  
			//$('#content').load($(this).attr('href') + ' #content .inside', stuffLoaded)
			return false;
		});
	
	} else {
		$('#content .txt').html($('#content .txt').html().split(' www.').join(' http://www.'));
		jQuery('#content .txt').linkify();
	
	}
	
	$('#ss').click(function() {  startSS(); return false; });
	
	
}

interv = 0 ;
function startSS() {
	$("a.group").eq(0).trigger('click');
	interv = setInterval(nextSlide, 2000); 
}
function nextSlide() {
	$.fancybox.next();	
}
function onFBClosed() {
	clearInterval(interv);
}



function nextStep() {
	
	var i = $(this).data('i');
	if ($(this).parent().attr('id') == 'sponsorzy') {
		i = i == $(this).find('.img').length - 1 - 2 - 2 ? 0 : i + 1;
	} else {
		i = i == $(this).find('.img').length - 1 - 2 ? 0 : i + 1;
	}
	
	var w = i * -113;
	$(this).data('i', i)
	
	//console.log($(this))
	
	if (i==0) {
		$(this).clearQueue().css({left:w}, 250).queue(nextStep);
	} else {
		$(this).clearQueue().delay(2500).animate({left:w}, 250).queue(nextStep);
	}
	
	
}

(function($){

  var url1 = /(^|&lt;|\s)(www\..+?\..+?)(\s|&gt;|$)/g,
      url2 = /(^|&lt;|\s)(((https?|ftp):\/\/|mailto:).+?)(\s|&gt;|$)/g,

      linkifyThis = function () {
        var childNodes = this.childNodes,
            i = childNodes.length;
        while(i--)
        {
          var n = childNodes[i];
          if (n.nodeType == 3) {
            var html = $.trim(n.nodeValue);
            if (html)
            {
              html = html.replace(/&/g, '&amp;')
                         .replace(/</g, '&lt;')
                         .replace(/>/g, '&gt;')
                         .replace(url1, '$1<a href="http://$2" target="_blank">$2</a>$3')
                         .replace(url2, '$1<a href="$2" target="_blank">$2</a>$5');
              $(n).after(html).remove();
            }
          }
          else if (n.nodeType == 1  &&  !/^(a|button|textarea)$/i.test(n.tagName)) {
            linkifyThis.call(n);
          }
        }
      };

  $.fn.linkify = function () {
    return this.each(linkifyThis);
  };

})(jQuery);

// Usage example:




