// tabs
$(function () {
	$('#tabnav').tabs();
});

// comments
$(function () {
	$('#comment').bind('ajaxify', function() {
		$(this).find('form').ajaxForm({
			target: '#comment',
			success: function() {
				$('#comment').triggerHandler('ajaxify');
				resetCommentCount();
			}	
		});
	}).triggerHandler('ajaxify');
	
	$('#postcomment').click(function() {
	      $('#comment').show('normal');
		$('#postcomment').closest('ul').remove();
		
		$('#commentList li.empty').remove();
	
	/*
		$('#comment').toggle('normal', 
			function() {
				if ( $(this).is(':hidden') ) {
					$('#postcomment').html('Post a Comment');
				} else {
		      		$('#postcomment').html('Hide Form');
				} 
		      }
		);
	*/
		return false;		
	});
	
	
	
	
	function resetCommentCount() {
		$('#commentCount').html($('li.comment').size());
		$('.empty').remove();
	};

	$('a.delete').click(function () {
		if (confirm('Are you sure you wish to delete this comment?')) {
			$anchor = $(this);
			$.post($anchor.attr('href'), {
				a: 1
			},

			function (data) {
				if (data[0] == 1) {
					$anchor.closest('li.comment').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;

		$scrollarea.height( $('#map').height()-offset );
		
		// console.log( $scrollarea.css('top') );
		
		var $map = $('#map');
		$('#grippie').css('top', $map.offset().top + ($map.outerHeight() /2) );
		
	}).resize();
});

$(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);
		}
	)
});