
$(function () {

	function showOverlay(callback, onclick) {
		if ($('#overlay').length) {
			return
		}
		$("<div id='overlay'></div>")
			.css({ opacity: 0 }).appendTo("body").show()
			.fadeTo("fast", 0.5, callback)
			.click(function () { onclick(); hideOverlay() })
	}

	function hideOverlay() {
		$("#overlay").fadeOut("fast", function() {
			$(this).remove();
		})
		return false
	}

	function loadUrl(url) {
		function fail() {
			location.href = url
		}
		
		function onSuccess(response) {
			if ($('#content-box').length < 1) {
				$('<div class="box content-box" id="content-box"></div>')
					.hide().appendTo("#boxes")
			}
			var cbox = $(response).find(".content-box")
			var sidebar = $(response).find(".sidebar-box")
			if (cbox.length < 1) fail()
			
			$('#box-loader').remove()

			q = $('body')

			// schowaj #sidebar-box
			q.queue(function () {
				if ($('#sidebar-box').length > 0 &&	(sidebar.length < 1 ||
					sidebar.html() != $('#sidebar-box').html()))
				{
					$('#sidebar-box').animate(
						{ left: 408 }, "slow",
						function () { $('#sidebar-box').remove(); q.dequeue() })
				} else {
					q.dequeue()
				}
			})
			
			// galeria: ustaw wymiary pola za zdjeciem
			q.queue(function () {
				if ($('#gallery-image').length &&
					cbox.find('.gallery-image').length)
				{
					h = cbox.find('.gallery-image img').attr("height")
					w = cbox.find('.gallery-image img').attr("width")
					if ($('#gallery-image img').attr("height") != h) {
						$('#gallery-image img')
							.attr("src", "javascript:")
							.animate(
								{ width: w, height: h }, "medium",
								function () { q.dequeue() })
					} else {
						q.dequeue()
					}
				} else {
					q.dequeue()
				}
			})
			
			// zmien wymiary i przesun #content-box
			q.queue(function () {
				cbox.css("visibility", "hidden").appendTo('#boxes')
				var w = cbox.width()
				var h = cbox.height()
				var pos = cbox.css("left")
				cbox.css("visibility", "visible").remove()

				var anim = { }
				if ($('#content-box').is(':visible')) {
					if ($('#content-box').height() != h) {
						anim.height = h
						$('#content-box')
							.css("height", $('#content-box').height())
						$('#content-box .box-content')
							.css({ height: "100%", padding: 0 })
						$('#content-box .box-text').empty()
					}
					if ($('#content-box').width() != w) {
						anim.width = w
						$('#content-box .box-content').empty()
					}
					if ($('#content-box').css("left") != pos) {
						anim.left = pos
					}
				}
				if (anim.width || anim.height || anim.left) {
					$('#content-box').animate(anim, "slow", function () {
						$('#content-box .box-content').css("height", "auto")
						$('#content-box').css("height", "auto")
						q.dequeue()
					})
				} else {
					q.dequeue()
				}
			})
			
			// zaladuj zawartosc do #content-box
			q.queue(function () {
				$('#content-box')
					.html(cbox.html())
					.attr("class", cbox.attr("class"))
				q.dequeue()
			})

			// pokaz #content-box
			q.queue(function () {
				$('#content-box').fadeIn("fast", function () { q.dequeue() })
			})
			
			// wysun #sidebar-box
			q.queue(function () {
				if ($('#sidebar-box').length < 1 && sidebar.length) {
					var pos = sidebar.show().appendTo("#boxes").css("left")
					sidebar.addClass("sidebar-box-hidden")
					sidebar.show().animate({ left: pos }, "slow",
						function () { q.dequeue() })
				} else {
					q.dequeue()
				}
			})
			
			// podepnij obsluge klikniec pod linki
			q.queue(function () {
				initLinks()
				q.dequeue()
			})
		}
		
		function doRequest() {
			$.ajax({
				type: "GET",
				url: url,
				dataType: "html",
				success: onSuccess,
				error: fail
			})
		}
		
		if ($('#boxes').length) {
			$('#content-box').append('<div id="box-loader"></div>')
			doRequest()
		} else {
			$('<div id="boxes"></div>').appendTo("body")
			showOverlay(doRequest, function () { $('#boxes').remove() })
		}
		
		return false;
	}
	
	function initLinks() {
		var ajaxLinks = "\
			#menu-lt a, #menu-rt a, #latest-news a, #news a, \
			.box-title a, #box-next a, .box-submenu a, #box-nav a, \
			#gallery-thumbs a, #gallery-caption .nav a, #gallery-groups a, \
			.sidebar-item h4 a, .sidebar-item a.more, \
			.box .pagination a, .gallery-entry a.thumb, .entry h3 a, \
			#gallery-image a"
		
		$(ajaxLinks).unbind("click").click(function () {
			return loadUrl($(this).attr("href"))
		})
		$('#overlay, #content-box .box-close a').unbind("click").click(function () {
			$('#boxes').remove()
			hideOverlay()
			return false
		})
		
		$(document).pngFix();
		
		/*$('#gallery-image').hover(
			function () { $('#box-logo, #box-title').fadeOut(); $('#gallery-caption').slideUp("fast") },
			function () { $('#box-logo, #box-title').fadeIn(); $('#gallery-caption').slideDown("fast") }
		);*/

	}
	initLinks()
})


