$(function() {
	$('a.fancybox').fancybox({
		centerOnScroll: false,
		hideOnContentClick: false,
		callbackOnStart: function() {
		},
		callbackOnShow: function() {
			
			window.currentlySelectedComments = this.itemArray[ this.itemCurrent ].orig.parent().attr('name');
			window.currentlySelected = this.itemArray[ this.itemCurrent ].orig.parent()[0].id.substr(1);
			
			$('#fancy_right').height( $('#fancy_img').height() + 10 );
			
			$('#fancy_content').append('<span style="width: 100%; text-align: right; display: block;"><a class="commentsLink" href="#">Comments (' + window.currentlySelectedComments + ')</a></span>').css('color', 'black');
			$('#fancy_content img').css({
				position: 'static',
				height: 'auto'
			});
			$('#fancy_outer').height( $('#fancy_outer').height() + 23 );
		}
	});
	//$('.gallery_image_list a').click(function() {
	//	window.currentlySelected = this.id.substr(1);
	//	window.currentlySelectedComments = this.rel;
	//});
	$('.commentsLink').live('click', function() {
		$('#fancy_content span').html('Loading Comments&hellip;');
		$.get( window.location.pathname + window.currentlySelected + '/?format=json', function(data) {
			$('#fancy_content span').remove();
			$('#fancy_content').append(data).css('overflow', 'scroll');
			$('#fancy_outer').animate({
				height: "+=" + ($('#fancy_content')[0].scrollHeight - $('#fancy_content').height()) + "px"
			});
			$('#fancy_content').css('overflow', 'hidden');
		});
		return false;
	});
	$('.comment_form').live('submit', function() {
		$(this).find('input,textarea').attr('disabled', 'disabled');
		var $self = $(this);
		$.post('/gallery/comment/' + window.currentlySelected + '/', {
			email: this.email.value,
			name: this.name.value,
			comment: this.comment.value
		}, function(data) {
			$self.replaceWith(data);
			$('#fancy_content').css('overflow', 'scroll');
			$('#fancy_outer').animate({
				height: "+=" + ($('#fancy_content')[0].scrollHeight - $('#fancy_content').height()) + "px"
			});
			$('#fancy_content').css('overflow', 'hidden');
		});
		return false;
	});
	$('.gallery_image_list li').hover(function() {
		$(this).find('span').stop().fadeTo(500, 0.8);
	}, function() {
		$(this).find('span').stop().fadeTo(500, 0);
	});
	$('.gallery_image_list li span').fadeTo(100, 0);
	$('form#vote').submit(function() {
		var $self = $(this);
		if($(this).find('input:radio:checked').length && $(this).find('input[name=email]').val() != '') {
			$(this).find('input').attr('disabled', 'disabled');
			$.post('/gallery/vote/' + $('input:radio:checked').val() + '/', {
				email: $(this).find('input[name=email]').val()
			}, function() {
				document.cookie = 'chc_competition_' + $self.attr('rel') + '=1;';
				$self.replaceWith('<p>Thank you for voting.</p>');
			});
		} else {
			alert('Please select your favourite image and enter your email.');
		}
		return false;
	});
	$('.gallery_image_list img').load(function() {
		var height = $(this).height(),
			width = $(this).width();
			
		if( height >= 139 && width >= 200) {
			
		} else if( height >= 139 ) {
			$(this).height( height * (200 / width) );
			$(this).width( width * (200 / width) );
			$(this).css('marginTop', "-" + (((height * (200 / width)) - 139) / 2) + "px")
		} else if(width >= 200) {
			$(this).height( height * (139 / height) );
			$(this).width( width * (139 / height) );
		}
	});
});
