$(document).ready(function() {

	// Add class to external links
	$('a[@href^="http://"]').addClass('external').attr('target', '_blank');
	
	// Add date picker to calendar icon
	//Date.format = 'mm/dd/yyyy';
	Date.format = 'yyyy-mm-dd';
	$('.date-pick').datePicker().val(new Date().asString()).trigger('change');
	
	// Hide all event descriptions
	$('.event div').hide();
	
	/* Main Navigation
	------------------------------------------------------------------------- */
	// Define the active tab	
	$('#active > ul').show();

	// Menu functions
	$('#mainNavigation > li.drop').hover(function() {
		// Mouseover functions
		$(this).addClass('over');
		if ($(this).attr('id') != 'active') {
			$('#active > ul').hide();
		}
	}, function() {
		// Mouseout functions
		$(this).removeClass('over');
		if ($(this).attr('id') != 'active') {
			$('#active > ul').show();
		}
	});
	
	/* Login form toggle
	------------------------------------------------------------------------- */
	$('#signInTop').toggle(function() {
		$('#topLogin').show();
		$(this).css('text-decoration','underline');
		$(this).css('font-weight','bold');
		return false;
	},function() {
		$('#topLogin').hide();
		$(this).css('text-decoration','none');
		$(this).css('font-weight','normal');
		return false;
	});


	/* Select box navigation 
	------------------------------------------------------------------------- */
	$('#how-do-i').change( function() { 
  		//window.location.href = $(this).children('option:selected').attr('value');
		window.location.href = $(this).children('option:selected').val();
 	});

	/* MyNewark Functions (AJAX)
	------------------------------------------------------------------------- */
	$('.addLoading').hide();
	
	// Add to My Newark
	$('.addToMyNewark').click(function() {
		// get the url of clicked link
		var thisURL = $(this).attr('href');
		// remove parts we don't need 
		queryString = thisURL.replace('/accounts/mynewark/save.php?','');
		// hide this link     
		$(this).hide();
  		// display loading info
		$(this).next().show();
		$.ajax({
			type: 'POST',
			url: '/accounts/mynewark/save.php',
			data: queryString,
			//data: "name=John&location=Boston",
			success: function(response) {
				$('.addResult').hide();
				setTimeout("finishAjax('addResult', '"+escape(response)+"')", 100);
			}
		});
		return false;
	});
	
	// Add to My Newark
	$('.addToMyNewarkEvents').click(function() {
		// get the url of clicked link
		var thisURL = $(this).attr('href');
		var anchorId = $(this).attr('id');
		resultSpan = $('.addResult'+anchorId);
		// remove parts we don't need 
		queryString = thisURL.replace('/accounts/mynewark/save.php?','');
		// hide this link     
		$(this).hide();
  		// display loading info
		$(this).next().show();
		$.ajax({
			type: 'POST',
			url: '/accounts/mynewark/save.php',
			data: queryString,
			//data: "name=John&location=Boston",
			success: function(response) {
				$(resultSpan).hide();
				setTimeout("finishAjaxEvent(resultSpan, '"+escape(response)+"')", 100);
			}
		});
		return false;
	});
	
	// Delete from My Newark
	$('.deleteFromMyNewark').click(function() {
		// get the url of clicked link
		var thisURL = $(this).attr('href');
		var anchorId = $(this).attr('id');
		// remove parts we don't need 
		queryString = thisURL.replace('/accounts/mynewark/delete.php?','');
		// display confirm for one last check
		if(confirm('Are you sure you want to permanently delete this save?')) {
			// hide this link
			$(this).hide();
			// display loading info
			$(this).next().show();
			// do the deletion process
			$.ajax({
				type: 'POST',
				url: '/accounts/mynewark/delete.php',
				data: queryString,
				success: function(response) {
					if (response == true) {
						$('.deleteLoading').hide();
						$('#save'+anchorId).fadeOut('slow');
					} else {
						alert('There was an error deleting this item!');
						$('.deleteLoading').hide();
						$('.deleteFromMyNewark').show();
					}
				}
			});
		}
		return false;
	});

	// Update Ward / Council Member
	$('#wardSelect').change(function() {
		// get the value of selected option
		var ward = $('#wardSelect option:selected').val();
		// if ward exists
		if (ward) {
			// process ajax request
			$.ajax({
				type: 'POST',
				cache: false,
				url: '/accounts/mynewark/save_ward_new.php',
				data: "ward=" + ward,
				success: function(response) {
					if (response == true) {
						// Get the id of selected option
						var wardImg = $('#wardSelect option:selected').attr('id');
						// Update selected value
						$('#wardSelect option:selected').val(ward);
						// Fade old council image
						$('#wardImg').hide();
						// Update new council image
						setTimeout("swapImage('"+wardImg+"')", 300);
						// Hide loading
						setTimeout("showImage()", 1000);
					} else {
						alert('There was an error updating your council member.');
					}
				}
			});
		}
		return false;
	});

	/* Back button
	------------------------------------------------------------------------- */
	$('.back').click(function() {
		history.go(-1);
		return false;
	});

	/* Event toggle
	------------------------------------------------------------------------- */
	$('.event a').toggle(function(){
		$(this).next().show();
		//$(this).next().slideDown('slow').blur(); // Add effects
		$(this).css('background','url(/assets/images/arrow_dn.gif) no-repeat 0 50%');
	},function(){
		$(this).next().hide();
		//$(this).next().slideUp('slow').blur(); // Add effects
		$(this).css('background','url(/assets/images/arrow_rt.gif) no-repeat 0 50%');
	});
	return false;
});

/* Show/Hide News */
function toggleNews(e) {
	var item = 'news' + e.substring(5);
	if (document.getElementById(e).style.display == 'none') {
		document.getElementById(e).style.display = 'block';
		document.getElementById(item).style.background = 'url(/assets/images/arrow_dn.gif) no-repeat 0 50%';
	} else {
		document.getElementById(e).style.display = 'none';
		document.getElementById(item).style.background = 'url(/assets/images/arrow_rt.gif) no-repeat 0 50%';
	}
}

// Finish ajax
function finishAjax(id, response) {
	$('.addLoading').hide();
	$('.addToMyNewark').hide();
	$('.'+id).html(unescape(response));
	$('.'+id).fadeIn();
} 
function finishAjaxEvent(id, response) {
	$('.eventLoading').hide();
	//$('.addToMyNewark').hide();
	$(id).html(unescape(response));
	$(id).fadeIn();
}
// Updates new council image
function showImage() {
	$('#wardImg').fadeIn('slow');
	//$('#wardLoading').fadeOut();
}

function swapImage(wardImgName) {
	$('#wardImg').attr('src','/userimages/city_officials/'+wardImgName+'.jpg');
}

	