var description = '';
var facts = '';
var thumbwidth = 0;
var timescale = 5;
var ajaxUrl = '';
$(function(){

	// Show expose when click on big image

	$("#project-image-large").click(function(){

		id = $('.cf-images li.active').attr('pihlproject:uid');

		if(id>0) {
			// Load data
			//$.getJSON("/index.php?type=148&tx_pihlproject_pi1[projectid]="+id, function(data) {
			$.getJSON(ajaxUrl+"&tx_pihlproject_pi1[projectid]="+id, function(data) {
				// Hide sidebar
				$(".project-lightbox-content-inner-text").hide();
				hideSidebar();
				// Set title
				$('.project-lightbox-top-1').html(data.title);

				// Set background
				$('.project-lightbox-content-inner').css("background-image","url("+data.images[0]+")");

				// Build thumbs
				thumblist = "<ul>";
				$.each(data.thumbs,function(i,image){
					if(i == 0){actclass=' class="act"';}else{actclass='';}
					thumblist += '<li'+actclass+'><a href="'+data.images[i]+'"><img src="'+image+'" alt="" /></a></li>';
				});
				thumblist += "</ul>";
				$('.project-lightbox-content-thumbs').html(thumblist);

				thumbwidth = 60*data.thumbs.length;

				// Activate thumb clicks
				$('.project-lightbox-content-thumbs li a').click(function(){
					// Fade the active
					if(!$(this).parent('li').hasClass('act')) {
						$('.project-lightbox-content-thumbs li.act a img').fadeTo(100,0.5);
						$('.project-lightbox-content-thumbs li.act').removeClass("act");
						$(this).parent('li').addClass("act");

						var img = $(this).attr("href");
						$('.project-lightbox-content-inner').stop(true,true);
						$('.project-lightbox-content-inner').fadeOut(function(){
							$('.project-lightbox-content-inner').css("background-image","url("+img+")");
							$('.project-lightbox-content-inner').fadeIn();
						});

					}

					return false;

				});

				// Initiate to faded
				$('.project-lightbox-content-thumbs li[class!="act"] a img').fadeTo(0,0.5);

				// Activate fadeover/out
				$('.project-lightbox-content-thumbs li a img').hover(function(){
					$(this).fadeTo(200,1);
				},function(){
					// If active - dont fade
					if(!$(this).parent().parent().hasClass("act")) {
						$(this).fadeTo(200,0.5);
					}
				});

				// Set text
				description = data.description;
				facts = data.facts;


				// Hide arrows if too long (more less than or equal to 10)
				if($('.project-lightbox-content-thumbs li').length <= 10) {
					$('.project-lightbox-content-arrows-left').hide();
					$('.project-lightbox-content-arrows-right').hide();
				} else {
					$('.project-lightbox-content-arrows-left').show();
					$('.project-lightbox-content-arrows-right').show();
				}

				// Fade left arrow on init
				$('.project-lightbox-content-arrows-left img').fadeTo(0,0.2);

				// Set pictures

				// Show lightbox
				$("#project-lightbox").expose({
				    /* expose configuration goes here */

				    // one configuration property
				    color: '#000',

				    // another property
				    opacity: 0.8,

					loadSpeed: 'fast',

					onBeforeClose: function() {
						$("#project-lightbox").hide();
						hideSidebar();
					},

					onLoad: function() {
						$("#project-lightbox").show();
					} //Note: Removed , - IE does'nt like those, when no further items

				    // ... rest of the of configuration properties
				});
			});
		}
	});




	// Sidebar link click
	$(".project-lightbox-sidebar ul li a").click(function(){
		if($(this).hasClass("act")) {
			hideSidebar();
			$(".project-lightbox-sidebar ul li a.act").removeClass("act");
		} else {
			var content;

			if($(this).hasClass('description')) {
				content = description;
			} else {
				content = facts;
			}

			$(".project-lightbox-sidebar ul li a.act").removeClass("act");

			$(".project-lightbox-content-inner-text").animate({left: "-280px"},function() {
				$(".project-lightbox-content-inner-text-content").html(content);
				$(".project-lightbox-content-inner-text").show().animate({left: "0px"});
			});

			$(this).addClass("act");
		}

		return false;
	});


	// Close button to close sidebar
	$(".project-lightbox-content-inner-text-close a").click(function(){
		hideSidebar();
		//$(".project-lightbox-sidebar ul li a.act").removeClass("act");
		return false;
	});

	// Lightbox close (tilbage >>)
	$(".project-lightbox-top-2 a").click(function(){
		hideSidebar();
		$.expose.close();
		return false;
	});



	// Scroll
	$('.project-lightbox-content-arrows-left').hover(function(){
		left = -1*parseInt($('.project-lightbox-content-thumbs ul').css("left")); // Positiv left value
		time = left * timescale;
		$('.project-lightbox-content-arrows-right img').fadeTo(0,1);
		$('.project-lightbox-content-thumbs ul').animate({left: "0px"},time,'linear', function(){
			$('.project-lightbox-content-arrows-left img').fadeTo(0,0.2);
		});
	},function(){
		$('.project-lightbox-content-thumbs ul').stop();
	});


	// Right
	$('.project-lightbox-content-arrows-right').hover(function(){
		left = -1*parseInt($('.project-lightbox-content-thumbs ul').css("left")); // Positiv left value

		time = (thumbwidth - 600 - left) * timescale;
		$('.project-lightbox-content-arrows-left img').fadeTo(0,1);
		$('.project-lightbox-content-thumbs ul').animate({left: 600-thumbwidth+"px"},time,'linear', function(){
			$('.project-lightbox-content-arrows-right img').fadeTo(0,0.2);
		});
	},function(){
		$('.project-lightbox-content-thumbs ul').stop();
	});

});

function hideSidebar() {
	$(".project-lightbox-sidebar ul li a.act").removeClass("act");
	$(".project-lightbox-content-inner-text").animate({left: "-280px"});
}