$(function() {
	var filters = $('#quicklinks-container p:has(input)');
	var gallery = $('#gallery-thumbs');
	var fresh = true;
	var filterCount = 0;

	gallery.addFilter = function(tag) {
		filterCount++;

		if (filterCount == filters.length) {
			this.find('a').each(function() {
				var self = $(this);
				self.data('on', 0);
			});
		} else {
			this.find('[rel^=' + tag + ']').each(function() {
				var self = $(this);
				var on = self.data('on') || 0;
				on++;
				self.data('on', on);
			});
		}
	};
	gallery.removeFilter = function(tag) {
		filterCount--;

		if (filterCount == 0) {
			this.find('a').each(function() {
				var self = $(this);
				self.data('on', 0);
			});
		} else {
			this.find('[rel^=' + tag + ']').each(function() {
				var self = $(this);
				on = self.data('on') || 1;
				on--;
				self.data('on', on);
			});
		}
	};
	gallery.commit = function() {
		$('a.gallery-thumb').unwrap();
		this.find('a.gallery-thumb').each(function() {
			var self=$(this);
			if (filterCount == 0) {
				self.show();
			} else {
				if (self.data('on')) {
					self.show();
				} else {
					self.appendTo('#gallery-slider').hide();
				}
			}
		});
		imageLoaded();
	};

	filters.each(function() {
		var self = $(this);
		var input = self.find('input');

		input.change(function() {
			var filter = input.val();

			if($(this).attr('checked')) {
				gallery.addFilter(filter);
			}
			else {
				gallery.removeFilter(filter);
			}

			gallery.commit();
		});
	});

	var count = 0;
	$('a.gallery-thumb').each(function(){
		$(this).addClass('loading');
		$('img',this).hide();
	})
	
	if($('#gallery-thumbs').size()>0){
		initialiseGallery();
	}
	
	function initialiseGallery() {
		$('#gallery-thumbs').append('<a href="#" class="next-gallery">next</a><a href="#" class="prev-gallery">prev</a>');
		$('a.next-gallery').click(function(){
			slideRight();
			return false;
		});
		$('a.prev-gallery').click(function(){
			slideLeft();
			return false;
		});
		paginate();
		checkArrows()
	}
		
	function loadThumb() {
		if ($.browser.msie) {
			$('a.gallery-thumb img').fadeIn(function(){
				$(this).parent().removeClass('loading');
			});
		} else {
			$('a.gallery-thumb img').load(function(){
				$(this).fadeIn(function(){
					$(this).parent().removeClass('loading');
				});
			})
		}
	}

	function checkPages() {
		$('.pageSlide').each(function(){
			$('a.gallery-thumb:first:visible',this).addClass('blargh');
		});
	}
	
	function paginate() {
		var count2 = 0;
		$this = $('a.gallery-thumb:first');
		for(var i=0; i < $('a.gallery-thumb').size();i++) {
			if(count2<24) {
				$this = $this.add($('a.gallery-thumb:eq('+ i +')'));
				count2++;
			} else {
				$this.wrapAll('<div class="pageSlide" />')
				$this = $('a.gallery-thumb:eq('+ i +')');
				count2 = 0;
			}
		}
		$this.wrapAll('<div class="pageSlide" />');
		reArrangeThumbs();
		loadThumb();			
	}

	function reArrangeThumbs() {
		var galleryLeft = 0;
		$('#gallery-slider').css({
			'position':'absolute',
			'top':'0',
			'left':'0',
			'width':'960px',
			'height':'728px',
			'overflow':'hidden'
		});
		$('.pageSlide').each(function(){
			$(this).css({
				'position':'absolute',
				'top':'0',
				'left':galleryLeft+'px',
				'width':'960px',
				'height':'728px'
			})
			galleryLeft= galleryLeft+960;
		})
		checkPages();
	}
	
	function slideRight() { 
		var leftPos = $('.pageSlide:first').css('left').replace('px','');
		var maxLeftPos = ($('.pageSlide:visible').size() * $('.pageSlide').width()) - $('.pageSlide').width();
		if(leftPos > -maxLeftPos && $('.pageSlide:animated').size() == 0) {
			$('.pageSlide').animate({'left':'-='+$('.pageSlide').width()},1000, function(){
				checkArrows();
			})
		}
	}
	
	function slideLeft() {
		var leftPos = $('.pageSlide:first').css('left').replace('px','');
		if(leftPos < 0 && $('.pageSlide:animated').size() == 0) {
			$('.pageSlide').animate({'left':'+='+$('.pageSlide').width()},1000, function(){
				checkArrows();
			})
		}
	}

	function imageLoaded() {
		$('.loading').removeClass('loading').children('img').show();
		
		paginate();
		checkArrows();;
	}
	
	function checkPages() {
		$('.pageSlide').each(function(){
			if($('a.gallery-thumb:first',this).is(':visible')) {
			} else {
				$(this).hide();
			}
		});
	}
	
	function checkArrows() {
		var leftPos = $('.pageSlide:first').css('left').replace('px','');
		var maxLeftPos = ($('.pageSlide:visible').size() * $('.pageSlide').width()) - $('.pageSlide').width();
		if (leftPos == -maxLeftPos) {
			if(leftPos == 0 && leftPos == -maxLeftPos){
				$('a.prev-gallery, a.next-gallery').fadeOut();
			} else {
				$('a.next-gallery').fadeOut();
				$('a.prev-gallery').fadeIn();
			}
		}
		else if (leftPos == 0) {
			$('a.prev-gallery').fadeOut();
			$('a.next-gallery').fadeIn();
		} else {
			$('a.prev-gallery, a.next-gallery').fadeIn();
		}
	}
	
	$('#gallery-slider a').mouseenter(function(){
		$(this).animate({'opacity':'0.7'},150).animate({'opacity':'0.9'},100).animate({'opacity':'.5'},200);
		$(this).children().animate({'height':'200px','width':'180px','marginTop':'-10px','marginLeft':'-10px'},200);
	}).mouseleave(function(){
		$(this).stop(true,true).animate({'opacity':'1'});
		$(this).children().animate({'height':'180px','width':'160px','marginTop':'0px','marginLeft':'0px'},200);
	});
});
