function imgpreload(imgs,settings)
{
	if (settings instanceof Function) { settings = {all:settings}; }

	if (typeof imgs == "string") { imgs = [imgs]; }

	var loaded = [];
	var t = imgs.length;
	var i = 0;

	for (i; i<t; i++)
	{
		var img = new Image();
		img.onload = function()
		{
			loaded.push(this);
			if (settings.each instanceof Function) { settings.each.call(this); }
			if (loaded.length>=t && settings.all instanceof Function) { settings.all.call(loaded); }
		};
		img.src = imgs[i];
	}
}

if (typeof jQuery != "undefined")
{
	(function($){

		$.imgpreload = imgpreload;

		$.fn.imgpreload = function(settings)
		{
			settings = $.extend({},$.fn.imgpreload.defaults,(settings instanceof Function)?{all:settings}:settings);

			this.each(function()
			{
				var elem = this;

				imgpreload($(this).attr('src'),function()
				{
					if (settings.each instanceof Function) { settings.each.call(elem); }
				});
			});

			var urls = [];

			this.each(function()
			{
				urls.push($(this).attr('src'));
			});

			var selection = this;

			imgpreload(urls,function()
			{
				if (settings.all instanceof Function) { settings.all.call(selection); }
			});

			return this;
		};

		$.fn.imgpreload.defaults =
		{
			each: null 
			, all: null 
		};

	})(jQuery);
}

jQuery(function($){
	var config = {    
		 over: menuOn, // function = onMouseOver callback (REQUIRED)    
		 timeout: 200, // number = milliseconds delay before onMouseOut    
		 interval: 300,
		 out: menuOut // function = onMouseOut callback (REQUIRED)    
	};
	
	$("nav ul li").hoverIntent( config )
				
	$.supersized({
		slideshow : 1,
		autoplay : 1,
		slide_interval : 6000,
		transition :   1, 
		transition_speed :	1000,
		performance	:	1,
		slides	:  [ {image : 'img/background.jpg' } ]
	});
	
	/*
	$("nav > ul > li").mouseenter(function() {		
		$('ul',this).show( 'slide',{},500);
	}).mouseleave(function() {
		$('ul',this).hide( 'slide',{},500);
	})
	*/
	
	function menuOn()
	{
		$(' > ul',this).show( 'slide',{},500);
	}
	
	function menuOut()
	{
		$(' > ul',this).hide( 'slide',{},500);
	}
	
	$("nav ul li a").click(function() {
		if ($(this).attr('href')=='' || $(this).attr('href')=='#') return false;
	})
	
	$('.calendar_link').live('click',function() { 
		if ($(this).attr('type').length)
		$.getJSON('/ajax.php',{rodzaj: 'callendar', id: $(this).attr('id'), type: $(this).attr('type'), ID: $(this).attr('lang') }, function (data) {
			$('.page .jspPane').html(data.html);
			return false;
		});
		return false;
	});
	
	$("#contact-slide").click(function() {
		if ($('aside').css('right')=='0px')		
			$('aside').animate({"right": "-190px"}, 1000);
			else			
			$('aside').animate({"right": "0px"}, 1000);			
	});
	
	$("article .close").click(function() {
		$("article").hide( 'slide',{},1000);
		return false;
	})
	
	$('.close, .next, .prev, .lang a, #contact-slide').each( function() {
		text = $(this).text();
		$(this).append('<span class="hover">'+text+'</span>').each(function () {
			var $span = $('> span.hover', this).css('opacity', 0);
			$(this).hover(function () {
				$span.stop().fadeTo(1000, 1);
			}, function () {
				$span.stop().fadeTo(1000, 0);
			});
		});
	});
	
	$('.close, .next, .close, .prev').click( function() {
		$.getJSON('/ajax.php',{rodzaj: 'news', id: $(this).attr('name'), lang: $(this).attr('lang') }, function (data) {
			$('.news .content h2').html(data.title);
			$('.news .content .jspPane').html(data.text);
			if (data.next>0)
				{
					$('.news .next').attr('name',data.next).attr('href',data.next_link).show();
				}
				else
				{
					$('.news .next').attr('name','').attr('href','#').hide();
				}	
			if (data.prev>0)
				{
					$('.news .prev').attr('name',data.prev).attr('href',data.prev_link).show();
				}
				else
				{
					$('.news .prev').attr('name','').attr('href','#').hide();
				}
			if ($(".news .image img").length)
				{
					$(".news .image img").fadeOut(500,function() { 
						$(this).remove();
						if (data.image)
						{
							$.imgpreload(data.image,function() {
								var img = new Image();
								$(img).attr('src',data.image).hide().appendTo('.news .image').fadeIn(500);
							})
						}
					});			
				}
				else
				{
					if (data.image)
						{
							$.imgpreload(data.image,function() {
								var img = new Image();
								$(img).attr('src',data.image).hide().appendTo('.news .image').fadeIn(500);
							})
						}
				}
				
			console.log(data);
		return false;
		});				
		return false;
	})
	
	$('a.projektowanie').each( function() {		
		$(this).append('<span class="hover"></span>').each(function () {
			var $span = $('> span.hover', this).css('opacity', 0);
			$(this).hover(function () {
				$span.stop().fadeTo(1000, 1);
			}, function () {
				$span.stop().fadeTo(1000, 0);
			});
		});
	});
	
	$('article').show( 'slide',{},1000);
	
	$('.drukuj').click( function() {
		window.print();
		return false;
	})
	
	if ($('.page .slider').length)
	var scroller = $('.page .slider').jScrollPane({
		showArrows: false,
		verticalDragMinHeight: 136,
		verticalDragMaxHeight: 136,
		autoReinitialise: true
	});
	
	if ($('.news .slider').length)
	var scroller = $('.news .slider').jScrollPane({
		showArrows: false,
		verticalDragMinHeight: 136,
		verticalDragMaxHeight: 136,
		autoReinitialise: true
	});
	
	$("#home-news-calendar .left").live('click',function() {
		$.get('/ajax.php',{rodzaj: 'kalendarz', option: 'less', ID: $(this).attr('lang') }, function (data) {
			$("#home-news-calendar").replaceWith(data);
			$('.page .jspPane').html('');
		return false;
		});
	});

	

	$("#home-news-calendar .right").live('click',function() {
		$.get('/ajax.php',{rodzaj: 'kalendarz', option: 'more', ID: $(this).attr('lang') }, function (data) {
			$("#home-news-calendar").replaceWith(data);
			$('.page .jspPane').html('');
		return false;
		});				
	});
	
});
