// create the closure - any functions within this closeure are private to the function
 (function($) {

   $.fn.pagenav = function(settings) {
	 var config = { 'page': 1,
					'pages':1,
					'curResult':0,
					'perPage':10,
					'count':0,
					'results':0,
					'imgPath':  '/images/tourconnect/ui-icons_454545_256x240.png',
					'dimgPath': '/images/tourconnect/ui-icons_888888_256x240.png',
					'ranges': ['10','20','30','40']
	 };

	 if (settings) $.extend(config, settings);

	 this.each(function() {
		// get the element id
		var jQid = '#'+this.id;

		// element-specific code here
		var top = config.indent;
		var left = -1;
		var botleft = 0;
		var right = config.indent;
		var bottom = -1;
		var filter = config.opacity;
		var opacity = filter * .01;

		$(jQid).addClass('pageNav');

		setButtons();
		render();

		// -------------------------------------------------------------------------------------

		function render() {
			// only render the full html if it is not already in place
			if ($(jQid).html() == '') {
				var html;
				var sel = '';
				html =  '<div class="navBtn ui-icon ui-icon-seek-first"></div>'
					 +  '<div class="navBtn ui-icon ui-icon-seek-prev"></div>'
					 +  '<div class="navInfo">Page <input style="width:20px; height:14px; position:relative; font-size:10px; top:-1px;" value="'+config.page+'" /><span></span></div>'
					 +  '<div class="navBtn ui-icon ui-icon-seek-next"></div>'
					 +  '<div class="navBtn ui-icon ui-icon-seek-end"></div>'
					 +  '<div class="navPerPage" style="padding-left:6px;">'
					 +  '<select style="width:48px; height:18px; position:relative; font-size:11px; top:1px;">';

				$.each(config['ranges'], function(index, value) {
					if (value == config['perPage']) {
						sel = ' selected="selected" ';
					}
					else {
						sel= '';
					}
					html += '       <option ' + sel + ' value="'+value+'">'+value+'</option>';
				});

				html += '</select>Results per page</div><div class="navStat" style="float:right; padding-right:10px;"><span></span></div>';

				$(jQid).html(html);
			}

			// set the current page
			else {
				$(jQid + ' > .navInfo > input').val(config.page);
			}

			setStats();

			// only bind the event if one does not already exist
			if ( !$(jQid + ' > .navPerPage > select').change ) {
				$(jQid + ' > .navPerPage > select').change(config['search']);
			}

		}

		// -------------------------------------------------------------------------------------

		function setStats() {
			// change the page count
			$(jQid + ' > .navInfo > span').html(" of " + config['pages']);

			// change the view stats
			var viewing;

			var viewTo = ( parseInt(config['page']) * parseInt(config['perPage']) );
			if ( viewTo > parseInt(config['results']) ) {
				viewTo = parseInt(config['results']);
			}

			if ( parseInt(config.results) < 1 || config.results == '') {
				viewing = 'No Results';
			}
			else {
				viewing = 'Viewing '+config['curResult']+' - '+viewTo+' of '+config['results']+' Results';
			}
			$(jQid + ' > .navStat > span').html(viewing);
		}

		// -------------------------------------------------------------------------------------

		function enable(elem) {
			$(elem).hover(
			  function () {
				$(this).css('border','1px solid gray');
				$(this).css('background-color','#FEBB14');
				$(this).css('cursor','pointer');
			  },
			  function () {
				$(this).css('background-color','');
				$(this).css('border','1px solid #ccc');
			  }
			);
			$(elem).css('background-image','url('+config['imgPath']+')');
		}

		// -------------------------------------------------------------------------------------

		function disable(elem) {
			$(elem).unbind('mouseenter mouseleave');
			$(elem).unbind('click');
			$(elem).css('background-color','');
			$(elem).css('border','1px solid #ccc');
			$(elem).css('background-image','url('+config['dimgPath']+')');
			$(elem).unbind('click', config.search);
			$(elem).css('cursor','arrow');

		}

		// -------------------------------------------------------------------------------------

		function setButtons() {

			var page = parseInt(config.page);

			// disable back buttons

			if (page <= 1) {
				disable( $(jQid + ' > div:lt(2)') );
			}

			// enable back buttons

			else {
				// unbind all old events
				$(jQid + ' > div:lt(2)').unbind('click');

				// bind the normal click
				$(jQid + ' > div:eq(0)').bind('click', function() {
					config['page'] = 1;
					$(jQid + ' > .navInfo > input').val(config['page']);
				});

				$(jQid + ' > div:eq(1)').bind('click', function() {
					config['page'] = parseInt(config['page']) - 1;
					$(jQid + ' > .navInfo > input').val(config['page']);
				});

				enable( $(jQid + ' > div:lt(2)') );

				// bind the search
				$(jQid + ' > div:lt(2)').bind('click', config.search);
			}


			// disable fwd buttons
			if (page >= parseInt(config.pages)) {
				disable( $(jQid + ' > div:gt(2)').filter(':lt(2)') );
			}
			// enable fwd buttons
			else {

				// unbind existing click events
				$(jQid + ' > div:gt(2)').filter(':lt(2)').unbind('click');

				// bind new events
				$(jQid + ' > div:eq(3)').bind('click', function() {
					config['page'] = parseInt(config['page']) + 1;
					$(jQid + ' > .navInfo > input').val(config['page']);
				});

				$(jQid + ' > div:eq(4)').bind('click', function() {
					config['page'] = config['pages'];
					$(jQid + ' > .navInfo > input').val(config['page']);
				});

				// enable the icons
				enable($(jQid + ' > div:gt(2)').filter(':lt(2)'));

				// bind the search
				$(jQid + ' > div:gt(2)').filter(':lt(2)').bind('click', config.search);

			}

		}

		// -------------------------------------------------------------------------------------

	 });

	 return this;
   }

 })(jQuery);


