
$(document).ready(function() {
	
	var contentXmlHttpReq = null;
	var recentWorkXmlHttpReq = null;
	var modalPhotoGallery = null;
	var modalPhotoGalleryLoadingAnimation = null;
	
	var addRecentWorkItemTriggers = function() {
		$('#contentbottom a').unbind().click(function() {
			$(this).fadeOut(200, function() {
				$(this).parent().remove();
				$('#contentleft .hide').slideUp(0).hide();
				$(document.body).addClass('viewmore');
				$('#contentleft .hide').slideDown(400);
			});
			return false;
		});
	};
	
	var addRecentWorkTriggers = function() {
		$('.recentwork').filter('#contentright-far').find('.recentwork-scrollbar').jScrollPane().find('a').unbind().click(function() {
			$(this).siblings('.recentwork-selected').removeClass('recentwork-selected').triggerHandler('mouseout');
			$(this).addClass('recentwork-selected');
			$('#contentleft .hide').slideUp(200, function() {
				$(document.body).removeClass('viewmore');
			});
			
			if(recentWorkXmlHttpReq != null) { recentWorkXmlHttpReq.abort(); }
			$('#contentleft').html('<p class="small">Loading ' + $(this).attr('title') + '...</p>');
			$('#contentbottom').empty();
			var url = $(this).attr('href');
			$('#contentright').animate({opacity: 0.3}, 200, function() {
				recentWorkXmlHttpReq = $.get(url, {}, function(data) {
					$('#contentleft').empty().html($(data).find('#contentleft > *'));
					$('#contentright').empty().html($(data).find('#contentright > *')).animate({opacity: 1}, 400);
					$('#contentbottom').empty().html($(data).find('#contentbottom > *'));
					addRecentWorkItemTriggers();
					setupPhotoGallery();
					setupModalPhotoGallery();
					recentWorkXmlHttpReq = null;
				})
			});
			
			return false;
		}).hover(function() {
			if(!$(this).hasClass('recentwork-selected')) {
				$(this).animate({opacity: 1}, 200);
			}
		}, function() {
			if(!$(this).hasClass('recentwork-selected')) {
				$(this).animate({opacity: 0.6}, 200);
			}
		}).animate({opacity: 0.6});
		addRecentWorkItemTriggers();
		setupPhotoGallery();
	};
	
	var createModalPhotoGalleryLoading = function() {
		var url = 'Images/ajax_loader.gif';
		$.preloadImage(url);
		return $('<div id="recentwork-photogallery-loading-animation"></div>').css({
			width: 20,
			height: 20,
			position: 'absolute',
			display: 'none',
			'z-index': 9999,
			background: 'transparent url("'+ url + '") no-repeat center center'
		});
	};
	
	var createModalPhotoGallery = function() {
		return $('<div id="recentwork-photogallery"></div>').addClass('jqmWindow').jqm({
			modal: false, 
			onHide: function(hash) {
				$(document).unbind('keyup');
				hash.w.fadeOut(400, function() { hash.o.remove(); hash.w.hide(); });
			},
			onShow: function(hash) {
				hash.w.fadeIn(400, function() {
					hash.w.find('a[href=\'' + $(hash.t).attr('href') + '\']').trigger('click').trigger('focus');
					$(document).unbind('keyup').keyup(function(e) {
						switch(e.keyCode) {
							case 27:
								hash.w.jqmHide();
								break;
							case 37:
								(hash.w.find('a.photogallery-thumbs-active').length > 0 ? hash.w.find('a.photogallery-thumbs-active').prev() : hash.w.find('a:first')).trigger('click').trigger('focus');
								break;
							case 39:
								(hash.w.find('a.photogallery-thumbs-active').length > 0 ? hash.w.find('a.photogallery-thumbs-active').next() : hash.w.find('a:last')).trigger('click').trigger('focus');
								break;
						}
					});
				}).find('.photogallery-thumbs').jScrollPane();
			}
		}).appendTo(document.body);
	};
	
	var setupModalPhotoGallery = function() {
		// Setup markup and layout
		modalPhotoGallery.empty(); // Make sure its empty else items duplicate
		var thumbs = $(document.createElement('div')).addClass('photogallery-thumbs photogallery-thumbs-dialog');
		thumbs.append($('.photogallery-thumbs').find('a').clone());
		var image = $(document.createElement('img')).attr('src', thumbs.find('a').eq(0).attr('href'));
		var main = $(document.createElement('div')).addClass('photogallery-image').append(image);
		modalPhotoGallery.append(main).append(thumbs).jqmAddTrigger('.photogallery-thumbs a');
		var loader = modalPhotoGalleryLoadingAnimation.clone().css({
			top: 140, 
			left: 290
		});
		main.append(loader);
		
		// Setup onclick handlers
		thumbs.find('a').unbind().click(function() {
			$(this).siblings('.photogallery-thumbs-active').removeClass('photogallery-thumbs-active').animate({ opacity: 0.6 }, 400);
			$(this).addClass('photogallery-thumbs-active').animate({ opacity: 1 }, 400);
			var href = $(this).attr('href');
			image.stop().animate({ opacity: 0.6 }, 200, function() {
				loader.fadeIn(200);
				$.preloadImage(href, function() {
					loader.fadeOut(100);
					image.attr('src', href).animate({ opacity: 1 }, 600);
				});
			});
			return false;
		}).hover(function() {
			if (!$(this).hasClass('photogallery-thumbs-active')) {
				$(this).stop().animate({ opacity: 1 }, 400);
			}
		}, function() {
			if (!$(this).hasClass('photogallery-thumbs-active')) {
				$(this).stop().animate({ opacity: 0.6 }, 400);
			}
		}).animate({ opacity: 0.6 }, 400);
	};
	
	var setupPhotoGallery = function() {
		$('#contentleft .photogallery-thumbs').find('a').unbind().hover(function() {
			$(this).stop().animate({ opacity: 1 }, 400);
		}, function() {
			$(this).stop().animate({ opacity: 0.6 }, 400);
		}).animate({ opacity: 0.6 }, 400).each(function() {
			var url = $(this).css('background-image');
			$.preloadImage(url.substring(4, url.length - 1));
		});
	};
	
	var init = function() {
		addRecentWorkTriggers(); // Incase we are directed straight to a recent work item page
		setupPhotoGallery();
		setupModalPhotoGallery();
	};
	
	// Startup
	$('#menumap area').click(function() {
		if(contentXmlHttpReq != null) { contentXmlHttpReq.abort(); }
		// Incase we move away from a work item page
		$('#contentleft .hide').slideUp(200, function() {
			$(document.body).removeClass('viewmore');
		});
		var url = $(this).attr('href');
		var title = $(this).attr('title');
		setTimeout(function() {
			$('#content').html('<div class="text"><p class="small">Loading ' + title + '...</p></div>');
			contentXmlHttpReq = $.get(url, {'_a': 1}, function(data) {
				$('#content').empty().append($(data).children());
				contentXmlHttpReq = null;
				init();
			});
		}, 200);
		return false;
	});
	
	modalPhotoGalleryLoadingAnimation = createModalPhotoGalleryLoading();
	modalPhotoGallery = createModalPhotoGallery();
	init(); // Incase we are directed straight to a recent work item page
});