var PA = function () {
	var $ = jQuery;
	return {
		init: function() {
			var instance = this;
			
			instance.handleSearchForm();
			//instance.dropDownMenu();
			instance.handleLastChild();
			instance.handleMyPlaces();
		},

		handleSearchForm: function() {
			var searchForm = $('#banner .search');	
			
			var searchInput = searchForm.find('input[@type=image]');
			var searchLink = $('<a class="search-input-link" href="javascript:;"></a>');
			
			searchLink.click(
				function() {
					$(this).parents('form')[0].submit();
				}
			);
			
			searchInput.hide();
			searchInput.before(searchLink);
		},

		handleLastChild: function () {
			var instance = this;

			$('#footer ul li:last').addClass('last-child');
		},

		dropDownMenu: function() {
			$(".main-item").hoverIntent(
				{
					interval: 25,
					timeout: 0,
					over: function () {
						var instance = $(this);
						var child = $('.child-menu', this);
						instance.addClass("init");
						child.slideDown(100);
					},
					
					out: function () {
						var instance = $(this);
						var child = $('.child-menu', this);
						child.slideUp(50);
						instance.removeClass("init");
					}
				}
			);
		},
		
		hideCommunities: function() {
			var myPlaces = $(".show-my-places");

			var communities = myPlaces.find('> ul > li');
			var communityList = communities.find('ul');
			var currentCommunity = communityList.find('li.current');
			var heading = communities.find('h3');
			heading.wrap('<div class="my-places-toggle"></div>');

			heading = heading.parent();

			communityList.hide();
			currentCommunity.parent().show();

			var currentCommunityHeading = currentCommunity.parent().prev();

			currentCommunityHeading.addClass('hide');

			heading.click(
				function() {
					var heading = jQuery(this);

					heading.next("ul").BlindToggleVertically("fast");
					heading.toggleClass('hide');
				}
			);
		},
		
		handleMyPlaces: function() {
			$(".my-places").hoverIntent(
				{
					interval: 25,
					timeout: 0,
					over: function () {
						var instance = $(".show-my-places");
						
						instance.removeClass("hide");
					},
					
					out: function () {
						var instance = $(".show-my-places");
						instance.addClass("hide");
					}
				}
			);
			var instance = this;
			instance.hideCommunities();
		}
	};
}();
PA.Paginate = PA.Paginate || {};

	PA.Paginate.PaginateNewsArticles =  {

			init: function(params) {
				var instance = this;
				if (!params.container) {
					return;
				}
				instance._container = jQuery('#' + params.container);
				instance._limit = params.limit || 10;
				if (params.itemsClass) {
					params.itemsClass = '.' + params.itemsClass;
				}
				instance._rows = instance._container.find(params.itemsClass || '> div');
				instance._page = 0;
				instance._total = Math.ceil(instance._rows.length / instance._limit);
				instance._links = params.container + 'Links';

				var span = jQuery('<span class="' + instance._links + '"></span>');
				var a;
				for (var i = 1; i <= instance._total; i++) {
					a = jQuery('<a href="javascript: ;">' + i + '</a>');
					a.click(
						function() {
							var number = jQuery(this).text();
							number = parseInt(number) - 1;
							instance.pageTo(number);
						}
					);
					span.append(a);
				}
				instance._container.append(span);
				span.find('a').not(':last').after(' | ');
				
				instance._linksContainer = span;

				instance.pageTo(0);
			},
			pageTo: function(page) {
				var instance = this;
				if (!instance._container.length) {
					return;
				}
				
				var links = instance._linksContainer.find('a');
				
				instance._rows.hide();
				
				var start = instance._limit * page;
				var end = start + (instance._limit - 1);

				instance.setPage(start, end, 'show');
				instance._page = page;
				
				links.css({
						'font-weight': 'normal',
						'text-decoration': ''
					});
				links.eq(page).css(
					{
						'font-weight': 'bold',
						'text-decoration': 'none'
					}
				);
			},
			setPage: function(start, end, display) {
				var instance = this;

				var table = instance._container;
				var rows = instance._rows;
				
				end = Math.min(end, rows.length - 1);
				rows = rows.filter(
					function(i) {
						return (i >= start && i <= end);	
					}
				);
				
				if (display == 'none' || display == 'hidden') {
					rows.hide();
				} else {
					rows.show();
				}
			}
	};
jQuery(document).ready(

	/*
	This function gets loaded when all the HTML, not including the portlets, is
	loaded.
	*/

	function() {
		PA.init();
		PA.Paginate.PaginateNewsArticles.init({container: 'article-news-reader', itemsClass: 'article-news-reader-container', limit: 1});
	}
);

Liferay.Portlet.ready(

	/*
	This function gets loaded after each and every portlet on the page.

	portletId: the current portlet's id
	jQueryObj: the jQuery wrapped object of the current portlet
	*/

	function(portletId, jQueryObj) {
	}
);

jQuery(document).last(

	/*
	This function gets loaded when everything, including the portlets, is on
	the page.
	*/

	function() {
	}
);