var wvFriendsGalleryNonAjax= function(el, options){ 
	
	var defaults = {
		JSON: 'friendsgallery/json/request.json.txt',
		prevButton: '<span class="previousButton"></span>',
		nextButton: '<span class="nextButton"></span>',
		pathToThumbs: 'friendsgallery/images/',
		linkForFriends: '#'
	};
	var options = $.extend(defaults, options || {}); 
	var displayedFriends, totalFriends, friendWidth, totalWidth, friendsList = new Array();
	
	var currentPosition = 0, limitLeft = 0, limitRight;
	
	var leftScroll, rightScroll;
	
	var _getDisplayedFriends = function()	{
		return el.find('li').length;
	};
	
	var _getFriendWidth = function()	{
		try	{
			return el.find('li').eq(1).offset().left - el.find('li').eq(0).offset().left;
		}	catch(e)	{
			return el.find('li').eq(0).width();
		}
	};
	
	var _makeScrollButtons = function()	{
		$(options.prevButton).addClass('prev').addClass('buttonDisabled').css('opacity', 0.5).prependTo(el);
		$(options.nextButton).addClass('next').addClass('buttonDisabled').appendTo(el);
	};
	
	var _scrollButtonStatus = function()	{
		currentPosition==leftLimit ? el.find('.prev').css('opacity',.5).addClass('buttonDisabled') : el.find('.prev').css('opacity',1).removeClass('buttonDisabled');
		currentPosition==rightLimit ? el.find('.next').css('opacity',.5).addClass('buttonDisabled') : el.find('.next').css('opacity',1).removeClass('buttonDisabled');	};
	
	var _smoothScrollLeft = function()	{
		if(currentPosition < leftLimit)	{
			currentPosition += friendWidth;
			el.find('ul').animate({
				left: currentPosition
			}, {
				duration: 400,
				queue: false
			});		
		}
	};
	
	var _smoothScrollRight = function()	{
		if(currentPosition > rightLimit)	{
			currentPosition -= friendWidth;
			el.find('ul').animate({
				left: currentPosition
			}, {
				duration: 400,
				queue: false
			});		
		}
	};
	
	var _fixScrollButtonEvents = function()	{
		el.find('.prev').click(function()	{
			_smoothScrollLeft();
			_scrollButtonStatus();
		}).mousedown(function()	{
			leftScroll = window.setInterval(_smoothScrollLeft, 500);
		}).mouseup(function()	{
			window.clearInterval(leftScroll);
		});
		el.find('.next').click(function()	{
			_scrollButtonStatus();
			_smoothScrollRight();
		}).mousedown(function()	{
			rightScroll = window.setInterval(_smoothScrollRight, 500);
		}).mouseup(function()	{
			window.clearInterval(rightScroll);
		});
	};
	
	var _makeScrollSleeve = function()	{
		sleeve = $('<div class="friends_sleeve"></div>').width(el.find('ul').width()).css('left', 132);
		sleeve.height(el.find('ul').height());
		el.find('ul').wrap(sleeve);
		el.find('ul').css('position', 'absolute');
	};
	
	var _setupScrolling = function()	{
		_makeScrollSleeve();
		// Set the width to accomodate all the friends
		totalWidth = friendWidth * totalFriends;
		
		el.find('ul').width(totalWidth);
		// Set the scroll limits
		leftLimit = 0;
		rightLimit = -totalWidth + (friendWidth * displayedFriends);
		// Add in all the new people
		$.each(friendsList, function(i, friend)	{
			// Only if it's not already displayed
			if(i > displayedFriends - 1)	{
				// Create the new list item
				listitem = $('<li></li>');
				avatar = $('<a href="#"><img src="' + options.pathToThumbs + friend['@UserAvatar'] + '" alt="' + friend['@UserName'] + '" /></a>"').appendTo(listitem);
				name = $('<a href="#" class="username">' + friend['@UserName'] + '</a>').appendTo(listitem);
				// Set the link to the relevant url
				listitem.find('a').attr('href', options.linkForFriends + '?uc=' + friend['@UserID']);
				// Stick it inside the list
				listitem.appendTo(el.find('ul'));
			}
		});
		// Add in the scroll buttons
		_makeScrollButtons();
		// Fix scroll button events
		_fixScrollButtonEvents();
		
	};
	
	var _init = function()	{
		// Get the number of displayed friends
		displayedFriends = options.displayFriends;
		// Get the width of one item, to use in future calculations
		friendWidth = _getFriendWidth();
		// Save the total number of friends
		totalFriends = _getDisplayedFriends();
		// If there are more friends than displayed then set up scrolling
		if(el.find('ul').length > 0)
			_setupScrolling();
	}();

};