/* SVN FILE: $Id$ 
Default Layout JS file
*/ 

$(document).ready( function() {
	YouFoot.init();
});

/**
 * Collapsing elements found throughout the site.
 */
YouFoot.collapsers = {
	init: function() {
		$('.collapser').each(function(){
			var $this = $(this);
			var content = $this.find('.content');
			$this.find('.title-bar a').click(function() {
				content.slideToggle();
				$this.toggleClass('collapsed');
				return false;
			});
			if (!$this.hasClass('heavy-bar')) {
				content.hide();
				$this.toggleClass('collapsed');
			}
			if ($this.hasClass('notcollapse')) {
				content.show();
				$this.toggleClass('collapsed');
			}
		});
	}
};

/** 
 * Behaviors attached to the navigation menu.
 */
YouFoot.navigationMenu = {
	menuFavoritesCollection : null,
	
	//attach events to all the favorites menus.
	init: function() {
		var self = this;
		var menuItemVisible = false;
		this.menuFavoriteCollection = $('.header-bottom div.favorites-menu');
		
		var handleClick = function (event) {
			event.stopPropagation();
			event.preventDefault();
			self.hideAll();
			var menuBlock = $(this).next('div.favorites-menu');
			menuBlock.show();
			menuItemVisible = true;
		}
		
		$('.header-bottom a.arrow').each(function() {
			var openButton = $(this);
			if (openButton.next('div.favorites-menu')) {
				openButton.click(handleClick);
			}
		});
		$('.more').click(function(){
			var openButton = $(this);
			if (openButton.next().next().next('div.favorites-menu')) {
				var handleClickMore = function (event) {
					event.stopPropagation();
					event.preventDefault();
					self.hideAll();
					var menuBlockMore = $(this).next().next().next('div.favorites-menu');
					menuBlockMore.show();
					menuItemVisible = true;
				}
				openButton.click(handleClickMore);
			}
		})

		$(document).click(function() {
			if (menuItemVisible) {
				self.hideAll();
			}
		});
		self.hideAll();
	},
	hideAll: function() {
		this.menuFavoriteCollection.hide();
	}
}

/**
 * Mostly Static collection of utility methods.
 */
YouFoot.utils = {
	init: function() {
		$(".jsHide").hide();
		$(".jsShow").show();
	},
//
// Change a named parameter's value in the `url`
// Will append the param if it doesn't exist in the URL
// and will swap the value if it does. 
// @return string.
	changeNamedParam: function (url, name, value) {
		var search = new RegExp(name + ':([^/]+)(/)?');
		var matches = url.match(search);
		if (matches) {
			var endSlash = (matches[2] === undefined) ? '' : matches[2];
			return url.replace(search, name + ':' + value + endSlash);
		} else {
			return url + '/' + name + ':' + value;
		}
	}
};

YouFoot.resultsFixtureUtils = {
	makeLegends: function (context) {
		context = context || document
		$('.show-legend', context).bind('click', function (event) {
			event.preventDefault();
			var target = $(this).attr('href');
			$(target).toggle();
		});
		$('.legend').hide();
	},

	toggleDetails: function () {
		$('.results-details .toggle-details').live('click', function (event) {
			event.preventDefault();
			var $this = $(this);
			$this.prev().toggle();
		});
		$('.results-details .results-details-contents').hide();
	}

};

/**
 * 
 */
YouFoot.loginSidebarValidation = {
	init: function() {
		if (!$('#SidebarLogin').length) {
			return;
		}
		$("#SidebarLogin").validate();
		$("#SidebarLoginEmail").rules("add", {
			minlength: 2
		});
	}
};

YouFoot.expandingText = {
	init: function () {
		$('a.expanding-more').bind('click', function (event) {
			$(this).prev().toggle();
			return false;
		});
	}
};

YouFoot.pagination = {
	init: function () {
		var form = $('#pagination-page-form');
		if (!form.length) {
			return;
		}
		form.bind('submit', this.submitPageChange.bind(this));
		form.find('input[type=text]')
			.bind('blur', this.blurPageChange.bind(this))
			.bind('focus', function (event) {
				$(this).val('');
			});
	},
	
	submitPageChange: function (event) {
		event.preventDefault();
		var pageValue = $(event.target).find('input[type=text]').val();
		this.changePage(pageValue);
	},
	
	blurPageChange: function (event) {
		var pageValue = $(event.target).val();
		this.changePage(pageValue);
	},
	
	changePage: function (pageNumber) {
		var location = window.location.href;
		if (!location.match(/find/)) {
			location += '/find';
		}
		var newUrl = YouFoot.utils.changeNamedParam(location, 'page', pageNumber);
		window.location = newUrl;
	}
};