jQuery.fn.delay = function(time,func){
	this.each(function(){
		setTimeout(func,time);
	});
	
	return this;
};

$(document).ready(function(){
	
	$('.content .right').jScrollPane({
		bottomCapHeight: 170,
		scrollbarWidth: 31,
		dragMaxHeight: 7,
		dragMinHeight: 7,
		showArrows: true
	});
	
	$('.footer').append('<div class="animated-background"><div></div></div><div class="prostokat1"><div></div></div><div class="prostokat2"><div></div></div><div class="prostokat3"><div></div></div><div class="prostokat4"><div></div></div><div class="kucharz"><div></div></div>');
	
	$('.prostokat1').css("width","51px").css("height", "18px").css("position","absolute").css("margin", "13px 0 0 659px");
	$('.prostokat1 div').css('float', 'right').css('width', '0').css('height', '18px').css('background', 'rgb(255,0,65)').animate({width: '51px'}, 1000);
	
	$('.prostokat2').css("width","18px").css("height", "51px").css("position","absolute").css("margin", "13px 0 0 692px");
	$('.prostokat2 div').css('width', '18px').css('height', '0').css('background', 'rgb(255,0,65)').animate({height: '51px'}, 1000);
	
	if(/Opera/.test (navigator.userAgent)) {
		$('.prostokat3').css("width","18px").css("height", "0").css("position","absolute").css("margin", "353px 0 0 14px").css('background', 'rgb(255,0,65)').animate({'height':'51px', 'top': '-=51px'}, 1000);
	}
	else {
		$('.prostokat3').css("width","18px").css("height", "0").css("position","absolute").css("margin", "708px 0 0 14px").css('background', 'rgb(255,0,65)').animate({'height':'51px', 'top': '-=51px'}, 1000);
	}
	
	$('.prostokat4').css("width","0").css("height", "18px").css("position","absolute").css("margin", "690px 0 0 14px").css('background', 'rgb(255,0,65)').animate({'width':'51px'}, 1000);
	
	var og = $('.open-gallery');
	var prev = $('.prev');
	var photos = $('.news-photos');
	var img = $('.photo');
	var news = $('.jScrollPaneContainer');
	var gallery = $('.gallery');
	var noOfPhotos = $('.all-photos li').length;
	var i = 0;
	var close = $('.close');
	var link = close.html();
	var ico = $('.ico');
	var dateAndTitle = $('.date-and-title');
	var photoTitle = $('.ptitle');
	var ktore = $('.ktore');

	og.click(function() {
		gallery.css('width', '489px');
		prev.animate({'width' : '41px'}, 'slow');
		news.fadeOut();
		close.html('');
		og.removeClass('open-gallery');
		photos.animate({'width' : '407px'}, 'slow', 'linear', function() {
			og.addClass('next-picture');
			close.fadeIn('slow', function() {
				ico.fadeIn();
			});
			dateAndTitle.fadeIn('slow', function() {
				photoTitle.fadeIn();
			});
		});
	});

	$('.next-picture').live('click', function() {
		i++;
		if(i > noOfPhotos - 1) {
			i=0;
		}
		var image = getImage(i);
		var title = getTitle(i);
		img.fadeOut(200, function() {
			$(this).removeAttr('src').attr('src', image).load(function() {
				$(this).fadeIn(200);
				ktore.html(i+1);
				$(this).parent().next().next().next().next().next().html(title);
			});
		});
	});

	$('.prev').click(function() {
		i--;
		if(i<0) {
			i=noOfPhotos-1;
		}
		var image = getImage(i);
		var title = getTitle(i);
		img.fadeOut(200, function() {
			$(this).removeAttr('src').attr('src', image).load(function() {
				$(this).fadeIn(200);
				ktore.html(i+1);
				$(this).parent().next().next().next().next().next().html(title);
			});
		});
	});

	close.click(function() {
		//$(this).fadeOut();
		ico.fadeOut();
		var next = $('.next');
		photoTitle.fadeOut('slow');
		dateAndTitle.fadeOut('slow', function() {
			next.addClass('open-gallery').removeClass('next-picture');
			prev.animate({'width' : '0px'}, 300);
			news.fadeIn();
			photos.animate({'width' : '183px'}, 'slow', 'linear', function() {
				next.addClass('.open-gallery');
				close.html(link);
			});
		});
	});

	function getImage(i) {
		return $('.all-photos li:eq(' + i + ')').children('p:eq(0)').html();
	}

	function getTitle(i) {
		return $('.all-photos li:eq(' + i + ')').children('p:eq(1)').html();
	}

});
