(function($){  
	$.fn.sklist = function(){
		
		var id = $(this).attr('id');
		var type = $('#'+id+'-type').val();
		var container = null;
		
		// handles a successful Ajax call
		function handleSuccess(data){
			var total = parseInt($('#'+id+'-totalpages').val());
			var pageNo = $('#'+id+'-pageNo').val();
			
			$('#'+id+'-pageNo').val(pageNo+1);
			
			if(pageNo==total){
				$('#'+id+'-pager').hide();
			}
		
			var current =  parseInt($('#'+id+'-current').val())+1; // increment counter
			
			$('#'+id+'-current').val(current);
			
			var newid = 'new-'+(current);
			
			var html = '<div id="'+newid+'" class="newpage">'+data+'</div>';
			
			$('div#'+id).append(html);
			$('div#'+newid).slideDown();
			
			$('#pager-'+id).find('button.pager').removeAttr('disabled');
			
			// setup addresses
			var newaddresses = $('div#'+newid).find('em.address');
			
			for(var x=0; x<newaddresses.length; x++){
				$(newaddresses[x]).skaddress(container);
			}
		}
		
	
		// handle the click of the pager button
        $('#'+id+'-pager').click(function(){
        	
        	this.disabled = true;
        	
        	var listid = this.id.replace('-pager', '');
        
        	// get setup variables
        	var type = $('#'+listid+'-type').val();
        	var current = $('#'+listid+'-current').val();
        	var pageNo = $('#'+listid+'-pageNo').val();
        	var totalpages = $('#'+listid+'-totalpages').val();
        	var totalitems = $('#'+listid+'-totalitems').val();
    		var desclength = $('#'+listid+'-desclength').val();
    		var length = $('#'+listid+'-length').val();
        	var orderby = $('#'+listid+'-orderby').val();
        	var groupby = $('#'+listid+'-groupby').val();
        	var url = siteroot + 'controller.php';
        	
        	message.showMessage('progress', 'Loading '+type.toLowerCase()+'...');
	        	
        	// get the next page with ajax
			$.post(url, {
				Ajax: 'list.page',
				Type: type,
				Current: current,
				PageNo: pageNo,
				TotalPages: totalpages,
				TotalItems: totalitems,
				DescLength: desclength,
				Length: length,
				OrderBy: orderby,
				GroupBy: groupby,
				SiteRoot: siteroot,
				Root: root
			}, function(data){
	        	message.showMessage('success', type+' loaded successfully.');
				handleSuccess(data);
			});

        });
        
        // handle map
        var containers = $(this).find('.map-container');
		
        if(containers.length>0){
        	var mc = containers[0];
        	var addresses = $(this).find('em.address');
        	
        	// create a map
        	var latitude = 38.646991;
    		var longitude = -90.224967;
        	container = new VEMap(mc.id);
    		container.SetDashboardSize(VEDashboardSize.Tiny);
    		container.LoadMap(new VELatLong(latitude, longitude), 10, 'r', false);
    		
			var context = this;
			
        	for(var x=0; x<addresses.length; x++){
        		$(addresses[x]).skaddress(container);
        	}
        	
        }
		
	}	
})(jQuery);


(function($){  
	$.fn.skaddress = function(c){
		
		var name = $(this).parent().find('h4 a').html();
		
		var address = $(this).html();
		var container = c;
		
		// handle callback
		function callback(shapeLayer, findResults, places, moreResults, errorMsg){
			// if there are no results, display any error message and return
			if(places == null){
				// alert( (errorMsg == null) ? "There were no results" : errorMsg );
				return;
			}
		
			var bestPlace = places[0];
		   
			// Add pushpin to the *best* place
			var location = bestPlace.LatLong;
			
			// set url for full map
			var fullurl = 'http://bing.com/maps/default.aspx?v=2&lvl=12&rtp=~pos.' + location.Latitude + '_' + location.Longitude;
		   
			var newShape = new VEShape(VEShapeType.Pushpin, location);
		   
			var desc = address + '<br>(' + location.Latitude + ', ' + location.Longitude + ')<br><br>';
			desc += '<a href="'+fullurl+'" target="_blank">Get Directions</a>';
		    
			newShape.SetDescription(desc);
			newShape.SetTitle(name);
			container.AddShape(newShape);
			container.SetZoomLevel(7);
		}
		
		container.Find(null, // what
      		  	  address, // where
				  null, // VEFindType (always VEFindType.Businesses)
				  null, // VEShapeLayer (base by default)
				  null, // start index for results (0 by default)
				  null, // max number of results (default is 10)
				  null, // show results? (default is true)
				  null, // create pushpin for what results? (ignored since what is null)
				  false, // use default disambiguation? (default is true)
				  null, // set best map view? (default is true)
				  callback); // call back function
	}	
})(jQuery);
