
$(document).ready(function() {
	Cufon.replace("#header ul.menu a", {hover: true, separate: 'none'});


	var  beginSetup
		,slide
		,setupProjectInfos
		,hasPrev
		,hasNext
		,hideShowNavButtons
		,$centerWrapper = $("#gallery .center-item-wrapper")
		,$leftWrapper = $("#gallery .left-item-wrapper")
		,$rightWrapper = $("#gallery .right-item-wrapper")
		,$navPrev = $("#gallery a.nav-prev")
		,$navNext = $("#gallery a.nav-next")
		,$piInfos = $("#gallery-item-infos .infos a")
		,$piName = $("#gallery-item-infos .name h3")
		,$piWww = $("#gallery-item-infos .www a")
		,emptyProject = {
			 imgBig: $centerWrapper.css('background-image').replace(/^url\("/i, '').replace(/"\)$/i, '')
			,imgMedium: $leftWrapper.css('background-image').replace(/^url\("/i, '').replace(/"\)$/i, '')
		}
		,projects = []
		,currentProject
		,duration = 700
		,fancyboxOptions = {
			 transitionIn: 'none'
			,transitionOut: 'none'
			,titleShow: false
			,changeSpeed: 200
			,changeFade: 200
			,autoScale: false
			,overlayColor: '#000'
			,overlayOpacity: 0.5
		}
	;


	$(".project-images a").fancybox(fancyboxOptions);


	$("#projects div.project").each(function(idx, project) {
		var  $project = $(project)
			,projectData = {}
		;
		projectData.name = $project.find(".project-name").text() || null
		projectData.www = $project.find("a.www").attr('href') || null
		projectData.imgBig = $project.find("div.project-images a.img:not(.img-hidden) img:eq(0)").attr('src') || null
		projectData.imgMedium = projectData.imgBig ? projectData.imgBig.replace('_tn-big.', '_tn-medium.') : null
		projectData.infos = $project.find("div.project-infos") || null
		projectData.images = $project.find("div.project-images") || null

		projects[idx] = projectData;
	});
	projects.unshift(emptyProject);
	projects.push(emptyProject);


	beginSetup = function() {
		if (projects.length > 2) {
			currentProject = 1;
			$leftWrapper.css('background-image', 'url(' + projects[currentProject-1].imgMedium + ')');
			$centerWrapper.css('background-image', 'url(' + projects[currentProject].imgBig + ')');
			$rightWrapper.css('background-image', 'url(' + projects[currentProject+1].imgMedium + ')');
			setupProjectInfos(projects[currentProject]);
			hideShowNavButtons();
		}
	};

	setupProjectInfos = function(project) {
		$piInfos.stop(true, true).fadeOut(duration/2, function() {
			if (project.infos) {
				$piInfos.fancybox($.extend({}, fancyboxOptions, {
					 padding: 0
					,type: 'html'
					,content: project.infos.clone().wrap('<div class="project-infos" />').parent().html()
				}));

				$piInfos.fadeIn(duration/2, function() {
					$centerWrapper.find("a").remove();
					$("<a>").attr('href', '#').bind('click', function(e) {
						e.preventDefault();
						project.images.find("a:eq(0)").trigger('click');
					}).appendTo($centerWrapper.find("> div"));
				});
			}
		});

		$piName.stop(true, true).fadeOut(duration/2, function() {
			if (project.name) {
				$piName.text(project.name).fadeIn(duration/2);
			}
		});

		$piWww.stop(true, true).fadeOut(duration/2, function() {
			if (project.www) {
				$piWww.attr('href', project.www).fadeIn(duration/2);
			}
		});
	};

	slide = function($wrapper, newImageUrl, direction) {
		var  $item = $wrapper.find("> div")
			,wrapperWidth = $wrapper.innerWidth()
			,wrapperBeginPositionX = 0
			,itemBeginPositionX
			,wrapperEndPositionX
			,itemEndPositionX = 0
		;

		if (direction > 0) {
			wrapperEndPositionX = wrapperWidth
			itemBeginPositionX = -wrapperWidth
		}
		else {
			wrapperEndPositionX = -wrapperWidth
			itemBeginPositionX = wrapperWidth
		}

		$wrapper.animate({backgroundPosition: wrapperEndPositionX + 'px 0'}, duration);

		$item.css({
			 'background-image': 'url(' + newImageUrl + ')'
			,'background-position': itemBeginPositionX + 'px 0'
		}).animate({backgroundPosition: itemEndPositionX + 'px 0'}, duration, function() {
			// obecnie widoczny obraz ustawiamy dla wrappera
			$wrapper.css({
				 'background-image': 'url(' + newImageUrl + ')'
				,'background-position': wrapperBeginPositionX + 'px 0'
			});
			// pomociczy obraz usuwamy z pola widzenia
			$item.css('background-position', itemBeginPositionX + 'px 0');
		});
	};

	hasPrev = function() {
		return currentProject > 1;
	};

	hasNext = function() {
		return currentProject < projects.length - 2;
	};

	hideShowNavButtons = function() {
		if (hasPrev()) {
			$navPrev.removeClass('nav-hidden');
		}
		else {
			$navPrev.addClass('nav-hidden');
		}

		if (hasNext()) {
			$navNext.removeClass('nav-hidden');
		}
		else {
			$navNext.addClass('nav-hidden');
		}
	};

	$navNext.add($navPrev).bind('click', function(e) {
		e.preventDefault();
		if ($leftWrapper.is(":animated")) { return false; }
		else {
			var  $this = $(this)
				,direction
			;
			if ($this.hasClass('nav-prev')) {
				--currentProject;
				direction = 1;
			}
			else {
				++currentProject;
				direction = -1;
			}

			hideShowNavButtons();
			slide($leftWrapper, projects[currentProject-1].imgMedium, direction);
			slide($centerWrapper, projects[currentProject].imgBig, direction);
			slide($rightWrapper, projects[currentProject+1].imgMedium, direction);
			setupProjectInfos(projects[currentProject]);
		}
	});


	beginSetup();



	// preload!!!!!!!!!!!!!!!!!!!!!!!!

				$(".about-me a").fancybox($.extend({}, fancyboxOptions, {
					 padding: 0
					,type: 'html'
					,content: $("#infos-content1").clone().wrap('<div class="project-infos" />').parent().html()
				}));
				$(".contact a").fancybox($.extend({}, fancyboxOptions, {
					 padding: 0
					,type: 'html'
					,content: $("#infos-content2").clone().wrap('<div class="project-infos" />').parent().html()
				}));

});

