// comments
$(function () {

	$('#comment').bind('ajaxify', function() {
		$(this).find('form').ajaxForm({
			success: function(responseText) {
				if ( $(responseText).is('li') ) {
					$('#commentList').prepend( responseText );
					$('#comment, li.empty').remove();
					resetCommentCount();
					
				} else {
					$('#comment').html(responseText).triggerHandler('ajaxify');
				};
			}	
		});
	}).triggerHandler('ajaxify');

/*
	$('#comment form').ajaxForm({

		success: function(responseText) {
			$('#comment').append( responseText );
			resetCommentCount();
		}
	});
*/
	
	$('#postcomment').click(function() {
	      $('#comment').show('normal');
		$('#postcomment').closest('ul').remove();
		
		return false;		
	});
	
	function resetCommentCount() {
		$('#commentCount').html( $('#commentList li').size() );
	};

	$('a.delete').click(function () {
		if ( confirm('Are you sure you wish to delete this comment?') ) {
			var $anchor = $(this);

			$.post($anchor.attr('href'), function(data) {
				if (data[0] == 1) {
						$anchor.closest('li').hide('normal', function () {
							$(this).remove();
							resetCommentCount();
						});
					};
				},
				'json'
			);
		}

		return false;
	}).show();
});

/* Navigation Buttons */
$(function () { // Attach event handlers to navigation buttons
	$('#first').click(function () {
		// $("a[href=#updates]").click();
		// $('a[href=#updates], #waypoints li:last').click();
		$('#waypoints li:last').click();
	});

	$('#last').click(function () {
		// $('a[href=#updates], #waypoints li:first').click();
		$('#waypoints li:first').click();
	});

	$("#left").click(function () {
		// $('a[href=#updates]').click();
		$("#waypoints li.selected").next().click();
	});

	$("#right").click(function () {
		// $('a[href=#updates]').click();
		$("#waypoints li.selected").prev().click();
	});

	var images = ['/static/images/navigation-controls-click_01.png',
		                  '/static/images/navigation-controls-over_01.png',
		                  '/static/images/navigation-controls-click_02.png',
		                  '/static/images/navigation-controls-over_02.png',
		                  '/static/images/navigation-controls-click_03.png',
		                  '/static/images/navigation-controls-over_03.png',
		                  '/static/images/navigation-controls-click_04.png',
		                  '/static/images/navigation-controls-over_04.png'];

	// Loop through and declare an element that will not actually be used.
	$.each(images, function (i, val) {
		$('<img>').attr('src', val);
	});
});


$(function () {
	$(window).resize(function () {
		var $scrollarea = $('#sidescrollarea');
		var offset = $scrollarea.offset().top - $scrollarea.parent().offset().top;

            var $side = $('#side');
		$scrollarea.height( $side.height()-offset );
		
		$('#grippie').css('top', $side.offset().top + ($side.outerHeight() /2) );	
	}).resize();
});

// grippie
$(function() {
	var mapLeft= $('#map').css('left');
	var grippieLeft = $('#grippie').css('left');
			
	$('#grippie').toggle(
		function() {
			$('#side').hide();
			$('#map').css('left','10px');
			$('#grippie').css('left','0px');
		},
		function() {
			$('#side').show();
			$('#map').css('left', mapLeft);
			$('#grippie').css('left', grippieLeft);
		}
	);
});