(function ($) {

/**
 * PlaceDetail
 * @constructor jQuery
 */
$.widget('ui.Pagination', $.extend({}, $.ui.Component.prototype, {
    type: 'Pagination',
    init: function () {

		$('#pagination').each(function(){
			pagesCount = $(this).find('span.pages').children('span , a.page').size();
			indexCur = $(this).find('span.pages').children().index($(this).find('span.pages').children('span.currentPage'))-1;
			pages = $(this).find('span.pages').children('a.page');
			allPages = 11;
			if (pagesCount > allPages) {
				
				if (pagesCount-indexCur>'6') {
					if (indexCur<7) {
						cut = 8-(indexCur);
					} else {cut = 3};
					pages.slice((indexCur+cut), (pagesCount-2)).wrapAll('<span class="otherPages lastGroup" style="display: none;"><span class="pagesBg"></span><div class="bottomBg"><div class="png_bg bgLeft">&nbsp;</div><div class="png_bg bgRight">&nbsp;</div></div></span>');
				};

				if (indexCur>'5') {
					if ((pagesCount-indexCur)<7) {
						cut = 9-(pagesCount-indexCur);
					} else {cut = 3};
					pages.slice(1, (indexCur-cut)).wrapAll('<span class="otherPages firstGroup" style="display: none;"><span class="pagesBg"></span><div class="bottomBg"><div class="png_bg bgLeft">&nbsp;</div><div class="png_bg bgRight">&nbsp;</div></div></span>');
				};

				if (pages.slice(1, (indexCur-3)).text() != '' && indexCur>'5') {
					pages.filter(':first').after('<a class="page morePages" href="#">...</a>')
				};
				if (pages.slice((indexCur+3), (pagesCount-3)).text() != '' && pagesCount-indexCur>'6') {
					pages.filter(':last').prev().before('<a class="page morePages" href="#">...</a>')
				}
			};
			
		});


		$('a.morePages').click(function() {
			$(this).next().toggle();
			return false;
		});


    }
}));

})(jQuery);